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