m3Dpi robot, based on the Pololu 3pi and m3pi. m3Dpi has multiple distance sensors, gyroscope, compass and accelerometer sensor to be fully aware of its environment. With the addition of xbee or nrf24n01 module it has wireless communication capabilities.

Dependencies:   m3Dpi mbed-rtos mbed MbedJSONValue

Committer:
sillevl
Date:
Sat Dec 19 13:28:17 2015 +0000
Revision:
11:ccb8653e285f
Parent:
7:9068fc754a0b
Child:
13:d6374484b953
add reporter interface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 7:9068fc754a0b 1 #include "jsonReporter.h"
sillevl 7:9068fc754a0b 2
sillevl 11:ccb8653e285f 3 JsonReporter::JsonReporter(mbed::Stream* _out, const char _id[]) : Reporter(_out, _id)
sillevl 7:9068fc754a0b 4 {
sillevl 7:9068fc754a0b 5
sillevl 7:9068fc754a0b 6 }
sillevl 7:9068fc754a0b 7
sillevl 7:9068fc754a0b 8 MbedJSONValue* JsonReporter::jsonFactory()
sillevl 7:9068fc754a0b 9 {
sillevl 7:9068fc754a0b 10 MbedJSONValue* json = new MbedJSONValue();
sillevl 7:9068fc754a0b 11 (*json)["id"] = id;
sillevl 7:9068fc754a0b 12 return json;
sillevl 7:9068fc754a0b 13 }
sillevl 7:9068fc754a0b 14
sillevl 7:9068fc754a0b 15 void JsonReporter::print(MbedJSONValue* json)
sillevl 7:9068fc754a0b 16 {
sillevl 7:9068fc754a0b 17 out->printf("%s\n", json->serialize().c_str());
sillevl 7:9068fc754a0b 18 }
sillevl 7:9068fc754a0b 19
sillevl 7:9068fc754a0b 20 void JsonReporter::time(time_t seconds)
sillevl 7:9068fc754a0b 21 {
sillevl 7:9068fc754a0b 22 char buffer[32];
sillevl 7:9068fc754a0b 23 std::strftime(buffer, 32, "%d-%m-%Y %T", localtime(&seconds));
sillevl 7:9068fc754a0b 24 MbedJSONValue* jsonTime = jsonFactory();
sillevl 7:9068fc754a0b 25 (*jsonTime)["time"] = buffer;
sillevl 7:9068fc754a0b 26 print(jsonTime);
sillevl 7:9068fc754a0b 27 delete jsonTime;
sillevl 7:9068fc754a0b 28 }
sillevl 7:9068fc754a0b 29
sillevl 7:9068fc754a0b 30 void JsonReporter::distance(m3dpi::Distance distance)
sillevl 7:9068fc754a0b 31 {
sillevl 7:9068fc754a0b 32 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 33 const char property[] = "distance";
sillevl 7:9068fc754a0b 34
sillevl 7:9068fc754a0b 35 (*json)[property][0] = distance.front;
sillevl 7:9068fc754a0b 36 (*json)[property][1] = distance.front_right;
sillevl 7:9068fc754a0b 37 (*json)[property][2] = distance.right;
sillevl 7:9068fc754a0b 38 (*json)[property][3] = distance.back_right;
sillevl 7:9068fc754a0b 39 (*json)[property][4] = distance.back;
sillevl 7:9068fc754a0b 40 (*json)[property][5] = distance.back_left;
sillevl 7:9068fc754a0b 41 (*json)[property][6] = distance.left;
sillevl 7:9068fc754a0b 42 (*json)[property][7] = distance.front_left;
sillevl 7:9068fc754a0b 43
sillevl 7:9068fc754a0b 44 print(json);
sillevl 7:9068fc754a0b 45 delete json;
sillevl 7:9068fc754a0b 46 }
sillevl 7:9068fc754a0b 47
sillevl 7:9068fc754a0b 48 void JsonReporter::acceleration(m3dpi::Acceleration acc)
sillevl 7:9068fc754a0b 49 {
sillevl 7:9068fc754a0b 50 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 51 const char property[] = "acceleration";
sillevl 7:9068fc754a0b 52
sillevl 7:9068fc754a0b 53 (*json)[property]["x"] = acc.x;
sillevl 7:9068fc754a0b 54 (*json)[property]["y"] = acc.y;
sillevl 7:9068fc754a0b 55 (*json)[property]["z"] = acc.z;
sillevl 7:9068fc754a0b 56
sillevl 7:9068fc754a0b 57 print(json);
sillevl 7:9068fc754a0b 58 delete json;
sillevl 7:9068fc754a0b 59 }
sillevl 7:9068fc754a0b 60
sillevl 7:9068fc754a0b 61 void JsonReporter::direction(m3dpi::Direction direction)
sillevl 7:9068fc754a0b 62 {
sillevl 7:9068fc754a0b 63 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 64 const char property[] = "direction";
sillevl 7:9068fc754a0b 65
sillevl 7:9068fc754a0b 66 (*json)[property]["x"] = direction.x;
sillevl 7:9068fc754a0b 67 (*json)[property]["y"] = direction.y;
sillevl 7:9068fc754a0b 68 (*json)[property]["z"] = direction.z;
sillevl 7:9068fc754a0b 69
sillevl 7:9068fc754a0b 70 print(json);
sillevl 7:9068fc754a0b 71 delete json;
sillevl 7:9068fc754a0b 72 }
sillevl 7:9068fc754a0b 73
sillevl 7:9068fc754a0b 74 void JsonReporter::rotation(m3dpi::Rotation rotation)
sillevl 7:9068fc754a0b 75 {
sillevl 7:9068fc754a0b 76 MbedJSONValue* json = jsonFactory();
sillevl 7:9068fc754a0b 77 const char property[] = "rotation";
sillevl 7:9068fc754a0b 78
sillevl 7:9068fc754a0b 79 (*json)[property]["x"] = rotation.x;
sillevl 7:9068fc754a0b 80 (*json)[property]["y"] = rotation.y;
sillevl 7:9068fc754a0b 81 (*json)[property]["z"] = rotation.z;
sillevl 7:9068fc754a0b 82
sillevl 7:9068fc754a0b 83 print(json);
sillevl 7:9068fc754a0b 84 delete json;
sillevl 7:9068fc754a0b 85 }