From 4cf82bf0ea1137e173f302e883c6829ddb68cb12 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 14 Apr 2020 12:56:08 +0200 Subject: [PATCH 01/11] Fix compilation error --- tasmota/xdsp_interface.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tasmota/xdsp_interface.ino b/tasmota/xdsp_interface.ino index f713bf2de..c92d6eceb 100644 --- a/tasmota/xdsp_interface.ino +++ b/tasmota/xdsp_interface.ino @@ -17,6 +17,7 @@ along with this program. If not, see . */ +#if defined(USE_I2C) || defined(USE_SPI) #ifdef USE_DISPLAY #ifdef XFUNC_PTR_IN_ROM @@ -137,3 +138,4 @@ bool XdspCall(uint8_t Function) } #endif // USE_DISPLAY +#endif // USE_I2C or USE_SPI From a52d4c520818e6a9900ce9856aede736778ee844 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 14 Apr 2020 14:58:17 +0200 Subject: [PATCH 02/11] Revert "Merge pull request #8174 from gemu2015/scripter_update" This reverts commit e66b961a546b93622d4177e3af3930dd2772e9ae, reversing changes made to 07ce4315381cb9984d5139d22a380c88b1ec659c. --- tasmota/xdrv_10_scripter.ino | 50 ------------------------------------ 1 file changed, 50 deletions(-) diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index fb8aa3706..e086adedb 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -65,31 +65,6 @@ keywords if then else endif, or, and are better readable for beginners (others m uint32_t EncodeLightId(uint8_t relay_id); uint32_t DecodeLightId(uint32_t hue_id); -#ifdef ESP32 - -#include "FS.h" -#include "SPIFFS.h" -void SaveFile(char *name,const uint8_t *buf,uint32_t len) { - File file = SPIFFS.open(name, FILE_WRITE); - if (!file) return; - file.write(buf, len); - file.close(); -} - -#define FORMAT_SPIFFS_IF_FAILED true - -void LoadFile(char *name,uint8_t *buf,uint32_t len) { - - if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){ - return; - } - File file = SPIFFS.open(name); - if (!file) return; - file.read(buf, len); - file.close(); -} -#endif - // offsets epoch readings by 1.1.2019 00:00:00 to fit into float with second resolution #define EPOCH_OFFSET 1546300800 @@ -3622,15 +3597,6 @@ void ScriptSaveSettings(void) { } #endif -#ifndef ESP32_SCRIPT_SIZE -#define ESP32_SCRIPT_SIZE 8192 -#endif - -#if defined(ESP32) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) - if (glob_script_mem.flags&1) { - SaveFile("/script.txt",(uint8_t*)glob_script_mem.script_ram,ESP32_SCRIPT_SIZE); - } -#endif } if (glob_script_mem.script_mem) { @@ -4854,7 +4820,6 @@ bool Xdrv10(uint8_t function) switch (function) { case FUNC_PRE_INIT: - //webcam_setup(); // set defaults to rules memory glob_script_mem.script_ram=Settings.rules[0]; glob_script_mem.script_size=MAX_SCRIPT_SIZE; @@ -4924,21 +4889,6 @@ bool Xdrv10(uint8_t function) } #endif - -#if defined(ESP32) && !defined(USE_24C256) && !defined(USE_SCRIPT_FATFS) - char *script; - script=(char*)calloc(ESP32_SCRIPT_SIZE+4,1); - if (!script) break; - LoadFile("/script.txt",(uint8_t*)script,ESP32_SCRIPT_SIZE); - glob_script_mem.script_ram=script; - glob_script_mem.script_size=ESP32_SCRIPT_SIZE; - script[ESP32_SCRIPT_SIZE-1]=0; - // use rules storage for permanent vars - glob_script_mem.script_pram=(uint8_t*)Settings.rules[0]; - glob_script_mem.script_pram_size=MAX_SCRIPT_SIZE; - glob_script_mem.flags=1; -#endif - // assure permanent memory is 4 byte aligned { uint32_t ptr=(uint32_t)glob_script_mem.script_pram; ptr&=0xfffffffc; From dd44163f3b99892d6afbd1f0c5a6bc19d58110d0 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Tue, 14 Apr 2020 10:45:23 -0300 Subject: [PATCH 03/11] Update settings.ino --- tasmota/settings.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 9426c9b04..18b8017ee 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -1243,11 +1243,11 @@ void SettingsDelta(void) Settings.ex_serial_config = TS_SERIAL_8N1; } if (Settings.version < 0x07010204) { - if (Settings.flag3.ex_cors_enabled == 1) { - strlcpy(Settings.ex_cors_domain, CORS_ENABLED_ALL, sizeof(Settings.ex_cors_domain)); - } else { - Settings.ex_cors_domain[0] = 0; - } + // if (Settings.flag3.ex_cors_enabled == 1) { + // strlcpy(Settings.ex_cors_domain, CORS_ENABLED_ALL, sizeof(Settings.ex_cors_domain)); + // } else { + // Settings.ex_cors_domain[0] = 0; + // } } if (Settings.version < 0x07010205) { Settings.seriallog_level = Settings.ex_seriallog_level; // 09E -> 452 From 8cf4a2ca50e3f9cbd7125024a1763ec3b243fac3 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Tue, 14 Apr 2020 10:46:22 -0300 Subject: [PATCH 04/11] Update settings.h --- tasmota/settings.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tasmota/settings.h b/tasmota/settings.h index 0014cac60..9a5cb917e 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -86,8 +86,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t energy_weekend : 1; // bit 20 (v6.6.0.8) - CMND_TARIFF uint32_t dds2382_model : 1; // bit 21 (v6.6.0.14) - SetOption71 - Select different Modbus registers for Active Energy (#6531) uint32_t hardware_energy_total : 1; // bit 22 (v6.6.0.15) - SetOption72 - Enable hardware energy total counter as reference (#6561) - uint32_t ex_cors_enabled : 1; // bit 23 (v7.0.0.1) - SetOption73 - Enable HTTP CORS - uint32_t ds18x20_internal_pullup : 1; // bit 24 (v7.0.0.1) - SetOption74 - Enable internal pullup for single DS18x20 sensor + uint32_t mqtt_buttons : 1; // bit 23 (v8.2.0.3) - SetOption73 - Detach buttons from relays and enable MQTT action state for multipress uint32_t ds18x20_internal_pullup : 1; // bit 24 (v7.0.0.1) - SetOption74 - Enable internal pullup for single DS18x20 sensor uint32_t grouptopic_mode : 1; // bit 25 (v7.0.0.1) - SetOption75 - GroupTopic replaces %topic% (0) or fixed topic cmnd/grouptopic (1) uint32_t bootcount_update : 1; // bit 26 (v7.0.0.4) - SetOption76 - Enable incrementing bootcount when deepsleep is enabled uint32_t slider_dimmer_stay_on : 1; // bit 27 (v7.0.0.6) - SetOption77 - Do not power off if slider moved to far left From 03c9d75f1859c7d2c4b54ac9e35966a05ff5bbc2 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Tue, 14 Apr 2020 10:47:01 -0300 Subject: [PATCH 05/11] Update tasmota.h --- tasmota/tasmota.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 042f1b1d4..000639505 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -240,6 +240,7 @@ enum ExecuteCommandPowerOptions { POWER_OFF, POWER_ON, POWER_TOGGLE, POWER_BLINK POWER_SHOW_STATE = 16 }; enum SendKeyPowerOptions { POWER_HOLD = 3, POWER_INCREMENT = 4, POWER_INV = 5, POWER_CLEAR = 6, CLEAR_RETAIN = 9 }; enum SendKeyOptions { KEY_BUTTON, KEY_SWITCH }; +enum SendKeyMultiClick { SINGLE = 10, DOUBLE = 11, TRIPLE = 12, QUAD = 13, PENTA = 14}; enum PowerOnStateOptions { POWER_ALL_OFF, POWER_ALL_ON, POWER_ALL_SAVED_TOGGLE, POWER_ALL_SAVED, POWER_ALL_ALWAYS_ON, POWER_ALL_OFF_PULSETIME_ON }; From 7a12a6f0b20cacbc106c05859b88523e02861c37 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Tue, 14 Apr 2020 10:47:50 -0300 Subject: [PATCH 06/11] Add files via upload --- tasmota/support_button_V2 | 316 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 tasmota/support_button_V2 diff --git a/tasmota/support_button_V2 b/tasmota/support_button_V2 new file mode 100644 index 000000000..b6bb1e1ca --- /dev/null +++ b/tasmota/support_button_V2 @@ -0,0 +1,316 @@ +/* + support_button.ino - button support for Tasmota + + Copyright (C) 2020 Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#define BUTTON_V2 +#ifdef BUTTON_V2 +/*********************************************************************************************\ + * Button support +\*********************************************************************************************/ + +#define MAX_BUTTON_COMMANDS 3 // Max number of button commands supported +#define MAX_RELAY_BUTTON1 4 // Max number of relay controlled by button1 + +const char kCommands[] PROGMEM = + D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; + //D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; +const char kMultiPress[] PROGMEM = + "|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|"; + +struct BUTTON { + unsigned long debounce = 0; // Button debounce timer + uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold + uint16_t dual_code = 0; // Sonoff dual received code + + uint8_t last_state[MAX_KEYS] = { NOT_PRESSED, NOT_PRESSED, NOT_PRESSED, NOT_PRESSED }; // Last button states + uint8_t window_timer[MAX_KEYS] = { 0 }; // Max time between button presses to record press count + uint8_t press_counter[MAX_KEYS] = { 0 }; // Number of button presses within Button.window_timer + + uint8_t dual_receive_count = 0; // Sonoff dual input flag + uint8_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup) + uint8_t inverted_mask = 0; // Key inverted flag (1 = inverted) + uint8_t present = 0; // Number of buttons found flag + uint8_t adc = 99; // ADC0 button number +} Button; + +/********************************************************************************************/ + +void ButtonPullupFlag(uint8 button_bit) +{ + bitSet(Button.no_pullup_mask, button_bit); +} + +void ButtonInvertFlag(uint8 button_bit) +{ + bitSet(Button.inverted_mask, button_bit); +} + +void ButtonInit(void) +{ + Button.present = 0; + for (uint32_t i = 0; i < MAX_KEYS; i++) { + if (pin[GPIO_KEY1 +i] < 99) { + Button.present++; + pinMode(pin[GPIO_KEY1 +i], bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : INPUT_PULLUP)); + } +#ifndef USE_ADC_VCC + else if ((99 == Button.adc) && ((ADC0_BUTTON == my_adc0) || (ADC0_BUTTON_INV == my_adc0))) { + Button.present++; + Button.adc = i; + } +#endif // USE_ADC_VCC + } +} + +uint8_t ButtonSerial(uint8_t serial_in_byte) +{ + if (Button.dual_receive_count) { + Button.dual_receive_count--; + if (Button.dual_receive_count) { + Button.dual_code = (Button.dual_code << 8) | serial_in_byte; + serial_in_byte = 0; + } else { + if (serial_in_byte != 0xA1) { + Button.dual_code = 0; // 0xA1 - End of Sonoff dual button code + } + } + } + if (0xA0 == serial_in_byte) { // 0xA0 - Start of Sonoff dual button code + serial_in_byte = 0; + Button.dual_code = 0; + Button.dual_receive_count = 3; + } + + return serial_in_byte; +} + +/*********************************************************************************************\ + * Button handler with single press only or multi-press and hold on all buttons + * + * ButtonDebounce (50) - Debounce time in mSec + * SetOption1 (0) - If set do not execute config commands + * SetOption11 (0) - If set perform single press action on double press and reverse + * SetOption13 (0) - If set act on single press only + * SetOption32 (40) - Max button hold time in Seconds + * SetOption40 (0) - Number of 0.1 seconds until hold is discarded if SetOption1 1 and SetOption13 0 + * SetOption73 (0) - Decouple button from relay and send just mqtt topic +\*********************************************************************************************/ + +void ButtonHandler(void) +{ + if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit + + uint8_t hold_time_extent = IMMINENT_RESET_FACTOR; // Extent hold time factor in case of iminnent Reset command + uint16_t loops_per_second = 1000 / Settings.button_debounce; // ButtonDebounce (50) + char scmnd[20]; + char scommand[CMDSZ]; + char stopic[TOPSZ]; + +// uint8_t maxdev = (devices_present > MAX_KEYS) ? MAX_KEYS : devices_present; +// for (uint32_t button_index = 0; button_index < maxdev; button_index++) { + for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++) { + uint8_t button = NOT_PRESSED; + uint8_t button_present = 0; + + if (!button_index && ((SONOFF_DUAL == my_module_type) || (CH4 == my_module_type))) { + button_present = 1; + if (Button.dual_code) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON " " D_CODE " %04X"), Button.dual_code); + button = PRESSED; + if (0xF500 == Button.dual_code) { // Button hold + Button.hold_timer[button_index] = (loops_per_second * Settings.param[P_HOLD_TIME] / 10) -1; // SetOption32 (40) + hold_time_extent = 1; + } + Button.dual_code = 0; + } + } + else if (pin[GPIO_KEY1 +button_index] < 99) { + button_present = 1; + button = (digitalRead(pin[GPIO_KEY1 +button_index]) != bitRead(Button.inverted_mask, button_index)); + } +#ifndef USE_ADC_VCC + if (Button.adc == button_index) { + button_present = 1; + if (ADC0_BUTTON_INV == my_adc0) { + button = (AdcRead(1) < 128); + } + else if (ADC0_BUTTON == my_adc0) { + button = (AdcRead(1) > 128); + } + } +#endif // USE_ADC_VCC + + if (button_present) { + XdrvMailbox.index = button_index; + XdrvMailbox.payload = button; + if (XdrvCall(FUNC_BUTTON_PRESSED)) { + // Serviced + } + else if (SONOFF_4CHPRO == my_module_type) { + if (Button.hold_timer[button_index]) { Button.hold_timer[button_index]--; } + + bool button_pressed = false; + if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_10), button_index +1); + Button.hold_timer[button_index] = loops_per_second; + button_pressed = true; + } + if ((NOT_PRESSED == button) && (PRESSED == Button.last_state[button_index])) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_01), button_index +1); + if (!Button.hold_timer[button_index]) { button_pressed = true; } // Do not allow within 1 second + } + if (button_pressed) { + if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set + ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + + } + } + } else { + + if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) { + + if (Settings.flag.button_single && !Settings.flag3.mqtt_buttons) { // SetOption13 (0) - Allow only single button press for immediate action, SetOption73 (0) - Decouple button from relay and send just mqtt topic + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1); + if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set + ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + } + + } else { + Button.press_counter[button_index] = (Button.window_timer[button_index]) ? Button.press_counter[button_index] +1 : 1; + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_MULTI_PRESS " %d"), button_index +1, Button.press_counter[button_index]); + Button.window_timer[button_index] = loops_per_second / 2; // 0.5 second multi press window + } + blinks = 201; + } + + if (NOT_PRESSED == button) { + Button.hold_timer[button_index] = 0; + } else { + Button.hold_timer[button_index]++; + if (Settings.flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action + if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_SETOPTION "13 0")); // Disable single press only + ExecuteCommand(scmnd, SRC_BUTTON); + } + } else { + if (Button.hold_timer[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button hold + Button.press_counter[button_index] = 0; + if (Settings.flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic + snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_index +1); + GetTopic_P(stopic, STAT, mqtt_topic, scommand); + Response_P(S_JSON_COMMAND_SVALUE, "ACTION", GetStateText(3)); + MqttPublish(stopic); + } + SendKey(KEY_BUTTON, button_index +1, POWER_HOLD); // Execute Hold command via MQTT if ButtonTopic is set + } else { + if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer + Button.press_counter[button_index] = 0; + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_RESET " 1")); + ExecuteCommand(scmnd, SRC_BUTTON); + } + } + } + } + + if (!Settings.flag.button_single) { // SetOption13 (0) - Allow multi-press + if (Button.window_timer[button_index]) { + Button.window_timer[button_index]--; + } else { + if (!restart_flag && !Button.hold_timer[button_index] && (Button.press_counter[button_index] > 0) && (Button.press_counter[button_index] < MAX_BUTTON_COMMANDS +6)) { + bool single_press = false; + if (Button.press_counter[button_index] < 3) { // Single or Double press + if ((SONOFF_DUAL_R2 == my_module_type) || (SONOFF_DUAL == my_module_type) || (CH4 == my_module_type)) { + single_press = true; + } else { + single_press = (Settings.flag.button_swap +1 == Button.press_counter[button_index]); // SetOption11 (0) + if ((1 == Button.present) && (2 == devices_present)) { // Single Button with two devices only + if (Settings.flag.button_swap) { // SetOption11 (0) + Button.press_counter[button_index] = (single_press) ? 1 : 2; + } + // } else { + // if (!Settings.flag3.mqtt_buttons && button_index != 0) { + // Button.press_counter[button_index] = 1; + // } + } + } + } +#if defined(USE_LIGHT) && defined(ROTARY_V1) + if (!((0 == button_index) && RotaryButtonPressed())) { +#endif + if (!Settings.flag3.mqtt_buttons && single_press && SendKey(KEY_BUTTON, button_index + Button.press_counter[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set + // Success + } else { + if (Button.press_counter[button_index] < 6) { // Single to Penta press + if (WifiState() > WIFI_RESTART) { // Wifimanager active + restart_flag = 1; + } + if (!Settings.flag3.mqtt_buttons) { + if (Button.press_counter[button_index] == 1) { // By default first press always send a TOGGLE (2) + ExecuteCommandPower(button_index + Button.press_counter[button_index], POWER_TOGGLE, SRC_BUTTON); + } else { + SendKey(KEY_BUTTON, button_index +1, Button.press_counter[button_index] +9); // 2,3,4 and 5 press send just the key value (11,12,13 and 14) for rules + if (0 == button_index) { // First button can toggle up to 4 relays if present + if ((Button.press_counter[button_index] > 1 && pin[GPIO_REL1 + Button.press_counter[button_index]-1] < 99) && Button.press_counter[button_index] <= MAX_RELAY_BUTTON1) { + ExecuteCommandPower(button_index + Button.press_counter[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DBG: Relay%d found on GPIO%d"), Button.press_counter[button_index], pin[GPIO_REL1 + Button.press_counter[button_index]-1]); + } + } + } + } + } else { // 6 - 8 press are used to send commands + GetTextIndexed(scmnd, sizeof(scmnd), Button.press_counter[button_index] -6, kCommands); + ExecuteCommand(scmnd, SRC_BUTTON); + } + if (Settings.flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic + if (Button.press_counter[button_index] >= 1 && Button.press_counter[button_index] <= 5) { + char mqttstate[7]; + + GetTextIndexed(mqttstate, sizeof(mqttstate), Button.press_counter[button_index], kMultiPress); + SendKey(KEY_BUTTON, button_index +1, Button.press_counter[button_index] +9); + snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_index +1); + GetTopic_P(stopic, STAT, mqtt_topic, scommand); + Response_P(S_JSON_COMMAND_SVALUE, "ACTION", mqttstate); + MqttPublish(stopic); + + } + } + } +#if defined(USE_LIGHT) && defined(ROTARY_V1) + } +#endif + Button.press_counter[button_index] = 0; + } + } + } + } + } + Button.last_state[button_index] = button; + } +} + +void ButtonLoop(void) +{ + if (Button.present) { + if (TimeReached(Button.debounce)) { + SetNextTimeInterval(Button.debounce, Settings.button_debounce); // ButtonDebounce (50) + ButtonHandler(); + } + } +} + +#endif // BUTTON_V2 \ No newline at end of file From 0f1cd33323c02a8dfb69f96f462ed08fdc5623e7 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Tue, 14 Apr 2020 11:51:00 -0300 Subject: [PATCH 07/11] Update settings.h --- tasmota/settings.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tasmota/settings.h b/tasmota/settings.h index 9a5cb917e..3f8827636 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -86,7 +86,8 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t energy_weekend : 1; // bit 20 (v6.6.0.8) - CMND_TARIFF uint32_t dds2382_model : 1; // bit 21 (v6.6.0.14) - SetOption71 - Select different Modbus registers for Active Energy (#6531) uint32_t hardware_energy_total : 1; // bit 22 (v6.6.0.15) - SetOption72 - Enable hardware energy total counter as reference (#6561) - uint32_t mqtt_buttons : 1; // bit 23 (v8.2.0.3) - SetOption73 - Detach buttons from relays and enable MQTT action state for multipress uint32_t ds18x20_internal_pullup : 1; // bit 24 (v7.0.0.1) - SetOption74 - Enable internal pullup for single DS18x20 sensor + uint32_t mqtt_buttons : 1; // bit 23 (v8.2.0.3) - SetOption73 - Detach buttons from relays and enable MQTT action state for multipress + uint32_t ds18x20_internal_pullup : 1; // bit 24 (v7.0.0.1) - SetOption74 - Enable internal pullup for single DS18x20 sensor uint32_t grouptopic_mode : 1; // bit 25 (v7.0.0.1) - SetOption75 - GroupTopic replaces %topic% (0) or fixed topic cmnd/grouptopic (1) uint32_t bootcount_update : 1; // bit 26 (v7.0.0.4) - SetOption76 - Enable incrementing bootcount when deepsleep is enabled uint32_t slider_dimmer_stay_on : 1; // bit 27 (v7.0.0.6) - SetOption77 - Do not power off if slider moved to far left From eb025461a1001fbbbbfc987b687bddf9370247eb Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 14 Apr 2020 16:58:11 +0200 Subject: [PATCH 08/11] Update settings.ino --- tasmota/settings.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 18b8017ee..3cd6ceb49 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -1243,11 +1243,11 @@ void SettingsDelta(void) Settings.ex_serial_config = TS_SERIAL_8N1; } if (Settings.version < 0x07010204) { - // if (Settings.flag3.ex_cors_enabled == 1) { - // strlcpy(Settings.ex_cors_domain, CORS_ENABLED_ALL, sizeof(Settings.ex_cors_domain)); - // } else { - // Settings.ex_cors_domain[0] = 0; - // } + if (Settings.flag3.mqtt_buttons == 1) { + strlcpy(Settings.ex_cors_domain, CORS_ENABLED_ALL, sizeof(Settings.ex_cors_domain)); + } else { + Settings.ex_cors_domain[0] = 0; + } } if (Settings.version < 0x07010205) { Settings.seriallog_level = Settings.ex_seriallog_level; // 09E -> 452 From fe63ddbd206290065febeff4c6820930d7eac217 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 14 Apr 2020 17:18:34 +0200 Subject: [PATCH 09/11] Fix prep for Buttons V2 Fix prep for Buttons V2 for ESP32 (#8177) --- tasmota/settings.ino | 10 +- tasmota/support_button.ino | 1 + ...upport_button_V2 => support_button_v2.ino} | 639 +++++++++--------- 3 files changed, 330 insertions(+), 320 deletions(-) rename tasmota/{support_button_V2 => support_button_v2.ino} (92%) diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 3cd6ceb49..53110155f 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -1243,11 +1243,11 @@ void SettingsDelta(void) Settings.ex_serial_config = TS_SERIAL_8N1; } if (Settings.version < 0x07010204) { - if (Settings.flag3.mqtt_buttons == 1) { - strlcpy(Settings.ex_cors_domain, CORS_ENABLED_ALL, sizeof(Settings.ex_cors_domain)); - } else { - Settings.ex_cors_domain[0] = 0; - } + if (Settings.flag3.mqtt_buttons == 1) { + strlcpy(Settings.ex_cors_domain, CORS_ENABLED_ALL, sizeof(Settings.ex_cors_domain)); + } else { + Settings.ex_cors_domain[0] = 0; + } } if (Settings.version < 0x07010205) { Settings.seriallog_level = Settings.ex_seriallog_level; // 09E -> 452 diff --git a/tasmota/support_button.ino b/tasmota/support_button.ino index 4b41c2a74..914263add 100644 --- a/tasmota/support_button.ino +++ b/tasmota/support_button.ino @@ -24,6 +24,7 @@ \*********************************************************************************************/ #define MAX_BUTTON_COMMANDS 5 // Max number of button commands supported + const char kCommands[] PROGMEM = D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; diff --git a/tasmota/support_button_V2 b/tasmota/support_button_v2.ino similarity index 92% rename from tasmota/support_button_V2 rename to tasmota/support_button_v2.ino index b6bb1e1ca..0e706b44f 100644 --- a/tasmota/support_button_V2 +++ b/tasmota/support_button_v2.ino @@ -1,316 +1,325 @@ -/* - support_button.ino - button support for Tasmota - - Copyright (C) 2020 Theo Arends - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#define BUTTON_V2 -#ifdef BUTTON_V2 -/*********************************************************************************************\ - * Button support -\*********************************************************************************************/ - -#define MAX_BUTTON_COMMANDS 3 // Max number of button commands supported -#define MAX_RELAY_BUTTON1 4 // Max number of relay controlled by button1 - -const char kCommands[] PROGMEM = - D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; - //D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; -const char kMultiPress[] PROGMEM = - "|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|"; - -struct BUTTON { - unsigned long debounce = 0; // Button debounce timer - uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold - uint16_t dual_code = 0; // Sonoff dual received code - - uint8_t last_state[MAX_KEYS] = { NOT_PRESSED, NOT_PRESSED, NOT_PRESSED, NOT_PRESSED }; // Last button states - uint8_t window_timer[MAX_KEYS] = { 0 }; // Max time between button presses to record press count - uint8_t press_counter[MAX_KEYS] = { 0 }; // Number of button presses within Button.window_timer - - uint8_t dual_receive_count = 0; // Sonoff dual input flag - uint8_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup) - uint8_t inverted_mask = 0; // Key inverted flag (1 = inverted) - uint8_t present = 0; // Number of buttons found flag - uint8_t adc = 99; // ADC0 button number -} Button; - -/********************************************************************************************/ - -void ButtonPullupFlag(uint8 button_bit) -{ - bitSet(Button.no_pullup_mask, button_bit); -} - -void ButtonInvertFlag(uint8 button_bit) -{ - bitSet(Button.inverted_mask, button_bit); -} - -void ButtonInit(void) -{ - Button.present = 0; - for (uint32_t i = 0; i < MAX_KEYS; i++) { - if (pin[GPIO_KEY1 +i] < 99) { - Button.present++; - pinMode(pin[GPIO_KEY1 +i], bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : INPUT_PULLUP)); - } -#ifndef USE_ADC_VCC - else if ((99 == Button.adc) && ((ADC0_BUTTON == my_adc0) || (ADC0_BUTTON_INV == my_adc0))) { - Button.present++; - Button.adc = i; - } -#endif // USE_ADC_VCC - } -} - -uint8_t ButtonSerial(uint8_t serial_in_byte) -{ - if (Button.dual_receive_count) { - Button.dual_receive_count--; - if (Button.dual_receive_count) { - Button.dual_code = (Button.dual_code << 8) | serial_in_byte; - serial_in_byte = 0; - } else { - if (serial_in_byte != 0xA1) { - Button.dual_code = 0; // 0xA1 - End of Sonoff dual button code - } - } - } - if (0xA0 == serial_in_byte) { // 0xA0 - Start of Sonoff dual button code - serial_in_byte = 0; - Button.dual_code = 0; - Button.dual_receive_count = 3; - } - - return serial_in_byte; -} - -/*********************************************************************************************\ - * Button handler with single press only or multi-press and hold on all buttons - * - * ButtonDebounce (50) - Debounce time in mSec - * SetOption1 (0) - If set do not execute config commands - * SetOption11 (0) - If set perform single press action on double press and reverse - * SetOption13 (0) - If set act on single press only - * SetOption32 (40) - Max button hold time in Seconds - * SetOption40 (0) - Number of 0.1 seconds until hold is discarded if SetOption1 1 and SetOption13 0 - * SetOption73 (0) - Decouple button from relay and send just mqtt topic -\*********************************************************************************************/ - -void ButtonHandler(void) -{ - if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit - - uint8_t hold_time_extent = IMMINENT_RESET_FACTOR; // Extent hold time factor in case of iminnent Reset command - uint16_t loops_per_second = 1000 / Settings.button_debounce; // ButtonDebounce (50) - char scmnd[20]; - char scommand[CMDSZ]; - char stopic[TOPSZ]; - -// uint8_t maxdev = (devices_present > MAX_KEYS) ? MAX_KEYS : devices_present; -// for (uint32_t button_index = 0; button_index < maxdev; button_index++) { - for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++) { - uint8_t button = NOT_PRESSED; - uint8_t button_present = 0; - - if (!button_index && ((SONOFF_DUAL == my_module_type) || (CH4 == my_module_type))) { - button_present = 1; - if (Button.dual_code) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON " " D_CODE " %04X"), Button.dual_code); - button = PRESSED; - if (0xF500 == Button.dual_code) { // Button hold - Button.hold_timer[button_index] = (loops_per_second * Settings.param[P_HOLD_TIME] / 10) -1; // SetOption32 (40) - hold_time_extent = 1; - } - Button.dual_code = 0; - } - } - else if (pin[GPIO_KEY1 +button_index] < 99) { - button_present = 1; - button = (digitalRead(pin[GPIO_KEY1 +button_index]) != bitRead(Button.inverted_mask, button_index)); - } -#ifndef USE_ADC_VCC - if (Button.adc == button_index) { - button_present = 1; - if (ADC0_BUTTON_INV == my_adc0) { - button = (AdcRead(1) < 128); - } - else if (ADC0_BUTTON == my_adc0) { - button = (AdcRead(1) > 128); - } - } -#endif // USE_ADC_VCC - - if (button_present) { - XdrvMailbox.index = button_index; - XdrvMailbox.payload = button; - if (XdrvCall(FUNC_BUTTON_PRESSED)) { - // Serviced - } - else if (SONOFF_4CHPRO == my_module_type) { - if (Button.hold_timer[button_index]) { Button.hold_timer[button_index]--; } - - bool button_pressed = false; - if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_10), button_index +1); - Button.hold_timer[button_index] = loops_per_second; - button_pressed = true; - } - if ((NOT_PRESSED == button) && (PRESSED == Button.last_state[button_index])) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_01), button_index +1); - if (!Button.hold_timer[button_index]) { button_pressed = true; } // Do not allow within 1 second - } - if (button_pressed) { - if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set - ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally - - } - } - } else { - - if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) { - - if (Settings.flag.button_single && !Settings.flag3.mqtt_buttons) { // SetOption13 (0) - Allow only single button press for immediate action, SetOption73 (0) - Decouple button from relay and send just mqtt topic - AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1); - if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set - ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally - } - - } else { - Button.press_counter[button_index] = (Button.window_timer[button_index]) ? Button.press_counter[button_index] +1 : 1; - AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_MULTI_PRESS " %d"), button_index +1, Button.press_counter[button_index]); - Button.window_timer[button_index] = loops_per_second / 2; // 0.5 second multi press window - } - blinks = 201; - } - - if (NOT_PRESSED == button) { - Button.hold_timer[button_index] = 0; - } else { - Button.hold_timer[button_index]++; - if (Settings.flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action - if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer - snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_SETOPTION "13 0")); // Disable single press only - ExecuteCommand(scmnd, SRC_BUTTON); - } - } else { - if (Button.hold_timer[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button hold - Button.press_counter[button_index] = 0; - if (Settings.flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic - snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_index +1); - GetTopic_P(stopic, STAT, mqtt_topic, scommand); - Response_P(S_JSON_COMMAND_SVALUE, "ACTION", GetStateText(3)); - MqttPublish(stopic); - } - SendKey(KEY_BUTTON, button_index +1, POWER_HOLD); // Execute Hold command via MQTT if ButtonTopic is set - } else { - if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer - Button.press_counter[button_index] = 0; - snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_RESET " 1")); - ExecuteCommand(scmnd, SRC_BUTTON); - } - } - } - } - - if (!Settings.flag.button_single) { // SetOption13 (0) - Allow multi-press - if (Button.window_timer[button_index]) { - Button.window_timer[button_index]--; - } else { - if (!restart_flag && !Button.hold_timer[button_index] && (Button.press_counter[button_index] > 0) && (Button.press_counter[button_index] < MAX_BUTTON_COMMANDS +6)) { - bool single_press = false; - if (Button.press_counter[button_index] < 3) { // Single or Double press - if ((SONOFF_DUAL_R2 == my_module_type) || (SONOFF_DUAL == my_module_type) || (CH4 == my_module_type)) { - single_press = true; - } else { - single_press = (Settings.flag.button_swap +1 == Button.press_counter[button_index]); // SetOption11 (0) - if ((1 == Button.present) && (2 == devices_present)) { // Single Button with two devices only - if (Settings.flag.button_swap) { // SetOption11 (0) - Button.press_counter[button_index] = (single_press) ? 1 : 2; - } - // } else { - // if (!Settings.flag3.mqtt_buttons && button_index != 0) { - // Button.press_counter[button_index] = 1; - // } - } - } - } -#if defined(USE_LIGHT) && defined(ROTARY_V1) - if (!((0 == button_index) && RotaryButtonPressed())) { -#endif - if (!Settings.flag3.mqtt_buttons && single_press && SendKey(KEY_BUTTON, button_index + Button.press_counter[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set - // Success - } else { - if (Button.press_counter[button_index] < 6) { // Single to Penta press - if (WifiState() > WIFI_RESTART) { // Wifimanager active - restart_flag = 1; - } - if (!Settings.flag3.mqtt_buttons) { - if (Button.press_counter[button_index] == 1) { // By default first press always send a TOGGLE (2) - ExecuteCommandPower(button_index + Button.press_counter[button_index], POWER_TOGGLE, SRC_BUTTON); - } else { - SendKey(KEY_BUTTON, button_index +1, Button.press_counter[button_index] +9); // 2,3,4 and 5 press send just the key value (11,12,13 and 14) for rules - if (0 == button_index) { // First button can toggle up to 4 relays if present - if ((Button.press_counter[button_index] > 1 && pin[GPIO_REL1 + Button.press_counter[button_index]-1] < 99) && Button.press_counter[button_index] <= MAX_RELAY_BUTTON1) { - ExecuteCommandPower(button_index + Button.press_counter[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DBG: Relay%d found on GPIO%d"), Button.press_counter[button_index], pin[GPIO_REL1 + Button.press_counter[button_index]-1]); - } - } - } - } - } else { // 6 - 8 press are used to send commands - GetTextIndexed(scmnd, sizeof(scmnd), Button.press_counter[button_index] -6, kCommands); - ExecuteCommand(scmnd, SRC_BUTTON); - } - if (Settings.flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic - if (Button.press_counter[button_index] >= 1 && Button.press_counter[button_index] <= 5) { - char mqttstate[7]; - - GetTextIndexed(mqttstate, sizeof(mqttstate), Button.press_counter[button_index], kMultiPress); - SendKey(KEY_BUTTON, button_index +1, Button.press_counter[button_index] +9); - snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_index +1); - GetTopic_P(stopic, STAT, mqtt_topic, scommand); - Response_P(S_JSON_COMMAND_SVALUE, "ACTION", mqttstate); - MqttPublish(stopic); - - } - } - } -#if defined(USE_LIGHT) && defined(ROTARY_V1) - } -#endif - Button.press_counter[button_index] = 0; - } - } - } - } - } - Button.last_state[button_index] = button; - } -} - -void ButtonLoop(void) -{ - if (Button.present) { - if (TimeReached(Button.debounce)) { - SetNextTimeInterval(Button.debounce, Settings.button_debounce); // ButtonDebounce (50) - ButtonHandler(); - } - } -} - +/* + support_button.ino - button support for Tasmota + + Copyright (C) 2020 Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +//#define BUTTON_V2 +#ifdef BUTTON_V2 +/*********************************************************************************************\ + * Button support +\*********************************************************************************************/ + +#define MAX_BUTTON_COMMANDS_V2 3 // Max number of button commands supported +#define MAX_RELAY_BUTTON1 4 // Max number of relay controlled by button1 + +const char kCommands[] PROGMEM = + D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; + //D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1"; +const char kMultiPress[] PROGMEM = + "|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|"; + +struct BUTTON { + unsigned long debounce = 0; // Button debounce timer + uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold + uint16_t dual_code = 0; // Sonoff dual received code + + uint8_t last_state[MAX_KEYS] = { NOT_PRESSED, NOT_PRESSED, NOT_PRESSED, NOT_PRESSED }; // Last button states + uint8_t window_timer[MAX_KEYS] = { 0 }; // Max time between button presses to record press count + uint8_t press_counter[MAX_KEYS] = { 0 }; // Number of button presses within Button.window_timer + + uint8_t dual_receive_count = 0; // Sonoff dual input flag + uint8_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup) + uint8_t inverted_mask = 0; // Key inverted flag (1 = inverted) + uint8_t present = 0; // Number of buttons found flag + uint8_t adc = 99; // ADC0 button number +} Button; + +/********************************************************************************************/ + +void ButtonPullupFlag(uint8 button_bit) +{ + bitSet(Button.no_pullup_mask, button_bit); +} + +void ButtonInvertFlag(uint8 button_bit) +{ + bitSet(Button.inverted_mask, button_bit); +} + +void ButtonInit(void) +{ + Button.present = 0; + for (uint32_t i = 0; i < MAX_KEYS; i++) { + if (pin[GPIO_KEY1 +i] < 99) { + Button.present++; + pinMode(pin[GPIO_KEY1 +i], bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : INPUT_PULLUP)); + } +#ifndef USE_ADC_VCC + else if ((99 == Button.adc) && ((ADC0_BUTTON == my_adc0) || (ADC0_BUTTON_INV == my_adc0))) { + Button.present++; + Button.adc = i; + } +#endif // USE_ADC_VCC + } +} + +uint8_t ButtonSerial(uint8_t serial_in_byte) +{ + if (Button.dual_receive_count) { + Button.dual_receive_count--; + if (Button.dual_receive_count) { + Button.dual_code = (Button.dual_code << 8) | serial_in_byte; + serial_in_byte = 0; + } else { + if (serial_in_byte != 0xA1) { + Button.dual_code = 0; // 0xA1 - End of Sonoff dual button code + } + } + } + if (0xA0 == serial_in_byte) { // 0xA0 - Start of Sonoff dual button code + serial_in_byte = 0; + Button.dual_code = 0; + Button.dual_receive_count = 3; + } + + return serial_in_byte; +} + +/*********************************************************************************************\ + * Button handler with single press only or multi-press and hold on all buttons + * + * ButtonDebounce (50) - Debounce time in mSec + * SetOption1 (0) - If set do not execute config commands + * SetOption11 (0) - If set perform single press action on double press and reverse + * SetOption13 (0) - If set act on single press only + * SetOption32 (40) - Max button hold time in Seconds + * SetOption40 (0) - Number of 0.1 seconds until hold is discarded if SetOption1 1 and SetOption13 0 + * SetOption73 (0) - Decouple button from relay and send just mqtt topic +\*********************************************************************************************/ + +void ButtonHandler(void) +{ + if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit + + uint8_t hold_time_extent = IMMINENT_RESET_FACTOR; // Extent hold time factor in case of iminnent Reset command + uint16_t loops_per_second = 1000 / Settings.button_debounce; // ButtonDebounce (50) + char scmnd[20]; + char scommand[CMDSZ]; + char stopic[TOPSZ]; + +// uint8_t maxdev = (devices_present > MAX_KEYS) ? MAX_KEYS : devices_present; +// for (uint32_t button_index = 0; button_index < maxdev; button_index++) { + for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++) { + uint8_t button = NOT_PRESSED; + uint8_t button_present = 0; + +#ifdef ESP8266 + if (!button_index && ((SONOFF_DUAL == my_module_type) || (CH4 == my_module_type))) { + button_present = 1; + if (Button.dual_code) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON " " D_CODE " %04X"), Button.dual_code); + button = PRESSED; + if (0xF500 == Button.dual_code) { // Button hold + Button.hold_timer[button_index] = (loops_per_second * Settings.param[P_HOLD_TIME] / 10) -1; // SetOption32 (40) + hold_time_extent = 1; + } + Button.dual_code = 0; + } + } + else +#endif // ESP8266 + if (pin[GPIO_KEY1 +button_index] < 99) { + button_present = 1; + button = (digitalRead(pin[GPIO_KEY1 +button_index]) != bitRead(Button.inverted_mask, button_index)); + } +#ifndef USE_ADC_VCC + if (Button.adc == button_index) { + button_present = 1; + if (ADC0_BUTTON_INV == my_adc0) { + button = (AdcRead(1) < 128); + } + else if (ADC0_BUTTON == my_adc0) { + button = (AdcRead(1) > 128); + } + } +#endif // USE_ADC_VCC + + if (button_present) { + XdrvMailbox.index = button_index; + XdrvMailbox.payload = button; + if (XdrvCall(FUNC_BUTTON_PRESSED)) { + // Serviced + } +#ifdef ESP8266 + else if (SONOFF_4CHPRO == my_module_type) { + if (Button.hold_timer[button_index]) { Button.hold_timer[button_index]--; } + + bool button_pressed = false; + if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_10), button_index +1); + Button.hold_timer[button_index] = loops_per_second; + button_pressed = true; + } + if ((NOT_PRESSED == button) && (PRESSED == Button.last_state[button_index])) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_01), button_index +1); + if (!Button.hold_timer[button_index]) { button_pressed = true; } // Do not allow within 1 second + } + if (button_pressed) { + if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set + ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + + } + } + } +#endif // ESP8266 + else { + + if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) { + + if (Settings.flag.button_single && !Settings.flag3.mqtt_buttons) { // SetOption13 (0) - Allow only single button press for immediate action, SetOption73 (0) - Decouple button from relay and send just mqtt topic + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1); + if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set + ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + } + + } else { + Button.press_counter[button_index] = (Button.window_timer[button_index]) ? Button.press_counter[button_index] +1 : 1; + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_MULTI_PRESS " %d"), button_index +1, Button.press_counter[button_index]); + Button.window_timer[button_index] = loops_per_second / 2; // 0.5 second multi press window + } + blinks = 201; + } + + if (NOT_PRESSED == button) { + Button.hold_timer[button_index] = 0; + } else { + Button.hold_timer[button_index]++; + if (Settings.flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action + if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_SETOPTION "13 0")); // Disable single press only + ExecuteCommand(scmnd, SRC_BUTTON); + } + } else { + if (Button.hold_timer[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button hold + Button.press_counter[button_index] = 0; + if (Settings.flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic + snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_index +1); + GetTopic_P(stopic, STAT, mqtt_topic, scommand); + Response_P(S_JSON_COMMAND_SVALUE, "ACTION", GetStateText(3)); + MqttPublish(stopic); + } + SendKey(KEY_BUTTON, button_index +1, POWER_HOLD); // Execute Hold command via MQTT if ButtonTopic is set + } else { + if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer + Button.press_counter[button_index] = 0; + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_RESET " 1")); + ExecuteCommand(scmnd, SRC_BUTTON); + } + } + } + } + + if (!Settings.flag.button_single) { // SetOption13 (0) - Allow multi-press + if (Button.window_timer[button_index]) { + Button.window_timer[button_index]--; + } else { + if (!restart_flag && !Button.hold_timer[button_index] && (Button.press_counter[button_index] > 0) && (Button.press_counter[button_index] < MAX_BUTTON_COMMANDS_V2 +6)) { + bool single_press = false; + if (Button.press_counter[button_index] < 3) { // Single or Double press +#ifdef ESP8266 + if ((SONOFF_DUAL_R2 == my_module_type) || (SONOFF_DUAL == my_module_type) || (CH4 == my_module_type)) { + single_press = true; + } else +#endif // ESP8266 + { + single_press = (Settings.flag.button_swap +1 == Button.press_counter[button_index]); // SetOption11 (0) + if ((1 == Button.present) && (2 == devices_present)) { // Single Button with two devices only + if (Settings.flag.button_swap) { // SetOption11 (0) + Button.press_counter[button_index] = (single_press) ? 1 : 2; + } + // } else { + // if (!Settings.flag3.mqtt_buttons && button_index != 0) { + // Button.press_counter[button_index] = 1; + // } + } + } + } +#if defined(USE_LIGHT) && defined(ROTARY_V1) + if (!((0 == button_index) && RotaryButtonPressed())) { +#endif + if (!Settings.flag3.mqtt_buttons && single_press && SendKey(KEY_BUTTON, button_index + Button.press_counter[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set + // Success + } else { + if (Button.press_counter[button_index] < 6) { // Single to Penta press + if (WifiState() > WIFI_RESTART) { // Wifimanager active + restart_flag = 1; + } + if (!Settings.flag3.mqtt_buttons) { + if (Button.press_counter[button_index] == 1) { // By default first press always send a TOGGLE (2) + ExecuteCommandPower(button_index + Button.press_counter[button_index], POWER_TOGGLE, SRC_BUTTON); + } else { + SendKey(KEY_BUTTON, button_index +1, Button.press_counter[button_index] +9); // 2,3,4 and 5 press send just the key value (11,12,13 and 14) for rules + if (0 == button_index) { // First button can toggle up to 4 relays if present + if ((Button.press_counter[button_index] > 1 && pin[GPIO_REL1 + Button.press_counter[button_index]-1] < 99) && Button.press_counter[button_index] <= MAX_RELAY_BUTTON1) { + ExecuteCommandPower(button_index + Button.press_counter[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DBG: Relay%d found on GPIO%d"), Button.press_counter[button_index], pin[GPIO_REL1 + Button.press_counter[button_index]-1]); + } + } + } + } + } else { // 6 - 8 press are used to send commands + GetTextIndexed(scmnd, sizeof(scmnd), Button.press_counter[button_index] -6, kCommands); + ExecuteCommand(scmnd, SRC_BUTTON); + } + if (Settings.flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic + if (Button.press_counter[button_index] >= 1 && Button.press_counter[button_index] <= 5) { + char mqttstate[7]; + + GetTextIndexed(mqttstate, sizeof(mqttstate), Button.press_counter[button_index], kMultiPress); + SendKey(KEY_BUTTON, button_index +1, Button.press_counter[button_index] +9); + snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_index +1); + GetTopic_P(stopic, STAT, mqtt_topic, scommand); + Response_P(S_JSON_COMMAND_SVALUE, "ACTION", mqttstate); + MqttPublish(stopic); + + } + } + } +#if defined(USE_LIGHT) && defined(ROTARY_V1) + } +#endif + Button.press_counter[button_index] = 0; + } + } + } + } + } + Button.last_state[button_index] = button; + } +} + +void ButtonLoop(void) +{ + if (Button.present) { + if (TimeReached(Button.debounce)) { + SetNextTimeInterval(Button.debounce, Settings.button_debounce); // ButtonDebounce (50) + ButtonHandler(); + } + } +} + #endif // BUTTON_V2 \ No newline at end of file From e46fddf3b703d2761fb0ccb471e719c9afce40a5 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 14 Apr 2020 17:21:39 +0200 Subject: [PATCH 10/11] More ESP compatibility --- tasmota/tasmota_template_ESP32.h | 1 - tasmota/xdrv_01_webserver.ino | 9 +++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tasmota/tasmota_template_ESP32.h b/tasmota/tasmota_template_ESP32.h index 04e18af17..11443826c 100644 --- a/tasmota/tasmota_template_ESP32.h +++ b/tasmota/tasmota_template_ESP32.h @@ -42,7 +42,6 @@ #undef USE_DEEPSLEEP #undef USE_MY92X1 #undef USE_TUYA_MCU -#undef USE_I2C #undef USE_PS_16_DZ /********************************************************************************************/ diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index 5206b701d..178502150 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -1477,7 +1477,6 @@ void HandleTemplateConfiguration(void) WSContentSend_P(PSTR("}1")); // Field separator for (uint32_t i = 0; i < sizeof(cmodule); i++) { // 17,148,29,149,7,255,255,255,138,255,139,255,255 -// if ((i < 6) || ((i > 8) && (i != 11))) { // Ignore flash pins GPIO06, 7, 8 and 11 if (!FlashPin(i)) { WSContentSend_P(PSTR("%s%d"), (i>0)?",":"", cmodule.io[i]); } @@ -1501,7 +1500,6 @@ void HandleTemplateConfiguration(void) "
")); WSContentSend_P(HTTP_TABLE100); for (uint32_t i = 0; i < MAX_GPIO_PIN; i++) { -// if ((i < 6) || ((i > 8) && (i != 11))) { // Ignore flash pins GPIO06, 7, 8 and 11 if (!FlashPin(i)) { WSContentSend_P(PSTR("" D_GPIO "%d"), ((9==i)||(10==i)) ? WebColor(COL_TEXT_WARNING) : WebColor(COL_TEXT), i, (0==i) ? " style='width:200px'" : "", i); @@ -1633,17 +1631,24 @@ void HandleModuleConfiguration(void) for (uint32_t i = 0; i < sizeof(cmodule); i++) { if (ValidGPIO(i, cmodule.io[i])) { snprintf_P(stemp, 3, PINS_WEMOS +i*2); +#ifdef ESP8266 char sesp8285[40]; snprintf_P(sesp8285, sizeof(sesp8285), PSTR("ESP8285"), WebColor(COL_TEXT_WARNING)); WSContentSend_P(PSTR("%s " D_GPIO "%d %s"), (WEMOS==my_module_type)?stemp:"", i, (0==i)? D_SENSOR_BUTTON "1":(1==i)? D_SERIAL_OUT :(3==i)? D_SERIAL_IN :((9==i)||(10==i))? sesp8285 :(12==i)? D_SENSOR_RELAY "1":(13==i)? D_SENSOR_LED "1i":(14==i)? D_SENSOR :"", i); +#else // ESP32 + WSContentSend_P(PSTR("%s " D_GPIO "%d"), + (WEMOS==my_module_type)?stemp:"", i, i); +#endif // ESP8266 } } +#ifdef ESP8266 #ifndef USE_ADC_VCC if (ValidAdc()) { WSContentSend_P(PSTR("%s " D_ADC "0"), (WEMOS==my_module_type)?"A0":""); } #endif // USE_ADC_VCC +#endif // ESP8266 WSContentSend_P(PSTR("")); WSContentSend_P(HTTP_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); From e3503ab5b82aecac3068ead0640d134570522f2c Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 14 Apr 2020 17:36:36 +0200 Subject: [PATCH 11/11] Enable I2C in ESP32 and remove MQTT checks --- tasmota/support_features.ino | 12 ++++++------ tasmota/tasmota_template_ESP32.h | 8 +++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/tasmota/support_features.ino b/tasmota/support_features.ino index f9dac8b4a..aa02d676e 100644 --- a/tasmota/support_features.ino +++ b/tasmota/support_features.ino @@ -55,15 +55,15 @@ void GetFeatures(void) #ifdef USE_EMULATION_HUE feature_drv1 |= 0x00000200; // xdrv_20_hue.ino #endif -#if (MQTT_LIBRARY_TYPE == MQTT_PUBSUBCLIENT) +//#if (MQTT_LIBRARY_TYPE == MQTT_PUBSUBCLIENT) feature_drv1 |= 0x00000400; // xdrv_02_mqtt.ino -#endif -#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT) +//#endif +//#if (MQTT_LIBRARY_TYPE == MQTT_TASMOTAMQTT) // feature_drv1 |= 0x00000800; // xdrv_02_mqtt.ino -#endif -#if (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) // Obsolete since 6.2.1.11 +//#endif +//#if (MQTT_LIBRARY_TYPE == MQTT_ESPMQTTARDUINO) // Obsolete since 6.2.1.11 // feature_drv1 |= 0x00001000; // xdrv_02_mqtt.ino -#endif +//#endif #ifdef MQTT_HOST_DISCOVERY feature_drv1 |= 0x00002000; // xdrv_02_mqtt.ino #endif diff --git a/tasmota/tasmota_template_ESP32.h b/tasmota/tasmota_template_ESP32.h index 11443826c..b2d7a5846 100644 --- a/tasmota/tasmota_template_ESP32.h +++ b/tasmota/tasmota_template_ESP32.h @@ -22,9 +22,6 @@ #ifdef ESP32 -// mqtt -#undef MQTT_LIBRARY_TYPE -#define MQTT_LIBRARY_TYPE MQTT_PUBSUBCLIENT // Hardware has no ESP32 #undef USE_TUYA_DIMMER #undef USE_PWM_DIMMER @@ -36,9 +33,10 @@ #undef USE_SONOFF_L1 #undef USE_SONOFF_D1 #undef USE_RF_FLASH -// not ported + +// Not ported (yet) #undef USE_DISCOVERY -#undef USE_ADC_VCC // Needs to be ported +#undef USE_ADC_VCC // Needs to be ported #undef USE_DEEPSLEEP #undef USE_MY92X1 #undef USE_TUYA_MCU