Serial Packet Handler

Dependencies:   mbed MbedJSONValue

Files at this revision

API Documentation at this revision

Comitter:
Arkadi
Date:
Mon Jan 15 12:42:06 2018 +0000
Parent:
0:6080c5029b3f
Child:
2:c887a1b1b762
Commit message:
Added MbedJsonValue library to code

Changed in this revision

MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
picojson.lib Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Mon Jan 15 12:42:06 2018 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/samux/code/MbedJSONValue/#10a99cdf7846
--- a/main.cpp	Mon Jan 15 10:40:06 2018 +0000
+++ b/main.cpp	Mon Jan 15 12:42:06 2018 +0000
@@ -18,11 +18,14 @@
     PA_3 (Rx) --> STLINK
 */
 
+
 ///////////////
 // Libraries //
 ///////////////
 #include "mbed.h"
-#include "picojson.h"
+#include "MbedJSONValue.h"
+#include <string>
+
 //#include "BufferedSerial.h" // uart handler library
 ///////////////
 // #defines  //
@@ -70,6 +73,9 @@
 
 // initialize packet struct
 void initPacket(void);
+
+// Packet Parser
+void parsePacket(void);
 ////////////////////////
 //  Main Code Setup : //
 ////////////////////////
@@ -92,6 +98,9 @@
         //sleep();
     }// end main loop
 }// end main
+
+
+
 ///////////////
 // Functions //
 ///////////////
@@ -122,9 +131,7 @@
                 }
                 //pc.printf("case 0 , %d  \r\n",packetMSG.syncIndex);
                 break;
-
-            } // waiting for header
-
+            }
             // waiting for footer
             case 1: {
                 // add byte to buffer
@@ -141,8 +148,7 @@
                 }
                 //pc.printf("case 2 , %d  \r\n",packetMSG.syncIndex);
                 break;
-            } // waiting for footer
-
+            }
             // verify footer
             case 2: {
                 // add byte to buffer
@@ -157,19 +163,13 @@
                     packetMSG.syncIndex++;
                     if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC
                         packetMSG.syncFlag = 3;
-
-                        // copy packet to json buffer - failed attempt
-                        //char *json = (char*) malloc(packetMSG.bufferIndex+1);
+                        // copy packet to json buffer
                         memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex);
-                        json[packetMSG.bufferIndex]=NULL; // end NULL to indicate end of string
-
+                        json[packetMSG.bufferIndex]=NULL; // end with NULL to indicate end of string
                         // copy packet to json buffer with sprintf
                         //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer );
-
                         // send msg to parse.
-                        pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer );
-                        pc.printf("%d, %s", sizeof(json), json);
-                        //free(json);
+                        parsePacket();
                         // reset buffer
                         packetMSG.bufferIndex = 0;
                         packetMSG.syncIndex = 0;
@@ -185,8 +185,7 @@
                     }
                 }
                 break;
-
-            } // verify footer
+            }
             default: {
                 pc.printf("Sonmething went wrong \r\n");
                 break;
@@ -212,4 +211,34 @@
     packetMSG.syncIndex=0; // sync index for header / footer
     packetMSG.syncFlag=0; // 0 - waiting for header, 1 -  waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer
     packetMSG.bufferIndex=0; // buffer index
+}
+
+// Packet Parser
+void parsePacket(void)
+{
+#ifdef DEBUG_MOD1
+    // write buffer to screen
+    //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer );
+    pc.printf("%s", json);
+#endif
+
+  MbedJSONValue demo;
+
+  //const  char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
+  // 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>();
+  
+   printf("my_str: %s\r\n", my_str.c_str());
+   printf("my_int: %d\r\n", my_int);
+   printf("my_bool: %s\r\n", my_bool ? "true" : "false");
+ 
 }
\ No newline at end of file
--- a/picojson.lib	Mon Jan 15 10:40:06 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mimil/code/picojson/#2bb500b021e2