|
| 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 browser with WebUSB support (e.g Chrome). |
| 13 | + * After enumerated successfully, Browser will pop-up notification |
| 14 | + * with URL to landing page, click on it to test |
| 15 | + * - Click "Connect" and select device, When connected the neopixel LED will change color to Green. |
| 16 | + * - When received color from browser in format '#RRGGBB', device will change the color of neopixel accordingly |
| 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 landing page (below) 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 8 |
| 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 | +// convert a hex character to number |
| 76 | +uint8_t char2num(char c) |
| 77 | +{ |
| 78 | + if (c >= 'a') return c - 'a' + 10; |
| 79 | + if (c >= 'A') return c - 'A' + 10; |
| 80 | + return c - '0'; |
| 81 | +} |
| 82 | + |
| 83 | +void loop() |
| 84 | +{ |
| 85 | + // Landing Page 7 characters as hex color '#RRGGBB' |
| 86 | + if (usb_web.available() < 7) return; |
| 87 | + |
| 88 | + uint8_t input[7]; |
| 89 | + usb_web.readBytes(input, 7); |
| 90 | + |
| 91 | + // Print to serial for debugging |
| 92 | + Serial.write(input, 7); |
| 93 | + Serial.println(); |
| 94 | + |
| 95 | + uint8_t red = 16*char2num(input[1]) + char2num(input[2]); |
| 96 | + uint8_t green = 16*char2num(input[3]) + char2num(input[4]); |
| 97 | + uint8_t blue = 16*char2num(input[5]) + char2num(input[6]); |
| 98 | + |
| 99 | + pixels.setPixelColor(0, red, green, blue); |
| 100 | + pixels.show(); |
| 101 | +} |
| 102 | + |
| 103 | +void line_state_callback(bool connected) |
| 104 | +{ |
| 105 | + // connected = green, disconnected = red |
| 106 | + pixels.setPixelColor(0, connected ? 0x00ff00 : 0xff0000); |
| 107 | + pixels.show(); |
| 108 | +} |
0 commit comments