GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
Trumple
Date:
Tue Nov 11 17:33:41 2014 +0000
Revision:
0:fcab3b154e49
Child:
1:27b35752c5d0
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Trumple 0:fcab3b154e49 1 #include "mbed.h"
Trumple 0:fcab3b154e49 2 #include "snail.h"
Trumple 0:fcab3b154e49 3 #include "sensorinterface.h"
Trumple 0:fcab3b154e49 4 #include "sdcard.h"
Trumple 0:fcab3b154e49 5
Trumple 0:fcab3b154e49 6 #define DEBUG
Trumple 0:fcab3b154e49 7
Trumple 0:fcab3b154e49 8 time_t lastPollTime = 0;
Trumple 0:fcab3b154e49 9 time_t pollInterval = 30;
Trumple 0:fcab3b154e49 10
Trumple 0:fcab3b154e49 11 bool isBasenode = false;
Trumple 0:fcab3b154e49 12
Trumple 0:fcab3b154e49 13 Serial pc(USBTX, USBRX);
Trumple 0:fcab3b154e49 14
Trumple 0:fcab3b154e49 15 char* getBasenodeAddress(snail* xbee)
Trumple 0:fcab3b154e49 16 {
Trumple 0:fcab3b154e49 17 snail::baseaddressrequest request;
Trumple 0:fcab3b154e49 18 xbee->send(request);
Trumple 0:fcab3b154e49 19
Trumple 0:fcab3b154e49 20 //TODO: only continue if type is base address reply
Trumple 0:fcab3b154e49 21 while(!xbee->isMessageWaiting())
Trumple 0:fcab3b154e49 22 {}
Trumple 0:fcab3b154e49 23
Trumple 0:fcab3b154e49 24 //snail::message reply = xbee->readMessage();
Trumple 0:fcab3b154e49 25
Trumple 0:fcab3b154e49 26 return "";
Trumple 0:fcab3b154e49 27 }
Trumple 0:fcab3b154e49 28
Trumple 0:fcab3b154e49 29 int main()
Trumple 0:fcab3b154e49 30 {
Trumple 0:fcab3b154e49 31 #ifdef DEBUG
Trumple 0:fcab3b154e49 32 pc.printf("[MAIN] Starting up...\r\n");
Trumple 0:fcab3b154e49 33 printf(" . .\r\n");
Trumple 0:fcab3b154e49 34 printf(" / `. .' \\\r\n");
Trumple 0:fcab3b154e49 35 printf(" .---. < > < > .---.\r\n");
Trumple 0:fcab3b154e49 36 printf(" | \\ \\ - ~ ~ - / / |\r\n");
Trumple 0:fcab3b154e49 37 printf(" ~-..-~ ~-..-~\r\n");
Trumple 0:fcab3b154e49 38 printf(" \\~~~\\.' `./~~~/\r\n");
Trumple 0:fcab3b154e49 39 printf(" .-~~^-. \\__/ \\__/\r\n");
Trumple 0:fcab3b154e49 40 printf(" .' O \\ / / \\ \\\r\n");
Trumple 0:fcab3b154e49 41 printf("(_____, `._.' | } \\/~~~/\r\n");
Trumple 0:fcab3b154e49 42 printf(" `----. / } | / \\__/\r\n");
Trumple 0:fcab3b154e49 43 printf(" `-. | / | / `. ,~~|\r\n");
Trumple 0:fcab3b154e49 44 printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n");
Trumple 0:fcab3b154e49 45 printf(" | / | / ~-. `-. _||_||_\r\n");
Trumple 0:fcab3b154e49 46 printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n");
Trumple 0:fcab3b154e49 47 printf("\r\n");
Trumple 0:fcab3b154e49 48 printf("\r\n");
Trumple 0:fcab3b154e49 49 printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
Trumple 0:fcab3b154e49 50 printf("\r\n");
Trumple 0:fcab3b154e49 51 printf("\r\n");
Trumple 0:fcab3b154e49 52 #endif
Trumple 0:fcab3b154e49 53
Trumple 0:fcab3b154e49 54 sensorinterface sensors = sensorinterface();
Trumple 0:fcab3b154e49 55 sdcard sd = sdcard();
Trumple 0:fcab3b154e49 56 snail xbee = snail();
Trumple 0:fcab3b154e49 57
Trumple 0:fcab3b154e49 58 //TODO: read basenode pin
Trumple 0:fcab3b154e49 59 #ifdef DEBUG
Trumple 0:fcab3b154e49 60 pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode);
Trumple 0:fcab3b154e49 61 #endif
Trumple 0:fcab3b154e49 62
Trumple 0:fcab3b154e49 63 time_t timestamp = 0;
Trumple 0:fcab3b154e49 64 //if (isBasenode)
Trumple 0:fcab3b154e49 65 //TODO: get timestamp from API
Trumple 0:fcab3b154e49 66 //TODO: be ready to broadcast as the basenode
Trumple 0:fcab3b154e49 67 //else
Trumple 0:fcab3b154e49 68 //TODO: request timestamp from base node
Trumple 0:fcab3b154e49 69 //TODO: find out who basenode is
Trumple 0:fcab3b154e49 70
Trumple 0:fcab3b154e49 71 set_time(timestamp);
Trumple 0:fcab3b154e49 72
Trumple 0:fcab3b154e49 73 //TODO: load configuration from SD
Trumple 0:fcab3b154e49 74
Trumple 0:fcab3b154e49 75 if (isBasenode)
Trumple 0:fcab3b154e49 76 while(1)
Trumple 0:fcab3b154e49 77 {
Trumple 0:fcab3b154e49 78 #ifdef DEBUG
Trumple 0:fcab3b154e49 79 pc.printf("[MAIN] Basenode is idle\r\n");
Trumple 0:fcab3b154e49 80 #endif
Trumple 0:fcab3b154e49 81
Trumple 0:fcab3b154e49 82 wait(5);
Trumple 0:fcab3b154e49 83 }
Trumple 0:fcab3b154e49 84 else
Trumple 0:fcab3b154e49 85 while(1)
Trumple 0:fcab3b154e49 86 {
Trumple 0:fcab3b154e49 87 //if xbee interrupt has woken us up
Trumple 0:fcab3b154e49 88 //transmit 10 latest readings
Trumple 0:fcab3b154e49 89
Trumple 0:fcab3b154e49 90 //check if it's time to poll TODO: add check to see if sensorinterface is ready
Trumple 0:fcab3b154e49 91 if (time(NULL) - lastPollTime > 10)
Trumple 0:fcab3b154e49 92 {
Trumple 0:fcab3b154e49 93 #ifdef DEBUG
Trumple 0:fcab3b154e49 94 pc.printf("[MAIN] Requesting data...\r\n");
Trumple 0:fcab3b154e49 95 #endif
Trumple 0:fcab3b154e49 96 sensors.requestData();
Trumple 0:fcab3b154e49 97 lastPollTime = time(NULL);
Trumple 0:fcab3b154e49 98 }
Trumple 0:fcab3b154e49 99
Trumple 0:fcab3b154e49 100 //if there is data waiting for us...
Trumple 0:fcab3b154e49 101 if (sensors.isDataWaiting())
Trumple 0:fcab3b154e49 102 {
Trumple 0:fcab3b154e49 103 #ifdef DEBUG
Trumple 0:fcab3b154e49 104 pc.printf("[MAIN] Data waiting, reading data...\r\n");
Trumple 0:fcab3b154e49 105 #endif
Trumple 0:fcab3b154e49 106 d_reply data = sensors.readData();
Trumple 0:fcab3b154e49 107 #ifdef DEBUG
Trumple 0:fcab3b154e49 108 for (int i = 0; i < data.readings.size(); i++)
Trumple 0:fcab3b154e49 109 pc.printf("0x%.4X|", data.readings[i]);
Trumple 0:fcab3b154e49 110 #endif
Trumple 0:fcab3b154e49 111 //log
Trumple 0:fcab3b154e49 112 sd.write(static_cast<long int>(time(NULL)), data);
Trumple 0:fcab3b154e49 113 }
Trumple 0:fcab3b154e49 114
Trumple 0:fcab3b154e49 115 }
Trumple 0:fcab3b154e49 116 }