:)

Dependencies:   MbedJSONValue DebounceIn TextLCD USBDevice mbed WebSocketClient cc3000_hostdriver_mbedsocket Adafruit_LEDBackpack_2

Revision:
13:209da1dcb6e1
Parent:
11:b520586dc0a1
Parent:
12:c4b046975527
Child:
14:2d47b97c2028
--- a/main.cpp	Tue Dec 02 23:25:02 2014 +0000
+++ b/main.cpp	Wed Dec 03 00:14:32 2014 +0000
@@ -6,7 +6,6 @@
 #include "DebounceIn.h"
 #include "cc3000.h"
 #include "Websocket.h"
-#include "MbedJSONValue.h"
 
 //Debug
 Serial pc(USBTX, USBRX); // tx, rx
@@ -48,7 +47,7 @@
 
 
 //WIFI STUFF
-/*
+
 
 // cc3000 KL25Z wifi connection
 // we need to define connection pins for:
@@ -64,9 +63,44 @@
                          "SSID", "PASSWORD", WPA2, false);
 Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
 
-MbedJSONValue demo;
-*/
+
+// json encoding
+std::string s;
+std::string t;
+std::back_insert_iterator<std::string> json_str = std::back_inserter(t);
+char row[1] = ""; // holder for row tokens
+char rowvoltage[4] = ""; // holder for voltage values
+
+void jsoncopy(const std::string& s, std::back_insert_iterator<std::string> oi) {
+    std::copy(s.begin(), s.end(), oi);
+}
 
+void add_to_json(const std::string& s, std::back_insert_iterator<std::string> oi) {
+    // this chunk of code lifted from the MbedJSONValue library
+    for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
+        switch (*i) {
+#define MAP(val, sym) case val: jsoncopy(sym, oi); break
+                MAP('"', "\\\"");
+                MAP('\\', "\\\\");
+                MAP('/', "\\/");
+                MAP('\b', "\\b");
+                MAP('\f', "\\f");
+                MAP('\n', "\\n");
+                MAP('\r', "\\r");
+                MAP('\t', "\\t");
+#undef MAP
+            default:
+                if ((unsigned char)*i < 0x20 || *i == 0x7f) {
+                    char buf[7];
+                    sprintf(buf, "\\u%04x", *i & 0xff);
+                    copy(buf, buf + 6, oi);
+                } else {
+                    *oi++ = *i;
+                }
+                break;
+        }
+    }
+}
 
 
 int main()
@@ -91,6 +125,13 @@
 
     //LCD init
     lcd.cls();
+    
+    // wifi init
+    wifi.init();
+    wifi.connect();
+    // websocket init
+    Websocket ws("ws://sockets.mbed.org/ws/toastboard/rw");
+    ws.connect();
 
     //Osci
     int loopcount = 0, pressed = 0;
@@ -254,19 +295,31 @@
                     selected = selected+24;
                 }
 
-                /*
+                
                 //STUFF INTO JSON FORMAT
+                add_to_json("{\"vddval\":",json_str);
+                sprintf(rowvoltage,"%.f",vddval);
+                add_to_json(rowvoltage,json_str);
+                add_to_json(", \"selected\":",json_str);
+                selected = rowselect;
+                sprintf(rowvoltage,"%d",selected);
+                add_to_json(", \"rows\": [",json_str);
+                
                 char str[1];
                 for (int i= 0; i < 48; i++) {
-                sprintf(str, "%d", i);
-                demo[str] = clientdata[i];
+                    if (i != 0) {
+                        add_to_json(",",json_str);
                     }
-                demo["vdd"] = vddval;
-                demo["rowval"] = rowval;
-
-                demo["selected"] = selected;
-                */
-
+                    add_to_json("{\"",json_str);
+                    sprintf(str, "%d", i);
+                    add_to_json(str,json_str);
+                    add_to_json("\":",json_str);
+                    sprintf(rowvoltage,"%.f",clientdata[i]);
+                    add_to_json(rowvoltage,json_str);
+                    add_to_json("}",json_str);
+                }
+                
+                
 
                 moved = 0;
 
@@ -298,6 +351,12 @@
                 lcd.printf("Vdd: %1.1f V",vddval);
                 lcd.locate(0,0);
                 lcd.printf("Row %d: %1.1f V",selected,rowval);
+                // send data to websocket
+                char * writable = new char[t.size() + 1];
+                std::copy(t.begin(), t.end(), writable);
+                writable[t.size()] = '\0';
+                ws.send(writable);
+                delete[] writable;
             } else {
                 lcd.cls();
                 string programname = "The ToastBoard";