WLED/wled00/button.cpp

240 wiersze
7.3 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)
2021-05-20 17:54:07 +00:00
void shortPressAction(uint8_t b)
{
2021-05-20 17:54:07 +00:00
if (!macroButton[b])
{
toggleOnOff();
2020-02-22 15:17:32 +00:00
colorUpdated(NOTIFIER_CALL_MODE_BUTTON);
} else {
2021-05-20 17:54:07 +00:00
applyPreset(macroButton[b]);
}
}
2021-05-20 17:54:07 +00:00
bool isButtonPressed(uint8_t i)
{
2021-05-20 17:54:07 +00:00
if (btnPin[i]<0) return false;
switch (buttonType[i]) {
case BTN_TYPE_NONE:
case BTN_TYPE_RESERVED:
break;
case BTN_TYPE_PUSH:
case BTN_TYPE_SWITCH:
if (digitalRead(btnPin[i]) == LOW) return true;
break;
case BTN_TYPE_PUSH_ACT_HIGH:
case BTN_TYPE_SWITCH_ACT_HIGH:
if (digitalRead(btnPin[i]) == HIGH) return true;
break;
case BTN_TYPE_TOUCH:
#ifdef ARDUINO_ARCH_ESP32
if (touchRead(btnPin[i]) <= touchThreshold) return true;
#endif
break;
}
return false;
}
2021-05-20 17:54:07 +00:00
void handleSwitch(uint8_t b)
2021-04-11 22:45:33 +00:00
{
2021-05-20 17:54:07 +00:00
if (buttonPressedBefore[b] != isButtonPressed(b)) {
buttonPressedTime[b] = millis();
buttonPressedBefore[b] = !buttonPressedBefore[b];
2021-04-11 22:45:33 +00:00
}
2021-05-20 17:54:07 +00:00
if (buttonLongPressed[b] == buttonPressedBefore[b]) return;
2021-04-11 22:45:33 +00:00
2021-05-20 17:54:07 +00:00
if (millis() - buttonPressedTime[b] > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce)
if (buttonPressedBefore[b]) { //LOW, falling edge, switch closed
if (macroButton[b]) applyPreset(macroButton[b]);
2021-04-11 22:45:33 +00:00
else { //turn on
if (!bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);}
}
} else { //HIGH, rising edge, switch opened
2021-05-20 17:54:07 +00:00
if (macroLongPress[b]) applyPreset(macroLongPress[b]);
2021-04-11 22:45:33 +00:00
else { //turn off
if (bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);}
}
}
2021-05-20 17:54:07 +00:00
buttonLongPressed[b] = buttonPressedBefore[b]; //save the last "long term" switch state
2021-04-11 22:45:33 +00:00
}
}
void handleAnalog(uint8_t b)
{
static uint8_t oldRead[WLED_MAX_BUTTONS];
#ifdef ESP8266
uint16_t aRead = analogRead(A0) >> 4; // convert 10bit read to 6bit (remove noise/reduce range & frequency of UI updates)
#else
uint16_t aRead = analogRead(btnPin[b]) >> 6; // convert 12bit read to 6bit (remove noise/reduce range & frequency of UI updates)
#endif
if (oldRead[b] == aRead) return; // no change in reading
2021-05-25 21:59:43 +00:00
oldRead[b] = aRead;
// if no macro for "short press" and "long press" is defined use brightness control
if (!macroButton[b] && !macroLongPress[b]) {
// if "double press" macro defines which option to change
2021-05-25 21:59:43 +00:00
if (macroDoublePress[b] >= 250) {
// global brightness
2021-05-25 21:59:43 +00:00
if (aRead == 0) {
briLast = bri;
bri = 0;
} else{
bri = aRead << 2;
}
} else if (macroDoublePress[b] == 249) {
// effect speed
effectSpeed = aRead << 2;
effectChanged = true;
for (uint8_t i = 0; i < strip.getMaxSegments(); i++) {
WS2812FX::Segment& seg = strip.getSegment(i);
if (!seg.isSelected()) continue;
seg.speed = effectSpeed;
}
} else if (macroDoublePress[b] == 248) {
// effect intensity
effectIntensity = aRead << 2;
effectChanged = true;
for (uint8_t i = 0; i < strip.getMaxSegments(); i++) {
WS2812FX::Segment& seg = strip.getSegment(i);
if (!seg.isSelected()) continue;
seg.intensity = effectIntensity;
}
} else if (macroDoublePress[b] == 247) {
// selected palette
effectPalette = map(aRead, 0, 63, 0, strip.getPaletteCount()-1);
effectChanged = true;
for (uint8_t i = 0; i < strip.getMaxSegments(); i++) {
WS2812FX::Segment& seg = strip.getSegment(i);
if (!seg.isSelected()) continue;
seg.palette = effectPalette;
2021-05-25 21:59:43 +00:00
}
} else {
// otherwise use "double press" for segment selection
//uint8_t mainSeg = strip.getMainSegmentId();
WS2812FX::Segment& seg = strip.getSegment(macroDoublePress[b]);
if (aRead == 0) {
2021-05-25 21:59:43 +00:00
seg.setOption(SEG_OPTION_ON, 0); // off
} else {
2021-05-25 21:59:43 +00:00
seg.setOpacity(aRead << 3, macroDoublePress[b]);
seg.setOption(SEG_OPTION_ON, 1);
}
2021-05-25 21:59:43 +00:00
// this will notify clients of update (websockets,mqtt,etc)
//call for notifier -> 0: init 1: direct change 2: button 3: notification 4: nightlight 5: other (No notification)
// 6: fx changed 7: hue 8: preset cycle 9: blynk 10: alexa
updateInterfaces(NOTIFIER_CALL_MODE_BUTTON);
}
} else {
//TODO:
// we can either trigger a preset depending on the level (between short and long entries)
// or use it for RGBW direct control
}
2021-05-25 21:59:43 +00:00
//call for notifier -> 0: init 1: direct change 2: button 3: notification 4: nightlight 5: other (No notification)
// 6: fx changed 7: hue 8: preset cycle 9: blynk 10: alexa
colorUpdated(NOTIFIER_CALL_MODE_BUTTON);
}
void handleButton()
{
2021-05-25 21:59:43 +00:00
static unsigned long lastRead = 0UL;
2021-05-20 17:54:07 +00:00
for (uint8_t b=0; b<WLED_MAX_BUTTONS; b++) {
2021-05-25 21:59:43 +00:00
if (btnPin[b]<0 || buttonType[b] == BTN_TYPE_NONE) continue;
2021-04-11 22:45:33 +00:00
2021-05-25 21:59:43 +00:00
if (buttonType[b] == BTN_TYPE_ANALOG && millis() - lastRead > 250) { // button is not a button but a potentiometer
if (b+1 == WLED_MAX_BUTTONS) lastRead = millis();
handleAnalog(b); continue;
}
2021-04-11 22:45:33 +00:00
if (buttonType[b] == BTN_TYPE_SWITCH || buttonType[b] == BTN_TYPE_SWITCH_ACT_HIGH) { //button is not momentary, but switch. This is only suitable on pins whose on-boot state does not matter (NOT gpio0)
2021-05-20 17:54:07 +00:00
handleSwitch(b); continue;
}
2019-10-18 10:19:52 +00:00
2021-05-20 17:54:07 +00:00
//momentary button logic
if (isButtonPressed(b)) //pressed
2019-10-18 10:19:52 +00:00
{
2021-05-20 17:54:07 +00:00
if (!buttonPressedBefore[b]) buttonPressedTime[b] = millis();
buttonPressedBefore[b] = true;
if (millis() - buttonPressedTime[b] > 600) //long press
2019-10-18 10:19:52 +00:00
{
2021-05-20 17:54:07 +00:00
if (!buttonLongPressed[b])
{
if (macroLongPress[b]) {applyPreset(macroLongPress[b]);}
else _setRandomColor(false,true);
2019-10-18 10:19:52 +00:00
2021-05-20 17:54:07 +00:00
buttonLongPressed[b] = true;
}
2019-10-18 10:19:52 +00:00
}
}
2021-05-20 17:54:07 +00:00
else if (!isButtonPressed(b) && buttonPressedBefore[b]) //released
{
2021-05-20 17:54:07 +00:00
long dur = millis() - buttonPressedTime[b];
if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore[b] = false; continue;} //too short "press", debounce
bool doublePress = buttonWaitTime[b];
buttonWaitTime[b] = 0;
if (dur > 6000 && b==0) //long press on button 0
{
2021-05-20 17:54:07 +00:00
WLED::instance().initAP(true);
}
else if (!buttonLongPressed[b]) { //short press
if (macroDoublePress[b])
{
if (doublePress) applyPreset(macroDoublePress[b]);
else buttonWaitTime[b] = millis();
} else shortPressAction(b);
}
buttonPressedBefore[b] = false;
buttonLongPressed[b] = false;
}
2021-05-20 17:54:07 +00:00
if (buttonWaitTime[b] && millis() - buttonWaitTime[b] > 450 && !buttonPressedBefore[b])
{
buttonWaitTime[b] = 0;
shortPressAction(b);
}
}
}
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
2021-05-20 17:54:07 +00:00
// turn off built-in LED if strip is turned off
// this will break digital bus so will need to be reinitialised on On
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;
}
}