WLED/wled00/button.cpp

139 wiersze
3.0 KiB
C++
Czysty Zwykły widok Historia

#include "wled.h"
2020-03-25 09:14:23 +00:00
/*
* Physical IO
*/
void shortPressAction()
{
if (!macroButton)
{
toggleOnOff();
2020-02-22 15:17:32 +00:00
colorUpdated(NOTIFIER_CALL_MODE_BUTTON);
} else {
applyMacro(macroButton);
}
}
bool isButtonPressed()
{
#ifdef BTNPIN
if (digitalRead(BTNPIN) == LOW) return true;
#endif
#ifdef TOUCHPIN
if (touchRead(TOUCHPIN) <= TOUCH_THRESHOLD) return true;
#endif
return false;
}
void handleButton()
{
#if defined(BTNPIN) || defined(TOUCHPIN)
if (!buttonEnabled) return;
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)
{
if (macroLongPress) {applyMacro(macroLongPress);}
else _setRandomColor(false,true);
buttonLongPressed = true;
}
}
}
else if (!isButtonPressed() && buttonPressedBefore) //released
{
long dur = millis() - buttonPressedTime;
if (dur < 50) {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)
{
if (doublePress) applyMacro(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();
}
#endif
}
void handleIO()
{
handleButton();
//set relay when LEDs turn on
if (strip.getBrightness())
{
lastOnTime = millis();
if (offMode)
{
#if RLYPIN >= 0
digitalWrite(RLYPIN, RLYMDE);
#endif
offMode = false;
}
} else if (millis() - lastOnTime > 600)
{
if (!offMode) {
#if LEDPIN == LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
#endif
#if RLYPIN >= 0
digitalWrite(RLYPIN, !RLYMDE);
#endif
}
offMode = true;
}
2017-01-27 21:59:01 +00:00
#if AUXPIN >= 0
2017-01-27 21:59:01 +00:00
//output
if (auxActive || auxActiveBefore)
{
if (!auxActiveBefore)
{
auxActiveBefore = true;
switch (auxTriggeredState)
{
2018-11-17 23:31:45 +00:00
case 0: pinMode(AUXPIN, INPUT); break;
case 1: pinMode(AUXPIN, OUTPUT); digitalWrite(AUXPIN, HIGH); break;
case 2: pinMode(AUXPIN, OUTPUT); digitalWrite(AUXPIN, LOW); break;
2017-01-27 21:59:01 +00:00
}
auxStartTime = millis();
}
if ((millis() - auxStartTime > auxTime*1000 && auxTime != 255) || !auxActive)
{
auxActive = false;
auxActiveBefore = false;
switch (auxDefaultState)
{
2018-11-17 23:31:45 +00:00
case 0: pinMode(AUXPIN, INPUT); break;
case 1: pinMode(AUXPIN, OUTPUT); digitalWrite(AUXPIN, HIGH); break;
case 2: pinMode(AUXPIN, OUTPUT); digitalWrite(AUXPIN, LOW); break;
2017-01-27 21:59:01 +00:00
}
}
}
#endif
}