diff --git a/examples/CDC/no_serial/no_serial.ino b/examples/CDC/no_serial/no_serial.ino index c7538d5b..6883a444 100644 --- a/examples/CDC/no_serial/no_serial.ino +++ b/examples/CDC/no_serial/no_serial.ino @@ -23,6 +23,11 @@ int led = LED_BUILTIN; void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } + // clear configuration will remove all USB interfaces including CDC (Serial) TinyUSBDevice.clearConfiguration(); @@ -31,8 +36,16 @@ void setup() void loop() { - digitalWrite(led, HIGH); - delay(1000); - digitalWrite(led, LOW); - delay(1000); + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // toggle LED + static uint32_t ms = 0; + static uint8_t led_state = 0; + if (millis() - ms > 1000) { + ms = millis(); + digitalWrite(LED_BUILTIN, 1-led_state); + } } diff --git a/examples/Composite/mouse_external_flash/.feather52833.test.skip b/examples/Composite/mouse_external_flash/.feather52833.test.skip deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/Composite/mouse_external_flash/.pico_rp2040_tinyusb_host.test.skip b/examples/Composite/mouse_external_flash/.pico_rp2040_tinyusb_host.test.skip deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/Composite/mouse_external_flash/mouse_external_flash.ino b/examples/Composite/mouse_external_flash/mouse_external_flash.ino deleted file mode 100644 index 8cdbbb1d..00000000 --- a/examples/Composite/mouse_external_flash/mouse_external_flash.ino +++ /dev/null @@ -1,200 +0,0 @@ -/********************************************************************* - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing - products from Adafruit! - - MIT license, check LICENSE for more information - Copyright (c) 2019 Ha Thach for Adafruit Industries - All text above, and the splash screen below must be included in - any redistribution -*********************************************************************/ - -/* This sketch demonstrates USB Mass Storage and HID mouse (and CDC) - * - Enumerated as disk using on-board external flash - * - Press button pin will move mouse toward bottom right of monitor - */ - -#include "SPI.h" -#include "SdFat.h" -#include "Adafruit_SPIFlash.h" -#include "Adafruit_TinyUSB.h" - -//--------------------------------------------------------------------+ -// MSC External Flash Config -//--------------------------------------------------------------------+ - -// Un-comment to run example with custom SPI SPI and SS e.g with FRAM breakout -// #define CUSTOM_CS A5 -// #define CUSTOM_SPI SPI - -#if defined(CUSTOM_CS) && defined(CUSTOM_SPI) - Adafruit_FlashTransport_SPI flashTransport(CUSTOM_CS, CUSTOM_SPI); - -#elif defined(ARDUINO_ARCH_ESP32) - // ESP32 use same flash device that store code. - // Therefore there is no need to specify the SPI and SS - Adafruit_FlashTransport_ESP32 flashTransport; - -#elif defined(ARDUINO_ARCH_RP2040) - // RP2040 use same flash device that store code. - // Therefore there is no need to specify the SPI and SS - // Use default (no-args) constructor to be compatible with CircuitPython partition scheme - Adafruit_FlashTransport_RP2040 flashTransport; - - // For generic usage: - // Adafruit_FlashTransport_RP2040 flashTransport(start_address, size) - // If start_address and size are both 0, value that match filesystem setting in - // 'Tools->Flash Size' menu selection will be used - -#else - // On-board external flash (QSPI or SPI) macros should already - // defined in your board variant if supported - // - EXTERNAL_FLASH_USE_QSPI - // - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI - #if defined(EXTERNAL_FLASH_USE_QSPI) - Adafruit_FlashTransport_QSPI flashTransport; - - #elif defined(EXTERNAL_FLASH_USE_SPI) - Adafruit_FlashTransport_SPI flashTransport(EXTERNAL_FLASH_USE_CS, EXTERNAL_FLASH_USE_SPI); - - #else - #error No QSPI/SPI flash are defined on your board variant.h ! - #endif -#endif - -Adafruit_SPIFlash flash(&flashTransport); - -Adafruit_USBD_MSC usb_msc; - -//--------------------------------------------------------------------+ -// HID Config -//--------------------------------------------------------------------+ - -// HID report descriptor using TinyUSB's template -// Single Report (no ID) descriptor -uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_MOUSE() -}; - -// USB HID object -Adafruit_USBD_HID usb_hid; - -#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) - const int pin = 4; // Left Button - bool activeState = true; - -#elif defined(ARDUINO_FUNHOUSE_ESP32S2) - const int pin = BUTTON_DOWN; - bool activeState = true; - -#elif defined PIN_BUTTON1 - const int pin = PIN_BUTTON1; - bool activeState = false; - -#elif defined(ARDUINO_ARCH_ESP32) - const int pin = 0; - bool activeState = false; - -#else - const int pin = 12; - bool activeState = false; -#endif - - -// the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif - - flash.begin(); - - pinMode(LED_BUILTIN, OUTPUT); - - // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively - usb_msc.setID("Adafruit", "External Flash", "1.0"); - - // Set callback - usb_msc.setReadWriteCallback(msc_read_cb, msc_write_cb, msc_flush_cb); - - // Set disk size, block size should be 512 regardless of spi flash page size - usb_msc.setCapacity(flash.size()/512, 512); - - // MSC is ready for read/write - usb_msc.setUnitReady(true); - - usb_msc.begin(); - - // Set up button - pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP); - - // Set up HID - usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); - usb_hid.setBootProtocol(HID_ITF_PROTOCOL_NONE); - usb_hid.setPollInterval(2); - usb_hid.begin(); - - Serial.begin(115200); - //while ( !Serial ) delay(10); // wait for native usb - - Serial.println("Adafruit TinyUSB Mouse + Mass Storage (external flash) example"); -} - -void loop() -{ - // poll gpio once each 10 ms - delay(10); - - // button is active low - uint32_t const btn = (digitalRead(pin) == activeState); - - // Remote wakeup - if ( TinyUSBDevice.suspended() && btn ) - { - // Wake up host if we are in suspend mode - // and REMOTE_WAKEUP feature is enabled by host - tud_remote_wakeup(); - } - - /*------------- Mouse -------------*/ - if ( usb_hid.ready() ) - { - if ( btn ) - { - int8_t const delta = 5; - usb_hid.mouseMove(0, delta, delta); // no ID: right + down - - // delay a bit before attempt to send keyboard report - delay(10); - } - } -} - -// Callback invoked when received READ10 command. -// Copy disk's data to buffer (up to bufsize) and -// return number of copied bytes (must be multiple of block size) -int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) -{ - // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks - // already include 4K sector caching internally. We don't need to cache it, yahhhh!! - return flash.readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1; -} - -// Callback invoked when received WRITE10 command. -// Process data in buffer to disk's storage and -// return number of written bytes (must be multiple of block size) -int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) -{ - // Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks - // already include 4K sector caching internally. We don't need to cache it, yahhhh!! - return flash.writeBlocks(lba, buffer, bufsize/512) ? bufsize : -1; -} - -// Callback invoked when WRITE10 command is completed (status received and accepted by host). -// used to flush any pending cache. -void msc_flush_cb (void) -{ - flash.syncBlocks(); -} diff --git a/examples/Composite/mouse_ramdisk/mouse_ramdisk.ino b/examples/Composite/mouse_ramdisk/mouse_ramdisk.ino index 09da8690..08c6e113 100644 --- a/examples/Composite/mouse_ramdisk/mouse_ramdisk.ino +++ b/examples/Composite/mouse_ramdisk/mouse_ramdisk.ino @@ -23,6 +23,7 @@ // 8KB is the smallest size that windows allow to mount #define DISK_BLOCK_NUM 16 #define DISK_BLOCK_SIZE 512 + #include "ramdisk.h" Adafruit_USBD_MSC usb_msc; @@ -41,37 +42,41 @@ uint8_t const desc_hid_report[] = { Adafruit_USBD_HID usb_hid; #if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) - const int pin = 4; // Left Button - bool activeState = true; +const int pin = 4; // Left Button +bool activeState = true; #elif defined(ARDUINO_FUNHOUSE_ESP32S2) - const int pin = BUTTON_DOWN; - bool activeState = true; +const int pin = BUTTON_DOWN; +bool activeState = true; #elif defined PIN_BUTTON1 - const int pin = PIN_BUTTON1; - bool activeState = false; +const int pin = PIN_BUTTON1; +bool activeState = false; #elif defined(ARDUINO_ARCH_ESP32) - const int pin = 0; - bool activeState = false; +const int pin = 0; +bool activeState = false; + +#elif defined(ARDUINO_ARCH_RP2040) +const int pin = D0; +bool activeState = false; #else - const int pin = 12; - bool activeState = false; +const int pin = A0; +bool activeState = false; #endif + // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } // Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively usb_msc.setID("Adafruit", "Mass Storage", "1.0"); - + // Set disk size usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE); @@ -80,7 +85,7 @@ void setup() // Set Lun ready (RAM disk is always ready) usb_msc.setUnitReady(true); - + usb_msc.begin(); // Set up button @@ -93,32 +98,23 @@ void setup() usb_hid.begin(); Serial.begin(115200); - while( !TinyUSBDevice.mounted() ) delay(1); // wait for native usb - Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example"); } -void loop() -{ - // poll gpio once each 10 ms - delay(10); - +void process_hid() { // button is active low uint32_t const btn = (digitalRead(pin) == activeState); // Remote wakeup - if ( TinyUSBDevice.suspended() && btn ) - { + if (TinyUSBDevice.suspended() && btn) { // Wake up host if we are in suspend mode // and REMOTE_WAKEUP feature is enabled by host tud_remote_wakeup(); } /*------------- Mouse -------------*/ - if ( usb_hid.ready() ) - { - if ( btn ) - { + if (usb_hid.ready()) { + if (btn) { int8_t const delta = 5; usb_hid.mouseMove(0, delta, delta); // no ID: right + down @@ -128,11 +124,29 @@ void loop() } } +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + + // poll gpio once each 10 ms + static uint32_t ms = 0; + if (millis() - ms > 10) { + ms = millis(); + process_hid(); + } +} + // Callback invoked when received READ10 command. // Copy disk's data to buffer (up to bufsize) and // return number of copied bytes (must be multiple of block size) -int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) -{ +int32_t msc_read_cb(uint32_t lba, void* buffer, uint32_t bufsize) { uint8_t const* addr = msc_disk[lba]; memcpy(buffer, addr, bufsize); @@ -142,8 +156,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) // Callback invoked when received WRITE10 command. // Process data in buffer to disk's storage and // return number of written bytes (must be multiple of block size) -int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) -{ +int32_t msc_write_cb(uint32_t lba, uint8_t* buffer, uint32_t bufsize) { uint8_t* addr = msc_disk[lba]; memcpy(addr, buffer, bufsize); @@ -152,7 +165,6 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) // Callback invoked when WRITE10 command is completed (status received and accepted by host). // used to flush any pending cache. -void msc_flush_cb (void) -{ +void msc_flush_cb(void) { // nothing to do } diff --git a/examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino b/examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino index c5bca727..38395220 100644 --- a/examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino +++ b/examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino @@ -10,10 +10,9 @@ *********************************************************************/ #include "Adafruit_TinyUSB.h" -#include /* This sketch demonstrates USB HID keyboard. - * - PIN A0-A5 is used to send digit '0' to '5' respectively + * - PIN A0-A3 is used to send digit '0' to '3' respectively * (On the RP2040, pins D0-D5 used) * - LED and/or Neopixels will be used as Capslock indicator */ @@ -21,7 +20,7 @@ // HID report descriptor using TinyUSB's template // Single Report (no ID) descriptor uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_KEYBOARD() + TUD_HID_REPORT_DESC_KEYBOARD() }; // USB HID object. For ESP32 these values cannot be changed after this declaration @@ -32,45 +31,29 @@ Adafruit_USBD_HID usb_hid; // Array of pins and its keycode. // Notes: these pins can be replaced by PIN_BUTTONn if defined in setup() #ifdef ARDUINO_ARCH_RP2040 - uint8_t pins[] = { D0, D1, D2, D3 }; +uint8_t pins[] = { D0, D1, D2, D3 }; #else - uint8_t pins[] = { A0, A1, A2, A3 }; +uint8_t pins[] = {A0, A1, A2, A3}; #endif // number of pins -uint8_t pincount = sizeof(pins)/sizeof(pins[0]); +uint8_t pincount = sizeof(pins) / sizeof(pins[0]); // For keycode definition check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h -uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP }; +uint8_t hidcode[] = {HID_KEY_0, HID_KEY_1, HID_KEY_2, HID_KEY_3}; #if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_FUNHOUSE_ESP32S2) - bool activeState = true; +bool activeState = true; #else - bool activeState = false; +bool activeState = false; #endif -//------------- Neopixel -------------// -// #define PIN_NEOPIXEL 8 -#ifdef PIN_NEOPIXEL - -// How many NeoPixels are attached to the Arduino? -// use on-board defined NEOPIXEL_NUM if existed -#ifndef NEOPIXEL_NUM - #define NEOPIXEL_NUM 10 -#endif - -Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_NUM, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); - -#endif - - // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } // Setup HID usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD); @@ -87,17 +70,6 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); - // neopixel if existed -#ifdef PIN_NEOPIXEL - pixels.begin(); - pixels.setBrightness(50); - - #ifdef NEOPIXEL_POWER - pinMode(NEOPIXEL_POWER, OUTPUT); - digitalWrite(NEOPIXEL_POWER, NEOPIXEL_POWER_ON); - #endif -#endif - // overwrite input pin with PIN_BUTTONx #ifdef PIN_BUTTON1 pins[0] = PIN_BUTTON1; @@ -116,32 +88,21 @@ void setup() #endif // Set up pin as input - for (uint8_t i=0; i 2) { + ms = millis(); + process_hid(); + } +} + // Output report callback for LED indicator such as Caplocks -void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) -{ +void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { (void) report_id; (void) bufsize; // LED indicator is output report with only 1 byte length - if ( report_type != HID_REPORT_TYPE_OUTPUT ) return; + if (report_type != HID_REPORT_TYPE_OUTPUT) return; // The LED bit map is as follows: (also defined by KEYBOARD_LED_* ) // Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0) @@ -196,9 +171,4 @@ void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8 // turn on LED if capslock is set digitalWrite(LED_BUILTIN, ledIndicator & KEYBOARD_LED_CAPSLOCK); - -#ifdef PIN_NEOPIXEL - pixels.fill(ledIndicator & KEYBOARD_LED_CAPSLOCK ? 0xff0000 : 0x000000); - pixels.show(); -#endif } diff --git a/examples/HID/hid_boot_mouse/hid_boot_mouse.ino b/examples/HID/hid_boot_mouse/hid_boot_mouse.ino index 1715d768..39455822 100644 --- a/examples/HID/hid_boot_mouse/hid_boot_mouse.ino +++ b/examples/HID/hid_boot_mouse/hid_boot_mouse.ino @@ -19,43 +19,45 @@ * and its active state (when pressed) are different */ #if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) - const int pin = 4; // Left Button - bool activeState = true; +const int pin = 4; // Left Button +bool activeState = true; #elif defined(ARDUINO_FUNHOUSE_ESP32S2) - const int pin = BUTTON_DOWN; - bool activeState = true; +const int pin = BUTTON_DOWN; +bool activeState = true; #elif defined PIN_BUTTON1 - const int pin = PIN_BUTTON1; - bool activeState = false; +const int pin = PIN_BUTTON1; +bool activeState = false; #elif defined(ARDUINO_ARCH_ESP32) - const int pin = 0; - bool activeState = false; +const int pin = 0; +bool activeState = false; +#elif defined(ARDUINO_ARCH_RP2040) +const int pin = D0; +bool activeState = false; #else - const int pin = 12; - bool activeState = false; +const int pin = A0; +bool activeState = false; #endif // HID report descriptor using TinyUSB's template // Single Report (no ID) descriptor uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_MOUSE() + TUD_HID_REPORT_DESC_MOUSE() }; // USB HID object Adafruit_USBD_HID usb_hid; // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } // Set up button, pullup opposite to active state pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP); @@ -69,18 +71,10 @@ void setup() usb_hid.begin(); Serial.begin(115200); - - // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); - Serial.println("Adafruit TinyUSB HID Mouse example"); } -void loop() -{ - // poll gpio once each 10 ms - delay(10); - +void process_hid() { // Whether button is pressed bool btn_pressed = (digitalRead(pin) == activeState); @@ -88,17 +82,34 @@ void loop() if (!btn_pressed) return; // Remote wakeup - if ( TinyUSBDevice.suspended() ) - { + if (TinyUSBDevice.suspended()) { // Wake up host if we are in suspend mode // and REMOTE_WAKEUP feature is enabled by host TinyUSBDevice.remoteWakeup(); } - if ( usb_hid.ready() ) - { + if (usb_hid.ready()) { uint8_t const report_id = 0; // no ID int8_t const delta = 5; usb_hid.mouseMove(report_id, delta, delta); // right + down } } + +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + + // poll gpio once each 10 ms + static uint32_t ms = 0; + if (millis() - ms > 10) { + ms = millis(); + process_hid(); + } +} diff --git a/examples/HID/hid_composite/hid_composite.ino b/examples/HID/hid_composite/hid_composite.ino index 3f179260..f2b9fdf4 100644 --- a/examples/HID/hid_composite/hid_composite.ino +++ b/examples/HID/hid_composite/hid_composite.ino @@ -20,30 +20,34 @@ * and its active state (when pressed) are different */ #if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) - const int pin = 4; // Left Button - bool activeState = true; +const int pin = 4; // Left Button +bool activeState = true; #elif defined(ARDUINO_FUNHOUSE_ESP32S2) - const int pin = BUTTON_DOWN; - bool activeState = true; +const int pin = BUTTON_DOWN; +bool activeState = true; #elif defined PIN_BUTTON1 - const int pin = PIN_BUTTON1; - bool activeState = false; +const int pin = PIN_BUTTON1; +bool activeState = false; #elif defined(ARDUINO_ARCH_ESP32) - const int pin = 0; - bool activeState = false; +const int pin = 0; +bool activeState = false; + +#elif defined(ARDUINO_ARCH_RP2040) +const int pin = D0; +bool activeState = false; #else - const int pin = 12; - bool activeState = false; +const int pin = A0; +bool activeState = false; + #endif // Report ID -enum -{ +enum { RID_KEYBOARD = 1, RID_MOUSE, RID_CONSUMER_CONTROL, // Media, volume etc .. @@ -51,17 +55,21 @@ enum // HID report descriptor using TinyUSB's template uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD) ), - TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE) ), - TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(RID_CONSUMER_CONTROL) ) + TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(RID_KEYBOARD)), + TUD_HID_REPORT_DESC_MOUSE (HID_REPORT_ID(RID_MOUSE)), + TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(RID_CONSUMER_CONTROL)) }; // USB HID object. Adafruit_USBD_HID usb_hid; // the setup function runs once when you press reset or power the board -void setup() -{ +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } + // Set up HID usb_hid.setPollInterval(2); usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); @@ -73,32 +81,22 @@ void setup() pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP); Serial.begin(115200); - - // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); - Serial.println("Adafruit TinyUSB HID Composite example"); } -void loop() -{ - // poll gpio once each 10 ms - delay(10); - +void process_hid() { // Whether button is pressed bool btn_pressed = (digitalRead(pin) == activeState); // Remote wakeup - if ( TinyUSBDevice.suspended() && btn_pressed ) - { + if (TinyUSBDevice.suspended() && btn_pressed) { // Wake up host if we are in suspend mode // and REMOTE_WAKEUP feature is enabled by host TinyUSBDevice.remoteWakeup(); } /*------------- Mouse -------------*/ - if ( usb_hid.ready() && btn_pressed ) - { + if (usb_hid.ready() && btn_pressed) { int8_t const delta = 5; usb_hid.mouseMove(RID_MOUSE, delta, delta); // right + down @@ -107,21 +105,18 @@ void loop() } /*------------- Keyboard -------------*/ - if ( usb_hid.ready() ) - { + if (usb_hid.ready()) { // use to send key release report static bool has_key = false; - if ( btn_pressed ) - { - uint8_t keycode[6] = { 0 }; + if (btn_pressed) { + uint8_t keycode[6] = {0}; keycode[0] = HID_KEY_A; usb_hid.keyboardReport(RID_KEYBOARD, 0, keycode); has_key = true; - }else - { + } else { // send empty key report if previously has key pressed if (has_key) usb_hid.keyboardRelease(RID_KEYBOARD); has_key = false; @@ -132,8 +127,7 @@ void loop() } /*------------- Consumer Control -------------*/ - if ( usb_hid.ready() ) - { + if (usb_hid.ready()) { // Consumer Control is used to control Media playback, Volume, Brightness etc ... // Consumer report is 2-byte containing the control code of the key // For list of control check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h @@ -141,16 +135,33 @@ void loop() // use to send consumer release report static bool has_consumer_key = false; - if ( btn_pressed ) - { + if (btn_pressed) { // send volume down (0x00EA) usb_hid.sendReport16(RID_CONSUMER_CONTROL, HID_USAGE_CONSUMER_VOLUME_DECREMENT); has_consumer_key = true; - }else - { + } else { // release the consume key by sending zero (0x0000) if (has_consumer_key) usb_hid.sendReport16(RID_CONSUMER_CONTROL, 0); has_consumer_key = false; } } } + +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + + // poll gpio once each 10 ms + static uint32_t ms = 0; + if (millis() - ms > 10) { + ms = millis(); + process_hid(); + } +} diff --git a/examples/HID/hid_composite_joy_featherwing/hid_composite_joy_featherwing.ino b/examples/HID/hid_composite_joy_featherwing/hid_composite_joy_featherwing.ino index 01f4481d..11bcf414 100644 --- a/examples/HID/hid_composite_joy_featherwing/hid_composite_joy_featherwing.ino +++ b/examples/HID/hid_composite_joy_featherwing/hid_composite_joy_featherwing.ino @@ -33,17 +33,15 @@ uint32_t button_mask = (1 << BUTTON_A) | (1 << BUTTON_B) | Adafruit_seesaw ss; // Report ID -enum -{ +enum { RID_KEYBOARD = 1, RID_MOUSE }; // HID report descriptor using TinyUSB's template -uint8_t const desc_hid_report[] = -{ - TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD) ), - TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE) ) +uint8_t const desc_hid_report[] = { + TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(RID_KEYBOARD)), + TUD_HID_REPORT_DESC_MOUSE (HID_REPORT_ID(RID_MOUSE)) }; // USB HID object. For ESP32 these values cannot be changed after this declaration @@ -53,20 +51,23 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT int last_x, last_y; // the setup function runs once when you press reset or power the board -void setup() -{ +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } + // Notes: following commented-out functions has no affect on ESP32 // usb_hid.setPollInterval(2); // usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); - usb_hid.begin(); Serial.begin(115200); Serial.println("Adafruit TinyUSB HID Mouse with Joy FeatherWing example"); - if(!ss.begin(0x49)){ + if (!ss.begin(0x49)) { Serial.println("ERROR! seesaw not found"); - while(1); + while (1) {} } else { Serial.println("seesaw started"); Serial.print("version: "); @@ -77,16 +78,9 @@ void setup() last_y = ss.analogRead(2); last_x = ss.analogRead(3); - - // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); } -void loop() -{ - // poll gpio once each 10 ms - delay(10); - +void process_hid() { // If either analog stick or any buttons is pressed bool has_action = false; @@ -98,12 +92,10 @@ void loop() int dx = (x - last_x) / 2; int dy = (y - last_y) / 2; - if ( (abs(dx) > 3) || (abs(dy) > 3) ) - { + if ((abs(dx) > 3) || (abs(dy) > 3)) { has_action = true; - if ( usb_hid.ready() ) - { + if (usb_hid.ready()) { usb_hid.mouseMove(RID_MOUSE, dx, dy); // no ID: right + down last_x = x; @@ -114,31 +106,27 @@ void loop() } } - /*------------- Keyboard -------------*/ // button is active low, invert read value for convenience uint32_t buttons = ~ss.digitalReadBulk(button_mask); - if ( usb_hid.ready() ) - { + if (usb_hid.ready()) { // use to prevent sending multiple consecutive zero report static bool has_key = false; - if ( buttons & button_mask ) - { + if (buttons & button_mask) { has_action = true; has_key = true; - uint8_t keycode[6] = { 0 }; - - if ( buttons & (1 << BUTTON_A) ) keycode[0] = HID_KEY_A; - if ( buttons & (1 << BUTTON_B) ) keycode[0] = HID_KEY_B; - if ( buttons & (1 << BUTTON_X) ) keycode[0] = HID_KEY_X; - if ( buttons & (1 << BUTTON_Y) ) keycode[0] = HID_KEY_Y; + uint8_t keycode[6] = {0}; + + if (buttons & (1 << BUTTON_A)) keycode[0] = HID_KEY_A; + if (buttons & (1 << BUTTON_B)) keycode[0] = HID_KEY_B; + if (buttons & (1 << BUTTON_X)) keycode[0] = HID_KEY_X; + if (buttons & (1 << BUTTON_Y)) keycode[0] = HID_KEY_Y; usb_hid.keyboardReport(RID_KEYBOARD, 0, keycode); - }else - { + } else { // send empty key report if previously has key pressed if (has_key) usb_hid.keyboardRelease(RID_KEYBOARD); has_key = false; @@ -147,10 +135,28 @@ void loop() /*------------- Remote Wakeup -------------*/ // Remote wakeup if PC is suspended and we has user interaction with joy feather wing - if ( has_action && TinyUSBDevice.suspended() ) - { + if (has_action && TinyUSBDevice.suspended()) { // Wake up only works if REMOTE_WAKEUP feature is enable by host // Usually this is the case with Mouse/Keyboard device TinyUSBDevice.remoteWakeup(); } } + +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + + // poll gpio once each 10 ms + static uint32_t ms = 0; + if (millis() - ms > 10) { + ms = millis(); + process_hid(); + } +} diff --git a/examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino b/examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino index 43f5445b..7a863c06 100644 --- a/examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino +++ b/examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino @@ -19,37 +19,41 @@ * and its active state (when pressed) are different */ #if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) - const int pin = 4; // Left Button - bool activeState = true; +const int pin = 4; // Left Button +bool activeState = true; #elif defined(ARDUINO_FUNHOUSE_ESP32S2) - const int pin = BUTTON_DOWN; - bool activeState = true; +const int pin = BUTTON_DOWN; +bool activeState = true; #elif defined PIN_BUTTON1 - const int pin = PIN_BUTTON1; - bool activeState = false; +const int pin = PIN_BUTTON1; +bool activeState = false; #elif defined PIN_BUTTON - const int pin = PIN_BUTTON; - bool activeState = false; +const int pin = PIN_BUTTON; +bool activeState = false; #elif defined(ARDUINO_ARCH_ESP32) - const int pin = 0; - bool activeState = false; +const int pin = 0; +bool activeState = false; +#elif defined(ARDUINO_ARCH_RP2040) +const int pin = D0; +bool activeState = false; #else - const int pin = 12; - bool activeState = false; + +const int pin = A0; +bool activeState = false; #endif // HID report descriptor using TinyUSB's template uint8_t const desc_keyboard_report[] = { - TUD_HID_REPORT_DESC_KEYBOARD() + TUD_HID_REPORT_DESC_KEYBOARD() }; uint8_t const desc_mouse_report[] = { - TUD_HID_REPORT_DESC_MOUSE() + TUD_HID_REPORT_DESC_MOUSE() }; // USB HID objects @@ -57,8 +61,12 @@ Adafruit_USBD_HID usb_keyboard; Adafruit_USBD_HID usb_mouse; // the setup function runs once when you press reset or power the board -void setup() -{ +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } + // HID Keyboard usb_keyboard.setPollInterval(2); usb_keyboard.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD); @@ -77,54 +85,61 @@ void setup() pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP); Serial.begin(115200); - - // wait until device mounted - //while( !TinyUSBDevice.mounted() ) delay(1); Serial.println("Adafruit TinyUSB HID Composite example"); } -void loop() -{ - // poll gpio once each 10 ms - delay(10); - +void process_hid() { // Whether button is pressed bool btn_pressed = (digitalRead(pin) == activeState); // Remote wakeup - if ( TinyUSBDevice.suspended() && btn_pressed ) - { + if (TinyUSBDevice.suspended() && btn_pressed) { // Wake up host if we are in suspend mode // and REMOTE_WAKEUP feature is enabled by host TinyUSBDevice.remoteWakeup(); } /*------------- Mouse -------------*/ - if (usb_mouse.ready() && btn_pressed ) - { + if (usb_mouse.ready() && btn_pressed) { int8_t const delta = 5; usb_mouse.mouseMove(0, delta, delta); // right + down } /*------------- Keyboard -------------*/ - if ( usb_keyboard.ready() ) - { + if (usb_keyboard.ready()) { // use to send key release report static bool has_key = false; - if ( btn_pressed ) - { - uint8_t keycode[6] = { 0 }; + if (btn_pressed) { + uint8_t keycode[6] = {0}; keycode[0] = HID_KEY_A; usb_keyboard.keyboardReport(0, 0, keycode); has_key = true; - }else - { + } else { // send empty key report if previously has key pressed if (has_key) usb_keyboard.keyboardRelease(0); has_key = false; } } +} + +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + + // poll gpio once each 10 ms + static uint32_t ms = 0; + if (millis() - ms > 10) { + ms = millis(); + process_hid(); + } } \ No newline at end of file diff --git a/examples/HID/hid_gamepad/hid_gamepad.ino b/examples/HID/hid_gamepad/hid_gamepad.ino index 84b3289c..79fc8f07 100644 --- a/examples/HID/hid_gamepad/hid_gamepad.ino +++ b/examples/HID/hid_gamepad/hid_gamepad.ino @@ -23,7 +23,7 @@ // HID report descriptor using TinyUSB's template // Single Report (no ID) descriptor uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_GAMEPAD() + TUD_HID_REPORT_DESC_GAMEPAD() }; // USB HID object @@ -32,31 +32,35 @@ Adafruit_USBD_HID usb_hid; // Report payload defined in src/class/hid/hid.h // - For Gamepad Button Bit Mask see hid_gamepad_button_bm_t // - For Gamepad Hat Bit Mask see hid_gamepad_hat_t -hid_gamepad_report_t gp; +hid_gamepad_report_t gp; -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } Serial.begin(115200); - + // Setup HID usb_hid.setPollInterval(2); usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); - usb_hid.begin(); - // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); - Serial.println("Adafruit TinyUSB HID Gamepad example"); } -void loop() -{ +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + // // Remote wakeup // if ( TinyUSBDevice.suspended() && btn ) // { @@ -65,22 +69,22 @@ void loop() // TinyUSBDevice.remoteWakeup(); // } - if ( !usb_hid.ready() ) return; + if (!usb_hid.ready()) return; // Reset buttons Serial.println("No pressing buttons"); - gp.x = 0; - gp.y = 0; - gp.z = 0; - gp.rz = 0; - gp.rx = 0; - gp.ry = 0; - gp.hat = 0; + gp.x = 0; + gp.y = 0; + gp.z = 0; + gp.rz = 0; + gp.rx = 0; + gp.ry = 0; + gp.hat = 0; gp.buttons = 0; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Hat/DPAD UP Serial.println("Hat/DPAD UP"); gp.hat = 1; // GAMEPAD_HAT_UP; @@ -105,12 +109,12 @@ void loop() usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - // Hat/DPAD DOWN + // Hat/DPAD DOWN Serial.println("Hat/DPAD DOWN"); gp.hat = 5; // GAMEPAD_HAT_DOWN; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Hat/DPAD DOWN LEFT Serial.println("Hat/DPAD DOWN LEFT"); gp.hat = 6; // GAMEPAD_HAT_DOWN_LEFT; @@ -135,14 +139,14 @@ void loop() usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Joystick 1 UP Serial.println("Joystick 1 UP"); gp.x = 0; gp.y = -127; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Joystick 1 DOWN Serial.println("Joystick 1 DOWN"); gp.x = 0; @@ -156,7 +160,7 @@ void loop() gp.y = 0; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Joystick 1 LEFT Serial.println("Joystick 1 LEFT"); gp.x = -127; @@ -174,35 +178,35 @@ void loop() // Joystick 2 UP Serial.println("Joystick 2 UP"); - gp.z = 0; + gp.z = 0; gp.rz = 127; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Joystick 2 DOWN Serial.println("Joystick 2 DOWN"); - gp.z = 0; + gp.z = 0; gp.rz = -127; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); // Joystick 2 RIGHT Serial.println("Joystick 2 RIGHT"); - gp.z = 127; + gp.z = 127; gp.rz = 0; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Joystick 2 LEFT Serial.println("Joystick 2 LEFT"); - gp.z = -127; + gp.z = -127; gp.rz = 0; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); // Joystick 2 CENTER Serial.println("Joystick 2 CENTER"); - gp.z = 0; + gp.z = 0; gp.rz = 0; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); @@ -213,7 +217,7 @@ void loop() gp.rx = 127; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Analog Trigger 1 DOWN Serial.println("Analog Trigger 1 DOWN"); gp.rx = -127; @@ -232,7 +236,7 @@ void loop() gp.ry = 127; usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Analog Trigger 2 DOWN Serial.println("Analog Trigger 2 DOWN"); gp.ry = -127; @@ -245,11 +249,11 @@ void loop() usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); - + // Test buttons (up to 32 buttons) - for (int i=0; i<32; ++i) - { - Serial.print("Pressing button "); Serial.println(i); + for (int i = 0; i < 32; ++i) { + Serial.print("Pressing button "); + Serial.println(i); gp.buttons = (1U << i); usb_hid.sendReport(0, &gp, sizeof(gp)); delay(1000); @@ -258,13 +262,13 @@ void loop() // Random touch Serial.println("Random touch"); - gp.x = random(-127, 128); - gp.y = random(-127, 128); - gp.z = random(-127, 128); - gp.rz = random(-127, 128); - gp.rx = random(-127, 128); - gp.ry = random(-127, 128); - gp.hat = random(0, 9); + gp.x = random(-127, 128); + gp.y = random(-127, 128); + gp.z = random(-127, 128); + gp.rz = random(-127, 128); + gp.rx = random(-127, 128); + gp.ry = random(-127, 128); + gp.hat = random(0, 9); gp.buttons = random(0, 0xffff); usb_hid.sendReport(0, &gp, sizeof(gp)); delay(2000); diff --git a/examples/HID/hid_generic_inout/hid_generic_inout.ino b/examples/HID/hid_generic_inout/hid_generic_inout.ino index b543dd78..ab4974ef 100644 --- a/examples/HID/hid_generic_inout/hid_generic_inout.ino +++ b/examples/HID/hid_generic_inout/hid_generic_inout.ino @@ -40,19 +40,18 @@ // HID report descriptor using TinyUSB's template // Generic In Out with 64 bytes report (max) uint8_t const desc_hid_report[] = { - TUD_HID_REPORT_DESC_GENERIC_INOUT(64) + TUD_HID_REPORT_DESC_GENERIC_INOUT(64) }; // USB HID object Adafruit_USBD_HID usb_hid; // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } // Notes: following commented-out functions has no affect on ESP32 usb_hid.enableOutEndpoint(true); @@ -64,23 +63,20 @@ void setup() usb_hid.begin(); Serial.begin(115200); - - // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); - Serial.println("Adafruit TinyUSB HID Generic In Out example"); } -void loop() -{ - // nothing to do +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif } // Invoked when received GET_REPORT control request // Application must fill buffer report's content and return its length. // Return zero will cause the stack to STALL request -uint16_t get_report_callback (uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) -{ +uint16_t get_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { // not used in this example (void) report_id; (void) report_type; @@ -91,8 +87,7 @@ uint16_t get_report_callback (uint8_t report_id, hid_report_type_t report_type, // Invoked when received SET_REPORT control request or // received data on OUT endpoint ( Report ID = 0, Type = 0 ) -void set_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) -{ +void set_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { // This example doesn't use multiple report and report ID (void) report_id; (void) report_type; diff --git a/examples/MIDI/midi_multi_ports/midi_multi_ports.ino b/examples/MIDI/midi_multi_ports/midi_multi_ports.ino index 8d39c06c..c7edd8ee 100644 --- a/examples/MIDI/midi_multi_ports/midi_multi_ports.ino +++ b/examples/MIDI/midi_multi_ports/midi_multi_ports.ino @@ -23,10 +23,10 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } // Set name for each cable, must be done before usb_midi.begin() usb_midi.setCableName(1, "Keyboard"); @@ -38,9 +38,16 @@ void setup() void loop() { - digitalWrite(LED_BUILTIN, HIGH); - delay(1000); - - digitalWrite(LED_BUILTIN, LOW); - delay(1000); + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // toggle LED + static uint32_t ms = 0; + static uint8_t led_state = 0; + if (millis() - ms > 1000) { + ms = millis(); + digitalWrite(LED_BUILTIN, 1-led_state); + } } diff --git a/examples/MIDI/midi_test/midi_test.ino b/examples/MIDI/midi_test/midi_test.ino index ce41970b..22cd124c 100644 --- a/examples/MIDI/midi_test/midi_test.ino +++ b/examples/MIDI/midi_test/midi_test.ino @@ -31,20 +31,19 @@ uint32_t position = 0; // Store example melody as an array of note values byte note_sequence[] = { - 74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78, - 74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61, - 56,61,64,68,74,78,81,86,90,93,98,102 + 74, 78, 81, 86, 90, 93, 98, 102, 57, 61, 66, 69, 73, 78, 81, 85, 88, 92, 97, 100, 97, 92, 88, 85, 81, 78, + 74, 69, 66, 62, 57, 62, 66, 69, 74, 78, 81, 86, 90, 93, 97, 102, 97, 93, 90, 85, 81, 78, 73, 68, 64, 61, + 56, 61, 64, 68, 74, 78, 81, 86, 90, 93, 98, 102 }; -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } pinMode(LED_BUILTIN, OUTPUT); - + usb_midi.setStringDescriptor("TinyUSB MIDI"); // Initialize MIDI, and listen to all MIDI channels @@ -59,37 +58,42 @@ void setup() MIDI.setHandleNoteOff(handleNoteOff); Serial.begin(115200); - - // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); } -void loop() -{ +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + + // not enumerated()/mounted() yet: nothing to do + if (!TinyUSBDevice.mounted()) { + return; + } + static uint32_t start_ms = 0; - if ( millis() - start_ms > 266 ) - { + if (millis() - start_ms > 266) { start_ms += 266; - + // Setup variables for the current and previous // positions in the note sequence. int previous = position - 1; - + // If we currently are at position 0, set the // previous position to the last note in the sequence. if (previous < 0) { previous = sizeof(note_sequence) - 1; } - + // Send Note On for current position at full velocity (127) on channel 1. MIDI.sendNoteOn(note_sequence[position], 127, 1); - + // Send Note Off for previous note. MIDI.sendNoteOff(note_sequence[previous], 0, 1); - + // Increment position position++; - + // If we are at the end of the sequence, start over. if (position >= sizeof(note_sequence)) { position = 0; @@ -97,11 +101,10 @@ void loop() } // read any new MIDI messages - MIDI.read(); + MIDI.read(); } -void handleNoteOn(byte channel, byte pitch, byte velocity) -{ +void handleNoteOn(byte channel, byte pitch, byte velocity) { // Log when a note is pressed. Serial.print("Note on: channel = "); Serial.print(channel); @@ -113,8 +116,7 @@ void handleNoteOn(byte channel, byte pitch, byte velocity) Serial.println(velocity); } -void handleNoteOff(byte channel, byte pitch, byte velocity) -{ +void handleNoteOff(byte channel, byte pitch, byte velocity) { // Log when a note is released. Serial.print("Note off: channel = "); Serial.print(channel); diff --git a/examples/MassStorage/msc_ramdisk_dual/msc_ramdisk_dual.ino b/examples/MassStorage/msc_ramdisk_dual/msc_ramdisk_dual.ino index 302fb7e4..27322cba 100644 --- a/examples/MassStorage/msc_ramdisk_dual/msc_ramdisk_dual.ino +++ b/examples/MassStorage/msc_ramdisk_dual/msc_ramdisk_dual.ino @@ -14,20 +14,20 @@ // 8KB is the smallest size that windows allow to mount #define DISK_BLOCK_NUM 16 #define DISK_BLOCK_SIZE 512 + #include "ramdisk.h" Adafruit_USBD_MSC usb_msc; // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } usb_msc.setMaxLun(2); - + // Set disk size and callback for Logical Unit 0 (LUN 0) usb_msc.setID(0, "Adafruit", "Lun0", "1.0"); usb_msc.setCapacity(0, DISK_BLOCK_NUM, DISK_BLOCK_SIZE); @@ -39,7 +39,7 @@ void setup() usb_msc.setCapacity(1, DISK_BLOCK_NUM, DISK_BLOCK_SIZE); usb_msc.setReadWriteCallback(1, ram1_read_cb, ram1_write_cb, ram1_flush_cb); usb_msc.setUnitReady(1, true); - + usb_msc.begin(); Serial.begin(115200); @@ -48,9 +48,11 @@ void setup() Serial.println("Adafruit TinyUSB Mass Storage Dual RAM Disks example"); } -void loop() -{ - // nothing to do +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif } @@ -61,8 +63,7 @@ void loop() // Callback invoked when received READ10 command. // Copy disk's data to buffer (up to bufsize) and // return number of copied bytes (must be multiple of block size) -int32_t ram0_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) -{ +int32_t ram0_read_cb(uint32_t lba, void* buffer, uint32_t bufsize) { uint8_t const* addr = msc_disk0[lba]; memcpy(buffer, addr, bufsize); @@ -72,8 +73,7 @@ int32_t ram0_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) // Callback invoked when received WRITE10 command. // Process data in buffer to disk's storage and // return number of written bytes (must be multiple of block size) -int32_t ram0_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) -{ +int32_t ram0_write_cb(uint32_t lba, uint8_t* buffer, uint32_t bufsize) { uint8_t* addr = msc_disk0[lba]; memcpy(addr, buffer, bufsize); @@ -82,8 +82,7 @@ int32_t ram0_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) // Callback invoked when WRITE10 command is completed (status received and accepted by host). // used to flush any pending cache. -void ram0_flush_cb (void) -{ +void ram0_flush_cb(void) { // nothing to do } @@ -95,8 +94,7 @@ void ram0_flush_cb (void) // Callback invoked when received READ10 command. // Copy disk's data to buffer (up to bufsize) and // return number of copied bytes (must be multiple of block size) -int32_t ram1_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) -{ +int32_t ram1_read_cb(uint32_t lba, void* buffer, uint32_t bufsize) { uint8_t const* addr = msc_disk1[lba]; memcpy(buffer, addr, bufsize); @@ -106,8 +104,7 @@ int32_t ram1_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) // Callback invoked when received WRITE10 command. // Process data in buffer to disk's storage and // return number of written bytes (must be multiple of block size) -int32_t ram1_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) -{ +int32_t ram1_write_cb(uint32_t lba, uint8_t* buffer, uint32_t bufsize) { uint8_t* addr = msc_disk1[lba]; memcpy(addr, buffer, bufsize); @@ -116,7 +113,6 @@ int32_t ram1_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) // Callback invoked when WRITE10 command is completed (status received and accepted by host). // used to flush any pending cache. -void ram1_flush_cb (void) -{ +void ram1_flush_cb(void) { // nothing to do } diff --git a/examples/Video/video_capture/video_capture.ino b/examples/Video/video_capture/video_capture.ino index 353489b1..6cf47587 100644 --- a/examples/Video/video_capture/video_capture.ino +++ b/examples/Video/video_capture/video_capture.ino @@ -120,6 +120,11 @@ static unsigned already_sent = 0; static void fill_color_bar(uint8_t* buffer, unsigned start_position); void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } + Serial.begin(115200); usb_video.addTerminal(&desc_camera_terminal); @@ -132,6 +137,11 @@ void setup() { } void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + if (!tud_video_n_streaming(0, 0)) { already_sent = 0; frame_num = 0; diff --git a/examples/WebUSB/webusb_rgb/webusb_rgb.ino b/examples/WebUSB/webusb_rgb/webusb_rgb.ino index 72bc32bb..f1849801 100644 --- a/examples/WebUSB/webusb_rgb/webusb_rgb.ino +++ b/examples/WebUSB/webusb_rgb/webusb_rgb.ino @@ -54,12 +54,11 @@ Adafruit_USBD_WebUSB usb_web; WEBUSB_URL_DEF(landingPage, 1 /*https*/, "example.tinyusb.org/webusb-rgb/index.html"); // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } //usb_web.setStringDescriptor("TinyUSB WebUSB"); usb_web.setLandingPage(&landingPage); usb_web.setLineStateCallback(line_state_callback); @@ -79,21 +78,24 @@ void setup() pixels.show(); // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); + while (!TinyUSBDevice.mounted()) delay(1); Serial.println("TinyUSB WebUSB RGB example"); } // convert a hex character to number -uint8_t char2num(char c) -{ +uint8_t char2num(char c) { if (c >= 'a') return c - 'a' + 10; if (c >= 'A') return c - 'A' + 10; - return c - '0'; + return c - '0'; } -void loop() -{ +void loop() { + #ifdef TINYUSB_NEED_POLLING_TASK + // Manual call tud_task since it isn't called by Core's background + TinyUSBDevice.task(); + #endif + // Landing Page 7 characters as hex color '#RRGGBB' if (usb_web.available() < 7) return; @@ -104,17 +106,16 @@ void loop() Serial.write(input, 7); Serial.println(); - uint8_t red = 16*char2num(input[1]) + char2num(input[2]); - uint8_t green = 16*char2num(input[3]) + char2num(input[4]); - uint8_t blue = 16*char2num(input[5]) + char2num(input[6]); + uint8_t red = 16 * char2num(input[1]) + char2num(input[2]); + uint8_t green = 16 * char2num(input[3]) + char2num(input[4]); + uint8_t blue = 16 * char2num(input[5]) + char2num(input[6]); uint32_t color = (red << 16) | (green << 8) | blue; pixels.fill(color); pixels.show(); } -void line_state_callback(bool connected) -{ +void line_state_callback(bool connected) { // connected = green, disconnected = red pixels.fill(connected ? 0x00ff00 : 0xff0000); pixels.show(); diff --git a/examples/WebUSB/webusb_serial/webusb_serial.ino b/examples/WebUSB/webusb_serial/webusb_serial.ino index 6dd52d28..90cb9ee7 100644 --- a/examples/WebUSB/webusb_serial/webusb_serial.ino +++ b/examples/WebUSB/webusb_serial/webusb_serial.ino @@ -37,16 +37,15 @@ WEBUSB_URL_DEF(landingPage, 1 /*https*/, "example.tinyusb.org/webusb-serial/inde int led_pin = LED_BUILTIN; // the setup function runs once when you press reset or power the board -void setup() -{ -#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040) - // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040 - TinyUSB_Device_Init(0); -#endif +void setup() { + // Manual begin() is required on core without built-in support e.g. mbed rp2040 + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } pinMode(led_pin, OUTPUT); digitalWrite(led_pin, LOW); - + usb_web.setLandingPage(&landingPage); usb_web.setLineStateCallback(line_state_callback); //usb_web.setStringDescriptor("TinyUSB WebUSB"); @@ -55,57 +54,53 @@ void setup() Serial.begin(115200); // wait until device mounted - while( !TinyUSBDevice.mounted() ) delay(1); + while (!TinyUSBDevice.mounted()) delay(1); Serial.println("TinyUSB WebUSB Serial example"); } // function to echo to both Serial and WebUSB -void echo_all(uint8_t buf[], uint32_t count) -{ - if (usb_web.connected()) - { +void echo_all(uint8_t buf[], uint32_t count) { + if (usb_web.connected()) { usb_web.write(buf, count); usb_web.flush(); } - if ( Serial ) - { - for(uint32_t i=0; i #include +#if defined(CH32V20x) || defined(CH32V30x) +// HACK: required for ch32 core version 1.0.4 or prior, removed when 1.0.5 is +// released +extern "C" void yield(void); +#endif + class Adafruit_USBD_Interface { protected: uint8_t _strid;