Command processor to access I2C and SPI Takes URI coded commands and returns JSON array

Fork of SerialInterface by Greg Steiert

Revision:
6:c9b7256c8261
Parent:
5:9e27e2a46fa6
Child:
7:06a2eb2483f6
--- a/SerialInterface.cpp	Wed Dec 14 19:52:31 2016 +0000
+++ b/SerialInterface.cpp	Thu Dec 15 03:56:56 2016 +0000
@@ -5,7 +5,7 @@
 #include "mbed.h"
 #include "SerialInterface.h"
 
-SerialInterface::SerialInterface()
+SerialInterface::SerialInterface(I2C &i2c, SPI &spi): _i2c(i2c), _spi(spi) 
 {
 }
 
@@ -14,11 +14,32 @@
 }
 
 // Initialize SerialInterface
-void SerialInterface::init(I2C* i2c, SPI* spi, DigitalInOut* gpio)
+void SerialInterface::init(DigitalInOut* gpio, AnalogIn* ain)
+{
+    _gpio = gpio;  // save pointer to GPIO pins
+    _ain = ain;  // save pointer to GPIO pins
+}
+
+/* Analog In
+ * /a              read all
+ * /a/[pin]        read from pin
+ *   Returns 16bit normalized result
+ */
+void SerialInterface::fnc_ain(char* resp)
 {
-    _i2c = i2c;  // save pointer to I2C interface
-    _spi = spi;  // save pointer to SPI interface
-    _gpio = gpio;  // save pointer to GPIO pins
+    switch (_args[0]) {
+        case 0:
+            sprintf(resp, "[0x%04X,0x%04X,0x%04X,0x%04X,0x%04X,0x%04X,0x%04X,0x%04X]", 
+            _ain[0].read_u16(), _ain[1].read_u16(), _ain[2].read_u16(), _ain[3].read_u16(), 
+            _ain[4].read_u16(), _ain[5].read_u16(), _ain[6].read_u16(), _ain[7].read_u16() );
+            break;
+        case 1:
+            sprintf(resp, "[0x%04X]", _ain[_args[1]].read_u16());
+            break;
+        default:
+            sprintf(resp, "[-1]");
+            break;
+    }
 }
 
 /* Digital I/O
@@ -104,12 +125,12 @@
                 for (dcnt = 0; dcnt < (_args[IA_CNT] -2) ; dcnt++) {
                     _dbuf[dcnt] = _args[(dcnt +3)];
                 }
-                if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt, true) != 0) {
+                if (_i2c.write(_args[IA_ADD], _dbuf, dcnt, true) != 0) {
                     sprintf(resp, "-1,");
                     resp +=3;
                 }
             }
-            if ((*_i2c).read(_args[IA_ADD], _dbuf, _args[IA_DATA])!=0) {
+            if (_i2c.read(_args[IA_ADD], _dbuf, _args[IA_DATA])!=0) {
                 sprintf(resp, "-1]");
             } else {
                 for (dcnt = 0; dcnt < _args[IA_DATA]; dcnt++) {
@@ -122,7 +143,7 @@
             for (dcnt = 0; dcnt < (_args[IA_CNT] -1) ; dcnt++) {
                 _dbuf[dcnt] = _args[(dcnt +2)];
             }
-            if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt) == 0) {
+            if (_i2c.write(_args[IA_ADD], _dbuf, dcnt) == 0) {
                 sprintf(resp,"[%d]", dcnt);
             } else {
                 sprintf(resp, "[-1]");
@@ -167,10 +188,10 @@
     } else {
         spiCfg.merged = _args[1];
         if (spiCfg.freq) {
-            (*_spi).frequency(spiCfg.freq * 1000000);
+            _spi.frequency(spiCfg.freq * 1000000);
         }
         if (spiCfg.format) {
-            (*_spi).format(8, (spiCfg.format & 3));
+            _spi.format(8, (spiCfg.format & 3));
         }
         if (_args[0] > 1) {
             sprintf(resp, "[");
@@ -178,7 +199,7 @@
             _gpio[spiCfg.csPin] = (spiCfg.csPol);
             while(dcnt < _args[0]) {
                 dcnt++;
-                dataIn = (*_spi).write(_args[dcnt]);
+                dataIn = _spi.write(_args[dcnt]);
                 sprintf(resp, "0x%02X,", dataIn);
                 resp += 5;
             }
@@ -204,6 +225,10 @@
             }
         }
         switch (cmd) {
+            case 'a':
+            case 'A':
+                fnc_ain(output);
+                break;
             case 'd':
             case 'D':
                 fnc_dio(output);
@@ -217,7 +242,7 @@
                 fnc_spi(output);
                 break;
             default:
-                sprintf(output, "!commands: dio i2c spi");
+                sprintf(output, "!commands: ain dio i2c spi");
                 break;
         }
     } else {