From 7b25e85a5636ec70899902ead7e5a31a362257f1 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 25 Jun 2013 10:42:30 -0400 Subject: [PATCH 1/6] Updated XivelyClient to use Serial instead of Console --- .gitignore | 2 ++ .../Bridge/examples/XivelyClient/XivelyClient.ino | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 86788c225..1ff22582a 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ test-bin .idea hardware/arduino/avr/libraries/Bridge/examples/XivelyClient/passwords.h + +hardware/arduino/avr/libraries/Bridge/examples/.DS_Store diff --git a/hardware/arduino/avr/libraries/Bridge/examples/XivelyClient/XivelyClient.ino b/hardware/arduino/avr/libraries/Bridge/examples/XivelyClient/XivelyClient.ino index 48b1403cc..69f979c89 100644 --- a/hardware/arduino/avr/libraries/Bridge/examples/XivelyClient/XivelyClient.ino +++ b/hardware/arduino/avr/libraries/Bridge/examples/XivelyClient/XivelyClient.ino @@ -33,10 +33,10 @@ String dataString = ""; void setup() { // start serial port: Bridge.begin(); - Console.begin(); + Serial.begin(9600); - while(!Console); // wait for Network Console to open - Console.println("Xively client"); + while(!Serial); // wait for Network Serial to open + Serial.println("Xively client"); // Do a first update immediately updateData(); @@ -83,7 +83,7 @@ void sendData() { // sendData function finishes the resources are immediately // released. Declaring it global works too, BTW. Process xively; - Console.print("\n\nSending data... "); + Serial.print("\n\nSending data... "); xively.begin("curl"); xively.addParameter("-k"); xively.addParameter("--request"); @@ -94,13 +94,13 @@ void sendData() { xively.addParameter(apiString); xively.addParameter(url); xively.run(); - Console.println("done!"); + Serial.println("done!"); // If there's incoming data from the net connection, - // send it out the Console: + // send it out the Serial: while (xively.available()>0) { char c = xively.read(); - Console.write(c); + Serial.write(c); } } From 86438fc84b663ebee6dbe357178d92b3562a3606 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 25 Jun 2013 13:57:19 -0400 Subject: [PATCH 2/6] Added ConsolePixel example --- .../examples/ConsolePixel/ConsolePixel.ino | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 hardware/arduino/avr/libraries/Bridge/examples/ConsolePixel/ConsolePixel.ino diff --git a/hardware/arduino/avr/libraries/Bridge/examples/ConsolePixel/ConsolePixel.ino b/hardware/arduino/avr/libraries/Bridge/examples/ConsolePixel/ConsolePixel.ino new file mode 100644 index 000000000..420146512 --- /dev/null +++ b/hardware/arduino/avr/libraries/Bridge/examples/ConsolePixel/ConsolePixel.ino @@ -0,0 +1,58 @@ +/* + Console Pixel + + An example of using the Arduino board to receive data from the + Console on the Arduino Yun. In this case, the Arduino boards turns on an LED when + it receives the character 'H', and turns off the LED when it + receives the character 'L'. + + To see the Console, pick your Yun's name and IP address in the Port menu + then open the Port Monitor. You can also see it by opening a terminal window + and typing + ssh root@ yourYunsName.local 'telnet localhost 6571' + then pressing enter. When prompted for the password, enter it. + + + The circuit: + * LED connected from digital pin 13 to ground + + created 2006 + by David A. Mellis + modified 25 Jun 2013 + by Tom Igoe + + This example code is in the public domain. + + */ +#include + +const int ledPin = 13; // the pin that the LED is attached to +char incomingByte; // a variable to read incoming Console data into + +void setup() { + // initialize Console communication: + Bridge.begin(); + Console.begin(); + while(!Console); // wait for the Console to open from the remote side + Console.println("type H or L to turn pin 13 on or off"); + // initialize the LED pin as an output: + pinMode(ledPin, OUTPUT); +} + +void loop() { + // see if there's incoming Console data: + if (Console.available() > 0) { + // read the oldest byte in the Console buffer: + incomingByte = Console.read(); + Console.println(incomingByte); + // if it's a capital H (ASCII 72), turn on the LED: + if (incomingByte == 'H') { + digitalWrite(ledPin, HIGH); + } + // if it's an L (ASCII 76) turn off the LED: + if (incomingByte == 'L') { + digitalWrite(ledPin, LOW); + } + } +} + From 20be75fe773e52aff1c414c9dd0f5521e6090fb2 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 25 Jun 2013 14:07:53 -0400 Subject: [PATCH 3/6] Made ShellCommands example more physical --- .../examples/ShellCommands/ShellCommands.ino | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/hardware/arduino/avr/libraries/Bridge/examples/ShellCommands/ShellCommands.ino b/hardware/arduino/avr/libraries/Bridge/examples/ShellCommands/ShellCommands.ino index 47579f886..d2f9b7a7b 100644 --- a/hardware/arduino/avr/libraries/Bridge/examples/ShellCommands/ShellCommands.ino +++ b/hardware/arduino/avr/libraries/Bridge/examples/ShellCommands/ShellCommands.ino @@ -3,18 +3,21 @@ Running shell coommands using Process class. This sketch demonstrate how to run linux shell commands - using an Arduino Yún. - + using an Arduino Yún. It runs the wifiCheck script on the linino side + of the Yun, then uses grep to get just the signal strength line. + Then it uses parseInt() to read the wifi signal strength as an integer, + and finally uses that number to fade an LED using analogWrite(). + The circuit: - * Arduino Yun + * Arduino Yun with LED connected to pin 9 created 12 Jun 2013 by Cristian Maglie - modified 21 June 2013 + modified 25 June 2013 by Tom Igoe This example code is in the public domain. - + */ #include @@ -27,15 +30,23 @@ void setup() { void loop() { Process p; - // This command line prints the name of the wireless network - // that the board is connected to, or the network which the board has created: - p.runShellCommand(F("lua /usr/lib/lua/pretty_wifi_info.lua | grep SSID")); + // This command line runs the wifiCheck script, (lua /arduino/pretty...), then + // sends the result to the grep command to look for a line containing the word + // "Signal:" the result is passed to this sketch: + p.runShellCommand("lua /arduino/pretty_wifi_info.lua | grep Signal"); - // Read command output + // do nothing until the process finishes, so you get the whole output: + while(p.running()); + + // Read command output. runShellCommand() should have passed "Signal: xx&": while (p.available()) { - char c = p.read(); - Serial.print(c); + int result = p.parseInt(); // look for an integer + int signal = map(result, 0, 100, 0, 255); // map result from 0-100 range to 0-255 + analogWrite(9, signal); // set the brightness of LED on pin 9 + Serial.println(result); // print the number as well } - while (true); // do nothing more + delay(5000); // wait 5 seconds before you do it again } + + From 382fe3a080c585660530f75aacdeb6e218db2a59 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 21 Jun 2013 19:58:08 +0200 Subject: [PATCH 4/6] [sam] 2 bugfix to SPI library. - begin() after end() now works. - unconnected SPI pin is selected as CS when old (non extended) API is used. --- build/shared/revisions.txt | 2 ++ hardware/arduino/sam/libraries/SPI/SPI.cpp | 9 +++++---- hardware/arduino/sam/variants/arduino_due_x/variant.h | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/build/shared/revisions.txt b/build/shared/revisions.txt index 2fc67e11e..d9e9b863f 100644 --- a/build/shared/revisions.txt +++ b/build/shared/revisions.txt @@ -20,6 +20,8 @@ ARDUINO 1.5.3 BETA [libraries] * sam: Added CAN library (still in early stage of development) (Palliser) +* sam: Bugfix SPI library: begin() after end() now works (stimmer) +* sam: Bugfix SPI library: incorrent pin configuration in non-extended mode. [firmwares] * Arduino Due: fixed USB2Serial garbage at startup (https://github.com/arduino/Arduino/pull/1267) diff --git a/hardware/arduino/sam/libraries/SPI/SPI.cpp b/hardware/arduino/sam/libraries/SPI/SPI.cpp index ef931a3f4..017ec4edb 100644 --- a/hardware/arduino/sam/libraries/SPI/SPI.cpp +++ b/hardware/arduino/sam/libraries/SPI/SPI.cpp @@ -13,13 +13,14 @@ SPIClass::SPIClass(Spi *_spi, uint32_t _id, void(*_initCb)(void)) : spi(_spi), id(_id), initCb(_initCb) { - initCb(); - - SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS); - SPI_Enable(spi); + // Empty } void SPIClass::begin() { + initCb(); + SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS); + SPI_Enable(spi); + // NPCS control is left to the user // Default speed set to 4Mhz diff --git a/hardware/arduino/sam/variants/arduino_due_x/variant.h b/hardware/arduino/sam/variants/arduino_due_x/variant.h index 75fd65474..ef2587688 100644 --- a/hardware/arduino/sam/variants/arduino_due_x/variant.h +++ b/hardware/arduino/sam/variants/arduino_due_x/variant.h @@ -84,7 +84,7 @@ extern "C"{ #define BOARD_SPI_SS1 (4u) #define BOARD_SPI_SS2 (52u) #define BOARD_SPI_SS3 PIN_SPI_SS3 -#define BOARD_SPI_DEFAULT_SS BOARD_SPI_SS2 +#define BOARD_SPI_DEFAULT_SS BOARD_SPI_SS3 #define BOARD_PIN_TO_SPI_PIN(x) \ (x==BOARD_SPI_SS0 ? PIN_SPI_SS0 : \ From 37825c1711446463f91ddf9150b203be5158bb9b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 21 Jun 2013 21:23:12 +0200 Subject: [PATCH 5/6] String: changed default to 2 decimal digits --- hardware/arduino/avr/cores/arduino/WString.cpp | 4 ++-- hardware/arduino/avr/cores/arduino/WString.h | 4 ++-- hardware/arduino/avr/cores/robot/WString.cpp | 4 ++-- hardware/arduino/avr/cores/robot/WString.h | 4 ++-- hardware/arduino/sam/cores/arduino/WString.cpp | 4 ++-- hardware/arduino/sam/cores/arduino/WString.h | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/WString.cpp b/hardware/arduino/avr/cores/arduino/WString.cpp index 8c85cc592..48c30df77 100644 --- a/hardware/arduino/avr/cores/arduino/WString.cpp +++ b/hardware/arduino/avr/cores/arduino/WString.cpp @@ -325,14 +325,14 @@ unsigned char String::concat(unsigned long num) unsigned char String::concat(float num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 8, 2, buf); return concat(string, strlen(string)); } unsigned char String::concat(double num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 8, 2, buf); return concat(string, strlen(string)); } diff --git a/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index 44a841930..b3b9f1427 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -69,8 +69,8 @@ public: explicit String(unsigned int, unsigned char base=10); explicit String(long, unsigned char base=10); explicit String(unsigned long, unsigned char base=10); - explicit String(float, int decimalPlaces=6); - explicit String(double, int decimalPlaces=6); + explicit String(float, int decimalPlaces=2); + explicit String(double, int decimalPlaces=2); ~String(void); // memory management diff --git a/hardware/arduino/avr/cores/robot/WString.cpp b/hardware/arduino/avr/cores/robot/WString.cpp index 8c85cc592..48c30df77 100644 --- a/hardware/arduino/avr/cores/robot/WString.cpp +++ b/hardware/arduino/avr/cores/robot/WString.cpp @@ -325,14 +325,14 @@ unsigned char String::concat(unsigned long num) unsigned char String::concat(float num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 8, 2, buf); return concat(string, strlen(string)); } unsigned char String::concat(double num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 8, 2, buf); return concat(string, strlen(string)); } diff --git a/hardware/arduino/avr/cores/robot/WString.h b/hardware/arduino/avr/cores/robot/WString.h index 4b9e64e0b..0404f767a 100644 --- a/hardware/arduino/avr/cores/robot/WString.h +++ b/hardware/arduino/avr/cores/robot/WString.h @@ -69,8 +69,8 @@ public: explicit String(unsigned int, unsigned char base=10); explicit String(long, unsigned char base=10); explicit String(unsigned long, unsigned char base=10); - explicit String(float, int decimalPlaces=6); - explicit String(double, int decimalPlaces=6); + explicit String(float, int decimalPlaces=2); + explicit String(double, int decimalPlaces=2); ~String(void); // memory management diff --git a/hardware/arduino/sam/cores/arduino/WString.cpp b/hardware/arduino/sam/cores/arduino/WString.cpp index de1a2f1b2..d6b344c2a 100644 --- a/hardware/arduino/sam/cores/arduino/WString.cpp +++ b/hardware/arduino/sam/cores/arduino/WString.cpp @@ -326,14 +326,14 @@ unsigned char String::concat(unsigned long num) unsigned char String::concat(float num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 8, 2, buf); return concat(string, strlen(string)); } unsigned char String::concat(double num) { char buf[20]; - char* string = dtostrf(num, 8, 6, buf); + char* string = dtostrf(num, 8, 2, buf); return concat(string, strlen(string)); } diff --git a/hardware/arduino/sam/cores/arduino/WString.h b/hardware/arduino/sam/cores/arduino/WString.h index 4b9e64e0b..0404f767a 100644 --- a/hardware/arduino/sam/cores/arduino/WString.h +++ b/hardware/arduino/sam/cores/arduino/WString.h @@ -69,8 +69,8 @@ public: explicit String(unsigned int, unsigned char base=10); explicit String(long, unsigned char base=10); explicit String(unsigned long, unsigned char base=10); - explicit String(float, int decimalPlaces=6); - explicit String(double, int decimalPlaces=6); + explicit String(float, int decimalPlaces=2); + explicit String(double, int decimalPlaces=2); ~String(void); // memory management From c2897c4a3f280add7192e4f92522f684310078a7 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Tue, 25 Jun 2013 16:13:14 +0200 Subject: [PATCH 6/6] IDE network discovery now reacts to new networks: it monitors available IP addresses and, when one is added, it starts listening to bonjours coming from it --- app/src/cc/arduino/packages/Discovery.java | 2 +- .../cc/arduino/packages/DiscoveryManager.java | 17 +++--- .../discoverers/NetworkDiscovery.java | 52 +++++++++++++------ .../discoverers/network/NetworkChecker.java | 46 ++++++++++++++++ .../network/NetworkTopologyListener.java | 11 ++++ 5 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 app/src/cc/arduino/packages/discoverers/network/NetworkChecker.java create mode 100644 app/src/cc/arduino/packages/discoverers/network/NetworkTopologyListener.java diff --git a/app/src/cc/arduino/packages/Discovery.java b/app/src/cc/arduino/packages/Discovery.java index 60cb54708..17ba91588 100644 --- a/app/src/cc/arduino/packages/Discovery.java +++ b/app/src/cc/arduino/packages/Discovery.java @@ -23,7 +23,7 @@ public interface Discovery { /** * Stop discovery service */ - public void stop(); + public void stop() throws Exception; /** * Return the list of discovered ports. diff --git a/app/src/cc/arduino/packages/DiscoveryManager.java b/app/src/cc/arduino/packages/DiscoveryManager.java index 4a002755f..8681acc9b 100644 --- a/app/src/cc/arduino/packages/DiscoveryManager.java +++ b/app/src/cc/arduino/packages/DiscoveryManager.java @@ -1,12 +1,12 @@ package cc.arduino.packages; -import static processing.app.I18n._; +import cc.arduino.packages.discoverers.NetworkDiscovery; +import cc.arduino.packages.discoverers.SerialDiscovery; import java.util.ArrayList; import java.util.List; -import cc.arduino.packages.discoverers.NetworkDiscovery; -import cc.arduino.packages.discoverers.SerialDiscovery; +import static processing.app.I18n._; public class DiscoveryManager { @@ -25,7 +25,7 @@ public class DiscoveryManager { e.printStackTrace(); } } - + Runtime.getRuntime().addShutdownHook(closeHook); } @@ -39,8 +39,13 @@ public class DiscoveryManager { private Thread closeHook = new Thread(new Runnable() { @Override public void run() { - for (Discovery d : discoverers) - d.stop(); + for (Discovery d : discoverers) { + try { + d.stop(); + } catch (Exception e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } } }); } diff --git a/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java b/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java index 630109ade..039e959e2 100644 --- a/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java +++ b/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java @@ -2,6 +2,7 @@ package cc.arduino.packages.discoverers; import cc.arduino.packages.BoardPort; import cc.arduino.packages.Discovery; +import cc.arduino.packages.discoverers.network.NetworkChecker; import processing.app.Base; import processing.app.helpers.PreferencesMap; import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter; @@ -12,17 +13,18 @@ import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; +import java.util.*; -public class NetworkDiscovery implements Discovery, ServiceListener { +public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.packages.discoverers.network.NetworkTopologyListener { + private Timer timer; private List ports; + private final Map mappedJmDNSs; public NetworkDiscovery() { DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter()); this.ports = new ArrayList(); + this.mappedJmDNSs = new Hashtable(); } @Override @@ -56,21 +58,14 @@ public class NetworkDiscovery implements Discovery, ServiceListener { @Override public void start() throws IOException { - for (InetAddress addr : NetworkTopologyDiscovery.Factory.getInstance().getInetAddresses()) { - JmDNS jmDNS = JmDNS.create(addr); - jmDNS.addServiceListener("_arduino._tcp.local.", this); - } + this.timer = new Timer(this.getClass().getName() + " timer"); + new NetworkChecker(this, NetworkTopologyDiscovery.Factory.getInstance()).start(timer); } @Override - public void stop() { - // Removed cleanup: is extremely slow on closing - - // try { - // jmDNS.close(); - // } catch (IOException e) { - // e.printStackTrace(); - // } + public void stop() throws IOException { + timer.purge(); + // we don't close each JmDNS instance as it's too slow } @Override @@ -130,4 +125,29 @@ public class NetworkDiscovery implements Discovery, ServiceListener { } } + @Override + public void inetAddressAdded(InetAddress address) { + if (mappedJmDNSs.containsKey(address)) { + return; + } + try { + JmDNS jmDNS = JmDNS.create(address); + jmDNS.addServiceListener("_arduino._tcp.local.", this); + mappedJmDNSs.put(address, jmDNS); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void inetAddressRemoved(InetAddress address) { + JmDNS jmDNS = mappedJmDNSs.remove(address); + if (jmDNS != null) { + try { + jmDNS.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } diff --git a/app/src/cc/arduino/packages/discoverers/network/NetworkChecker.java b/app/src/cc/arduino/packages/discoverers/network/NetworkChecker.java new file mode 100644 index 000000000..8ee81b7a6 --- /dev/null +++ b/app/src/cc/arduino/packages/discoverers/network/NetworkChecker.java @@ -0,0 +1,46 @@ +package cc.arduino.packages.discoverers.network; + +import javax.jmdns.NetworkTopologyDiscovery; +import java.net.InetAddress; +import java.util.*; + +public class NetworkChecker extends TimerTask { + + private final NetworkTopologyListener topologyListener; + private final NetworkTopologyDiscovery topology; + + private Set knownAddresses; + + public NetworkChecker(NetworkTopologyListener topologyListener, NetworkTopologyDiscovery topology) { + super(); + this.topologyListener = topologyListener; + this.topology = topology; + this.knownAddresses = Collections.synchronizedSet(new HashSet()); + } + + public void start(Timer timer) { + timer.schedule(this, 0, 3000); + } + + @Override + public void run() { + try { + InetAddress[] curentAddresses = topology.getInetAddresses(); + Set current = new HashSet(curentAddresses.length); + for (InetAddress address : curentAddresses) { + current.add(address); + if (!knownAddresses.contains(address)) { + topologyListener.inetAddressAdded(address); + } + } + for (InetAddress address : knownAddresses) { + if (!current.contains(address)) { + topologyListener.inetAddressRemoved(address); + } + } + knownAddresses = current; + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/cc/arduino/packages/discoverers/network/NetworkTopologyListener.java b/app/src/cc/arduino/packages/discoverers/network/NetworkTopologyListener.java new file mode 100644 index 000000000..312deccb5 --- /dev/null +++ b/app/src/cc/arduino/packages/discoverers/network/NetworkTopologyListener.java @@ -0,0 +1,11 @@ +package cc.arduino.packages.discoverers.network; + +import java.net.InetAddress; + +public interface NetworkTopologyListener { + + void inetAddressAdded(InetAddress address); + + void inetAddressRemoved(InetAddress address); + +}