libraries / Bridge / examples / YunSerialTerminal / YunSerialTerminal.inoon commit Added link to project report (97a3ba0)
   1/*
   2  Arduino Yún USB-to-Serial
   3
   4  Allows you to use the Yún's 32U4 processor as a
   5  serial terminal for the Linux side on the Yún.
   6
   7  Upload this to an Arduino Yún via serial (not WiFi) then open
   8  the serial monitor at 115200 to see the boot process of Linux.
   9  You can also use the serial monitor as a basic command line
  10  interface for Linux using this sketch.
  11
  12  From the serial monitor the following commands can be issued:
  13
  14  '~' followed by '0' -> Set the UART speed to 57600 baud
  15  '~' followed by '1' -> Set the UART speed to 115200 baud
  16  '~' followed by '2' -> Set the UART speed to 250000 baud
  17  '~' followed by '3' -> Set the UART speed to 500000 baud
  18  '~' followed by '~' -> Sends the bridge's shutdown command to
  19                        obtain the console.
  20
  21  The circuit:
  22   Arduino Yún
  23
  24  created March 2013
  25  by Massimo Banzi
  26  modified by Cristian Maglie
  27
  28  This example code is in the public domain.
  29
  30  http://www.arduino.cc/en/Tutorial/YunSerialTerminal
  31
  32*/
  33
  34long linuxBaud = 250000;
  35
  36void setup() {
  37  SERIAL_PORT_USBVIRTUAL.begin(115200);  // open serial connection via USB-Serial
  38  SERIAL_PORT_HARDWARE.begin(linuxBaud); // open serial connection to Linux
  39}
  40
  41boolean commandMode = false;
  42
  43void loop() {
  44  // copy from USB-CDC to UART
  45  int c = SERIAL_PORT_USBVIRTUAL.read();    // read from USB-CDC
  46  if (c != -1) {                            // got anything?
  47    if (commandMode == false) {             // if we aren't in command mode...
  48      if (c == '~') {                       //    Tilde '~' key pressed?
  49        commandMode = true;                 //       enter in command mode
  50      } else {
  51        SERIAL_PORT_HARDWARE.write(c);      //    otherwise write char to UART
  52      }
  53    } else {                                // if we are in command mode...
  54      if (c == '0') {                       //     '0' key pressed?
  55        SERIAL_PORT_HARDWARE.begin(57600);  //        set speed to 57600
  56        SERIAL_PORT_USBVIRTUAL.println("Speed set to 57600");
  57      } else if (c == '1') {                //     '1' key pressed?
  58        SERIAL_PORT_HARDWARE.begin(115200); //        set speed to 115200
  59        SERIAL_PORT_USBVIRTUAL.println("Speed set to 115200");
  60      } else if (c == '2') {                //     '2' key pressed?
  61        SERIAL_PORT_HARDWARE.begin(250000); //        set speed to 250000
  62        SERIAL_PORT_USBVIRTUAL.println("Speed set to 250000");
  63      } else if (c == '3') {                //     '3' key pressed?
  64        SERIAL_PORT_HARDWARE.begin(500000); //        set speed to 500000
  65        SERIAL_PORT_USBVIRTUAL.println("Speed set to 500000");
  66      } else if (c == '~') {                //     '~` key pressed?
  67        SERIAL_PORT_HARDWARE.write((uint8_t *)"\xff\0\0\x05XXXXX\x7f\xf9", 11); // send "bridge shutdown" command
  68        SERIAL_PORT_USBVIRTUAL.println("Sending bridge's shutdown command");
  69      } else {                              //     any other key pressed?
  70        SERIAL_PORT_HARDWARE.write('~');    //        write '~' to UART
  71        SERIAL_PORT_HARDWARE.write(c);      //        write char to UART
  72      }
  73      commandMode = false;                  //     in all cases exit from command mode
  74    }
  75  }
  76
  77  // copy from UART to USB-CDC
  78  c = SERIAL_PORT_HARDWARE.read();          // read from UART
  79  if (c != -1) {                            // got anything?
  80    SERIAL_PORT_USBVIRTUAL.write(c);        //    write to USB-CDC
  81  }
  82}