Browse Source

Support for toggle switches

fastled
Xose Pérez 7 years ago
parent
commit
50915f0fb2
2 changed files with 32 additions and 8 deletions
  1. +10
    -8
      code/espurna/button.ino
  2. +22
    -0
      code/espurna/config/hardware.h

+ 10
- 8
code/espurna/button.ino View File

@ -82,28 +82,28 @@ typedef struct {
std::vector<button_t> _buttons; std::vector<button_t> _buttons;
#ifdef MQTT_BUTTON_TOPIC #ifdef MQTT_BUTTON_TOPIC
void buttonMQTT(unsigned char id) {
void buttonMQTT(unsigned char id, const char * payload) {
if (id >= _buttons.size()) return; if (id >= _buttons.size()) return;
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3]; char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str()); sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str());
mqttSend(buffer, _buttons[id].button->pressed() ? "1" : "0");
mqttSend(buffer, payload);
} }
#endif #endif
void buttonSetup() { void buttonSetup() {
#ifdef BUTTON1_PIN #ifdef BUTTON1_PIN
_buttons.push_back({new DebounceEvent(BUTTON1_PIN), BUTTON1_RELAY});
_buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), BUTTON1_RELAY});
#endif #endif
#ifdef BUTTON2_PIN #ifdef BUTTON2_PIN
_buttons.push_back({new DebounceEvent(BUTTON2_PIN), BUTTON2_RELAY});
_buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), BUTTON2_RELAY});
#endif #endif
#ifdef BUTTON3_PIN #ifdef BUTTON3_PIN
_buttons.push_back({new DebounceEvent(BUTTON3_PIN), BUTTON3_RELAY});
_buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), BUTTON3_RELAY});
#endif #endif
#ifdef BUTTON4_PIN #ifdef BUTTON4_PIN
_buttons.push_back({new DebounceEvent(BUTTON4_PIN), BUTTON4_RELAY});
_buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), BUTTON4_RELAY});
#endif #endif
#ifdef LED_PULSE #ifdef LED_PULSE
@ -125,7 +125,7 @@ void buttonLoop() {
DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event); DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
#ifdef MQTT_BUTTON_TOPIC #ifdef MQTT_BUTTON_TOPIC
buttonMQTT(i);
buttonMQTT(i, (event == EVENT_CHANGED || event == EVENT_PRESSED) ? "1" : "0");
#endif #endif
if (i == 0) { if (i == 0) {
@ -140,7 +140,9 @@ void buttonLoop() {
} }
#endif #endif
if (event == EVENT_SINGLE_CLICK) {
// Here we can have EVENT_CHANGED only when using BUTTON_SWITCH
// and EVENT_SINGLE_CLICK only when using BUTTON_PUSHBUTTON
if (event == EVENT_SINGLE_CLICK || event == EVENT_CHANGED) {
if (_buttons[i].relayID > 0) { if (_buttons[i].relayID > 0) {
relayToggle(_buttons[i].relayID - 1); relayToggle(_buttons[i].relayID - 1);
} }


+ 22
- 0
code/espurna/config/hardware.h View File

@ -8,6 +8,7 @@
#define DEVICE "LOLIN" #define DEVICE "LOLIN"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_SWITCH | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 2 #define LED1_PIN 2
@ -32,6 +33,7 @@
#define DEVICE "SONOFF" #define DEVICE "SONOFF"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -43,6 +45,7 @@
#define DEVICE "SONOFF_TH" #define DEVICE "SONOFF_TH"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -54,6 +57,7 @@
#define DEVICE "SONOFF_SV" #define DEVICE "SONOFF_SV"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -65,6 +69,7 @@
#define DEVICE "SLAMPHER" #define DEVICE "SLAMPHER"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -76,6 +81,7 @@
#define DEVICE "S20" #define DEVICE "S20"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -87,6 +93,7 @@
#define DEVICE "SONOFF_TOUCH" #define DEVICE "SONOFF_TOUCH"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -98,6 +105,7 @@
#define DEVICE "SONOFF_POW" #define DEVICE "SONOFF_POW"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 15 #define LED1_PIN 15
@ -120,12 +128,16 @@
#define DEVICE "SONOFF_4CH" #define DEVICE "SONOFF_4CH"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON2_PIN 9 #define BUTTON2_PIN 9
#define BUTTON2_RELAY 2 #define BUTTON2_RELAY 2
#define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON3_PIN 10 #define BUTTON3_PIN 10
#define BUTTON3_RELAY 3 #define BUTTON3_RELAY 3
#define BUTTON3_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON4_PIN 14 #define BUTTON4_PIN 14
#define BUTTON4_RELAY 4 #define BUTTON4_RELAY 4
#define BUTTON4_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define RELAY2_PIN 5 #define RELAY2_PIN 5
@ -147,8 +159,10 @@
#define DEVICE "1CH_INCHING" #define DEVICE "1CH_INCHING"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON2_PIN 15 #define BUTTON2_PIN 15
#define BUTTON2_RELAY 0 #define BUTTON2_RELAY 0
#define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -165,8 +179,10 @@
#define DEVICE "ESP_RELAY_BOARD" #define DEVICE "ESP_RELAY_BOARD"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON2_PIN 2 #define BUTTON2_PIN 2
#define BUTTON2_RELAY 2 #define BUTTON2_RELAY 2
#define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define RELAY2_PIN 13 #define RELAY2_PIN 13
@ -184,6 +200,7 @@
#define DEVICE "ECOPLUG" #define DEVICE "ECOPLUG"
#define BUTTON1_PIN 13 #define BUTTON1_PIN 13
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 15 #define RELAY1_PIN 15
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 2 #define LED1_PIN 2
@ -199,8 +216,10 @@
#define DEVICE "WIFI_RELAY_NC" #define DEVICE "WIFI_RELAY_NC"
#define BUTTON1_PIN 12 #define BUTTON1_PIN 12
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON2_PIN 13 #define BUTTON2_PIN 13
#define BUTTON2_RELAY 2 #define BUTTON2_RELAY 2
#define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 2 #define RELAY1_PIN 2
#define RELAY1_PIN_INVERSE 1 #define RELAY1_PIN_INVERSE 1
#define RELAY2_PIN 14 #define RELAY2_PIN 14
@ -212,8 +231,10 @@
#define DEVICE "WIFI_RELAY_NO" #define DEVICE "WIFI_RELAY_NO"
#define BUTTON1_PIN 12 #define BUTTON1_PIN 12
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON2_PIN 13 #define BUTTON2_PIN 13
#define BUTTON2_RELAY 2 #define BUTTON2_RELAY 2
#define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 2 #define RELAY1_PIN 2
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define RELAY2_PIN 14 #define RELAY2_PIN 14
@ -229,6 +250,7 @@
#define DEVICE "ESPURNA" #define DEVICE "ESPURNA"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1 #define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13


Loading…
Cancel
Save