Stripped-down version of GDP Node Core
Dependencies: EthernetInterface MbedJSONValue SDFileSystem mbed-rtos mbed snail
Revision 0:bb17d0395fb8, committed 2015-05-18
- Comitter:
- Trumple
- Date:
- Mon May 18 16:16:15 2015 +0000
- Commit message:
- Initial commit
Changed in this revision
diff -r 000000000000 -r bb17d0395fb8 EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#5887ae6c0c2c
diff -r 000000000000 -r bb17d0395fb8 MbedJSONValue.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 000000000000 -r bb17d0395fb8 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
diff -r 000000000000 -r bb17d0395fb8 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,68 @@ +#include "mbed.h" +#include "snail.h" +#include "sensorinterface.h" +#include "sdcard.h" +#include "MbedJSONValue.h" +#include <map> + +#define DEBUG + +time_t lastPollTime = 0; +time_t pollInterval = 30; + +bool isBasenode = false; + +sensorinterface* sif; + +char localAddress[8]; +char baseNodeAddress[8]; +bool networkParametersUpdated = false; +bool networkParametersTimedOut = false; + +Serial pc(USBTX, USBRX); + +int main() +{ + sdcard sd = sdcard(); + + //check if local node is basenode + #ifdef DEBUG + pc.printf("[MAIN] Basenode status: %i\r\n", isBasenode); + #endif + + sensorinterface sensors = sensorinterface(); + sif = &sensors; + + while(1) + { + //check if it's time to poll + if (time(NULL) - lastPollTime > pollInterval) + { + #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 + pc.printf("[MAIN] Got data: "); + for (int i = 0; i < data.readings.size(); i++) + pc.printf("0x%.4X|", data.readings[i]); + pc.printf("\r\n"); + #endif + + //log + sd.write(static_cast<long int>(time(NULL)), data); + } + } +} \ No newline at end of file
diff -r 000000000000 -r bb17d0395fb8 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#631c0f1008c3
diff -r 000000000000 -r bb17d0395fb8 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file
diff -r 000000000000 -r bb17d0395fb8 sdcard.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdcard.cpp Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,45 @@ +#include <string> +#include "sdcard.h" +#include <map> + +sdcard::sdcard(): sd(p5, p6, p7, p8, "sd") +{ + //first write sometimes fails, let's ensure the first write isn't real data + FILE *fp = fopen("/sd/boot", "w"); + fprintf(fp, "boot"); + fclose(fp); +} + +int sdcard::write(long int time, d_reply data) +{ + + string sd_name = "/sd/"; + string sd_location =""; + char time_s[64]; + + char buffer[20]; + sprintf(buffer, "%i", data.type); + string dataTypeStr = string(buffer); + + sd_location = sd_name + dataTypeStr; + mkdir(sd_location.c_str(), 0777); + + sprintf(time_s, "%d", time); + sd_location += "/" + string(time_s) + ".txt"; + + FILE *fp = fopen(sd_location.c_str(), "w"); + + if (fp == NULL) + { + printf("[SD] File pointer null, failed to open file\r\n"); + } + + string serializedData; + + for (int i = 0; i < data.readings.size(); i++) + fprintf(fp, "%i\r\n", data.readings[i]); + + fclose(fp); + + return 1; +} \ No newline at end of file
diff -r 000000000000 -r bb17d0395fb8 sdcard.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdcard.h Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,11 @@ +#include "SDFileSystem.h" +#include "sensorinterface.h" + +class sdcard +{ + public: + sdcard(); // MOSI, MISO, CKL, CS -> PINS 2, 7, 5, 1, ON SD CARD WITH 4=VCC, 6=GND, 8,9=NC + int write(long int time_holder, d_reply data); + private: + SDFileSystem sd; +}; \ No newline at end of file
diff -r 000000000000 -r bb17d0395fb8 sensorinterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensorinterface.cpp Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,210 @@ +#include "sensorinterface.h" + +sensorinterface::sensorinterface() : i2c(p28,p27), readyLine(p29) +{ + readyLine.fall(this, &sensorinterface::readySet); + readyLine.rise(this, &sensorinterface::readyUnset); + + this->ready = false; + + char buffer[255]; + + #ifdef DEBUG + printf("[SIF] Scanning for devices...\r\n"); + #endif + + for (char i=1; i<=254; i=i+2) + { + //last bit determines read/write not actual address + if (this->i2c.read(i, &buffer[0], 1) ==0) + { + //returns 0 if ack i.e. a device is found + sensor newSensor; + newSensor = findType(newSensor, i); + this->sensors[i] = newSensor; + #ifdef DEBUG + printf("[SIF] Device found at 0x%.2X, device count: %i\r\n", i, this->sensors.size()); + #endif + } + } +} + +bool sensorinterface::isDataWaiting() +{ + return this->ready; +} + +void sensorinterface::requestData() +{ + if (sensors.size() > 0) + { + currentSensor = sensors.begin(); + + #ifdef DEBUG + printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first); + #endif + + sendRequest(currentSensor->first); + } + else + { + #ifdef DEBUG + printf("[SIF] Data requested, but no devices present\r\n"); + #endif + } +} + +void sensorinterface::readySet() +{ + this->ready = true; +} + +void sensorinterface::readyUnset() +{ + this->ready = false; +} + +#ifdef DEBUG +void sensorinterface::error(int e_num) +{ + printf("[SIF] Error %i\r",e_num); +} +#endif + +int sensorinterface::update() +{ + char buffer[255]; + + for (char i=1; i<=254; i=i+2) + { + //last bit determines read/write not actual address + + bool connected = (this->i2c.read(i, &buffer[0], 1) == 0); + bool registered = (this->sensors.count(i) > 0); + + if (connected) + { + if (!registered) + { + //device added + #ifdef DEBUG + printf("[SIF] Device added at %i\r\n", i); + #endif + sensor newSensor; + newSensor = findType(newSensor, i); + this->sensors[i] = newSensor; + } + } + else if (registered) + { + if (!connected) + { + //device removed + #ifdef DEBUG + printf("[SIF] Device removed at %i\r\n", i); + #endif + this->sensors.erase(i); + } + } + } + + return 1; +} + + +sensor& sensorinterface::findType(sensor& s, int address) +{ + char response[I2C_TYPE_PACKET_SIZE]; + char cmd[1] = {I2C_TYPE_HEADER}; + + this->i2c.write(address, cmd, 1); + int type_timeout = 10; + i2c.stop(); + i2c.start(); + while( (this->i2c.read(address, response, I2C_TYPE_PACKET_SIZE)) && (type_timeout) ) + { + --type_timeout; + if (type_timeout == 2) + { + #ifdef DEBUG + error(3); + #endif + return s; + } + } + + if (response[0] != I2C_TYPE_HEADER) + { + #ifdef DEBUG + error(1); + #endif + } + + s.type = response[1]; + s.packetSize = response[2]; + s.readingSize = response[3]; + return s; +} + +int sensorinterface::sendRequest(char address) +{ + char cmd[1] = {I2C_REQUEST_HEADER}; + + this->i2c.write(address, cmd, 1); + + return 1; +} + +d_reply sensorinterface::readData() +{ + #ifdef DEBUG + printf("[SIF] Reading data from current device (0x%.2X)\r\n", currentSensor->first); + #endif + char address = currentSensor->first; + char cmd[1] = {I2C_DATA_HEADER}; + char bufSize = currentSensor->second.packetSize + 2; + char buffer[bufSize]; + this->i2c.write(address, cmd, 1); + wait(0.01); + this->i2c.read(address, buffer, bufSize); + + d_reply reply; + + reply.type = currentSensor->second.type; + + for (int i = 2; i < bufSize; i += currentSensor->second.readingSize) + { + int r = 0; + for (int j = 0; j < currentSensor->second.readingSize; j++) + { + r = r << 8; + r += buffer[i+j]; + } + reply.readings.push_back(r); + } + + if (buffer[0] != I2C_DATA_HEADER) + { + //if it doesn't send the correct header bit, something has gone wrong + #ifdef DEBUG + error(0); + #endif + } + + if (currentSensor != sensors.end() && sensors.size() > 1 ) + { + #ifdef DEBUG + printf("[SIF] Continuing chain of devices, just read from device: 0x%.2X\r\n", currentSensor->first); + #endif + currentSensor++; + sendRequest(currentSensor->first); + } + #ifdef DEBUG + else + { + printf("[SIF] All devices have been read from, end of chain\r\n"); + } + #endif + + return reply; +} \ No newline at end of file
diff -r 000000000000 -r bb17d0395fb8 sensorinterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensorinterface.h Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,52 @@ +#ifndef SENSORINTERFACE_H + +#define SENSORINTERFACE_H + +#include "mbed.h" +#include <map> +#include <vector> + +#define I2C_TYPE_HEADER 'T' +#define I2C_REQUEST_HEADER 'R' +#define I2C_DATA_HEADER 'D' +#define I2C_TYPE_PACKET_SIZE 4 + +#define DEBUG + +struct sensor +{ + char type; + char packetSize; + char readingSize; +}; + + +struct d_reply +{ + char type; + vector<int> readings; +}; + + +class sensorinterface +{ + public: + map<char, sensor> sensors; + sensorinterface(); + void requestData(); + bool isDataWaiting(); + d_reply readData(); + private: + map<char, sensor>::iterator currentSensor; + int update(); + void error(int); + sensor& findType(sensor& s, int address); + int sendRequest(char address); + void readySet(); + void readyUnset(); + bool ready; + + I2C i2c; + InterruptIn readyLine; +}; +#endif \ No newline at end of file
diff -r 000000000000 -r bb17d0395fb8 snail.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snail.lib Mon May 18 16:16:15 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/Trumple/code/snail/#068c8c062cc2