Skip to content

Commit

Permalink
Prepare MIDI Host support.
Browse files Browse the repository at this point in the history
  • Loading branch information
DatanoiseTV committed Sep 13, 2024
1 parent 7cde60e commit 1e337fc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ target_link_libraries(main
pico_multicore
pico_stdio_uart
tinyusb_device
tinyusb_host
tinyusb_board
Audio
#Oled
Expand Down
3 changes: 3 additions & 0 deletions include/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#error CFG_TUSB_MCU must be defined
#endif


// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_DEVICE_RHPORT_NUM
#define BOARD_DEVICE_RHPORT_NUM 0
Expand Down Expand Up @@ -101,6 +102,8 @@
#define CFG_TUD_MIDI 1
#define CFG_TUD_VENDOR 0

#define CFG_TUH_MIDI CFG_TUD_MIDI

// MIDI FIFO size of TX and RX
#define CFG_TUD_MIDI_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_MIDI_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ extern "C" {
while (1)
{
// TinyUSB Device Task
#if (OPT_MODE_HOST == 1)
tuh_task();
#else
tud_task();
#endif
usbMIDI.process();
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/midi_input_usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ uint16_t MIDIInputUSB::parsePitchBend(const uint8_t* packet) {
return (packet[2] << 7) | packet[1];
}

// macro for reading midi packets. in usb host mode, tuh_midi_stream_read is used instead, which is a wrapper for this function
#if (OPT_MODE_HOST == 1)
#define TINYUSB_MIDI_STREAM_READ(packet, len) tuh_midi_stream_read(packet, len)
#define TINYUSB_MIDI_STREAM_AVAILABLE() tuh_midi_available()
#error "We are in host mode"
#else
#define TINYUSB_MIDI_STREAM_READ(packet, len) tud_midi_stream_read(packet, len)
#define TINYUSB_MIDI_STREAM_AVAILABLE() tud_midi_available()
#endif

void MIDIInputUSB::process() {
uint8_t packet[4];

while (tud_midi_available()) {
tud_midi_stream_read(packet, 3);

while (TINYUSB_MIDI_STREAM_AVAILABLE()) {
TINYUSB_MIDI_STREAM_READ(packet, 3);
#ifdef DEBUG_MIDI
printf("%02X %02X %02X %02X\n", packet[0], packet[1], packet[2], packet[3]);
#endif
Expand Down

0 comments on commit 1e337fc

Please sign in to comment.