Files
loopy_midi_controller/src/app_task.cpp
T
ash 4d81386f78 Fix USB MIDI, add LED startup animation, improve logging
- Use ArduinoUSBMIDI library for proper USB MIDI device recognition
- Add LED startup colour cycle (red, green, blue, yellow, magenta, cyan, white)
- Add comprehensive console logging for all MIDI in/out and switch events
- Log unmapped MIDI messages
2026-06-23 13:06:57 +00:00

91 lines
3.0 KiB
C++

#include "app_task.h"
#include <Arduino.h>
AppTask::AppTask(LedStub* led, SwitchStub* sw, UsbMidiTransport* midi)
: led_driver(led), switch_driver(sw), midi_transport(midi) {
for (uint8_t i = 0; i < NUM_PADS; i++) {
pad_mapping[i].physical_switch = i;
pad_mapping[i].midi_channel = 1;
pad_mapping[i].midi_note = i;
pad_mapping[i].led_index = i;
last_switch_state[i] = false;
}
}
void AppTask::begin() {
Serial.println("[APP] Registering MIDI callbacks...");
midi_transport->on_midi_receive([this](const MidiEvent& event) {
process_midi_event(event);
});
Serial.println("[APP] Controller ready");
}
void AppTask::update() {
for (uint8_t i = 0; i < NUM_PADS; i++) {
bool is_pressed = switch_driver->is_pressed(i);
if (is_pressed && !last_switch_state[i]) {
Serial.printf("[APP] Switch %d pressed\n", i);
process_switch_event(i, true);
last_switch_state[i] = true;
} else if (!is_pressed && last_switch_state[i]) {
Serial.printf("[APP] Switch %d released\n", i);
process_switch_event(i, false);
last_switch_state[i] = false;
}
}
}
void AppTask::process_midi_event(const MidiEvent& event) {
uint8_t led_index = 0xFF;
uint8_t midi_channel = event.channel;
uint8_t midi_note = event.data1;
uint8_t midi_velocity = event.data2;
for (uint8_t i = 0; i < NUM_PADS; i++) {
if (pad_mapping[i].midi_channel == midi_channel &&
pad_mapping[i].midi_note == midi_note) {
led_index = pad_mapping[i].led_index;
break;
}
}
if (led_index < NUM_PADS) {
led_driver->set_led_state(
pad_mapping[led_index].midi_note,
pad_mapping[led_index].midi_channel,
event.type == MidiEvent::NOTE_ON ? midi_velocity : 0
);
Serial.printf("[APP] MIDI -> LED: Ch%d Note%d Vel%d -> LED%d\n",
midi_channel, midi_note, midi_velocity, led_index);
} else {
Serial.printf("[APP] MIDI Ch%d Note%d Vel%d - no LED mapping\n",
midi_channel, midi_note, midi_velocity);
}
}
void AppTask::process_switch_event(uint8_t switch_id, bool pressed) {
for (uint8_t i = 0; i < NUM_PADS; i++) {
if (pad_mapping[i].physical_switch == switch_id) {
uint8_t channel = pad_mapping[i].midi_channel;
uint8_t note = pad_mapping[i].midi_note;
uint8_t velocity = pressed ? 127 : 0;
if (pressed) {
midi_transport->send_note_on(channel, note, velocity);
} else {
midi_transport->send_note_off(channel, note, velocity);
}
Serial.printf("[APP] Switch %d -> Ch%d Note%d Vel%d (%s)\n",
switch_id, channel, note, velocity,
pressed ? "PRESS" : "RELEASE");
break;
}
}
}