MIDI input

This commit is contained in:
Dejvino
2026-02-21 21:12:30 +01:00
parent 94ade20143
commit 8ef4371711
4 changed files with 78 additions and 2 deletions
+65 -1
View File
@@ -2,19 +2,83 @@
// MIDI UART Pins (GP0/GP1)
#define PIN_MIDI_TX 0
#define PIN_MIDI_RX 1
MidiDriver midi;
MidiDriver::MidiDriver() {
MidiDriver::MidiDriver() : lastInputNote(-1), lastInputVelocity(0), _runningStatus(0), _byteIndex(0), _data1(0), _data2(0) {
}
void MidiDriver::begin() {
mutex_init(&_mutex);
Serial1.setTX(PIN_MIDI_TX);
Serial1.setRX(PIN_MIDI_RX);
Serial1.begin(31250);
Serial.println(F("MIDI Serial initialized on GP0/GP1"));
}
void MidiDriver::update() {
while (Serial1.available()) {
uint8_t b = Serial1.read();
Serial1.write(b); // Soft THRU: Merge input with output
// Realtime messages don't affect running status
if (b >= 0xF8) continue;
if (b >= 0x80) {
_runningStatus = b;
_byteIndex = 0;
} else if (_runningStatus) {
if (_byteIndex == 0) {
_data1 = b;
_byteIndex++;
// Handle 2-byte messages (Program Change 0xC0, Channel Pressure 0xD0)
uint8_t type = _runningStatus & 0xF0;
if (type == 0xC0 || type == 0xD0) {
_byteIndex = 0; // Message complete
}
} else if (_byteIndex == 1) {
_data2 = b;
_byteIndex = 0; // Message complete
uint8_t channel = (_runningStatus & 0x0F) + 1;
uint8_t type = _runningStatus & 0xF0;
if (type == 0x90) {
if (_data2 > 0) {
// Serial.print(F("Note On CH"));
// Serial.print(channel);
// Serial.print(F(": "));
// Serial.print(_data1);
// Serial.print(F(" Vel: "));
// Serial.println(_data2);
lastInputNote = _data1;
lastInputVelocity = _data2;
} else {
// Serial.print(F("Note Off CH"));
// Serial.print(channel);
// Serial.print(F(": "));
// Serial.println(_data1);
if (lastInputNote == _data1) {
lastInputNote = -1; // Note On vel 0 is Note Off
lastInputVelocity = 0;
}
}
} else if (type == 0x80) {
// Serial.print(F("Note Off CH"));
// Serial.print(channel);
// Serial.print(F(": "));
// Serial.println(_data1);
if (lastInputNote == _data1) {
lastInputNote = -1;
lastInputVelocity = 0;
}
}
}
}
}
}
void MidiDriver::lock() {
mutex_enter_blocking(&_mutex);
}