This program simply connects to a HTS221 I2C device to proximity sensor

Dependencies:   FXOS8700CQ mbed

Revision:
15:61df4a452d38
Parent:
12:7c94ec5069dc
--- a/main.cpp	Tue Jul 12 21:58:17 2016 +0000
+++ b/main.cpp	Wed Jul 13 05:55:06 2016 +0000
@@ -40,8 +40,9 @@
 
 Serial         pc(USBTX, USBRX);
 SerialBuffered mdm(PTD3, PTD2, 128);
-DigitalOut     led_red(LED_RED);
-DigitalOut     led_green(LED_GREEN);
+DigitalOut led_green(LED_GREEN);
+DigitalOut led_red(LED_RED);
+DigitalOut led_blue(LED_BLUE);
 
 DigitalOut  mdm_uart2_rx_boot_mode_sel(PTC17);  // on powerup, 0 = boot mode, 1 = normal boot
 DigitalOut  mdm_power_on(PTB9);                 // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem)
@@ -287,7 +288,7 @@
             sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
             break;
         }
-    } //switch(*ucCommandIndex)
+    } //switch(iSensorsToReport)
 } //GenerateModemString        
             
             
@@ -308,6 +309,116 @@
     }            
 } //OneMsFunction()
 
+//********************************************************************************************************************************************
+//* Set the RGB LED's Color
+//* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
+//********************************************************************************************************************************************
+void SetLedColor(unsigned char ucColor)
+{
+    //Note that when an LED is on, you write a 0 to it:
+    led_red = !(ucColor & 0x1); //bit 0
+    led_green = !(ucColor & 0x2); //bit 1
+    led_blue = !(ucColor & 0x4); //bit 2
+} //SetLedColor()
+
+//********************************************************************************************************************************************
+//* Process JSON response messages
+//********************************************************************************************************************************************
+bool extract_JSON(char* search_field, char* found_string)
+{
+    char* beginquote;
+    char* endquote;
+    beginquote = strchr(search_field, '{'); //start of JSON
+    endquote = strchr(search_field, '}'); //end of JSON
+    if (beginquote != 0)
+    {
+        uint16_t ifoundlen;
+        if (endquote != 0)
+        {
+            ifoundlen = (uint16_t) (endquote - beginquote) + 1;
+            strncpy(found_string, beginquote, ifoundlen );
+            found_string[ifoundlen] = 0; //null terminate
+            return true;
+        }
+        else
+        {
+            endquote = strchr(search_field, '\0'); //end of string...  sometimes the end bracket is missing
+            ifoundlen = (uint16_t) (endquote - beginquote) + 1;
+            strncpy(found_string, beginquote, ifoundlen );
+            found_string[ifoundlen] = 0; //null terminate
+            return false;
+        }
+    }
+    else
+    {
+        return false;
+    }
+} //extract_JSON
+
+bool parse_JSON(char* json_string)
+{
+    char* beginquote;
+    char token[] = "\"LED\":\"";
+    beginquote = strstr(json_string, token );
+    if ((beginquote != 0))
+    {
+        char cLedColor = beginquote[strlen(token)];
+        printf(GRN "LED Found : %c" DEF "\r\n", cLedColor);
+        switch(cLedColor)
+        {
+            case 'O':
+            { //Off
+                SetLedColor(0);
+                break;
+            }
+            case 'R':
+            { //Red
+                SetLedColor(1);
+                break;
+            }
+            case 'G':
+            { //Green
+                SetLedColor(2);
+                break;
+            }
+            case 'Y':
+            { //Yellow
+                SetLedColor(3);
+                break;
+            }
+            case 'B':
+            { //Blue
+                SetLedColor(4);
+                break;
+            }
+            case 'M':
+            { //Magenta
+                SetLedColor(5);
+                break;
+            }
+            case 'T':
+            { //Turquoise
+                SetLedColor(6);
+                break;
+            }
+            case 'W':
+            { //White
+                SetLedColor(7);
+                break;
+            }
+            default:
+            {
+                break;
+            }
+        } //switch(cLedColor)
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+} //parse_JSON
+
 int main() {
     int i;
     HTS221 hts221;
@@ -359,9 +470,21 @@
             sockopen_mdm();
             char modem_string[512];
             GenerateModemString(&modem_string[0]);
-            printf(DEF "Sending to modem : %s\n", modem_string); 
+            printf(BLU "Sending to modem : %s" DEF "\n", modem_string); 
             sockwrite_mdm(modem_string);
             sockread_mdm(&MySocketData, 1024, 20);
+            printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]);
+            char * myJsonResponse;
+            if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
+            {
+                printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]);
+                parse_JSON(&myJsonResponse[0]);
+            }
+            else
+            {
+                printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string
+                parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
+            }
             sockclose_mdm();
         } //bTimerExpiredFlag
     } //forever loop