Skip to content

Commit 3801b5c

Browse files
committed
improve webusb example
1 parent d9dfa78 commit 3801b5c

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

docs/examples/webusb-rgb/serial.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ var serial = {};
1111

1212
serial.requestPort = function() {
1313
const filters = [
14-
{ 'vendorId': 0x239A }, // Adafruit boards
15-
{ 'vendorId': 0xcafe }, // TinyUSB example
16-
{ 'vendorId': 0x2341 }, // Arduino Nano RP2040 Connect
14+
{ 'vendorId': 0xcafe }, // TinyUSB
15+
{ 'vendorId': 0x239a }, // Adafruit
16+
{ 'vendorId': 0x2e8a }, // Raspberry Pi
17+
{ 'vendorId': 0x303a }, // Espressif
18+
{ 'vendorId': 0x2341 }, // Arduino
1719
];
1820
return navigator.usb.requestDevice({ 'filters': filters }).then(
1921
device => new serial.Port(device)

docs/examples/webusb-serial/serial.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ var serial = {};
1111

1212
serial.requestPort = function() {
1313
const filters = [
14-
{ 'vendorId': 0x239A }, // Adafruit boards
15-
{ 'vendorId': 0xcafe }, // TinyUSB example
16-
{ 'vendorId': 0x2341 }, // Arduino Nano RP2040 Connect
14+
{ 'vendorId': 0xcafe }, // TinyUSB
15+
{ 'vendorId': 0x239a }, // Adafruit
16+
{ 'vendorId': 0x2e8a }, // Raspberry Pi
17+
{ 'vendorId': 0x303a }, // Espressif
18+
{ 'vendorId': 0x2341 }, // Arduino
1719
];
1820
return navigator.usb.requestDevice({ 'filters': filters }).then(
1921
device => new serial.Port(device)

examples/WebUSB/webusb_rgb/webusb_rgb.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_NUM, PIN_NEOPIXEL, NEO_GRB
5050
Adafruit_USBD_WebUSB usb_web;
5151

5252
// Landing Page: scheme (0: http, 1: https), url
53-
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-rgb/index.html");
53+
// Page source can be found at https://github.com/hathach/tinyusb-webusb-page/tree/main/webusb-rgb
54+
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "example.tinyusb.org/webusb-rgb/index.html");
5455

5556
// the setup function runs once when you press reset or power the board
5657
void setup()

examples/WebUSB/webusb_serial/webusb_serial.ino

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
Adafruit_USBD_WebUSB usb_web;
3232

3333
// Landing Page: scheme (0: http, 1: https), url
34-
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "adafruit.github.io/Adafruit_TinyUSB_Arduino/examples/webusb-serial/index.html");
34+
// Page source can be found at https://github.com/hathach/tinyusb-webusb-page/tree/main/webusb-serial
35+
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "example.tinyusb.org/webusb-serial/index.html");
3536

3637
int led_pin = LED_BUILTIN;
3738

@@ -60,27 +61,52 @@ void setup()
6061
}
6162

6263
// function to echo to both Serial and WebUSB
63-
void echo_all(char chr)
64+
void echo_all(uint8_t buf[], uint32_t count)
6465
{
65-
Serial.write(chr);
66-
// print extra newline for Serial
67-
if ( chr == '\r' ) Serial.write('\n');
68-
69-
usb_web.write(chr);
66+
if (usb_web.connected())
67+
{
68+
usb_web.write(buf, count);
69+
usb_web.flush();
70+
}
71+
72+
if ( Serial )
73+
{
74+
for(uint32_t i=0; i<count; i++)
75+
{
76+
Serial.write(buf[i]);
77+
if ( buf[i] == '\r' ) Serial.write('\n');
78+
}
79+
Serial.flush();
80+
}
7081
}
7182

7283
void loop()
7384
{
74-
// from WebUSB to both Serial & webUSB
75-
if (usb_web.available()) echo_all(usb_web.read());
85+
uint8_t buf[64];
86+
uint32_t count;
7687

7788
// From Serial to both Serial & webUSB
78-
if (Serial.available()) echo_all(Serial.read());
89+
if (Serial.available())
90+
{
91+
count = Serial.read(buf, 64);
92+
echo_all(buf, count);
93+
}
94+
95+
// from WebUSB to both Serial & webUSB
96+
if (usb_web.available())
97+
{
98+
count = usb_web.read(buf, 64);
99+
echo_all(buf, count);
100+
}
79101
}
80102

81103
void line_state_callback(bool connected)
82104
{
83105
digitalWrite(led_pin, connected);
84106

85-
if ( connected ) usb_web.println("TinyUSB WebUSB Serial example");
107+
if ( connected )
108+
{
109+
usb_web.println("WebUSB interface connected !!");
110+
usb_web.flush();
111+
}
86112
}

0 commit comments

Comments
 (0)