07e5cd8994
PixelStomp MUX uses: - WS2812C LEDs: one data line via DAT (GPIO 9), NeoPixel protocol - 74HC165 shift register: LD/CLK/DI for reading 8 button states Changes: - Use Adafruit NeoPixel library for LED control - Proper 74HC165 parallel-load shift-in for buttons - 8 switches + 8 LEDs (was incorrectly 10) - Diagnostic commands: dump, ledtest, red, green, blue, read
62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#include "switch_stub.h"
|
|
#include "pixel_stomp_mux.h"
|
|
#include <Arduino.h>
|
|
|
|
static PixelStompMux* mux_ptr = nullptr;
|
|
|
|
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].current_state = false;
|
|
switch_states[i].previous_state = false;
|
|
switch_states[i].last_change_time = 0;
|
|
switch_states[i].debounce_time = 50;
|
|
}
|
|
}
|
|
|
|
void DefaultSwitchStub::begin() {
|
|
Serial.println("[SW] Using 74HC165 via PixelStomp MUX");
|
|
initialized = true;
|
|
}
|
|
|
|
void DefaultSwitchStub::set_mux(PixelStompMux* mux) {
|
|
mux_ptr = mux;
|
|
}
|
|
|
|
bool DefaultSwitchStub::is_pressed(uint8_t switch_id) {
|
|
if (!initialized || switch_id >= NUM_SWITCHES || !mux_ptr) {
|
|
return false;
|
|
}
|
|
|
|
SwitchState& sw = switch_states[switch_id];
|
|
bool raw = mux_ptr->is_button_pressed(switch_id);
|
|
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;
|
|
Serial.printf("[SW] Switch %d remapped to GPIO %d\n", switch_id, gpio_pin);
|
|
}
|
|
|
|
void DefaultSwitchStub::set_debounce_time(uint32_t time_ms) {
|
|
for (int i = 0; i < NUM_SWITCHES; i++) {
|
|
switch_states[i].debounce_time = time_ms;
|
|
}
|
|
Serial.printf("[SW] Debounce: %lu ms\n", time_ms);
|
|
}
|