GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Revision:
3:71b090ec5c69
Parent:
1:27b35752c5d0
Child:
11:190d07f37ec0
--- a/sensorinterface.cpp	Sun Dec 14 22:28:24 2014 +0000
+++ b/sensorinterface.cpp	Mon Dec 15 00:19:48 2014 +0000
@@ -19,7 +19,7 @@
         {
             //returns 0 if ack i.e. a device is found
             sensor newSensor;
-            newSensor.type = findType(i);
+            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());
@@ -85,7 +85,7 @@
                     printf("[SIF] Device added at %i\r\n", i);
                 #endif
                 sensor newSensor;
-                newSensor.type = findType(i);
+                newSensor = findType(newSensor, i);
                 this->sensors[i] = newSensor;
             }
         }
@@ -106,16 +106,16 @@
 }
  
  
-char sensorinterface::findType(int address)
+sensor& sensorinterface::findType(sensor& s, int address)
 {
-    char temp[3];
-    char cmd[1] = {0x54};
+    char response[3];
+    char cmd[1] = {'T'};
  
     this->i2c.write(address, cmd, 1);
     int type_timeout = 10;
     i2c.stop();
     i2c.start();
-    while( (this->i2c.read(address, temp, 3)) && (type_timeout) )
+    while( (this->i2c.read(address, response, 3)) && (type_timeout) )
     {
         --type_timeout;
         if (type_timeout == 2)
@@ -123,11 +123,11 @@
             #ifdef DEBUG
                 error(3);
             #endif
-            return 1;
+            return s;
         }
     }
 
-    if (temp[0] != 'T')
+    if (response[0] != 'T')
     {
         #ifdef DEBUG
             error(1);
@@ -136,7 +136,7 @@
     else
     {
         //seperate the incoming bits
-        if ((temp[1] != 0x00) && (temp[1] != 0x01) && (temp[1] != 0x02))
+        if ((response[1] != 0x00) && (response[1] != 0x01) && (response[1] != 0x02))
         {
             //if its not a recognised type
             #ifdef DEBUG
@@ -144,12 +144,14 @@
             #endif
         }
     }
-    return temp[1];
+    s.type = response[1];
+    s.readingSize = response[2]; 
+    return s;
 }
  
 int sensorinterface::sendRequest(char address)
 {
-    char cmd[1] = {0x52};
+    char cmd[1] = {'R'};
  
     this->i2c.write(address, cmd, 1);
     
@@ -162,22 +164,20 @@
         printf("[SIF] Reading data from current device (0x%.2X)\r\n", currentSensor->first);
     #endif
     char address = currentSensor->first;
-    char cmd[1] = {0x44};
-    char buffer [18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+    char cmd[1] = {'D'};
+    char bufSize = currentSensor->second.readingSize + 2;
+    char buffer[bufSize];
     this->i2c.write(address, cmd, 1);
     wait(0.01);
-    this->i2c.read(address, buffer, 18);
+    this->i2c.read(address, buffer, bufSize);
     
     d_reply reply;
     
     reply.type = currentSensor->second.type;
     
-    for (int i = 2; i <= 17; i += 2)
+    for (int i = 2; i < bufSize; i++)
     {
-        uint16_t reading = buffer[i];
-        reading = reading << 8;
-        reading |= buffer[i+1];
-        reply.readings.push_back(reading);
+        reply.readings.push_back(buffer[i]);
     }
  
     if (buffer[0] != 'D')
@@ -187,10 +187,6 @@
             error(0);
         #endif
     }
-    else
-    {
-        
-    }
     
     this->ready = false;