Skip to content

Commit

Permalink
ESP32S3 USB MIDI support
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Jan 4, 2024
1 parent c09b2a7 commit a2e4b95
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/MIDI_Interfaces/USBMIDI/USBMIDI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <AH/STL/cstdint>
#include <Settings/NamespaceSettings.hpp>

#ifdef ARDUINO_ARCH_ESP32
#include <esp_arduino_version.h>
#include <sdkconfig.h>
#endif

BEGIN_CS_NAMESPACE

inline AH::Array<uint8_t, 4> u32_to_bytes(uint32_t u) {
Expand Down Expand Up @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions src/MIDI_Interfaces/USBMIDI/USBMIDI_ESP32.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <AH/Containers/Array.hpp>
#include <Settings/NamespaceSettings.hpp>

#include <USB.h>
#include <USBMIDI.h>

BEGIN_CS_NAMESPACE

struct ESP32_USBDeviceMIDIBackend {
/// USB MIDI packages are 4 bytes.
using MIDIUSBPacket_t = AH::Array<uint8_t, 4>;
/// 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
4 changes: 2 additions & 2 deletions src/MIDI_Interfaces/USBMIDI_Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a2e4b95

Please sign in to comment.