diff options
author | Federico Fissore <f.fissore@arduino.cc> | 2013-06-20 16:54:08 +0200 |
---|---|---|
committer | Federico Fissore <f.fissore@arduino.cc> | 2013-06-20 16:54:08 +0200 |
commit | 5f32476ddb30f68a3bffe1d7eca21a3c6d7b92e8 (patch) | |
tree | 46b13d13871e9be72079ec7d8235e7136645b6f9 | |
parent | 4ec08ef6cc8189f49f45966c05d9283a81c4977a (diff) |
added writeJSON
-rw-r--r-- | libraries/Bridge/Bridge.cpp | 5 | ||||
-rw-r--r-- | libraries/Bridge/Bridge.h | 1 | ||||
-rw-r--r-- | libraries/Bridge/examples/Bridge/Bridge.ino | 36 |
3 files changed, 24 insertions, 18 deletions
diff --git a/libraries/Bridge/Bridge.cpp b/libraries/Bridge/Bridge.cpp index 2485852..d6d830c 100644 --- a/libraries/Bridge/Bridge.cpp +++ b/libraries/Bridge/Bridge.cpp @@ -72,6 +72,11 @@ void BridgeClass::writeMessage(const String& str) { writeMessage((uint8_t*) str.c_str(), str.length()); } +void BridgeClass::writeJSON(const String& str) { + uint8_t cmd[] = {'J'}; + transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0); +} + unsigned int BridgeClass::messageAvailable() { uint8_t tmp[] = {'n'}; uint8_t res[2]; diff --git a/libraries/Bridge/Bridge.h b/libraries/Bridge/Bridge.h index b671863..f606492 100644 --- a/libraries/Bridge/Bridge.h +++ b/libraries/Bridge/Bridge.h @@ -31,6 +31,7 @@ public: unsigned int readMessage(uint8_t *buffer, unsigned int size); void writeMessage(const uint8_t *buffer, unsigned int size); void writeMessage(const String& str); + void writeJSON(const String& str); unsigned int messageAvailable(); // Methods to handle key/value datastore diff --git a/libraries/Bridge/examples/Bridge/Bridge.ino b/libraries/Bridge/examples/Bridge/Bridge.ino index 7318618..df1e069 100644 --- a/libraries/Bridge/examples/Bridge/Bridge.ino +++ b/libraries/Bridge/examples/Bridge/Bridge.ino @@ -90,23 +90,23 @@ void modeCommand(String command) { } void reportPinMode(int pin, String mode) { - String message = "{\"pin\":"; - message += pin; - message += ", \"mode\": \""; - message += mode; - message += "\"}"; - Bridge.writeMessage(message); + String json = "{\"pin\":"; + json += pin; + json += ", \"mode\": \""; + json += mode; + json += "\"}"; + Bridge.writeJSON(json); } void reportDigitalRead(int pin, boolean dataset) { int value = digitalRead(pin); - String message = "{\"pin\":"; - message += pin; - message += ", \"value\": "; - message += value; - message += "}"; - Bridge.writeMessage(message); + String json = "{\"pin\":"; + json += pin; + json += ", \"value\": "; + json += value; + json += "}"; + Bridge.writeJSON(json); if (dataset) { String key = "D"; @@ -118,12 +118,12 @@ void reportDigitalRead(int pin, boolean dataset) { void reportAnalogRead(int pin, boolean dataset) { int value = analogRead(pin); - String message = "{\"pin\":"; - message += pin; - message += ", \"value\": "; - message += value; - message += "}"; - Bridge.writeMessage(message); + String json = "{\"pin\":"; + json += pin; + json += ", \"value\": "; + json += value; + json += "}"; + Bridge.writeJSON(json); if (dataset) { String key = "A"; |