diff options
Diffstat (limited to 'libraries/Bridge/examples')
-rw-r--r-- | libraries/Bridge/examples/Bridge/Bridge.ino | 89 |
1 files changed, 53 insertions, 36 deletions
diff --git a/libraries/Bridge/examples/Bridge/Bridge.ino b/libraries/Bridge/examples/Bridge/Bridge.ino index df1e069..c7627b2 100644 --- a/libraries/Bridge/examples/Bridge/Bridge.ino +++ b/libraries/Bridge/examples/Bridge/Bridge.ino @@ -1,51 +1,50 @@ -#include <Bridge.h> +//#include <Bridge.h> +#include <Mailbox.h> void setup() { pinMode(13,OUTPUT); digitalWrite(13, LOW); Bridge.begin(); - digitalWrite(13, HIGH); + digitalWrite(13, HIGH); + Serial.begin(9600); } void loop() { - while (Bridge.messageAvailable()) { - uint8_t buff[64]; - int l = Bridge.readMessage(buff, 64); - process(buff, l); + while (Mailbox.messageAvailable()) { + String msg; + Mailbox.readMessage(msg); + process(msg); } delay(100); // Poll every 0.100s } -void process(uint8_t buff[], int length) { - // "digital/13/1" -> digitalWrite(13, HIGH) +void process(String command) { + Serial.println(command); // "digital/13" -> digitalRead(13) + // "digital/13/1" -> digitalWrite(13, HIGH) // "analog/2/123" -> analogWrite(2, 123) // "analog/2" -> analogRead(2) // "mode/13/input" -> pinMode(13, INPUT) // "mode/13/output" -> pinMode(13, OUTPUT) - // Sanity check - if (length < 9 || length > 14) - return; - - // string terminator - buff[length] = '\0'; - - String command = String((char*)buff); - - // digital command - if (command.indexOf("digital/") == 0) { + // is digital command? + if (command.startsWith("digital/")) { + // extract subcommand (after the "/") command = command.substring(8); digitalCommand(command); - // analog command - } else if (command.indexOf("analog/") == 0) { + } + // is analog command? + else if (command.startsWith("analog/")) { + // extract subcommand (after the "/") command = command.substring(7); analogCommand(command); - - // mode command - } else if (command.indexOf("mode/") == 0) { + + } + // is mode command? + else if (command.startsWith("mode/")) { + // extract subcommand (after the "/") command = command.substring(5); modeCommand(command); } @@ -53,12 +52,27 @@ void process(uint8_t buff[], int length) { void digitalCommand(String command) { int pin, value; - if (command.indexOf("/") != -1) { - pin = command.substring(0, command.indexOf("/")).toInt(); - value = command.substring(command.indexOf("/") + 1, command.length()).toInt(); - digitalWrite(pin, value); - } else { + + // Find the position of the "/" inside the command + int slashIndex = command.indexOf("/"); + + // If there are no slashes + if (slashIndex == -1) { + // then we are in the following case: + // "digital/13" -> digitalRead(13) + + // so we can extract the pin number from the remainder of the command string pin = command.toInt(); + } + else { + // else, we found a slash, so we are in the following case: + // "digital/13/1" -> digitalWrite(13, HIGH) + + // we must estract pin number before the "/" + pin = command.substring(0, slashIndex).toInt(); + // and value after the "/" + value = command.substring(slashIndex+1).toInt(); + digitalWrite(pin, value); } reportDigitalRead(pin, true); } @@ -69,7 +83,8 @@ void analogCommand(String command) { pin = command.substring(0, command.indexOf("/")).toInt(); value = command.substring(command.indexOf("/") + 1, command.length()).toInt(); analogWrite(pin, value); - } else { + } + else { pin = command.toInt(); } reportAnalogRead(pin, true); @@ -83,7 +98,8 @@ void modeCommand(String command) { if (strValue == "output") { pinMode(pin, OUTPUT); reportPinMode(pin, strValue); - } else if (strValue == "input") { + } + else if (strValue == "input") { pinMode(pin, INPUT); reportPinMode(pin, strValue); } @@ -95,18 +111,18 @@ void reportPinMode(int pin, String mode) { json += ", \"mode\": \""; json += mode; json += "\"}"; - Bridge.writeJSON(json); + Mailbox.writeJSON(json); } void reportDigitalRead(int pin, boolean dataset) { int value = digitalRead(pin); - + String json = "{\"pin\":"; json += pin; json += ", \"value\": "; json += value; json += "}"; - Bridge.writeJSON(json); + Mailbox.writeJSON(json); if (dataset) { String key = "D"; @@ -117,13 +133,13 @@ void reportDigitalRead(int pin, boolean dataset) { void reportAnalogRead(int pin, boolean dataset) { int value = analogRead(pin); - + String json = "{\"pin\":"; json += pin; json += ", \"value\": "; json += value; json += "}"; - Bridge.writeJSON(json); + Mailbox.writeJSON(json); if (dataset) { String key = "A"; @@ -131,3 +147,4 @@ void reportAnalogRead(int pin, boolean dataset) { Bridge.put(key.c_str(), String(value).c_str()); } } + |