From a2e4b957ca314f4b206421ef792df0a55a947394 Mon Sep 17 00:00:00 2001 From: Pieter Pas Date: Thu, 4 Jan 2024 22:52:46 +0100 Subject: [PATCH] ESP32S3 USB MIDI support --- src/MIDI_Interfaces/USBMIDI/USBMIDI.hpp | 22 ++++++++++++ src/MIDI_Interfaces/USBMIDI/USBMIDI_ESP32.hpp | 36 +++++++++++++++++++ src/MIDI_Interfaces/USBMIDI_Interface.hpp | 4 +-- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/MIDI_Interfaces/USBMIDI/USBMIDI_ESP32.hpp diff --git a/src/MIDI_Interfaces/USBMIDI/USBMIDI.hpp b/src/MIDI_Interfaces/USBMIDI/USBMIDI.hpp index c2f34444b3..b560f4f5a4 100644 --- a/src/MIDI_Interfaces/USBMIDI/USBMIDI.hpp +++ b/src/MIDI_Interfaces/USBMIDI/USBMIDI.hpp @@ -4,6 +4,11 @@ #include #include +#ifdef ARDUINO_ARCH_ESP32 +#include +#include +#endif + BEGIN_CS_NAMESPACE inline AH::Array u32_to_bytes(uint32_t u) { @@ -72,6 +77,23 @@ using USBDeviceMIDIBackend = MIDIUSB_USBDeviceMIDIBackend; END_CS_NAMESPACE #endif +#elif defined(ARDUINO_ARCH_ESP32) && (ESP_ARDUINO_VERSION_MAJOR >= 3) && \ + CONFIG_IDF_TARGET_ESP32S3 + +#ifndef ARDUINO_USB_MODE +#error "ESP32S3 expects ARDUINO_USB_MODE to be set" +#endif +#if ARDUINO_USB_MODE != 1 +#include "USBMIDI_ESP32.hpp" +BEGIN_CS_NAMESPACE +using USBDeviceMIDIBackend = ESP32_USBDeviceMIDIBackend; +END_CS_NAMESPACE +#else +#define CS_USB_MIDI_NOT_SUPPORTED +#pragma message( \ + "ESP32S3: USB MIDI not enabled. Set the Tools > USB Type setting to \"USB-OTG\" to enable it.") +#endif + #elif defined(ARDUINO_ARCH_MBED_RP2040) #include "USBMIDI_RP2040.hpp" diff --git a/src/MIDI_Interfaces/USBMIDI/USBMIDI_ESP32.hpp b/src/MIDI_Interfaces/USBMIDI/USBMIDI_ESP32.hpp new file mode 100644 index 0000000000..453bf6ff66 --- /dev/null +++ b/src/MIDI_Interfaces/USBMIDI/USBMIDI_ESP32.hpp @@ -0,0 +1,36 @@ +#include +#include + +#include +#include + +BEGIN_CS_NAMESPACE + +struct ESP32_USBDeviceMIDIBackend { + /// USB MIDI packages are 4 bytes. + using MIDIUSBPacket_t = AH::Array; + /// Called once upon initialization. + void begin() { + backend.begin(); + USB.begin(); + } + /// Read a single packet. Return a packet of all zeros if there is no packet. + MIDIUSBPacket_t read() { + midiEventPacket_t packet {0, 0, 0, 0}; + backend.readPacket(&packet); + return {packet.header, packet.byte1, packet.byte2, packet.byte3}; + } + /// Write a single packet to the output buffer. + void write(MIDIUSBPacket_t p) { + midiEventPacket_t packet {p[0], p[1], p[2], p[3]}; + backend.writePacket(&packet); + } + /// Transmit the output buffer immediately (not implemented). + void sendNow() {} + /// No explicit call to sendNow is required. + bool preferImmediateSend() { return false; } + /// The actual low-level USB MIDI backend provided by the core libraries. + USBMIDI backend; +}; + +END_CS_NAMESPACE \ No newline at end of file diff --git a/src/MIDI_Interfaces/USBMIDI_Interface.hpp b/src/MIDI_Interfaces/USBMIDI_Interface.hpp index 732d5c0367..06bfc31268 100644 --- a/src/MIDI_Interfaces/USBMIDI_Interface.hpp +++ b/src/MIDI_Interfaces/USBMIDI_Interface.hpp @@ -118,8 +118,8 @@ END_CS_NAMESPACE #include "USBMIDI_Interface.ipp" #if defined(TEENSYDUINO) && !defined(TEENSY_MIDIUSB_ENABLED) -#warning \ - "Teensy: USB MIDI not enabled. Enable it from the Tools > USB Type menu." +#pragma message( \ + "Teensy: USB MIDI not enabled. Enable it from the Tools > USB Type menu.") #define CS_USB_MIDI_DISABLED #endif