Added MQTT message on button press.

This commit is contained in:
Blaž Kristan 2021-06-01 11:03:18 +02:00
parent 79ac85e048
commit 2f9eacdf66
2 changed files with 35 additions and 7 deletions

View File

@ -6,6 +6,8 @@
#define WLED_DEBOUNCE_THRESHOLD 50 //only consider button input of at least 50ms as valid (debouncing) #define WLED_DEBOUNCE_THRESHOLD 50 //only consider button input of at least 50ms as valid (debouncing)
static const char _mqtt_topic_button[] PROGMEM = "%s/button/%d"; // optimize flash usage
void shortPressAction(uint8_t b) void shortPressAction(uint8_t b)
{ {
if (!macroButton[b]) if (!macroButton[b])
@ -15,6 +17,13 @@ void shortPressAction(uint8_t b)
} else { } else {
applyPreset(macroButton[b]); applyPreset(macroButton[b]);
} }
// publish MQTT message
if (WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, PSTR(_mqtt_topic_button), mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, "short");
}
} }
bool isButtonPressed(uint8_t i) bool isButtonPressed(uint8_t i)
@ -62,6 +71,14 @@ void handleSwitch(uint8_t b)
if (bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);} if (bri) {toggleOnOff(); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);}
} }
} }
// publish MQTT message
if (WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, PSTR(_mqtt_topic_button), mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, buttonPressedBefore[b] ^ (buttonType[b]==BTN_TYPE_SWITCH_ACT_HIGH) ? "on" : "off");
}
buttonLongPressed[b] = buttonPressedBefore[b]; //save the last "long term" switch state buttonLongPressed[b] = buttonPressedBefore[b]; //save the last "long term" switch state
} }
} }
@ -135,8 +152,6 @@ void handleAnalog(uint8_t b)
seg.setOption(SEG_OPTION_ON, 1); seg.setOption(SEG_OPTION_ON, 1);
} }
// this will notify clients of update (websockets,mqtt,etc) // 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); updateInterfaces(NOTIFIER_CALL_MODE_BUTTON);
} }
} else { } else {
@ -144,8 +159,6 @@ void handleAnalog(uint8_t b)
// we can either trigger a preset depending on the level (between short and long entries) // we can either trigger a preset depending on the level (between short and long entries)
// or use it for RGBW direct control // or use it for RGBW direct control
} }
//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); colorUpdated(NOTIFIER_CALL_MODE_BUTTON);
} }
@ -182,6 +195,13 @@ void handleButton()
if (macroLongPress[b]) {applyPreset(macroLongPress[b]);} if (macroLongPress[b]) {applyPreset(macroLongPress[b]);}
else _setRandomColor(false,true); else _setRandomColor(false,true);
// publish MQTT message
if (WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, PSTR(_mqtt_topic_button), mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, "long");
}
buttonLongPressed[b] = true; buttonLongPressed[b] = true;
} }
} }
@ -200,8 +220,16 @@ void handleButton()
else if (!buttonLongPressed[b]) { //short press else if (!buttonLongPressed[b]) { //short press
if (macroDoublePress[b]) if (macroDoublePress[b])
{ {
if (doublePress) applyPreset(macroDoublePress[b]); if (doublePress) {
else buttonWaitTime[b] = millis(); applyPreset(macroDoublePress[b]);
// publish MQTT message
if (WLED_MQTT_CONNECTED) {
char subuf[64];
sprintf_P(subuf, PSTR(_mqtt_topic_button), mqttDeviceTopic, (int)b);
mqtt->publish(subuf, 0, false, "double");
}
} else buttonWaitTime[b] = millis();
} else shortPressAction(b); } else shortPressAction(b);
} }
buttonPressedBefore[b] = false; buttonPressedBefore[b] = false;

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2105301 #define VERSION 2106011
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG