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

Fork of SerialInterface by Greg Steiert

Revision:
9:f52181496512
Parent:
7:06a2eb2483f6
--- a/SerialInterface.cpp	Fri Dec 16 00:58:28 2016 +0000
+++ b/SerialInterface.cpp	Thu Oct 19 23:19:42 2017 +0000
@@ -5,10 +5,12 @@
 #include "mbed.h"
 #include "SerialInterface.h"
 
-SerialInterface::SerialInterface(I2C &i2c, SPI &spi, DigitalInOut* gpio, AnalogIn* ain): _i2c(i2c), _spi(spi) 
+SerialInterface::SerialInterface(I2C *i2c, SPI *spi, DigitalInOut* gpio, AnalogIn* ain): _i2c(i2c), _spi(spi) 
 {
+    _i2c = i2c;  // save pointer to I2C port
+    _spi = spi;  // save pointer to SPI port
     _gpio = gpio;  // save pointer to GPIO pins
-    _ain = ain;  // save pointer to GPIO pins
+    _ain = ain;  // save pointer to AIN pins
 }
 
 SerialInterface::~SerialInterface()
@@ -120,12 +122,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++) {
@@ -138,7 +140,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]");
@@ -183,10 +185,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, "[");
@@ -194,7 +196,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;
             }