JSON file in mbed.
Dependencies: MbedJSONValue SDFileSystem mbed
Fork of City_Game_JSON by
main.cpp@5:eedf89829452, 2017-11-27 (annotated)
- Committer:
- ajeet3004
- Date:
- Mon Nov 27 05:22:40 2017 +0000
- Revision:
- 5:eedf89829452
- Parent:
- 3:0ace19f0cc2b
ghjh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AlexVC97 | 0:b3949bab2aee | 1 | #include "mbed.h" |
AlexVC97 | 0:b3949bab2aee | 2 | #include "MbedJSONValue.h" |
ajeet3004 | 5:eedf89829452 | 3 | //#include "SDFileSystem.h" |
AlexVC97 | 2:e6a095fc2274 | 4 | #include "mission.h" |
AlexVC97 | 0:b3949bab2aee | 5 | #include <iostream> |
AlexVC97 | 2:e6a095fc2274 | 6 | #include <vector> |
ajeet3004 | 5:eedf89829452 | 7 | Serial pc(USBTX,USBRX); |
AlexVC97 | 2:e6a095fc2274 | 8 | |
AlexVC97 | 0:b3949bab2aee | 9 | using namespace VivesCityGame; |
AlexVC97 | 0:b3949bab2aee | 10 | |
ajeet3004 | 5:eedf89829452 | 11 | //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // mosi, miso, sclk, cs |
AlexVC97 | 2:e6a095fc2274 | 12 | |
AlexVC97 | 2:e6a095fc2274 | 13 | #define charlimit 100 |
AlexVC97 | 2:e6a095fc2274 | 14 | char words[charlimit]; |
AlexVC97 | 2:e6a095fc2274 | 15 | int n = 0, c; |
AlexVC97 | 2:e6a095fc2274 | 16 | |
AlexVC97 | 2:e6a095fc2274 | 17 | int main() { |
AlexVC97 | 0:b3949bab2aee | 18 | MbedJSONValue demo; |
AlexVC97 | 2:e6a095fc2274 | 19 | |
ajeet3004 | 5:eedf89829452 | 20 | /* FILE * fp1 |
AlexVC97 | 3:0ace19f0cc2b | 21 | = fopen("/sd/test.json", "r"); |
AlexVC97 | 2:e6a095fc2274 | 22 | if(fp1 == NULL) |
AlexVC97 | 2:e6a095fc2274 | 23 | { |
AlexVC97 | 2:e6a095fc2274 | 24 | error("Could not open file for read!\r\n"); |
AlexVC97 | 2:e6a095fc2274 | 25 | } |
AlexVC97 | 2:e6a095fc2274 | 26 | |
AlexVC97 | 2:e6a095fc2274 | 27 | printf("Reading from SD card...\r\n"); |
AlexVC97 | 2:e6a095fc2274 | 28 | |
AlexVC97 | 3:0ace19f0cc2b | 29 | while((c = fgetc(fp1)) && c!=EOF) |
AlexVC97 | 2:e6a095fc2274 | 30 | { |
AlexVC97 | 2:e6a095fc2274 | 31 | words[n] = c; |
AlexVC97 | 2:e6a095fc2274 | 32 | n++; |
AlexVC97 | 2:e6a095fc2274 | 33 | } |
AlexVC97 | 2:e6a095fc2274 | 34 | |
AlexVC97 | 2:e6a095fc2274 | 35 | printf("Read from SD card: %s\r\n", words); |
AlexVC97 | 2:e6a095fc2274 | 36 | |
ajeet3004 | 5:eedf89829452 | 37 | fclose(fp1);*/ |
ajeet3004 | 5:eedf89829452 | 38 | printf("Starting the program"); |
AlexVC97 | 2:e6a095fc2274 | 39 | |
AlexVC97 | 3:0ace19f0cc2b | 40 | //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}]}"; |
ajeet3004 | 5:eedf89829452 | 41 | const char * json = "{\"gameMode\":\"LocationGame\",\"gameModeId\":1,\"message\":\"Go to the big roundabout in Oostende.\"}"; |
ajeet3004 | 5:eedf89829452 | 42 | //const char * json = words; |
AlexVC97 | 0:b3949bab2aee | 43 | //const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; |
AlexVC97 | 2:e6a095fc2274 | 44 | |
AlexVC97 | 2:e6a095fc2274 | 45 | // parse the previous string and fill the object demo |
AlexVC97 | 0:b3949bab2aee | 46 | parse(demo, json); |
AlexVC97 | 2:e6a095fc2274 | 47 | |
AlexVC97 | 0:b3949bab2aee | 48 | printf(">>> testing if it works properly \r\n"); |
AlexVC97 | 2:e6a095fc2274 | 49 | |
AlexVC97 | 2:e6a095fc2274 | 50 | std::vector <Mission> missions; // declares an object of the class std::vector |
AlexVC97 | 2:e6a095fc2274 | 51 | |
AlexVC97 | 2:e6a095fc2274 | 52 | for(std::size_t i = 0; i < demo["missions"].size(); i++) |
AlexVC97 | 2:e6a095fc2274 | 53 | { |
AlexVC97 | 2:e6a095fc2274 | 54 | Mission mission; // declares a local variable of the class Mission |
AlexVC97 | 2:e6a095fc2274 | 55 | mission.from_json(demo["missions"][i]); |
AlexVC97 | 2:e6a095fc2274 | 56 | missions.push_back(mission); |
AlexVC97 | 2:e6a095fc2274 | 57 | } |
AlexVC97 | 2:e6a095fc2274 | 58 | |
AlexVC97 | 2:e6a095fc2274 | 59 | cout << "Missions: " << endl; |
ajeet3004 | 5:eedf89829452 | 60 | while(1) |
ajeet3004 | 5:eedf89829452 | 61 | { |
AlexVC97 | 2:e6a095fc2274 | 62 | |
ajeet3004 | 5:eedf89829452 | 63 | for(int i = 0; i < missions.size(); i++) |
AlexVC97 | 2:e6a095fc2274 | 64 | { |
ajeet3004 | 5:eedf89829452 | 65 | pc.printf("Type: %s\r\n", missions[i].get_type().c_str()); |
ajeet3004 | 5:eedf89829452 | 66 | pc.printf("Id: %d\r\n", missions[i].get_id()); |
ajeet3004 | 5:eedf89829452 | 67 | pc.printf("Message: %s\r\n", missions[i].get_message().c_str()); |
ajeet3004 | 5:eedf89829452 | 68 | /*printf("Hint: %s\r\n", missions[i].get_hint().c_str()); |
AlexVC97 | 2:e6a095fc2274 | 69 | printf("Deadline: %s\r\n", missions[i].get_deadline() ? "true" : "false"); |
AlexVC97 | 2:e6a095fc2274 | 70 | printf("DeadlineTime: %d\r\n", missions[i].get_deadlineTime()); |
AlexVC97 | 2:e6a095fc2274 | 71 | printf("Latitude: %f\r\n", missions[i].get_latitude()); |
AlexVC97 | 2:e6a095fc2274 | 72 | printf("Longitude: %f\r\n", missions[i].get_longitude()); |
ajeet3004 | 5:eedf89829452 | 73 | printf("Radius: %d\r\n", missions[i].get_radius());*/ |
AlexVC97 | 2:e6a095fc2274 | 74 | } |
ajeet3004 | 5:eedf89829452 | 75 | } |
AlexVC97 | 2:e6a095fc2274 | 76 | /* |
AlexVC97 | 2:e6a095fc2274 | 77 | for(std::size_t i = 0; i < missions.size(); i++) |
AlexVC97 | 2:e6a095fc2274 | 78 | { |
AlexVC97 | 2:e6a095fc2274 | 79 | cout << missions[i].to_string(); |
AlexVC97 | 2:e6a095fc2274 | 80 | } |
AlexVC97 | 2:e6a095fc2274 | 81 | */ |
AlexVC97 | 2:e6a095fc2274 | 82 | |
AlexVC97 | 2:e6a095fc2274 | 83 | /* |
AlexVC97 | 0:b3949bab2aee | 84 | std::string game_mode; |
AlexVC97 | 0:b3949bab2aee | 85 | int game_mode_id; |
AlexVC97 | 0:b3949bab2aee | 86 | std::string my_ver; |
AlexVC97 | 0:b3949bab2aee | 87 | std::string creation_date; |
AlexVC97 | 2:e6a095fc2274 | 88 | */ |
AlexVC97 | 2:e6a095fc2274 | 89 | |
AlexVC97 | 2:e6a095fc2274 | 90 | /* |
AlexVC97 | 0:b3949bab2aee | 91 | game_mode = demo["gameMode"].get<std::string>(); |
AlexVC97 | 0:b3949bab2aee | 92 | game_mode_id = demo["gameModeId"].get<int>(); |
AlexVC97 | 0:b3949bab2aee | 93 | my_ver = demo["version"].get<std::string>(); |
AlexVC97 | 0:b3949bab2aee | 94 | creation_date = demo["creationDate"].get<std::string>(); |
AlexVC97 | 2:e6a095fc2274 | 95 | */ |
AlexVC97 | 2:e6a095fc2274 | 96 | |
AlexVC97 | 2:e6a095fc2274 | 97 | /*for(int i = 0; i < 2; i++) |
AlexVC97 | 2:e6a095fc2274 | 98 | { |
AlexVC97 | 2:e6a095fc2274 | 99 | type = demo["missions"][i]["type"].get<std::string>(); |
AlexVC97 | 2:e6a095fc2274 | 100 | type = demo["missions"][i]["id"].get<int>(); |
AlexVC97 | 2:e6a095fc2274 | 101 | type = demo["missions"][i]["message"].get<std::string>(); |
AlexVC97 | 2:e6a095fc2274 | 102 | type = demo["missions"][i]["hint"].get<std::string>(); |
AlexVC97 | 2:e6a095fc2274 | 103 | type = demo["missions"][i]["deadline"].get<bool>(); |
AlexVC97 | 2:e6a095fc2274 | 104 | type = demo["missions"][i]["deadlineTime"].get<int>(); |
AlexVC97 | 2:e6a095fc2274 | 105 | type = demo["missions"][i]["altitude"].get<double>(); |
AlexVC97 | 2:e6a095fc2274 | 106 | type = demo["missions"][i]["longitude"].get<double>(); |
AlexVC97 | 2:e6a095fc2274 | 107 | type = demo["missions"][i]["radius"].get<int>(); |
AlexVC97 | 2:e6a095fc2274 | 108 | } |
AlexVC97 | 2:e6a095fc2274 | 109 | */ |
AlexVC97 | 2:e6a095fc2274 | 110 | |
AlexVC97 | 2:e6a095fc2274 | 111 | /* |
AlexVC97 | 0:b3949bab2aee | 112 | printf("game_mode: %s\r\n", game_mode.c_str()); |
AlexVC97 | 0:b3949bab2aee | 113 | printf("game_mode_id: %d\r\n", game_mode_id); |
AlexVC97 | 0:b3949bab2aee | 114 | printf("my_ver: %s\r\n", my_ver.c_str()); |
AlexVC97 | 0:b3949bab2aee | 115 | printf("creation_date: %s\r\n", creation_date.c_str()); |
AlexVC97 | 2:e6a095fc2274 | 116 | printf("lat: %f\r\n", latitude); |
AlexVC97 | 2:e6a095fc2274 | 117 | printf("long: %f\r\n", longitude); |
AlexVC97 | 2:e6a095fc2274 | 118 | printf("rad: %d\r\n", rad); |
AlexVC97 | 2:e6a095fc2274 | 119 | */ |
AlexVC97 | 2:e6a095fc2274 | 120 | } |