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