GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

main.cpp

Committer:
Trumple
Date:
2014-11-11
Revision:
0:fcab3b154e49
Child:
1:27b35752c5d0

File content as of revision 0:fcab3b154e49:

#include "mbed.h"
#include "snail.h"
#include "sensorinterface.h"
#include "sdcard.h"

#define DEBUG

time_t lastPollTime = 0;
time_t pollInterval = 30;

bool isBasenode = false;
 
Serial pc(USBTX, USBRX);

char* getBasenodeAddress(snail* xbee)
{
    snail::baseaddressrequest request;
    xbee->send(request);
    
    //TODO: only continue if type is base address reply
    while(!xbee->isMessageWaiting())
    {}
    
    //snail::message reply = xbee->readMessage();
    
    return "";
}

int main()
{
    #ifdef DEBUG
        pc.printf("[MAIN] Starting up...\r\n");
        printf("                         .       .\r\n");
        printf("                        / `.   .' \\\r\n");
        printf("                .---.  <    > <    >  .---.\r\n");
        printf("                |    \\  \\ - ~ ~ - /  /    |\r\n");
        printf("                 ~-..-~             ~-..-~\r\n");
        printf("             \\~~~\\.'                    `./~~~/\r\n");
        printf("   .-~~^-.    \\__/                        \\__/\r\n");
        printf(" .'  O    \\     /               /       \\  \\\r\n");
        printf("(_____,    `._.'               |         }  \\/~~~/\r\n");
        printf(" `----.          /       }     |        /    \\__/\r\n");
        printf("       `-.      |       /      |       /      `. ,~~|\r\n");
        printf("           ~-.__|      /_ - ~ ^|      /- _      `..-'   f: f:\r\n");
        printf("                |     /        |     /     ~-.     `-. _||_||_\r\n");
        printf("                |_____|        |_____|         ~ - . _ _ _ _ _>\r\n");
        printf("\r\n");
        printf("\r\n");
        printf("         Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
        printf("\r\n");
        printf("\r\n");
    #endif
    
    sensorinterface sensors = sensorinterface();
    sdcard sd = sdcard();
    snail xbee = snail();
    
    //TODO: read basenode pin
    #ifdef DEBUG
        pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode);
    #endif
    
    time_t timestamp = 0;
    //if (isBasenode)
        //TODO: get timestamp from API
        //TODO: be ready to broadcast as the basenode
    //else
        //TODO: request timestamp from base node
        //TODO: find out who basenode is
    
    set_time(timestamp);
    
    //TODO: load configuration from SD
    
    if (isBasenode)
        while(1)
        {
            #ifdef DEBUG
                pc.printf("[MAIN] Basenode is idle\r\n");
            #endif
            
            wait(5);
        }
    else
        while(1)
        {
            //if xbee interrupt has woken us up
                //transmit 10 latest readings
            
            //check if it's time to poll TODO: add check to see if sensorinterface is ready
            if (time(NULL) - lastPollTime > 10)
            {
                #ifdef DEBUG
                    pc.printf("[MAIN] Requesting data...\r\n");
                #endif
                sensors.requestData();
                lastPollTime = time(NULL);
            }
            
            //if there is data waiting for us...
            if (sensors.isDataWaiting())
            {
                #ifdef DEBUG
                    pc.printf("[MAIN] Data waiting, reading data...\r\n");
                #endif
                d_reply data = sensors.readData();
                #ifdef DEBUG
                for (int i = 0; i < data.readings.size(); i++)
                    pc.printf("0x%.4X|", data.readings[i]);
                #endif
                //log
                sd.write(static_cast<long int>(time(NULL)), data);
            }
            
        }
}