Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework ESP32 configuration descriptor builder #372

Merged
merged 13 commits into from
Feb 6, 2024
4 changes: 3 additions & 1 deletion examples/CDC/no_serial/no_serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ int led = LED_BUILTIN;

void setup()
{
Serial.end();
// clear configuration will remove all USB interfaces including CDC (Serial)
TinyUSBDevice.clearConfiguration();

pinMode(led, OUTPUT);
}

Expand Down
26 changes: 17 additions & 9 deletions examples/Composite/mouse_external_flash/mouse_external_flash.ino
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ Adafruit_USBD_MSC usb_msc;

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_MOUSE()
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_MOUSE()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
// USB HID object
Adafruit_USBD_HID usb_hid;

#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
const int pin = 4; // Left Button
Expand All @@ -93,6 +91,10 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT
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;
Expand All @@ -102,6 +104,11 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT
// 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);
Expand All @@ -123,9 +130,10 @@ void setup()
// Set up button
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));

// 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);
Expand Down
21 changes: 12 additions & 9 deletions examples/Composite/mouse_ramdisk/mouse_ramdisk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ Adafruit_USBD_MSC usb_msc;

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_MOUSE()
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_MOUSE()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
// USB HID object
Adafruit_USBD_HID usb_hid;

#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
const int pin = 4; // Left Button
Expand All @@ -54,6 +52,10 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT
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;
Expand Down Expand Up @@ -84,9 +86,10 @@ void setup()
// Set up button
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));

// 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);
Expand Down
15 changes: 7 additions & 8 deletions examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
Adafruit_USBD_HID usb_hid;

//------------- Input Pins -------------//
// Array of pins and its keycode.
Expand Down Expand Up @@ -73,11 +72,11 @@ void setup()
TinyUSB_Device_Init(0);
#endif

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// usb_hid.setStringDescriptor("TinyUSB Keyboard");
// Setup HID
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB Keyboard");

// Set up output report (on control endpoint) for Capslock indicator
usb_hid.setReportCallback(NULL, hid_report_callback);
Expand Down
22 changes: 12 additions & 10 deletions examples/HID/hid_boot_mouse/hid_boot_mouse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
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;
Expand All @@ -38,14 +42,12 @@

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_MOUSE()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_MOUSE, 2, false);
// USB HID object
Adafruit_USBD_HID usb_hid;

// the setup function runs once when you press reset or power the board
void setup()
Expand All @@ -58,11 +60,11 @@ void setup()
// Set up button, pullup opposite to active state
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// usb_hid.setStringDescriptor("TinyUSB Mouse");
// Set up HID
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB Mouse");

usb_hid.begin();

Expand Down
20 changes: 11 additions & 9 deletions examples/HID/hid_composite/hid_composite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
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;
Expand All @@ -46,24 +50,22 @@ enum
};

// HID report descriptor using TinyUSB's template
uint8_t const desc_hid_report[] =
{
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) )
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
// USB HID object.
Adafruit_USBD_HID usb_hid;

// the setup function runs once when you press reset or power the board
void setup()
{
// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor();
// usb_hid.setStringDescriptor("TinyUSB HID Composite");
// Set up HID
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB HID Composite");

usb_hid.begin();

Expand Down
33 changes: 20 additions & 13 deletions examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,43 @@
const int pin = PIN_BUTTON;
bool activeState = false;

#elif defined(ARDUINO_ARCH_ESP32)
const int pin = 0;
bool activeState = false;

#else
const int pin = 12;
bool activeState = false;
#endif

// HID report descriptor using TinyUSB's template
uint8_t const desc_keyboard_report[] =
{
uint8_t const desc_keyboard_report[] = {
TUD_HID_REPORT_DESC_KEYBOARD()
};

uint8_t const desc_mouse_report[] =
{
uint8_t const desc_mouse_report[] = {
TUD_HID_REPORT_DESC_MOUSE()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_keyboard(desc_keyboard_report, sizeof(desc_keyboard_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
Adafruit_USBD_HID usb_mouse(desc_mouse_report, sizeof(desc_mouse_report), HID_ITF_PROTOCOL_MOUSE, 2, false);
// USB HID objects
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()
{
// Notes: following commented-out functions has no affect on ESP32
// usb_keyboard.setPollInterval(2);
// usb_keyboard.setReportDescriptor();
// usb_keyboard.setStringDescriptor("TinyUSB HID Composite");

// HID Keyboard
usb_keyboard.setPollInterval(2);
usb_keyboard.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
usb_keyboard.setReportDescriptor(desc_keyboard_report, sizeof(desc_keyboard_report));
usb_keyboard.setStringDescriptor("TinyUSB HID Keyboard");
usb_keyboard.begin();

// HID Mouse
usb_mouse.setPollInterval(2);
usb_mouse.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
usb_mouse.setReportDescriptor(desc_mouse_report, sizeof(desc_mouse_report));
usb_mouse.setStringDescriptor("TinyUSB HID Keyboard");
usb_mouse.begin();

// Set up button, pullup opposite to active state
Expand Down
14 changes: 6 additions & 8 deletions examples/HID/hid_gamepad/hid_gamepad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_GAMEPAD()
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
// USB HID object
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
Expand All @@ -45,9 +43,9 @@ void setup()

Serial.begin(115200);

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// Setup HID
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));

usb_hid.begin();

Expand Down
16 changes: 7 additions & 9 deletions examples/HID/hid_generic_inout/hid_generic_inout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@

// HID report descriptor using TinyUSB's template
// Generic In Out with 64 bytes report (max)
uint8_t const desc_hid_report[] =
{
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_GENERIC_INOUT(64)
};

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, true);
// USB HID object
Adafruit_USBD_HID usb_hid;

// the setup function runs once when you press reset or power the board
void setup()
Expand All @@ -57,10 +55,10 @@ void setup()
#endif

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.enableOutEndpoint(true);
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// usb_hid.setStringDescriptor("TinyUSB HID Generic");
usb_hid.enableOutEndpoint(true);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB HID Generic");

usb_hid.setReportCallback(get_report_callback, set_report_callback);
usb_hid.begin();
Expand Down
2 changes: 1 addition & 1 deletion examples/MIDI/midi_test/midi_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void setup()

pinMode(LED_BUILTIN, OUTPUT);

//usb_midi.setStringDescriptor("TinyUSB MIDI");
usb_midi.setStringDescriptor("TinyUSB MIDI");

// Initialize MIDI, and listen to all MIDI channels
// This will also call usb_midi's begin()
Expand Down
7 changes: 7 additions & 0 deletions examples/Video/video_capture/video_capture.ino
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ void loop() {
tud_video_n_frame_xfer(0, 0, (void*) frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
}

//--------------------------------------------------------------------+
// TinyUSB Video Callbacks
//--------------------------------------------------------------------+
extern "C" {

void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
(void) ctl_idx;
(void) stm_idx;
Expand All @@ -173,6 +178,8 @@ int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,
return VIDEO_ERROR_NONE;
}

} // extern C

//------------- Helper -------------//
static void fill_color_bar(uint8_t* buffer, unsigned start_position) {
/* EBU color bars
Expand Down
Loading
Loading