|
| 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 | +/* This sketch demonstrates WebUSB as web serial with Chrome browser. |
| 13 | + * After enumerated successfully, Chrome will pop-up notification |
| 14 | + * with URL to landing page, click on it to test |
| 15 | + * - Click "Connect" and select device, When connected the on-board LED will litted up. |
| 16 | + * - Any charaters received from either webusb/Serial will be echo back to webusb and Serial |
| 17 | + * |
| 18 | + * Note: |
| 19 | + * - The WebUSB landing page notification is currently disabled in Chrome |
| 20 | + * on Windows due to Chromium issue 656702 (https://crbug.com/656702). You have to |
| 21 | + * go to https://adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial to test |
| 22 | + * |
| 23 | + * - On Windows 7 and prior: You need to use Zadig tool to manually bind the |
| 24 | + * WebUSB interface with the WinUSB driver for Chrome to access. From windows 8 and 10, this |
| 25 | + * is done automatically by firmware. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "Adafruit_TinyUSB.h" |
| 29 | +#include <Adafruit_NeoPixel.h> |
| 30 | + |
| 31 | +// Which pin on the Arduino is connected to the NeoPixels? |
| 32 | +// On a Trinket or Gemma we suggest changing this to 1 |
| 33 | +// use on-board neopixel PIN_NEOPIXEL if existed |
| 34 | +#ifdef PIN_NEOPIXEL |
| 35 | + #define PIN PIN_NEOPIXEL |
| 36 | +#else |
| 37 | + #define PIN 6 |
| 38 | +#endif |
| 39 | + |
| 40 | +// How many NeoPixels are attached to the Arduino? |
| 41 | +#define NUMPIXELS 1 |
| 42 | + |
| 43 | +// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. |
| 44 | +// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest |
| 45 | +// example for more information on possible values. |
| 46 | +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); |
| 47 | + |
| 48 | +// USB WebUSB object |
| 49 | +Adafruit_USBD_WebUSB usb_web; |
| 50 | + |
| 51 | +// Landing Page: scheme (0: http, 1: https), url |
| 52 | +WEBUSB_URL_DEF(landingPage, 1 /*https*/, "adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-rgb"); |
| 53 | + |
| 54 | +// the setup function runs once when you press reset or power the board |
| 55 | +void setup() |
| 56 | +{ |
| 57 | + usb_web.begin(); |
| 58 | + usb_web.setLandingPage(&landingPage); |
| 59 | + usb_web.setLineStateCallback(line_state_callback); |
| 60 | + |
| 61 | + Serial.begin(115200); |
| 62 | + |
| 63 | + // This initializes the NeoPixel with RED |
| 64 | + pixels.begin(); |
| 65 | + pixels.setBrightness(50); |
| 66 | + pixels.setPixelColor(0, 0xff0000); |
| 67 | + pixels.show(); |
| 68 | + |
| 69 | + // wait until device mounted |
| 70 | + while( !USBDevice.mounted() ) delay(1); |
| 71 | + |
| 72 | + Serial.println("TinyUSB WebUSB RGB example"); |
| 73 | +} |
| 74 | + |
| 75 | +void loop() |
| 76 | +{ |
| 77 | + // Landing Page send 3 bytes of color as RGB |
| 78 | + if (usb_web.available() < 3 ) return; |
| 79 | + |
| 80 | + pixels.setPixelColor(0, usb_web.read(), usb_web.read(), usb_web.read()); |
| 81 | + pixels.show(); |
| 82 | +} |
| 83 | + |
| 84 | +void line_state_callback(bool connected) |
| 85 | +{ |
| 86 | + // connected = green, disconnected = red |
| 87 | + pixels.setPixelColor(0, connected ? 0x00ff00 : 0xff0000); |
| 88 | + pixels.show(); |
| 89 | +} |
0 commit comments