JSON file in mbed.

Dependencies:   MbedJSONValue SDFileSystem mbed

Fork of City_Game_JSON by Alex Vancoillie

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MbedJSONValue.h"
00003 //#include "SDFileSystem.h"
00004 #include "mission.h"
00005 #include <iostream>
00006 #include <vector>
00007 Serial pc(USBTX,USBRX);
00008 
00009 using namespace VivesCityGame;
00010 
00011 //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // mosi, miso, sclk, cs
00012 
00013 #define charlimit 100
00014 char words[charlimit];
00015 int n = 0, c;
00016 
00017 int main() {
00018     MbedJSONValue demo;
00019 
00020    /* FILE * fp1
00021     = fopen("/sd/test.json", "r");
00022     if(fp1 == NULL)
00023     {
00024         error("Could not open file for read!\r\n");
00025     }
00026 
00027     printf("Reading from SD card...\r\n");
00028 
00029     while((c = fgetc(fp1)) && c!=EOF)
00030     {
00031         words[n] = c;
00032         n++;
00033     }
00034 
00035     printf("Read from SD card: %s\r\n", words);
00036 
00037     fclose(fp1);*/
00038      printf("Starting the program");
00039 
00040     //const char * json = "{\"gameMode\":\"LocationGame\",\"gameModeId\":1,\"version\":\"1.235\",\"creationDate\":\"11-02-2017\",\"missions\":[{\"type\":\"gotoLocation\",\"id\":1,\"message\":\"Go to the big roundabout in Oostende.\",\"deadline\":true,\"deadlineTime\":30,\"latitude\":51.21543,\"longitude\":2.928656,\"radius\":200},{\"type\":\"puzzle\",\"id\":2,\"message\":\"Find the key A\",\"hint\":\"Look in a tree\",\"deadline\":false}]}";
00041     const char * json = "{\"gameMode\":\"LocationGame\",\"gameModeId\":1,\"message\":\"Go to the big roundabout in Oostende.\"}";
00042     //const char * json = words;
00043     //const  char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}";
00044 
00045     // parse the previous string and fill the object demo
00046     parse(demo, json);
00047 
00048     printf(">>> testing if it works properly \r\n");
00049 
00050     std::vector <Mission> missions; // declares an object of the class std::vector
00051 
00052     for(std::size_t i = 0; i < demo["missions"].size(); i++)
00053     {
00054         Mission mission; // declares a local variable of the class Mission
00055         mission.from_json(demo["missions"][i]);
00056         missions.push_back(mission);
00057     }
00058 
00059     cout << "Missions: " << endl;
00060     while(1)
00061     {
00062 
00063     for(int i = 0; i < missions.size(); i++)
00064     {
00065         pc.printf("Type: %s\r\n", missions[i].get_type().c_str());
00066         pc.printf("Id: %d\r\n", missions[i].get_id());
00067         pc.printf("Message: %s\r\n", missions[i].get_message().c_str());
00068         /*printf("Hint: %s\r\n", missions[i].get_hint().c_str());
00069         printf("Deadline: %s\r\n", missions[i].get_deadline() ? "true" : "false");
00070         printf("DeadlineTime: %d\r\n", missions[i].get_deadlineTime());
00071         printf("Latitude: %f\r\n", missions[i].get_latitude());
00072         printf("Longitude: %f\r\n", missions[i].get_longitude());
00073         printf("Radius: %d\r\n", missions[i].get_radius());*/
00074     }
00075     }
00076     /*
00077     for(std::size_t i = 0; i < missions.size(); i++)
00078     {
00079         cout << missions[i].to_string();
00080     }
00081     */
00082 
00083     /*
00084     std::string game_mode;
00085     int game_mode_id;
00086     std::string my_ver;
00087     std::string creation_date;
00088     */
00089 
00090     /*
00091     game_mode = demo["gameMode"].get<std::string>();
00092     game_mode_id = demo["gameModeId"].get<int>();
00093     my_ver = demo["version"].get<std::string>();
00094     creation_date = demo["creationDate"].get<std::string>();
00095     */
00096 
00097     /*for(int i = 0; i < 2; i++)
00098     {
00099         type = demo["missions"][i]["type"].get<std::string>();
00100         type = demo["missions"][i]["id"].get<int>();
00101         type = demo["missions"][i]["message"].get<std::string>();
00102         type = demo["missions"][i]["hint"].get<std::string>();
00103         type = demo["missions"][i]["deadline"].get<bool>();
00104         type = demo["missions"][i]["deadlineTime"].get<int>();
00105         type = demo["missions"][i]["altitude"].get<double>();
00106         type = demo["missions"][i]["longitude"].get<double>();
00107         type = demo["missions"][i]["radius"].get<int>();
00108     }
00109     */
00110 
00111     /*
00112     printf("game_mode: %s\r\n", game_mode.c_str());
00113     printf("game_mode_id: %d\r\n", game_mode_id);
00114     printf("my_ver: %s\r\n", my_ver.c_str());
00115     printf("creation_date: %s\r\n", creation_date.c_str());
00116     printf("lat: %f\r\n", latitude);
00117     printf("long: %f\r\n", longitude);
00118     printf("rad: %d\r\n", rad);
00119     */
00120 }