Skip to content

Commit 01897a8

Browse files
authored
Merge pull request #22 from adafruit/develop
fix #11 added support for webusb with webusb_serial example
2 parents cbc44d8 + 8318005 commit 01897a8

File tree

7 files changed

+459
-2
lines changed

7 files changed

+459
-2
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ git:
1010
env:
1111
global:
1212
- PRETTYNAME="Adafruit TinyUSB Library"
13+
- BSP_PATH="$HOME/.arduino15/packages/adafruit/hardware"
1314
#- DOXYFILE=$TRAVIS_BUILD_DIR/.doxyfile
1415

1516
addons:
@@ -24,6 +25,14 @@ before_install:
2425
# add tinyusb option for m4
2526
- sed -i 's/\(m4:speed=120\)/\1,usbstack=tinyusb/g' install.sh
2627
- source install.sh
28+
# Remove release code and clone git master of nRF52
29+
- BSP_VERSION=`eval ls $BSP_PATH/nrf52`
30+
- rm -rf $BSP_PATH/nrf52/*
31+
- git clone --quiet https://github.com/adafruit/Adafruit_nRF52_Arduino.git $BSP_PATH/nrf52/$BSP_VERSION
32+
# Remove release code and clone git master of SAMD
33+
- BSP_VERSION=`eval ls $BSP_PATH/samd`
34+
- rm -rf $BSP_PATH/samd/*
35+
- git clone --quiet https://github.com/adafruit/ArduinoCore-samd.git $BSP_PATH/samd/$BSP_VERSION
2736

2837
install:
2938
- arduino --install-library "Adafruit SPIFlash","MIDI Library","Adafruit seesaw Library"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.com/adafruit/Adafruit_TinyUSB_Arduino.svg?branch=master)](https://travis-ci.com/adafruit/Adafruit_TinyUSB_Arduino)[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
44

5-
[TinyUSB](https://github.com/hathach/tinyusb) library for Arduino IDE. This library works with core platform that includes TinyUSB stack, typically you will see **Adafruit_TinyUSB_Core** inside core folder. Supported platform are:
5+
This library works with platform that includes [TinyUSB stack](https://github.com/hathach/tinyusb), typically you will see **Adafruit_TinyUSB_Core** inside core folder. Supported platform are:
66

77
- [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
88
- [Adafruit ArduinoCore-samd](https://github.com/adafruit/ArduinoCore-samd) **TinyUSB** must be selected in menu `Tools->USB Stack`
@@ -12,5 +12,6 @@ In addition to CDC that provided by the platform core, this library provide plat
1212
- Human Interface Device (HID): Generic (In & Out), Keyboard, Mouse, Gamepad etc ...
1313
- Mass Storage Class (MSC): with multiple LUNs
1414
- Musical Instrument Digital Interface (MIDI)
15+
- WebUSB with vendor specific class
1516

1617
More document to write ...
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

src/Adafruit_TinyUSB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
#include "Adafruit_USBD_MSC.h"
2929
#include "Adafruit_USBD_HID.h"
3030
#include "Adafruit_USBD_MIDI.h"
31+
#include "Adafruit_USBD_WebUSB.h"
3132

3233
#endif /* ADAFRUIT_TINYUSB_H_ */

src/Adafruit_USBD_HID.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ uint16_t Adafruit_USBD_HID::getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t
8181
if ( _out_endpoint )
8282
{
8383
// usb core will automatically update endpoint number
84-
uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR(itfnum, 0, _protocol, _desc_report_len, EPIN, EPOUT, CFG_TUD_HID_BUFSIZE, _interval_ms) };
84+
uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR(itfnum, 0, _protocol, _desc_report_len, EPOUT, EPIN, CFG_TUD_HID_BUFSIZE, _interval_ms) };
8585
uint16_t const len = sizeof(desc);
8686

8787
if ( bufsize < len ) return 0;

0 commit comments

Comments
 (0)