From fdfe4001d103d452ea6818f080a5020d3b75bd34 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Thu, 4 Jul 2013 16:33:10 +0200 Subject: BootWatcher removed BridgeNew is the new Bridge --- .../examples/BootWatcher001/BootWatcher001.ino | 90 -------- libraries/Bridge/examples/Bridge/Bridge.ino | 240 +++++++++++---------- libraries/Bridge/examples/BridgeNew/BridgeNew.ino | 174 --------------- 3 files changed, 132 insertions(+), 372 deletions(-) delete mode 100644 libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino delete mode 100644 libraries/Bridge/examples/BridgeNew/BridgeNew.ino (limited to 'libraries/Bridge') diff --git a/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino b/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino deleted file mode 100644 index 7833d54..0000000 --- a/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino +++ /dev/null @@ -1,90 +0,0 @@ - -/* - Arduino Yun Boot watcher - - Allows you to use the Yun's 32U4 processor as a - serial terminal for the linino processor - - Upload this to an Arduino Yun via serial (not WiFi) - then open the serial monitor at 115200 to see the boot process - of the linino processor. You can also use the serial monitor - as a basic command line interface for the linino processor using - this sketch. - - The circuit: - * Arduino Yun - - created March 2013 - by Massimo Banzi - modified 26 May 2013 - by Tom Igoe - - This example code is in the public domain. - */ - -long baud = 115200; - -// Pin 13 has an LED connected on most Arduino boards. -// give it a name: -int led = 13; -int ledState = HIGH; // whether the LED is high or low - -String bootString = ""; -int bootLineCount = 0; -boolean booting = true; - -void setup() { - Serial.begin(baud); // open serial connection to Linino - Serial1.begin(baud); // open serial connection via USB-Serial - - // initialize the digital pin as an output. - pinMode(led, OUTPUT); - digitalWrite(led, ledState); // turn the LED on (HIGH is the voltage level) - while(booting) { - listenForBoot(); - } - delay(500); -} - - -void loop() { - // After booting, become a serial terminal: - if (Serial.available()) { // got anything from USB-Serial? - char c = (char)Serial.read(); // read from USB-serial - Serial1.write(c); // write to Linino - ledState=!ledState; // invert LED state - digitalWrite(led, ledState); // toggle the LED - } - if (Serial1.available()) { // got anything from Linino? - char c = (char)Serial1.read(); // read from Linino - Serial.write(c); // write to USB-serial - } - -} - -void listenForBoot() { - char c; - if (Serial1.available()) { // got anything from Linino? - c = (char)Serial1.read(); // read from Linino - - if (c == '\n') { // clear the bootString every newline - bootLineCount++; // increment the boot line counter - Serial.println(bootLineCount); // print the count - bootString = ""; // clear the boot string - } - else { // anything other than newline, add to string - bootString += c; - } - } - - // look for the final boot string message: - if (bootString.endsWith("entered forwarding state")) { - Serial1.println(); - } - - // look for the command prompt: - if (bootString.endsWith(":/#")) { - Serial.println("Ready for action."); - booting = false; - } -} diff --git a/libraries/Bridge/examples/Bridge/Bridge.ino b/libraries/Bridge/examples/Bridge/Bridge.ino index c7627b2..eceab07 100644 --- a/libraries/Bridge/examples/Bridge/Bridge.ino +++ b/libraries/Bridge/examples/Bridge/Bridge.ino @@ -1,150 +1,174 @@ -//#include -#include +// Possible commands are listed here: +// +// "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) + +#include +#include + +// Listen on default port 5555, the webserver on the Yun +// will forward there all the HTTP requests for us. +YunServer server; void setup() { + Serial.begin(9600); + + // Bridge startup pinMode(13,OUTPUT); digitalWrite(13, LOW); Bridge.begin(); digitalWrite(13, HIGH); - Serial.begin(9600); + + // Listen for incoming connection only from localhost + // (no one from the external network could connect) + server.listenOnLocalhost(); + server.begin(); } void loop() { - while (Mailbox.messageAvailable()) { - String msg; - Mailbox.readMessage(msg); - process(msg); + // Get clients coming from server + YunClient client = server.accept(); + + // There is a new client? + if (client) { + // Process request + process(client); + + // Close connection and free resources. + client.stop(); } - delay(100); // Poll every 0.100s + + delay(50); // Poll every 50ms } -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) - - // is digital command? - if (command.startsWith("digital/")) { - // extract subcommand (after the "/") - command = command.substring(8); - digitalCommand(command); +void process(YunClient client) { + // read the command + String command = client.readStringUntil('/'); - } - // is analog command? - else if (command.startsWith("analog/")) { - // extract subcommand (after the "/") - command = command.substring(7); - analogCommand(command); + // is "digital" command? + if (command == "digital") { + digitalCommand(client); + } - } - // is mode command? - else if (command.startsWith("mode/")) { - // extract subcommand (after the "/") - command = command.substring(5); - modeCommand(command); + // is "analog" command? + if (command == "analog") { + analogCommand(client); + } + + // is "mode" command? + if (command == "mode") { + modeCommand(client); } } -void digitalCommand(String command) { +void digitalCommand(YunClient client) { int pin, value; - // Find the position of the "/" inside the command - int slashIndex = command.indexOf("/"); + // Read pin number + pin = client.parseInt(); - // 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(); + // If the next character is a '/' it means we have an URL + // with a value like: "/digital/13/1" + if (client.read() == '/') { + value = client.parseInt(); + digitalWrite(pin, value); } 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); + value = digitalRead(pin); } - reportDigitalRead(pin, true); + + // Send feedback to client + client.print(F("Pin D")); + client.print(pin); + client.print(F(" set to ")); + client.println(value); + + // Update datastore key with the current pin value + String key = "D"; + key += pin; + Bridge.put(key, String(value)); } -void analogCommand(String command) { +void analogCommand(YunClient client) { int pin, value; - if (command.indexOf("/") != -1) { - pin = command.substring(0, command.indexOf("/")).toInt(); - value = command.substring(command.indexOf("/") + 1, command.length()).toInt(); + + // Read pin number + pin = client.parseInt(); + + // If the next character is a '/' it means we have an URL + // with a value like: "/analog/5/120" + if (client.read() == '/') { + // Read value and execute command + value = client.parseInt(); analogWrite(pin, value); - } - else { - pin = command.toInt(); - } - reportAnalogRead(pin, true); -} -void modeCommand(String command) { - int pin; - String strValue; - pin = command.substring(0, command.indexOf("/")).toInt(); - strValue = command.substring(command.indexOf("/") + 1, command.length()); - if (strValue == "output") { - pinMode(pin, OUTPUT); - reportPinMode(pin, strValue); - } - else if (strValue == "input") { - pinMode(pin, INPUT); - reportPinMode(pin, strValue); + // Send feedback to client + client.print(F("Pin D")); + client.print(pin); + client.print(F(" set to analog ")); + client.println(value); + + // Update datastore key with the current pin value + String key = "D"; + key += pin; + Bridge.put(key, String(value)); } -} + else { + // Read analog pin + value = analogRead(pin); + + // Send feedback to client + client.print(F("Pin A")); + client.print(pin); + client.print(F(" reads analog ")); + client.println(value); -void reportPinMode(int pin, String mode) { - String json = "{\"pin\":"; - json += pin; - json += ", \"mode\": \""; - json += mode; - json += "\"}"; - Mailbox.writeJSON(json); + // Update datastore key with the current pin value + String key = "A"; + key += pin; + Bridge.put(key, String(value)); + } } -void reportDigitalRead(int pin, boolean dataset) { - int value = digitalRead(pin); +void modeCommand(YunClient client) { + int pin; - String json = "{\"pin\":"; - json += pin; - json += ", \"value\": "; - json += value; - json += "}"; - Mailbox.writeJSON(json); + // Read pin number + pin = client.parseInt(); - if (dataset) { - String key = "D"; - key += pin; - Bridge.put(key.c_str(), String(value).c_str()); + // If the next character is not a '/' we have a malformed URL + if (client.read() != '/') { + client.println(F("error")); + return; } -} -void reportAnalogRead(int pin, boolean dataset) { - int value = analogRead(pin); + String mode = client.readStringUntil('\r'); - String json = "{\"pin\":"; - json += pin; - json += ", \"value\": "; - json += value; - json += "}"; - Mailbox.writeJSON(json); + if (mode == "input") { + pinMode(pin, INPUT); + // Send feedback to client + client.print(F("Pin D")); + client.print(pin); + client.print(F(" configured as INPUT!")); + return; + } - if (dataset) { - String key = "A"; - key += pin; - Bridge.put(key.c_str(), String(value).c_str()); + if (mode == "output") { + pinMode(pin, OUTPUT); + // Send feedback to client + client.print(F("Pin D")); + client.print(pin); + client.print(F(" configured as OUTPUT!")); + return; } + + client.print(F("error: invalid mode ")); + client.print(mode); } + diff --git a/libraries/Bridge/examples/BridgeNew/BridgeNew.ino b/libraries/Bridge/examples/BridgeNew/BridgeNew.ino deleted file mode 100644 index eceab07..0000000 --- a/libraries/Bridge/examples/BridgeNew/BridgeNew.ino +++ /dev/null @@ -1,174 +0,0 @@ - -// Possible commands are listed here: -// -// "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) - -#include -#include - -// Listen on default port 5555, the webserver on the Yun -// will forward there all the HTTP requests for us. -YunServer server; - -void setup() { - Serial.begin(9600); - - // Bridge startup - pinMode(13,OUTPUT); - digitalWrite(13, LOW); - Bridge.begin(); - digitalWrite(13, HIGH); - - // Listen for incoming connection only from localhost - // (no one from the external network could connect) - server.listenOnLocalhost(); - server.begin(); -} - -void loop() { - // Get clients coming from server - YunClient client = server.accept(); - - // There is a new client? - if (client) { - // Process request - process(client); - - // Close connection and free resources. - client.stop(); - } - - delay(50); // Poll every 50ms -} - -void process(YunClient client) { - // read the command - String command = client.readStringUntil('/'); - - // is "digital" command? - if (command == "digital") { - digitalCommand(client); - } - - // is "analog" command? - if (command == "analog") { - analogCommand(client); - } - - // is "mode" command? - if (command == "mode") { - modeCommand(client); - } -} - -void digitalCommand(YunClient client) { - int pin, value; - - // Read pin number - pin = client.parseInt(); - - // If the next character is a '/' it means we have an URL - // with a value like: "/digital/13/1" - if (client.read() == '/') { - value = client.parseInt(); - digitalWrite(pin, value); - } - else { - value = digitalRead(pin); - } - - // Send feedback to client - client.print(F("Pin D")); - client.print(pin); - client.print(F(" set to ")); - client.println(value); - - // Update datastore key with the current pin value - String key = "D"; - key += pin; - Bridge.put(key, String(value)); -} - -void analogCommand(YunClient client) { - int pin, value; - - // Read pin number - pin = client.parseInt(); - - // If the next character is a '/' it means we have an URL - // with a value like: "/analog/5/120" - if (client.read() == '/') { - // Read value and execute command - value = client.parseInt(); - analogWrite(pin, value); - - // Send feedback to client - client.print(F("Pin D")); - client.print(pin); - client.print(F(" set to analog ")); - client.println(value); - - // Update datastore key with the current pin value - String key = "D"; - key += pin; - Bridge.put(key, String(value)); - } - else { - // Read analog pin - value = analogRead(pin); - - // Send feedback to client - client.print(F("Pin A")); - client.print(pin); - client.print(F(" reads analog ")); - client.println(value); - - // Update datastore key with the current pin value - String key = "A"; - key += pin; - Bridge.put(key, String(value)); - } -} - -void modeCommand(YunClient client) { - int pin; - - // Read pin number - pin = client.parseInt(); - - // If the next character is not a '/' we have a malformed URL - if (client.read() != '/') { - client.println(F("error")); - return; - } - - String mode = client.readStringUntil('\r'); - - if (mode == "input") { - pinMode(pin, INPUT); - // Send feedback to client - client.print(F("Pin D")); - client.print(pin); - client.print(F(" configured as INPUT!")); - return; - } - - if (mode == "output") { - pinMode(pin, OUTPUT); - // Send feedback to client - client.print(F("Pin D")); - client.print(pin); - client.print(F(" configured as OUTPUT!")); - return; - } - - client.print(F("error: invalid mode ")); - client.print(mode); -} - - -- cgit v1.2.3-18-g5258