From bb32ec65d1f1844ff4ceb766a709d4d26d1a8460 Mon Sep 17 00:00:00 2001 From: Ashley Strahle Date: Tue, 23 Jun 2026 13:18:20 +0000 Subject: [PATCH] Fix USB MIDI, add GPIO for switches and LEDs - Remove TinyUSBDevice.begin() conflict with Arduino core - Switch stub now reads GPIO with debounce (pins 1-10) - LED stub now drives GPIO pins (pins 11-20) with startup animation - All pins printed to console on init --- src/led_stub.cpp | 47 +++++++++++++++++++----------------------- src/midi_transport.cpp | 5 ++--- src/switch_stub.cpp | 36 +++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/led_stub.cpp b/src/led_stub.cpp index 72cdfd5..db15059 100644 --- a/src/led_stub.cpp +++ b/src/led_stub.cpp @@ -1,6 +1,10 @@ #include "led_stub.h" #include +static const uint8_t LED_PINS[10] = { + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 +}; + DefaultLedStub::DefaultLedStub() : initialized(false) { for (int i = 0; i < NUM_LEDS; i++) { led_states[i].active = false; @@ -12,39 +16,25 @@ DefaultLedStub::DefaultLedStub() : initialized(false) { } void DefaultLedStub::begin() { + for (int i = 0; i < NUM_LEDS; i++) { + pinMode(LED_PINS[i], OUTPUT); + digitalWrite(LED_PINS[i], LOW); + } initialized = true; Serial.println("[LED] Startup colour cycle..."); - uint16_t colours[] = { - 0xF800, // Red - 0x07E0, // Green - 0x001F, // Blue - 0xFFE0, // Yellow - 0xF81F, // Magenta - 0x07FF, // Cyan - 0xFFFF, // White - }; - int num_colours = sizeof(colours) / sizeof(colours[0]); - - for (int c = 0; c < num_colours; c++) { - uint16_t colour = colours[c]; - uint8_t r = (colour >> 11) & 0x1F; - uint8_t g = (colour >> 5) & 0x3F; - uint8_t b = colour & 0x1F; - - for (int i = 0; i < NUM_LEDS; i++) { - led_states[i].note = i; - led_states[i].channel = 1; - led_states[i].velocity = 100; - led_states[i].active = true; - led_states[i].timestamp = millis(); - } - - Serial.printf("[LED] Colour %d: R=%d G=%d B=%d\n", c, r, g, b); + for (int i = 0; i < NUM_LEDS; i++) { + digitalWrite(LED_PINS[i], HIGH); + Serial.printf("[LED] LED %d ON (GPIO %d)\n", i, LED_PINS[i]); delay(100); } + for (int i = 0; i < NUM_LEDS; i++) { + digitalWrite(LED_PINS[i], LOW); + delay(50); + } + clear_all(); Serial.println("[LED] Startup cycle complete"); } @@ -60,6 +50,8 @@ void DefaultLedStub::set_led_state(uint8_t note, uint8_t channel, uint8_t veloci led_states[led_index].velocity = velocity; led_states[led_index].active = (velocity > 0); led_states[led_index].timestamp = millis(); + + digitalWrite(LED_PINS[led_index], velocity > 0 ? HIGH : LOW); Serial.printf("[LED] Note %d -> LED %d Ch %d Vel %d (%s)\n", note, led_index, channel, velocity, @@ -73,6 +65,9 @@ void DefaultLedStub::clear_all() { for (int i = 0; i < NUM_LEDS; i++) { led_states[i].active = false; led_states[i].velocity = 0; + if (initialized) { + digitalWrite(LED_PINS[i], LOW); + } } Serial.println("[LED] All LEDs cleared"); } diff --git a/src/midi_transport.cpp b/src/midi_transport.cpp index ac6e80b..eef8589 100644 --- a/src/midi_transport.cpp +++ b/src/midi_transport.cpp @@ -11,7 +11,7 @@ UsbMidiTransport::~UsbMidiTransport() { } bool UsbMidiTransport::begin() { - Serial.println("[MIDI] Initializing USB MIDI..."); + Serial.println("[MIDI] Setting up USB MIDI device..."); TinyUSBDevice.setManufacturerDescriptor("Ashley Strahle"); TinyUSBDevice.setProductDescriptor("Loopy Foot Controller"); @@ -22,9 +22,8 @@ bool UsbMidiTransport::begin() { return false; } - TinyUSBDevice.begin(); initialized = true; - Serial.println("[MIDI] USB MIDI initialized - waiting for host"); + Serial.println("[MIDI] USB MIDI ready"); return true; } diff --git a/src/switch_stub.cpp b/src/switch_stub.cpp index b68ffb5..b0ee732 100644 --- a/src/switch_stub.cpp +++ b/src/switch_stub.cpp @@ -1,10 +1,14 @@ #include "switch_stub.h" #include +static const uint8_t SWITCH_PINS[10] = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 +}; + DefaultSwitchStub::DefaultSwitchStub() : initialized(false) { for (int i = 0; i < NUM_SWITCHES; i++) { switch_states[i].id = i; - switch_states[i].gpio_pin = 0; + switch_states[i].gpio_pin = SWITCH_PINS[i]; switch_states[i].current_state = false; switch_states[i].previous_state = false; switch_states[i].last_change_time = 0; @@ -13,20 +17,46 @@ DefaultSwitchStub::DefaultSwitchStub() : initialized(false) { } void DefaultSwitchStub::begin() { + for (int i = 0; i < NUM_SWITCHES; i++) { + pinMode(switch_states[i].gpio_pin, INPUT_PULLUP); + } initialized = true; - Serial.println("[SW] Stub initialized (GPIO pins not configured yet)"); + Serial.printf("[SW] Initialized %d switches on GPIOs: ", NUM_SWITCHES); + for (int i = 0; i < NUM_SWITCHES; i++) { + Serial.printf("%d ", switch_states[i].gpio_pin); + } + Serial.println(); } bool DefaultSwitchStub::is_pressed(uint8_t switch_id) { if (!initialized || switch_id >= NUM_SWITCHES) { return false; } - return switch_states[switch_id].current_state; + + SwitchState& sw = switch_states[switch_id]; + bool raw = (digitalRead(sw.gpio_pin) == LOW); + uint32_t now = millis(); + + if (raw != sw.previous_state) { + sw.last_change_time = now; + } + + if ((now - sw.last_change_time) >= sw.debounce_time) { + if (raw != sw.current_state) { + sw.current_state = raw; + } + } + + sw.previous_state = raw; + return sw.current_state; } void DefaultSwitchStub::configure_switch(uint8_t switch_id, uint8_t gpio_pin) { if (switch_id >= NUM_SWITCHES) return; switch_states[switch_id].gpio_pin = gpio_pin; + if (initialized) { + pinMode(gpio_pin, INPUT_PULLUP); + } Serial.printf("[SW] Switch %d configured to GPIO %d\n", switch_id, gpio_pin); }