GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
Trumple
Date:
Tue Jan 13 23:36:58 2015 +0000
Revision:
16:3ac37613f849
Parent:
14:bc0e2fa25f94
Child:
20:fb2a9f4d2de7
Separate readings in sensor interface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Trumple 0:fcab3b154e49 1 #include "sensorinterface.h"
Trumple 0:fcab3b154e49 2
jakehodges 11:190d07f37ec0 3 sensorinterface::sensorinterface() : i2c(p28,p27), readyLine(p29)
jakehodges 14:bc0e2fa25f94 4 {
jakehodges 14:bc0e2fa25f94 5 readyLine.fall(this, &sensorinterface::readySet);
jakehodges 14:bc0e2fa25f94 6 readyLine.rise(this, &sensorinterface::readyUnset);
Trumple 0:fcab3b154e49 7
Trumple 0:fcab3b154e49 8 this->ready = false;
Trumple 0:fcab3b154e49 9
Trumple 0:fcab3b154e49 10 char buffer[255];
Trumple 0:fcab3b154e49 11
Trumple 1:27b35752c5d0 12 #ifdef DEBUG
Trumple 1:27b35752c5d0 13 printf("[SIF] Scanning for devices...\r\n");
Trumple 1:27b35752c5d0 14 #endif
Trumple 1:27b35752c5d0 15
Trumple 0:fcab3b154e49 16 for (char i=1; i<=254; i=i+2)
Trumple 0:fcab3b154e49 17 {
Trumple 0:fcab3b154e49 18 //last bit determines read/write not actual address
Trumple 0:fcab3b154e49 19 if (this->i2c.read(i, &buffer[0], 1) ==0)
Trumple 0:fcab3b154e49 20 {
Trumple 0:fcab3b154e49 21 //returns 0 if ack i.e. a device is found
Trumple 0:fcab3b154e49 22 sensor newSensor;
jakehodges 3:71b090ec5c69 23 newSensor = findType(newSensor, i);
Trumple 0:fcab3b154e49 24 this->sensors[i] = newSensor;
Trumple 0:fcab3b154e49 25 #ifdef DEBUG
Trumple 0:fcab3b154e49 26 printf("[SIF] Device found at 0x%.2X, device count: %i\r\n", i, this->sensors.size());
Trumple 0:fcab3b154e49 27 #endif
Trumple 0:fcab3b154e49 28 }
Trumple 0:fcab3b154e49 29 }
Trumple 0:fcab3b154e49 30 }
Trumple 0:fcab3b154e49 31
Trumple 0:fcab3b154e49 32 bool sensorinterface::isDataWaiting()
Trumple 0:fcab3b154e49 33 {
Trumple 0:fcab3b154e49 34 return this->ready;
Trumple 0:fcab3b154e49 35 }
Trumple 0:fcab3b154e49 36
Trumple 0:fcab3b154e49 37 void sensorinterface::requestData()
Trumple 0:fcab3b154e49 38 {
Trumple 1:27b35752c5d0 39 if (sensors.size() > 0)
Trumple 1:27b35752c5d0 40 {
Trumple 1:27b35752c5d0 41 currentSensor = sensors.begin();
Trumple 1:27b35752c5d0 42
Trumple 1:27b35752c5d0 43 #ifdef DEBUG
Trumple 1:27b35752c5d0 44 printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first);
Trumple 1:27b35752c5d0 45 #endif
Trumple 1:27b35752c5d0 46
Trumple 1:27b35752c5d0 47 sendRequest(currentSensor->first);
Trumple 1:27b35752c5d0 48 }
Trumple 1:27b35752c5d0 49 else
Trumple 1:27b35752c5d0 50 {
Trumple 1:27b35752c5d0 51 #ifdef DEBUG
Trumple 1:27b35752c5d0 52 printf("[SIF] Data requested, but no devices present\r\n");
Trumple 1:27b35752c5d0 53 #endif
Trumple 1:27b35752c5d0 54 }
Trumple 0:fcab3b154e49 55 }
Trumple 0:fcab3b154e49 56
jakehodges 14:bc0e2fa25f94 57 void sensorinterface::readySet()
Trumple 0:fcab3b154e49 58 {
Trumple 0:fcab3b154e49 59 this->ready = true;
Trumple 0:fcab3b154e49 60 }
Trumple 0:fcab3b154e49 61
jakehodges 14:bc0e2fa25f94 62 void sensorinterface::readyUnset()
jakehodges 14:bc0e2fa25f94 63 {
jakehodges 14:bc0e2fa25f94 64 this->ready = false;
jakehodges 14:bc0e2fa25f94 65 }
jakehodges 14:bc0e2fa25f94 66
Trumple 0:fcab3b154e49 67 #ifdef DEBUG
Trumple 0:fcab3b154e49 68 void sensorinterface::error(int e_num)
Trumple 0:fcab3b154e49 69 {
Trumple 0:fcab3b154e49 70 printf("[SIF] Error %i\r",e_num);
Trumple 0:fcab3b154e49 71 }
Trumple 0:fcab3b154e49 72 #endif
Trumple 0:fcab3b154e49 73
Trumple 0:fcab3b154e49 74 int sensorinterface::update()
Trumple 0:fcab3b154e49 75 {
Trumple 0:fcab3b154e49 76 char buffer[255];
Trumple 0:fcab3b154e49 77
Trumple 0:fcab3b154e49 78 for (char i=1; i<=254; i=i+2)
Trumple 0:fcab3b154e49 79 {
Trumple 0:fcab3b154e49 80 //last bit determines read/write not actual address
Trumple 0:fcab3b154e49 81
Trumple 0:fcab3b154e49 82 bool connected = (this->i2c.read(i, &buffer[0], 1) == 0);
Trumple 0:fcab3b154e49 83 bool registered = (this->sensors.count(i) > 0);
Trumple 0:fcab3b154e49 84
Trumple 0:fcab3b154e49 85 if (connected)
Trumple 0:fcab3b154e49 86 {
Trumple 0:fcab3b154e49 87 if (!registered)
Trumple 0:fcab3b154e49 88 {
Trumple 0:fcab3b154e49 89 //device added
Trumple 0:fcab3b154e49 90 #ifdef DEBUG
Trumple 0:fcab3b154e49 91 printf("[SIF] Device added at %i\r\n", i);
Trumple 0:fcab3b154e49 92 #endif
Trumple 0:fcab3b154e49 93 sensor newSensor;
jakehodges 3:71b090ec5c69 94 newSensor = findType(newSensor, i);
Trumple 0:fcab3b154e49 95 this->sensors[i] = newSensor;
Trumple 0:fcab3b154e49 96 }
Trumple 0:fcab3b154e49 97 }
Trumple 0:fcab3b154e49 98 else if (registered)
Trumple 0:fcab3b154e49 99 {
Trumple 0:fcab3b154e49 100 if (!connected)
Trumple 0:fcab3b154e49 101 {
Trumple 0:fcab3b154e49 102 //device removed
Trumple 0:fcab3b154e49 103 #ifdef DEBUG
Trumple 0:fcab3b154e49 104 printf("[SIF] Device removed at %i\r\n", i);
Trumple 0:fcab3b154e49 105 #endif
Trumple 0:fcab3b154e49 106 this->sensors.erase(i);
Trumple 0:fcab3b154e49 107 }
Trumple 0:fcab3b154e49 108 }
Trumple 0:fcab3b154e49 109 }
Trumple 0:fcab3b154e49 110
Trumple 0:fcab3b154e49 111 return 1;
Trumple 0:fcab3b154e49 112 }
Trumple 0:fcab3b154e49 113
Trumple 0:fcab3b154e49 114
jakehodges 3:71b090ec5c69 115 sensor& sensorinterface::findType(sensor& s, int address)
Trumple 0:fcab3b154e49 116 {
jakehodges 14:bc0e2fa25f94 117 char response[I2C_TYPE_PACKET_SIZE];
jakehodges 14:bc0e2fa25f94 118 char cmd[1] = {I2C_TYPE_HEADER};
Trumple 0:fcab3b154e49 119
Trumple 0:fcab3b154e49 120 this->i2c.write(address, cmd, 1);
Trumple 0:fcab3b154e49 121 int type_timeout = 10;
Trumple 0:fcab3b154e49 122 i2c.stop();
Trumple 0:fcab3b154e49 123 i2c.start();
jakehodges 14:bc0e2fa25f94 124 while( (this->i2c.read(address, response, I2C_TYPE_PACKET_SIZE)) && (type_timeout) )
Trumple 0:fcab3b154e49 125 {
Trumple 0:fcab3b154e49 126 --type_timeout;
Trumple 0:fcab3b154e49 127 if (type_timeout == 2)
Trumple 0:fcab3b154e49 128 {
Trumple 0:fcab3b154e49 129 #ifdef DEBUG
Trumple 0:fcab3b154e49 130 error(3);
Trumple 0:fcab3b154e49 131 #endif
jakehodges 3:71b090ec5c69 132 return s;
Trumple 0:fcab3b154e49 133 }
Trumple 0:fcab3b154e49 134 }
Trumple 0:fcab3b154e49 135
jakehodges 14:bc0e2fa25f94 136 if (response[0] != I2C_TYPE_HEADER)
Trumple 0:fcab3b154e49 137 {
Trumple 0:fcab3b154e49 138 #ifdef DEBUG
Trumple 0:fcab3b154e49 139 error(1);
Trumple 0:fcab3b154e49 140 #endif
Trumple 0:fcab3b154e49 141 }
jakehodges 14:bc0e2fa25f94 142
jakehodges 3:71b090ec5c69 143 s.type = response[1];
jakehodges 14:bc0e2fa25f94 144 s.packetSize = response[2];
jakehodges 14:bc0e2fa25f94 145 s.readingSize = response[3];
jakehodges 3:71b090ec5c69 146 return s;
Trumple 0:fcab3b154e49 147 }
Trumple 0:fcab3b154e49 148
Trumple 0:fcab3b154e49 149 int sensorinterface::sendRequest(char address)
Trumple 0:fcab3b154e49 150 {
jakehodges 3:71b090ec5c69 151 char cmd[1] = {'R'};
Trumple 0:fcab3b154e49 152
Trumple 0:fcab3b154e49 153 this->i2c.write(address, cmd, 1);
Trumple 0:fcab3b154e49 154
Trumple 0:fcab3b154e49 155 return 1;
Trumple 0:fcab3b154e49 156 }
Trumple 0:fcab3b154e49 157
Trumple 0:fcab3b154e49 158 d_reply sensorinterface::readData()
Trumple 0:fcab3b154e49 159 {
Trumple 0:fcab3b154e49 160 #ifdef DEBUG
Trumple 0:fcab3b154e49 161 printf("[SIF] Reading data from current device (0x%.2X)\r\n", currentSensor->first);
Trumple 0:fcab3b154e49 162 #endif
Trumple 0:fcab3b154e49 163 char address = currentSensor->first;
jakehodges 3:71b090ec5c69 164 char cmd[1] = {'D'};
jakehodges 14:bc0e2fa25f94 165 char bufSize = currentSensor->second.packetSize + 2;
jakehodges 3:71b090ec5c69 166 char buffer[bufSize];
Trumple 0:fcab3b154e49 167 this->i2c.write(address, cmd, 1);
Trumple 0:fcab3b154e49 168 wait(0.01);
jakehodges 3:71b090ec5c69 169 this->i2c.read(address, buffer, bufSize);
Trumple 0:fcab3b154e49 170
Trumple 0:fcab3b154e49 171 d_reply reply;
Trumple 0:fcab3b154e49 172
Trumple 0:fcab3b154e49 173 reply.type = currentSensor->second.type;
Trumple 0:fcab3b154e49 174
Trumple 16:3ac37613f849 175 for (int i = 2; i < bufSize; i += currentSensor->second.readingSize)
Trumple 0:fcab3b154e49 176 {
Trumple 16:3ac37613f849 177 int r = 0;
Trumple 16:3ac37613f849 178 for (int j = 0; j < currentSensor->second.readingSize; j++)
Trumple 16:3ac37613f849 179 {
Trumple 16:3ac37613f849 180 r = r << 8;
Trumple 16:3ac37613f849 181 r += buffer[i+j];
Trumple 16:3ac37613f849 182 }
Trumple 16:3ac37613f849 183 reply.readings.push_back(r);
Trumple 0:fcab3b154e49 184 }
Trumple 0:fcab3b154e49 185
Trumple 0:fcab3b154e49 186 if (buffer[0] != 'D')
Trumple 0:fcab3b154e49 187 {
Trumple 0:fcab3b154e49 188 //if it doesn't send the correct header bit, something has gone wrong
Trumple 0:fcab3b154e49 189 #ifdef DEBUG
Trumple 0:fcab3b154e49 190 error(0);
Trumple 0:fcab3b154e49 191 #endif
Trumple 0:fcab3b154e49 192 }
Trumple 0:fcab3b154e49 193
Trumple 0:fcab3b154e49 194 if (currentSensor != sensors.end() && sensors.size() > 1 )
Trumple 0:fcab3b154e49 195 {
Trumple 0:fcab3b154e49 196 #ifdef DEBUG
Trumple 0:fcab3b154e49 197 printf("[SIF] Continuing chain of devices, just read from device: 0x%.2X\r\n", currentSensor->first);
Trumple 0:fcab3b154e49 198 #endif
Trumple 0:fcab3b154e49 199 currentSensor++;
Trumple 0:fcab3b154e49 200 sendRequest(currentSensor->first);
Trumple 0:fcab3b154e49 201 }
Trumple 0:fcab3b154e49 202 #ifdef DEBUG
Trumple 0:fcab3b154e49 203 else
Trumple 0:fcab3b154e49 204 {
Trumple 0:fcab3b154e49 205 printf("[SIF] All devices have been read from, end of chain\r\n");
Trumple 0:fcab3b154e49 206 }
Trumple 0:fcab3b154e49 207 #endif
Trumple 0:fcab3b154e49 208
Trumple 0:fcab3b154e49 209 return reply;
Trumple 0:fcab3b154e49 210 }