|
31 | 31 | Adafruit_USBD_WebUSB usb_web;
|
32 | 32 |
|
33 | 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/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"); |
35 | 36 |
|
36 | 37 | int led_pin = LED_BUILTIN;
|
37 | 38 |
|
@@ -60,27 +61,52 @@ void setup()
|
60 | 61 | }
|
61 | 62 |
|
62 | 63 | // function to echo to both Serial and WebUSB
|
63 |
| -void echo_all(char chr) |
| 64 | +void echo_all(uint8_t buf[], uint32_t count) |
64 | 65 | {
|
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 | + } |
70 | 81 | }
|
71 | 82 |
|
72 | 83 | void loop()
|
73 | 84 | {
|
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; |
76 | 87 |
|
77 | 88 | // 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 | + } |
79 | 101 | }
|
80 | 102 |
|
81 | 103 | void line_state_callback(bool connected)
|
82 | 104 | {
|
83 | 105 | digitalWrite(led_pin, connected);
|
84 | 106 |
|
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 | + } |
86 | 112 | }
|
0 commit comments