64 lines
1.8 KiB
C++
64 lines
1.8 KiB
C++
|
|
// === buttoncontrol.cpp ===
|
|
#include "buttoncontrol.h"
|
|
#include "ledcontrol.h" // Neue LED-Logik wird hier verwendet
|
|
|
|
static uint8_t btnPin;
|
|
static uint32_t pressStart = 0;
|
|
static bool pressed = false;
|
|
static const ButtonActionEntry *btnActions = nullptr;
|
|
static uint8_t btnActionCount = 0;
|
|
static uint8_t currentActionIndex = 0xFF;
|
|
static uint32_t lastColor = 0;
|
|
|
|
void ButtonControl_Init(uint8_t pin, const ButtonActionEntry *actions, uint8_t actionCount)
|
|
{
|
|
btnPin = pin;
|
|
pinMode(btnPin, INPUT_PULLUP);
|
|
btnActions = actions;
|
|
btnActionCount = actionCount;
|
|
}
|
|
|
|
void ButtonControl_Update()
|
|
{
|
|
bool currentState = digitalRead(btnPin) == LOW;
|
|
uint32_t now = millis();
|
|
|
|
if (currentState && !pressed)
|
|
{
|
|
pressStart = now;
|
|
pressed = true;
|
|
currentActionIndex = 0xFF;
|
|
lastColor = 0;
|
|
}
|
|
else if (currentState && pressed)
|
|
{
|
|
uint32_t duration = now - pressStart;
|
|
// Finde passende Aktion basierend auf Zeit
|
|
for (uint8_t i = 0; i < btnActionCount; i++)
|
|
{
|
|
if (duration >= btnActions[i].holdTimeMs)
|
|
{
|
|
if (currentActionIndex != i)
|
|
{
|
|
currentActionIndex = i;
|
|
lastColor = btnActions[i].ledColor;
|
|
// Farbe + Pattern setzen
|
|
LEDControl_SetOverride(lastColor, LED_PATTERN_BREATH, 0); // Kein Timeout, wird bei Release beendet
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (!currentState && pressed)
|
|
{
|
|
pressed = false;
|
|
if (currentActionIndex != 0xFF && currentActionIndex < btnActionCount)
|
|
{
|
|
if (btnActions[currentActionIndex].callback)
|
|
{
|
|
btnActions[currentActionIndex].callback();
|
|
}
|
|
}
|
|
LEDControl_ClearOverride(); // Override-Modus zurücksetzen
|
|
}
|
|
} |