WLED/wled00/button.cpp

143 wiersze
3.4 KiB
C++
Czysty Zwykły widok Historia

#include "wled.h"
2020-03-25 09:14:23 +00:00
/*
* Physical IO
*/
2021-04-11 22:45:33 +00:00
#define WLED_DEBOUNCE_THRESHOLD 50 //only consider button input of at least 50ms as valid (debouncing)
void shortPressAction()
{
if (!macroButton)
{
toggleOnOff();
2020-02-22 15:17:32 +00:00
colorUpdated(NOTIFIER_CALL_MODE_BUTTON);
} else {
2020-11-06 21:12:48 +00:00
applyPreset(macroButton);
}
}
bool isButtonPressed()
{
2021-01-16 23:20:31 +00:00
if (btnPin>=0 && digitalRead(btnPin) == LOW) return true;
#ifdef TOUCHPIN
if (touchRead(TOUCHPIN) <= TOUCH_THRESHOLD) return true;
#endif
return false;
}
2021-04-11 22:45:33 +00:00
void handleSwitch()
{
if (buttonPressedBefore != isButtonPressed()) {
buttonPressedTime = millis();
buttonPressedBefore = !buttonPressedBefore;
}
if (buttonLongPressed == buttonPressedBefore) return;
if (millis() - buttonPressedTime > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce)
if (buttonPressedBefore) { //LOW, falling edge, switch closed
if (macroButton) applyPreset(macroButton);
else { //turn on
if (!bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);}
}
} else { //HIGH, rising edge, switch opened
if (macroLongPress) applyPreset(macroLongPress);
else { //turn off
if (bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);}
}
}
buttonLongPressed = buttonPressedBefore; //save the last "long term" switch state
}
}
void handleButton()
{
2021-04-11 22:45:33 +00:00
if (btnPin<0 || buttonType < BTN_TYPE_PUSH) return;
if (buttonType == BTN_TYPE_SWITCH) { //button is not momentary, but switch. This is only suitable on pins whose on-boot state does not matter (NO gpio0)
handleSwitch(); return;
}
2021-04-11 22:45:33 +00:00
//momentary button logic
if (isButtonPressed()) //pressed
{
2019-10-18 10:19:52 +00:00
if (!buttonPressedBefore) buttonPressedTime = millis();
buttonPressedBefore = true;
2019-10-18 10:19:52 +00:00
if (millis() - buttonPressedTime > 600) //long press
{
if (!buttonLongPressed)
{
2020-11-06 21:12:48 +00:00
if (macroLongPress) {applyPreset(macroLongPress);}
2019-10-18 10:19:52 +00:00
else _setRandomColor(false,true);
buttonLongPressed = true;
}
}
}
else if (!isButtonPressed() && buttonPressedBefore) //released
{
long dur = millis() - buttonPressedTime;
2021-04-11 22:45:33 +00:00
if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore = false; return;} //too short "press", debounce
bool doublePress = buttonWaitTime;
buttonWaitTime = 0;
2019-10-18 10:19:52 +00:00
if (dur > 6000) //long press
{
2020-03-25 09:14:23 +00:00
WLED::instance().initAP(true);
}
2019-10-18 10:19:52 +00:00
else if (!buttonLongPressed) { //short press
if (macroDoublePress)
{
2020-11-06 21:12:48 +00:00
if (doublePress) applyPreset(macroDoublePress);
else buttonWaitTime = millis();
} else shortPressAction();
}
buttonPressedBefore = false;
2019-10-18 10:19:52 +00:00
buttonLongPressed = false;
}
if (buttonWaitTime && millis() - buttonWaitTime > 450 && !buttonPressedBefore)
{
buttonWaitTime = 0;
shortPressAction();
}
}
void handleIO()
{
handleButton();
//set relay when LEDs turn on
if (strip.getBrightness())
{
lastOnTime = millis();
if (offMode)
{
2021-01-16 23:20:31 +00:00
if (rlyPin>=0) {
pinMode(rlyPin, OUTPUT);
digitalWrite(rlyPin, rlyMde);
}
offMode = false;
}
} else if (millis() - lastOnTime > 600)
{
2021-01-16 23:20:31 +00:00
if (!offMode) {
#ifdef ESP8266
//turn off built-in LED if strip is turned off
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
#endif
2021-01-16 23:20:31 +00:00
if (rlyPin>=0) {
pinMode(rlyPin, OUTPUT);
digitalWrite(rlyPin, !rlyMde);
}
}
offMode = true;
}
}