GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Files at this revision

API Documentation at this revision

Comitter:
jakehodges
Date:
Mon Dec 15 00:19:48 2014 +0000
Parent:
2:1cbb20dd1733
Child:
4:0bab12a0cc9a
Commit message:
Added functionality to vary reading size depending on sensor type.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
sensorinterface.cpp Show annotated file Show diff for this revision Revisions of this file
sensorinterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Dec 14 22:28:24 2014 +0000
+++ b/main.cpp	Mon Dec 15 00:19:48 2014 +0000
@@ -223,7 +223,7 @@
                 
                 #ifdef DEBUG
                     for (int i = 0; i < data.readings.size(); i++)
-                        pc.printf("0x%.4X|", data.readings[i]);
+                        pc.printf("0x%.2X|", data.readings[i]);
                 #endif
                 
                 //log
--- 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;
     
--- a/sensorinterface.h	Sun Dec 14 22:28:24 2014 +0000
+++ b/sensorinterface.h	Mon Dec 15 00:19:48 2014 +0000
@@ -11,14 +11,14 @@
 struct sensor
 {
     char type;
-    int interval;
+    char readingSize;
 };
 
 
 struct d_reply
 {
     char type;
-    vector<uint16_t> readings;
+    vector<char> readings;
 };
 
 
@@ -34,7 +34,7 @@
         map<char, sensor>::iterator currentSensor;
         int update();
         void error(int);
-        char findType(int address);
+        sensor& findType(sensor& s, int address);
         int sendRequest(char address);
         void readyTrigger();
         bool ready;