From ff7999781c05b766cca2a8b66b3ed98473604593 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Fri, 18 Sep 2015 23:02:21 +0200 Subject: [PATCH] examples: added Arduino hello-world example --- examples/arduino_hello-world/Makefile | 20 ++++++ examples/arduino_hello-world/README.md | 43 +++++++++++++ .../arduino_hello-world/hello-world.sketch | 63 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 examples/arduino_hello-world/Makefile create mode 100644 examples/arduino_hello-world/README.md create mode 100644 examples/arduino_hello-world/hello-world.sketch diff --git a/examples/arduino_hello-world/Makefile b/examples/arduino_hello-world/Makefile new file mode 100644 index 000000000..44cc9d02b --- /dev/null +++ b/examples/arduino_hello-world/Makefile @@ -0,0 +1,20 @@ +# name of your application +APPLICATION = arduino_hello-world + +# If no BOARD is found in the environment, use this default: +BOARD ?= native + +# This has to be the absolute path to the RIOT base directory: +RIOTBASE ?= $(CURDIR)/../.. + +USEMODULE += arduino + +# Comment this out to disable code in RIOT that does safety checking +# which is not needed in a production environment but helps in the +# development process: +CFLAGS += -DDEVELHELP + +# Change this to 0 show compiler invocation lines by default: +QUIET ?= 1 + +include $(RIOTBASE)/Makefile.include diff --git a/examples/arduino_hello-world/README.md b/examples/arduino_hello-world/README.md new file mode 100644 index 000000000..aece6453e --- /dev/null +++ b/examples/arduino_hello-world/README.md @@ -0,0 +1,43 @@ +examples/arduino_hello-world +============================ +This application demonstrates the usage of Arduino sketches in RIOT. + +The sketch itself is fairly simple. On startup, it initializes the LED pin to +output mode, starts the serial port with a baudrate of 115200 and prints +"Hello Arduino!" on the serial port. When running, the application echoes any +newline terminated string that was received on the serial port, while toggling +the default LED with a 1Hz frequency. + +The sketch just uses some very primitive Arduino API elements for demonstration +purposes: +- control of digital pins (pinMode(), digital read and write) +- the delay() function +- reading and writing the serial port using the Serial class + +Arduino and RIOT +================ +For information on the Arduino support in RIOT please refer to the API +documentation at http://doc.riot-os.org/group__sys__arduino.html + +Usage +===== +Just send any newline terminated string to the board's serial port, and the +board will echo this string. + +Example output +============== +When using pyterm, the output will look similar to this: +``` +2015-11-26 14:04:58,307 - INFO # main(): This is RIOT! (Version: xxx) +2015-11-26 14:04:58,308 - INFO # Hello Arduino! +hello again +2015-11-26 14:06:29,800 - INFO # Echo: hello again +are you still there? +2015-11-26 14:06:48,301 - INFO # Echo: are you still there? + +``` +If your board is equipped with an on-board LED, you should see this LED toggling +every half a second. + +NOTE: if your board's STDIO baudrate is not configured to be 115200 (see your +board's `board.h`), the first line of the output may not be shown or scrambled. diff --git a/examples/arduino_hello-world/hello-world.sketch b/examples/arduino_hello-world/hello-world.sketch new file mode 100644 index 000000000..4247e352d --- /dev/null +++ b/examples/arduino_hello-world/hello-world.sketch @@ -0,0 +1,63 @@ +/* + Arduino Hello-World @ RIOT + Prints 'Hello Arduino!' once on the serial port during startup, toggles the + default LED twice each seconds and echoes incoming characters on the serial + port. + */ + +// Per convention, RIOT defines a macro that is assigned the pin number of an +// on-board LED. If no LED is available, the pin number defaults to 0. For +// compatibility with the Arduino IDE, we also fall back to pin 0 here, if the +// RIOT macro is not available +#ifndef ARDUINO_LED +#define ARDUINO_LED (0) +#endif + +// Assign the default LED pin +int ledPin = ARDUINO_LED; + +// input buffer for receiving chars on the serial port +int buf[64]; + +// counter that counts the number of received chars +int count = 0; + +void setup(void) +{ + // configure the LED pin to be output + pinMode(ledPin, OUTPUT); + // configure the first serial port to run with a baudrate of 115200 + Serial.begin(115200); + // say hello + Serial.println("Hello Arduino!"); +} + +void loop(void) +{ + // toggle the LED + digitalWrite(ledPin, !digitalRead(ledPin)); + // test if some chars were received + while (Serial.available() > 0) { + // read a single character + int tmp = Serial.read(); + // if we got a line end, we echo the buffer + if (tmp == '\n') { + // start with printing 'ECHO: ' + Serial.write("Echo: "); + // echo the buffer + for (int i = 0; i < count; i++) { + Serial.write(buf[i]); + } + // terminate the string with a newline + Serial.write('\n'); + // reset the count variable + count = 0; + } + // else we just remember the incoming char + else { + buf[count++] = tmp; + } + } + // wait for half a second + delay(500); +}