Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MbedJSONValue
Diff: main.cpp
- Revision:
- 1:711a27baa42d
- Parent:
- 0:6080c5029b3f
- Child:
- 2:c887a1b1b762
diff -r 6080c5029b3f -r 711a27baa42d main.cpp --- 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