Blame view

RIOT/examples/arduino_hello-world/hello-world.sketch 2.06 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  /*
    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
  
  // For some boards RIOT defines a macro assigning the required baudrate of the
  // serial link. If this macro is not set, the default baudrate is set to
  // 115200.
  #ifdef UART_STDIO_BAUDRATE
  #define SERIAL_BAUDRATE UART_STDIO_BAUDRATE
  #else
  #define SERIAL_BAUDRATE 115200
  #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 the previously defined
      // baudrate
      Serial.begin(SERIAL_BAUDRATE);
      // 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);
  }