WLED/wled00/button.cpp

318 lines
10 KiB
C++
Raw Normal View History

#include "wled.h"
2020-03-25 10:14:23 +01:00
/*
* Physical IO
*/
2021-04-12 00:45:33 +02:00
#define WLED_DEBOUNCE_THRESHOLD 50 //only consider button input of at least 50ms as valid (debouncing)
#define WLED_LONG_PRESS 600 //long press if button is released after held for at least 600ms
#define WLED_DOUBLE_PRESS 350 //double press if another press within 350ms after a short press
#define WLED_LONG_REPEATED_ACTION 300 //how often a repeated action (e.g. dimming) is fired on long press on button IDs >0
#define WLED_LONG_AP 6000 //how long the button needs to be held to activate WLED-AP
2021-04-12 00:45:33 +02:00
static const char _mqtt_topic_button[] PROGMEM = "%s/button/%d"; // optimize flash usage
2021-05-20 19:54:07 +02:00
void shortPressAction(uint8_t b)
{
if (!macroButton[b]) {
switch (b) {
case 0: toggleOnOff(); colorUpdated(CALL_MODE_BUTTON); break;
default: ++effectCurrent %= strip.getModeCount(); colorUpdated(CALL_MODE_BUTTON); break;
}
} else {
applyPreset(macroButton[b], CALL_MODE_BUTTON_PRESET);
}
// publish MQTT message
2021-07-01 20:51:52 +02:00
if (buttonPublishMqtt && WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, "short");
}
}
void longPressAction(uint8_t b)
{
if (!macroLongPress[b]) {
switch (b) {
case 0: _setRandomColor(false,true); break;
default: bri += 8; colorUpdated(CALL_MODE_BUTTON); buttonPressedTime[b] = millis(); break; // repeatable action
}
} else {
applyPreset(macroLongPress[b], CALL_MODE_BUTTON_PRESET);
}
// publish MQTT message
if (buttonPublishMqtt && WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, "long");
}
}
void doublePressAction(uint8_t b)
{
if (!macroDoublePress[b]) {
switch (b) {
//case 0: toggleOnOff(); colorUpdated(CALL_MODE_BUTTON); break; //instant short press on button 0 if no macro set
default: ++effectPalette %= strip.getPaletteCount(); colorUpdated(CALL_MODE_BUTTON); break;
}
} else {
applyPreset(macroDoublePress[b], CALL_MODE_BUTTON_PRESET);
}
// publish MQTT message
if (buttonPublishMqtt && WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, "double");
}
}
2021-05-20 19:54:07 +02:00
bool isButtonPressed(uint8_t i)
{
2021-05-20 19:54:07 +02: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_PIR_SENSOR:
2021-05-20 19:54:07 +02:00
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 19:54:07 +02:00
void handleSwitch(uint8_t b)
2021-04-12 00:45:33 +02:00
{
// isButtonPressed() handles inverted/noninverted logic
2021-05-20 19:54:07 +02:00
if (buttonPressedBefore[b] != isButtonPressed(b)) {
buttonPressedTime[b] = millis();
buttonPressedBefore[b] = !buttonPressedBefore[b];
2021-04-12 00:45:33 +02:00
}
2021-05-20 19:54:07 +02:00
if (buttonLongPressed[b] == buttonPressedBefore[b]) return;
2021-04-12 00:45:33 +02:00
2021-05-20 19:54:07 +02:00
if (millis() - buttonPressedTime[b] > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce)
if (!buttonPressedBefore[b]) { // on -> off
if (macroButton[b]) applyPreset(macroButton[b], CALL_MODE_BUTTON_PRESET);
2021-04-12 00:45:33 +02:00
else { //turn on
if (!bri) {toggleOnOff(); colorUpdated(CALL_MODE_BUTTON);}
2021-04-12 00:45:33 +02:00
}
} else { // off -> on
if (macroLongPress[b]) applyPreset(macroLongPress[b], CALL_MODE_BUTTON_PRESET);
2021-04-12 00:45:33 +02:00
else { //turn off
if (bri) {toggleOnOff(); colorUpdated(CALL_MODE_BUTTON);}
2021-04-12 00:45:33 +02:00
}
}
// publish MQTT message
2021-07-01 20:51:52 +02:00
if (buttonPublishMqtt && WLED_MQTT_CONNECTED) {
char subuf[64];
if (buttonType[b] == BTN_TYPE_PIR_SENSOR) sprintf_P(subuf, PSTR("%s/motion/%d"), mqttDeviceTopic, (int)b);
else sprintf_P(subuf, _mqtt_topic_button, mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, !buttonPressedBefore[b] ? "off" : "on");
}
2021-05-20 19:54:07 +02:00
buttonLongPressed[b] = buttonPressedBefore[b]; //save the last "long term" switch state
2021-04-12 00:45:33 +02:00
}
}
void handleAnalog(uint8_t b)
{
static uint8_t oldRead[WLED_MAX_BUTTONS];
#ifdef ESP8266
2021-05-28 14:14:50 +02:00
uint16_t aRead = analogRead(A0) >> 2; // convert 10bit read to 8bit
#else
2021-05-28 14:14:50 +02:00
uint16_t aRead = analogRead(btnPin[b]) >> 4; // convert 12bit read to 8bit
#endif
if (buttonType[b] == BTN_TYPE_ANALOG_INVERTED) aRead = 255 - aRead;
2021-05-28 14:14:50 +02:00
// remove noise & reduce frequency of UI updates
aRead &= 0xFC;
if (oldRead[b] == aRead) return; // no change in reading
2021-05-25 23:59:43 +02: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 23:59:43 +02:00
if (macroDoublePress[b] >= 250) {
// global brightness
2021-05-25 23:59:43 +02:00
if (aRead == 0) {
briLast = bri;
bri = 0;
} else{
2021-05-28 14:14:50 +02:00
bri = aRead;
}
} else if (macroDoublePress[b] == 249) {
// effect speed
2021-05-28 14:14:50 +02:00
effectSpeed = aRead;
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
2021-05-28 14:14:50 +02:00
effectIntensity = aRead;
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
2021-05-28 14:14:50 +02:00
effectPalette = map(aRead, 0, 252, 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 23:59:43 +02:00
}
2021-05-30 02:03:32 +02:00
} else if (macroDoublePress[b] == 200) {
// primary color, hue, full saturation
colorHStoRGB(aRead*256,255,col);
} 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 23:59:43 +02:00
seg.setOption(SEG_OPTION_ON, 0); // off
} else {
2021-05-28 14:14:50 +02:00
seg.setOpacity(aRead, macroDoublePress[b]);
2021-05-25 23:59:43 +02:00
seg.setOption(SEG_OPTION_ON, 1);
}
2021-05-25 23:59:43 +02:00
// this will notify clients of update (websockets,mqtt,etc)
updateInterfaces(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
}
colorUpdated(CALL_MODE_BUTTON);
}
void handleButton()
{
2021-05-25 23:59:43 +02:00
static unsigned long lastRead = 0UL;
bool analog = false;
2021-05-25 23:59:43 +02:00
2021-05-20 19:54:07 +02:00
for (uint8_t b=0; b<WLED_MAX_BUTTONS; b++) {
2021-05-28 17:45:14 +02:00
#ifdef ESP8266
if ((btnPin[b]<0 && !(buttonType[b] == BTN_TYPE_ANALOG || buttonType[b] == BTN_TYPE_ANALOG_INVERTED)) || buttonType[b] == BTN_TYPE_NONE) continue;
2021-05-28 17:45:14 +02:00
#else
2021-05-25 23:59:43 +02:00
if (btnPin[b]<0 || buttonType[b] == BTN_TYPE_NONE) continue;
2021-05-28 17:45:14 +02:00
#endif
2021-04-12 00:45:33 +02:00
if (usermods.handleButton(b)) continue; // did usermod handle buttons
if ((buttonType[b] == BTN_TYPE_ANALOG || buttonType[b] == BTN_TYPE_ANALOG_INVERTED) && millis() - lastRead > 250) { // button is not a button but a potentiometer
analog = true;
handleAnalog(b); continue;
}
2021-04-12 00:45:33 +02:00
//button is not momentary, but switch. This is only suitable on pins whose on-boot state does not matter (NOT gpio0)
if (buttonType[b] == BTN_TYPE_SWITCH || buttonType[b] == BTN_TYPE_PIR_SENSOR) {
2021-05-20 19:54:07 +02:00
handleSwitch(b); continue;
}
2019-10-18 12:19:52 +02:00
2021-05-20 19:54:07 +02:00
//momentary button logic
if (isButtonPressed(b)) { //pressed
2021-05-20 19:54:07 +02:00
if (!buttonPressedBefore[b]) buttonPressedTime[b] = millis();
buttonPressedBefore[b] = true;
if (millis() - buttonPressedTime[b] > WLED_LONG_PRESS) { //long press
if (!buttonLongPressed[b]) longPressAction(b);
else if (b) { //repeatable action (~3 times per s) on button > 0
longPressAction(b);
buttonPressedTime[b] = millis() - WLED_LONG_REPEATED_ACTION; //300ms
2021-05-20 19:54:07 +02:00
}
buttonLongPressed[b] = true;
2019-10-18 12:19:52 +02:00
}
} else if (!isButtonPressed(b) && buttonPressedBefore[b]) { //released
2021-05-20 19:54:07 +02:00
long dur = millis() - buttonPressedTime[b];
if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore[b] = false; continue;} //too short "press", debounce
bool doublePress = buttonWaitTime[b]; //did we have a short press before?
2021-05-20 19:54:07 +02:00
buttonWaitTime[b] = 0;
if (b == 0 && dur > WLED_LONG_AP) { //long press on button 0 (when released)
2021-05-20 19:54:07 +02:00
WLED::instance().initAP(true);
} else if (!buttonLongPressed[b]) { //short press
if (b == 0 && !macroDoublePress[b]) { //don't wait for double press on button 0 if no double press macro set
shortPressAction(b);
} else { //double press if less than 350 ms between current press and previous short press release (buttonWaitTime!=0)
if (doublePress) {
doublePressAction(b);
} else {
buttonWaitTime[b] = millis();
}
}
2021-05-20 19:54:07 +02:00
}
buttonPressedBefore[b] = false;
buttonLongPressed[b] = false;
}
//if 350ms elapsed since last short press release it is a short press
if (buttonWaitTime[b] && millis() - buttonWaitTime[b] > WLED_DOUBLE_PRESS && !buttonPressedBefore[b]) {
2021-05-20 19:54:07 +02:00
buttonWaitTime[b] = 0;
shortPressAction(b);
}
}
if (analog) lastRead = millis();
}
void handleIO()
{
handleButton();
//set relay when LEDs turn on
if (strip.getBrightness())
{
lastOnTime = millis();
if (offMode)
{
2021-01-17 00:20:31 +01:00
if (rlyPin>=0) {
pinMode(rlyPin, OUTPUT);
digitalWrite(rlyPin, rlyMde);
}
offMode = false;
}
} else if (millis() - lastOnTime > 600)
{
2021-01-17 00:20:31 +01:00
if (!offMode) {
#ifdef ESP8266
2021-05-20 19:54:07 +02:00
// turn off built-in LED if strip is turned off
// this will break digital bus so will need to be reinitialised on On
PinOwner ledPinOwner = pinManager.getPinOwner(LED_BUILTIN);
if (!strip.isOffRefreshRequred && (ledPinOwner == PinOwner::None || ledPinOwner == PinOwner::BusDigital)) {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
}
#endif
2021-01-17 00:20:31 +01:00
if (rlyPin>=0) {
pinMode(rlyPin, OUTPUT);
digitalWrite(rlyPin, !rlyMde);
}
}
offMode = true;
}
}