condens

Dependencies:   mbed MbedJSONValue

Revision:
1:5abef2328a97
Parent:
0:8dd13dfd2e4e
Child:
2:d5f4f429f3db
--- a/main.cpp	Thu Aug 22 10:51:23 2019 +0000
+++ b/main.cpp	Thu Aug 22 11:37:10 2019 +0000
@@ -1,4 +1,6 @@
 #include "mbed.h"
+#include "MbedJSONValue.h"
+#include <string>
 
 Ticker toggle_led_ticker;
 Ticker sendStuffTicker;
@@ -17,17 +19,54 @@
 }
 
 
+void makeJson(){
+    MbedJSONValue demo;
+    std::string s;
+ 
+    //fill the object
+    demo["my_array"][0] = "demo_string";
+    demo["my_array"][1] = 10;
+    demo["my_boolean"] = false;
+ 
+    //serialize it into a JSON string
+    s = demo.serialize();
+    pc.printf("json: %s\r\n", s.c_str());    
+}
+
+void parseJson(){
+     MbedJSONValue demo;
+ 
+    const  char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
+ 
+    //parse the previous string and fill the object demo
+    parse(demo, json);
+ 
+    std::string my_str;
+    int my_int;
+    bool my_bool;
+ 
+    my_str = demo["my_array"][0].get<std::string>();
+    my_int = demo["my_array"][1].get<int>();
+    my_bool = demo["my_boolean"].get<bool>();
+    
+    pc.printf("my_str: %s\r\n", my_str.c_str());
+    pc.printf("my_int: %d\r\n", my_int);
+    pc.printf("my_bool: %s\r\n", my_bool ? "true" : "false");
+}
+
 int main() {
     pc.baud(115200);
     pc.printf("start main()\n\r");    
     
-    // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms)
-    toggle_led_ticker.attach(&toggle_led, 0.1);
-    sendStuffTicker.attach(&sendStuff, 1);
+    makeJson();
+    parseJson();
+    
+    toggle_led_ticker.attach(&toggle_led, 0.2);
+    sendStuffTicker.attach(&sendStuff, 2);
     
     while (true) {
         // Do other things...
-        led2 = !led2;
+
         wait(1);
     }
 }
\ No newline at end of file