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

Fork of SerialInterface by Greg Steiert

Revision:
5:9e27e2a46fa6
Parent:
4:0bd9ec504040
Child:
6:c9b7256c8261
--- a/SerialInterface.cpp	Thu Dec 08 21:43:36 2016 +0000
+++ b/SerialInterface.cpp	Wed Dec 14 19:52:31 2016 +0000
@@ -14,12 +14,71 @@
 }
 
 // Initialize SerialInterface
-void SerialInterface::init(I2C* i2c, SPI* spi, DigitalOut* ssel)
+void SerialInterface::init(I2C* i2c, SPI* spi, DigitalInOut* gpio)
 {
     _i2c = i2c;  // save pointer to I2C interface
     _spi = spi;  // save pointer to SPI interface
-    _ssel = ssel;  // save pointer to Select pin
-    *_ssel = 1;  // deselect SPI port
+    _gpio = gpio;  // save pointer to GPIO pins
+}
+
+/* Digital I/O
+ * /d              read all
+ * /d/[pin]        read from pin
+ * /d/[pin]/[cfg]  write configuration to pin
+ * [pin] = number (from 0 to 7) of the pin to access
+ * [cfg] = pin configuration:
+ *     0 - output low
+ *     1 - output high
+ *     2 - input pull down
+ *     3 - input pull up
+ *     4 - input pull none
+ */
+void SerialInterface::fnc_dio(char* resp)
+{
+    int bdat;
+    int bcnt;
+    switch (_args[0]) {
+        case 0:
+            sprintf(resp, "[");
+            resp +=1;
+            for (bcnt = 0; bcnt < 8 ; bcnt++) {
+                bdat = _gpio[bcnt].read();
+                sprintf(resp, "%d,", bdat);
+                resp +=2;
+            }
+            sprintf((resp-1), "]");
+            break;
+        case 1:
+            if (_args[1] > 7) {
+                sprintf(resp, "[-1]");
+            } else {
+                sprintf(resp,"[%d]", _gpio[_args[1]].read());
+            }
+            break;
+        case 2:
+            if (_args[1] > 7) {
+                sprintf(resp, "[-1]");
+            } else {
+                if (_args[2] < 2) {
+                    _gpio[_args[1]].write(_args[2]);
+                    _gpio[_args[1]].output();
+                } else {
+                    if (_args[2] == 2) {
+                        _gpio[_args[1]].mode(PullDown);
+                    } else if (_args[2] == 3) {
+                        _gpio[_args[1]].mode(PullUp);
+                    } else {
+                        _gpio[_args[1]].mode(PullNone);
+                    }
+                    _gpio[_args[1]].input();
+                }
+                sprintf(resp,"[%d]", _args[2]);
+            }
+            break;
+        default:
+            sprintf(resp, "[-1]");
+            break;
+    }
 }
 
 /* I2C
@@ -73,29 +132,59 @@
 }
 
 /* SPI
- * /s/[data]...      read+write
+ * /s/[cfg]/[data]...      read+write
+ * [cfg] = SPI configuration specifies gpio to use for chip
+ *   select and other SPI parameters:
+ *   0x[Byte3][Byte2][Byte1][Byte0]
+ *     Byte3 = SCK frequency in MHz (0 = no change)
+ *     Byte2 = Format:
+ *       0 = no change
+ *       1 = Mode 1
+ *       2 = Mode 2
+ *       3 = Mode 3
+ *       4 = Mode 0
+ *     Byte1 = CS polarity 1 = active high, 0 = active low
+ *     Byte0 = GPIO to use for CS (0-7)
+ *       0 = P5_3
+ *       1 = P5_4
+ *       2 = P5_5
+ *       3 = P5_6
+ *       4 = P3_0
+ *       5 = P3_1
+ *       6 = P3_2
+ *       7 = P3_3
  * [data] = data to be writen
  *   this shifts out each data byte provided and
  *   returns each byte read in a JSON array
  */
 void SerialInterface::fnc_spi(char* resp)
 {
-    int dcnt=0;
+    int dcnt=1;
     int dataIn;
+    spiConfig_t spiCfg;
     if (_args[0] < 1) {
         sprintf(resp, "[-1]");
     } else {
-        sprintf(resp, "[");
-        resp++;
-        *_ssel = 0;
-        while(dcnt < _args[0]) {
-            dcnt++;
-            dataIn = (*_spi).write(_args[dcnt]);
-            sprintf(resp, "0x%02X,", dataIn);
-            resp += 5;
+        spiCfg.merged = _args[1];
+        if (spiCfg.freq) {
+            (*_spi).frequency(spiCfg.freq * 1000000);
+        }
+        if (spiCfg.format) {
+            (*_spi).format(8, (spiCfg.format & 3));
         }
-        *_ssel = 1;
-        sprintf((resp-1), "]");
+        if (_args[0] > 1) {
+            sprintf(resp, "[");
+            resp++;
+            _gpio[spiCfg.csPin] = (spiCfg.csPol);
+            while(dcnt < _args[0]) {
+                dcnt++;
+                dataIn = (*_spi).write(_args[dcnt]);
+                sprintf(resp, "0x%02X,", dataIn);
+                resp += 5;
+            }
+            _gpio[spiCfg.csPin] = !(spiCfg.csPol);
+            sprintf((resp-1), "]");
+        }
     }
 }
 
@@ -115,6 +204,10 @@
             }
         }
         switch (cmd) {
+            case 'd':
+            case 'D':
+                fnc_dio(output);
+                break;
             case 'i':
             case 'I':
                 fnc_i2c(output);
@@ -124,7 +217,7 @@
                 fnc_spi(output);
                 break;
             default:
-                sprintf(output, "!commands: i2c spi");
+                sprintf(output, "!commands: dio i2c spi");
                 break;
         }
     } else {