Skip to content

Commit cfa66fb

Browse files
authored
Merge pull request #180 from adafruit/fix-webusb-example
Fix webusb example
2 parents e8aa0ae + d68d991 commit cfa66fb

File tree

8 files changed

+70
-26
lines changed

8 files changed

+70
-26
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/application.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717

1818
let currentReceiverLine;
1919

20-
function appendLine(linesId, text) {
20+
function appendLines(linesId, text) {
21+
const lines = text.split('\r');
2122
if (currentReceiverLine) {
22-
currentReceiverLine.innerHTML = currentReceiverLine.innerHTML + text;
23+
currentReceiverLine.innerHTML = currentReceiverLine.innerHTML + lines[0];
24+
for (let i = 1; i < lines.length; i++) {
25+
currentReceiverLine = addLine(linesId, lines[i]);
26+
}
2327
} else {
24-
currentReceiverLine = addLine(linesId, text);
28+
for (let i = 0; i < lines.length; i++) {
29+
currentReceiverLine = addLine(linesId, lines[i]);
30+
}
2531
}
2632
}
2733

@@ -36,7 +42,7 @@
3642
if (data.getInt8() === 13) {
3743
currentReceiverLine = null;
3844
} else {
39-
appendLine('receiver_lines', textDecoder.decode(data));
45+
appendLines('receiver_lines', textDecoder.decode(data));
4046
}
4147
};
4248
port.onReceiveError = error => {

docs/examples/webusb-serial/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ <h1>TinyUSB - WebUSB Serial Example</h1>
3030
</div>
3131
</div>
3232
</body>
33-
</html>
33+
</html>

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
}

src/arduino/webusb/Adafruit_USBD_WebUSB.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ int Adafruit_USBD_WebUSB::read(void) {
239239
return tud_vendor_read(&ch, 1) ? (int)ch : -1;
240240
}
241241

242+
size_t Adafruit_USBD_WebUSB::read(uint8_t *buffer, size_t size) {
243+
return tud_vendor_read(buffer, size);
244+
}
245+
242246
size_t Adafruit_USBD_WebUSB::write(uint8_t b) { return this->write(&b, 1); }
243247

244248
size_t Adafruit_USBD_WebUSB::write(const uint8_t *buffer, size_t size) {
@@ -262,7 +266,7 @@ int Adafruit_USBD_WebUSB::peek(void) {
262266
return tud_vendor_peek(&ch) ? (int)ch : -1;
263267
}
264268

265-
void Adafruit_USBD_WebUSB::flush(void) {}
269+
void Adafruit_USBD_WebUSB::flush(void) { tud_vendor_flush(); }
266270

267271
//--------------------------------------------------------------------+
268272
// TinyUSB stack callbacks

src/arduino/webusb/Adafruit_USBD_WebUSB.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ class Adafruit_USBD_WebUSB : public Stream, public Adafruit_USBD_Interface {
4646
bool setLandingPage(const void *url);
4747
void setLineStateCallback(linestate_callback_t fp);
4848

49-
// Stream interface to use with MIDI Library
50-
virtual int read(void);
49+
// Stream API
5150
virtual int available(void);
5251
virtual int peek(void);
52+
53+
virtual int read(void);
54+
size_t read(uint8_t *buffer, size_t size);
55+
5356
virtual void flush(void);
5457
virtual size_t write(uint8_t b);
5558

0 commit comments

Comments
 (0)