From 50915f0fb2f77365fdb4802e2e2cd70cfd23ba7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 22 Jan 2017 05:24:28 +0100 Subject: [PATCH 1/2] Support for toggle switches --- code/espurna/button.ino | 18 ++++++++++-------- code/espurna/config/hardware.h | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 972186a1..f4214c87 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -82,28 +82,28 @@ typedef struct { std::vector _buttons; #ifdef MQTT_BUTTON_TOPIC -void buttonMQTT(unsigned char id) { +void buttonMQTT(unsigned char id, const char * payload) { if (id >= _buttons.size()) return; String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3]; sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str()); - mqttSend(buffer, _buttons[id].button->pressed() ? "1" : "0"); + mqttSend(buffer, payload); } #endif void buttonSetup() { #ifdef BUTTON1_PIN - _buttons.push_back({new DebounceEvent(BUTTON1_PIN), BUTTON1_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), BUTTON1_RELAY}); #endif #ifdef BUTTON2_PIN - _buttons.push_back({new DebounceEvent(BUTTON2_PIN), BUTTON2_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), BUTTON2_RELAY}); #endif #ifdef BUTTON3_PIN - _buttons.push_back({new DebounceEvent(BUTTON3_PIN), BUTTON3_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), BUTTON3_RELAY}); #endif #ifdef BUTTON4_PIN - _buttons.push_back({new DebounceEvent(BUTTON4_PIN), BUTTON4_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), BUTTON4_RELAY}); #endif #ifdef LED_PULSE @@ -125,7 +125,7 @@ void buttonLoop() { DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event); #ifdef MQTT_BUTTON_TOPIC - buttonMQTT(i); + buttonMQTT(i, (event == EVENT_CHANGED || event == EVENT_PRESSED) ? "1" : "0"); #endif if (i == 0) { @@ -140,7 +140,9 @@ void buttonLoop() { } #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) { relayToggle(_buttons[i].relayID - 1); } diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 2d0dd5f2..88fa41b2 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -8,6 +8,7 @@ #define DEVICE "LOLIN" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_SWITCH | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 @@ -32,6 +33,7 @@ #define DEVICE "SONOFF" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -43,6 +45,7 @@ #define DEVICE "SONOFF_TH" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -54,6 +57,7 @@ #define DEVICE "SONOFF_SV" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -65,6 +69,7 @@ #define DEVICE "SLAMPHER" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -76,6 +81,7 @@ #define DEVICE "S20" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -87,6 +93,7 @@ #define DEVICE "SONOFF_TOUCH" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -98,6 +105,7 @@ #define DEVICE "SONOFF_POW" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 15 @@ -120,12 +128,16 @@ #define DEVICE "SONOFF_4CH" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 9 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON3_PIN 10 #define BUTTON3_RELAY 3 + #define BUTTON3_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON4_PIN 14 #define BUTTON4_RELAY 4 + #define BUTTON4_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 5 @@ -147,8 +159,10 @@ #define DEVICE "1CH_INCHING" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 15 #define BUTTON2_RELAY 0 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -165,8 +179,10 @@ #define DEVICE "ESP_RELAY_BOARD" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 2 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 13 @@ -184,6 +200,7 @@ #define DEVICE "ECOPLUG" #define BUTTON1_PIN 13 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 15 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 @@ -199,8 +216,10 @@ #define DEVICE "WIFI_RELAY_NC" #define BUTTON1_PIN 12 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 13 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 2 #define RELAY1_PIN_INVERSE 1 #define RELAY2_PIN 14 @@ -212,8 +231,10 @@ #define DEVICE "WIFI_RELAY_NO" #define BUTTON1_PIN 12 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 13 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 2 #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 14 @@ -229,6 +250,7 @@ #define DEVICE "ESPURNA" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 From 399949da1596edaa3f512a96795475bd30b5fdff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 2 Feb 2017 12:34:32 +0100 Subject: [PATCH 2/2] Added inline documentation to hardware.h file --- code/espurna/config/hardware.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 49330d1a..f6979b9a 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -1,3 +1,21 @@ +// ----------------------------------------------------------------------------- +// Configuration HELP +// ----------------------------------------------------------------------------- +// +// MANUFACTURER: Name of the manufacturer of the board ("string") +// DEVICE: Name of the device ("string") +// BUTTON#_PIN: GPIO for the n-th button (1-based, up to 4 buttons) +// BUTTON#_RELAY: Relay number that will be bind to the n-th button (1-based) +// BUTTON#_MODE: A mask of options (BUTTON_PUSHBUTTON and BUTTON_SWITCH cannot be together) +// - BUTTON_PUSHBUTTON: button event is fired when released +// - BUTTON_SWITCH: button event is fired when pressed or released +// - BUTTON_DEFAULT_HIGH: there is a pull up in place +// - BUTTON_SET_PULLUP: set pullup by software +// RELAY#_PIN: GPIO for the n-th relay (1-based, up to 4 relays) +// RELAY#_PIN_INVERSE: Relay has inversed logic (closed or ON when pulled down) +// LED#_PIN: GPIO for the n-th LED (1-based, up to 4 LEDs) +// LED#_PIN_INVERSE: LED has inversed logic (lit when pulled down) + // ----------------------------------------------------------------------------- // Development boards // ----------------------------------------------------------------------------- @@ -167,6 +185,8 @@ #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 0 + + // Special LED that shows pulse mode status #define LED_PULSE 14 // -----------------------------------------------------------------------------