From dce754b693f24c8025cf8504de7be987fcf355e2 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:10:46 -0300 Subject: [PATCH] Improve USBVendor Example (#9349) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add WebUSB console * Improve Console Page * Improve example * Add comments * Add flush method --------- Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --- cores/esp32/USB.cpp | 2 +- docs/_static/webusb.html | 128 +++++++++++++++++ docs/en/api/usb.rst | 2 +- .../USB/examples/USBVendor/USBVendor.ino | 8 +- libraries/USB/examples/USBVendor/index.html | 133 ++++++++++++++++++ libraries/USB/src/USBVendor.cpp | 4 +- variants/esp32s2usb/pins_arduino.h | 2 +- 7 files changed, 273 insertions(+), 6 deletions(-) create mode 100644 docs/_static/webusb.html create mode 100644 libraries/USB/examples/USBVendor/index.html diff --git a/cores/esp32/USB.cpp b/cores/esp32/USB.cpp index ad1df4c22..8740bb717 100644 --- a/cores/esp32/USB.cpp +++ b/cores/esp32/USB.cpp @@ -48,7 +48,7 @@ #define USB_WEBUSB_ENABLED false #endif #ifndef USB_WEBUSB_URL -#define USB_WEBUSB_URL "https://espressif.github.io/arduino-esp32/webusb.html" +#define USB_WEBUSB_URL "https://docs.espressif.com/projects/arduino-esp32/en/latest/_static/webusb.html" #endif #if CFG_TUD_DFU diff --git a/docs/_static/webusb.html b/docs/_static/webusb.html new file mode 100644 index 000000000..34eb986ba --- /dev/null +++ b/docs/_static/webusb.html @@ -0,0 +1,128 @@ + + + + + + Espressif WebUSB Console Example + + + + + + +


+ +
+

+
+ + + diff --git a/docs/en/api/usb.rst b/docs/en/api/usb.rst index 94261185f..056e3fcc1 100644 --- a/docs/en/api/usb.rst +++ b/docs/en/api/usb.rst @@ -303,7 +303,7 @@ This function is used to get the ``webUSBURL``. const char * webUSBURL(void); -The default ``webUSBURL`` is: https://espressif.github.io/arduino-esp32/webusb.html +The default ``webUSBURL`` is: https://docs.espressif.com/projects/arduino-esp32/en/latest/_static/webusb.html enableDFU ^^^^^^^^^ diff --git a/libraries/USB/examples/USBVendor/USBVendor.ino b/libraries/USB/examples/USBVendor/USBVendor.ino index a2b236d8f..350c532b5 100644 --- a/libraries/USB/examples/USBVendor/USBVendor.ino +++ b/libraries/USB/examples/USBVendor/USBVendor.ino @@ -164,7 +164,8 @@ void setup() { USB.onEvent(usbEventCallback); USB.webUSB(true); - USB.webUSBURL("http://localhost/webusb"); + // Set the URL for your WebUSB landing page + USB.webUSBURL("https://docs.espressif.com/projects/arduino-esp32/en/latest/_static/webusb.html"); USB.begin(); } @@ -176,9 +177,11 @@ void loop() { if (buttonState == LOW) { Serial.println("Button Pressed"); Vendor.println("Button Pressed"); + Vendor.flush(); //Without flushing the data will only be sent when the buffer is full (64 bytes) } else { - Vendor.println("Button Released"); Serial.println("Button Released"); + Vendor.println("Button Released"); + Vendor.flush(); //Without flushing the data will only be sent when the buffer is full (64 bytes) } delay(100); } @@ -188,6 +191,7 @@ void loop() { uint8_t b[l]; l = Serial.read(b, l); Vendor.write(b, l); + Vendor.flush(); //Without flushing the data will only be sent when the buffer is full (64 bytes) } } #endif /* ARDUINO_USB_MODE */ diff --git a/libraries/USB/examples/USBVendor/index.html b/libraries/USB/examples/USBVendor/index.html new file mode 100644 index 000000000..23831bb45 --- /dev/null +++ b/libraries/USB/examples/USBVendor/index.html @@ -0,0 +1,133 @@ + + + + + + + + Espressif WebUSB Console Example + + + + + + +


+ +
+

+
+ + + diff --git a/libraries/USB/src/USBVendor.cpp b/libraries/USB/src/USBVendor.cpp index da3d0e186..d5dbd616b 100644 --- a/libraries/USB/src/USBVendor.cpp +++ b/libraries/USB/src/USBVendor.cpp @@ -212,7 +212,9 @@ size_t USBVendor::read(uint8_t *buffer, size_t size){ return count; } -void USBVendor::flush(void){} +void USBVendor::flush(void){ + tud_vendor_n_write_flush(itf); +} #endif /* CONFIG_TINYUSB_VENDOR_ENABLED */ #endif /* SOC_USB_OTG_SUPPORTED */ diff --git a/variants/esp32s2usb/pins_arduino.h b/variants/esp32s2usb/pins_arduino.h index 636c62482..222c1de75 100644 --- a/variants/esp32s2usb/pins_arduino.h +++ b/variants/esp32s2usb/pins_arduino.h @@ -10,7 +10,7 @@ #define USB_PRODUCT "ESP32-S2-USB" #define USB_SERIAL "0" #define USB_WEBUSB_ENABLED false -#define USB_WEBUSB_URL "https://espressif.github.io/arduino-esp32/webusb.html" +#define USB_WEBUSB_URL "https://docs.espressif.com/projects/arduino-esp32/en/latest/_static/webusb.html" // Default USB FirmwareMSC Settings #define USB_FW_MSC_VENDOR_ID "ESP32-S2" //max 8 chars