|
| 1 | +/********************************************************************* |
| 2 | + Adafruit invests time and resources providing this open source code, |
| 3 | + please support Adafruit and open-source hardware by purchasing |
| 4 | + products from Adafruit! |
| 5 | +
|
| 6 | + MIT license, check LICENSE for more information |
| 7 | + Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 8 | + All text above, and the splash screen below must be included in |
| 9 | + any redistribution |
| 10 | +*********************************************************************/ |
| 11 | + |
| 12 | +#include "Adafruit_TinyUSB.h" |
| 13 | + |
| 14 | +/* This sketch demonstrates WebUSB as web serial with Chrome browser. |
| 15 | + * After enumerated successfully, Chrome will pop-up notification |
| 16 | + * with URL to landing page, click on it to test |
| 17 | + * - Click "Connect" and select device, When connected the on-board LED will litted up. |
| 18 | + * - Any charaters received from either webusb/Serial will be echo back to webusb and Serial |
| 19 | + * |
| 20 | + * Note: |
| 21 | + * - The WebUSB landing page notification is currently disabled in Chrome |
| 22 | + * on Windows due to Chromium issue 656702 (https://crbug.com/656702). You have to |
| 23 | + * go to https://adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial to test |
| 24 | + * |
| 25 | + * - On Windows 7 and prior: You need to use Zadig tool to manually bind the |
| 26 | + * WebUSB interface with the WinUSB driver for Chrome to access. From windows 8 and 10, this |
| 27 | + * is done automatically by firmware. |
| 28 | + */ |
| 29 | + |
| 30 | +// USB WebUSB object |
| 31 | +Adafruit_USBD_WebUSB usb_web; |
| 32 | + |
| 33 | +// Landing Page: scheme (0: http, 1: https), url |
| 34 | +WEBUSB_URL_DEF(landingPage, 1 /*https*/, "adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial"); |
| 35 | + |
| 36 | +int led_pin = LED_BUILTIN; |
| 37 | + |
| 38 | +// the setup function runs once when you press reset or power the board |
| 39 | +void setup() |
| 40 | +{ |
| 41 | + pinMode(led_pin, OUTPUT); |
| 42 | + digitalWrite(led_pin, LOW); |
| 43 | + |
| 44 | + usb_web.begin(); |
| 45 | + usb_web.setLandingPage(&landingPage); |
| 46 | + usb_web.setLineStateCallback(line_state_callback); |
| 47 | + |
| 48 | + Serial.begin(115200); |
| 49 | + |
| 50 | + // wait until device mounted |
| 51 | + while( !USBDevice.mounted() ) delay(1); |
| 52 | + |
| 53 | + Serial.println("TinyUSB WebUSB example"); |
| 54 | +} |
| 55 | + |
| 56 | +// function to echo to both Serial and WebUSB |
| 57 | +void echo_all(char chr) |
| 58 | +{ |
| 59 | + Serial.write(chr); |
| 60 | + // print extra newline for Serial |
| 61 | + if ( chr == '\r' ) Serial.write('\n'); |
| 62 | + |
| 63 | + usb_web.write(chr); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() |
| 67 | +{ |
| 68 | + // from WebUSB to both Serial & webUSB |
| 69 | + if (usb_web.available()) echo_all(usb_web.read()); |
| 70 | + |
| 71 | + // From Serial to both Serial & webUSB |
| 72 | + if (Serial.available()) echo_all(Serial.read()); |
| 73 | +} |
| 74 | + |
| 75 | +void line_state_callback(bool connected) |
| 76 | +{ |
| 77 | + digitalWrite(led_pin, connected); |
| 78 | + |
| 79 | + if ( connected ) usb_web.println("TinyUSB WebUSB example"); |
| 80 | +} |
0 commit comments