Convert to PlatformIO/Arduino format
- Replace ESP-IDF structure with PlatformIO (platformio.ini) - Move source files to src/ and include/ - Move libraries to lib/ directory - Replace FreeRTOS with Arduino task/vTaskDelay - Use Arduino MIDI library instead of raw TinyUSB - Dual-core: MIDI on core 0, controller on core 1
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
#include "midi_transport.h"
|
||||
#include <Arduino.h>
|
||||
#include <USB.h>
|
||||
#include <USBMIDI.h>
|
||||
|
||||
// TinyUSB MIDI interface
|
||||
static USBMIDI MIDI;
|
||||
|
||||
UsbMidiTransport::UsbMidiTransport() : initialized(false) {
|
||||
}
|
||||
|
||||
UsbMidiTransport::~UsbMidiTransport() {
|
||||
}
|
||||
|
||||
bool UsbMidiTransport::begin() {
|
||||
USB.begin();
|
||||
MIDI.begin(MIDI_CHANNEL_OMNI);
|
||||
initialized = true;
|
||||
Serial.println("[MIDI] USB MIDI transport initialized");
|
||||
return true;
|
||||
}
|
||||
|
||||
void UsbMidiTransport::update() {
|
||||
if (!initialized) return;
|
||||
|
||||
// Check for incoming MIDI
|
||||
if (MIDI.read()) {
|
||||
MidiEvent event;
|
||||
midi_message_t msg = MIDI.getMessage();
|
||||
|
||||
event.channel = msg.channel;
|
||||
event.data1 = msg.data1;
|
||||
event.data2 = msg.data2;
|
||||
event.timestamp = millis();
|
||||
|
||||
// Map Arduino MIDI types to our types
|
||||
switch (msg.type) {
|
||||
case midi::NoteOn:
|
||||
event.type = (msg.data2 > 0) ? MidiEvent::NOTE_ON : MidiEvent::NOTE_OFF;
|
||||
break;
|
||||
case midi::NoteOff:
|
||||
event.type = MidiEvent::NOTE_OFF;
|
||||
break;
|
||||
case midi::ControlChange:
|
||||
event.type = MidiEvent::CONTROL_CHANGE;
|
||||
break;
|
||||
case midi::ProgramChange:
|
||||
event.type = MidiEvent::PROGRAM_CHANGE;
|
||||
break;
|
||||
case midi::PitchBend:
|
||||
event.type = MidiEvent::PITCH_BEND;
|
||||
break;
|
||||
default:
|
||||
return; // Unknown type, skip
|
||||
}
|
||||
|
||||
// Diagnostic logging
|
||||
const char* type_str = "UNK";
|
||||
switch (event.type) {
|
||||
case MidiEvent::NOTE_ON: type_str = "NOTE_ON"; break;
|
||||
case MidiEvent::NOTE_OFF: type_str = "NOTE_OFF"; break;
|
||||
case MidiEvent::CONTROL_CHANGE: type_str = "CC"; break;
|
||||
case MidiEvent::PROGRAM_CHANGE: type_str = "PC"; break;
|
||||
case MidiEvent::PITCH_BEND: type_str = "PB"; break;
|
||||
default: break;
|
||||
}
|
||||
Serial.printf("[MIDI IN] Ch:%d %s:%d:%d\n", event.channel, type_str, event.data1, event.data2);
|
||||
|
||||
// Call callback
|
||||
if (receive_callback) {
|
||||
receive_callback(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UsbMidiTransport::on_midi_receive(std::function<void(const MidiEvent&)> callback) {
|
||||
receive_callback = callback;
|
||||
}
|
||||
|
||||
void UsbMidiTransport::send_note_on(uint8_t channel, uint8_t note, uint8_t velocity) {
|
||||
if (!initialized) return;
|
||||
MIDI.sendNoteOn(note, velocity, channel);
|
||||
}
|
||||
|
||||
void UsbMidiTransport::send_note_off(uint8_t channel, uint8_t note, uint8_t velocity) {
|
||||
if (!initialized) return;
|
||||
MIDI.sendNoteOff(note, velocity, channel);
|
||||
}
|
||||
|
||||
void UsbMidiTransport::send_cc(uint8_t channel, uint8_t cc, uint8_t value) {
|
||||
if (!initialized) return;
|
||||
MIDI.sendControlChange(cc, value, channel);
|
||||
}
|
||||
|
||||
void UsbMidiTransport::parse_midi_packet(const uint8_t* buffer, uint32_t size, MidiEvent& event) {
|
||||
// Not used with Arduino MIDI library - kept for reference
|
||||
}
|
||||
Reference in New Issue
Block a user