Files
loopy_midi_controller/src/led_stub.cpp
T

99 lines
2.7 KiB
C++

#include "led_stub.h"
#include "pixel_stomp_mux.h"
#include <Arduino.h>
static PixelStompMux* mux_ptr = nullptr;
DefaultLedStub::DefaultLedStub() : initialized(false) {
for (int i = 0; i < NUM_LEDS; i++) {
led_states[i].active = false;
led_states[i].velocity = 0;
led_states[i].note = 0;
led_states[i].channel = 0;
led_states[i].timestamp = 0;
}
}
void DefaultLedStub::set_mux(PixelStompMux* mux) {
mux_ptr = mux;
}
void DefaultLedStub::begin() {
Serial.println("[LED] Using WS2812C via PixelStomp MUX (FastLED I2S)");
initialized = true;
if (!mux_ptr) {
Serial.println("[LED] ERROR: No MUX configured");
return;
}
Serial.println("[LED] Startup animation...");
uint32_t colors[] = {
0xFF0000,
0x00FF00,
0x0000FF,
0xFFFF00,
0xFF00FF,
0x00FFFF,
0xFFFFFF,
};
int num_colors = sizeof(colors) / sizeof(colors[0]);
for (int c = 0; c < num_colors; c++) {
uint32_t color = colors[c];
uint8_t r = (color >> 16) & 0xFF;
uint8_t g = (color >> 8) & 0xFF;
uint8_t b = color & 0xFF;
for (int i = 0; i < NUM_LEDS; i++) {
mux_ptr->set_led_color(i, r, g, b);
}
mux_ptr->show();
Serial.printf("[LED] Colour %d: R=%d G=%d B=%d\n", c, r, g, b);
delay(300);
}
clear_all();
Serial.println("[LED] Startup complete");
}
void DefaultLedStub::set_led_state(uint8_t note, uint8_t channel, uint8_t velocity) {
if (!initialized || !mux_ptr) return;
uint8_t led_index = note_to_index(note);
if (led_index < NUM_LEDS) {
led_states[led_index].note = note;
led_states[led_index].channel = channel;
led_states[led_index].velocity = velocity;
led_states[led_index].active = (velocity > 0);
led_states[led_index].timestamp = millis();
if (velocity > 0) {
uint8_t brightness = map(velocity, 1, 127, 50, 255);
mux_ptr->set_led_color(led_index, brightness, brightness, brightness);
} else {
mux_ptr->set_led_color(led_index, 0, 0, 0);
}
mux_ptr->show();
Serial.printf("[LED] Note %d -> LED %d Vel %d (%s)\n",
note, led_index, velocity,
velocity > 0 ? "ON" : "OFF");
} else {
Serial.printf("[LED] Out of range: %d (Note: %d)\n", led_index, note);
}
}
void DefaultLedStub::clear_all() {
for (int i = 0; i < NUM_LEDS; i++) {
led_states[i].active = false;
led_states[i].velocity = 0;
}
if (mux_ptr) {
mux_ptr->clear_all();
}
Serial.println("[LED] All cleared");
}