mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-20 09:16:30 +00:00
Refactor switches and buttons
- Fix Tuya switches
This commit is contained in:
parent
ec56473631
commit
e0584b2157
@ -1,419 +0,0 @@
|
|||||||
/*
|
|
||||||
support_button.ino - button support for Tasmota
|
|
||||||
|
|
||||||
Copyright (C) 2021 Federico Leoni and 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//#define BUTTON_V2
|
|
||||||
#ifdef BUTTON_V2
|
|
||||||
/*********************************************************************************************\
|
|
||||||
* Button support
|
|
||||||
\*********************************************************************************************/
|
|
||||||
|
|
||||||
#define MAX_RELAY_BUTTON1 5 // Max number of relay controlled by BUTTON1
|
|
||||||
|
|
||||||
#define TOUCH_PIN_THRESHOLD 12 // Smaller value will treated as button press
|
|
||||||
#define TOUCH_HIT_THRESHOLD 3 // successful hits to filter out noise
|
|
||||||
|
|
||||||
const char kMultiPress[] PROGMEM = "|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|CLEAR|";
|
|
||||||
|
|
||||||
struct BUTTON {
|
|
||||||
uint32_t debounce = 0; // Button debounce timer
|
|
||||||
uint32_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup)
|
|
||||||
uint32_t pulldown_mask = 0; // key pulldown flag (1 = pulldown)
|
|
||||||
uint32_t inverted_mask = 0; // Key inverted flag (1 = inverted)
|
|
||||||
#ifdef ESP32
|
|
||||||
uint32_t touch_mask = 0; // Touch flag (1 = inverted)
|
|
||||||
#endif // ESP32
|
|
||||||
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]; // 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
|
|
||||||
#ifdef ESP32
|
|
||||||
uint8_t touch_hits[MAX_KEYS] = { 0 }; // Hits in a row to filter out noise
|
|
||||||
#endif // ESP32
|
|
||||||
uint8_t present = 0; // Number of buttons found flag
|
|
||||||
} Button;
|
|
||||||
|
|
||||||
#ifdef ESP32
|
|
||||||
struct TOUCH_BUTTON {
|
|
||||||
uint32_t calibration = 0; // Bitfield
|
|
||||||
uint32_t pin_threshold = TOUCH_PIN_THRESHOLD;
|
|
||||||
uint8_t hit_threshold = TOUCH_HIT_THRESHOLD;
|
|
||||||
} TouchButton;
|
|
||||||
#endif // ESP32
|
|
||||||
|
|
||||||
/********************************************************************************************/
|
|
||||||
|
|
||||||
void ButtonPullupFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.no_pullup_mask, button_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonPulldownFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.pulldown_mask, button_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonInvertFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.inverted_mask, button_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef ESP32
|
|
||||||
void ButtonTouchFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.touch_mask, button_bit);
|
|
||||||
}
|
|
||||||
#endif // ESP32
|
|
||||||
|
|
||||||
void ButtonInit(void) {
|
|
||||||
Button.present = 0;
|
|
||||||
#ifdef ESP8266
|
|
||||||
if ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type)) {
|
|
||||||
Button.present++;
|
|
||||||
}
|
|
||||||
#endif // ESP8266
|
|
||||||
for (uint32_t i = 0; i < MAX_KEYS; i++) {
|
|
||||||
Button.last_state[i] = NOT_PRESSED;
|
|
||||||
if (PinUsed(GPIO_KEY1, i)) {
|
|
||||||
Button.present++;
|
|
||||||
#ifdef ESP8266
|
|
||||||
pinMode(Pin(GPIO_KEY1, i), bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_KEY1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
|
||||||
#endif // ESP8266
|
|
||||||
#ifdef ESP32
|
|
||||||
pinMode(Pin(GPIO_KEY1, i), bitRead(Button.pulldown_mask, i) ? INPUT_PULLDOWN : bitRead(Button.no_pullup_mask, i) ? INPUT : INPUT_PULLUP);
|
|
||||||
#endif // ESP32
|
|
||||||
}
|
|
||||||
#ifdef USE_ADC
|
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON, i) || PinUsed(GPIO_ADC_BUTTON_INV, i)) {
|
|
||||||
Button.present++;
|
|
||||||
}
|
|
||||||
#endif // USE_ADC
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 commands WifiConfig and Reset
|
|
||||||
* SetOption11 (0) - If set perform single press action on double press and reverse (on two relay devices only)
|
|
||||||
* SetOption13 (0) - If set act on single press only
|
|
||||||
* SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
\*********************************************************************************************/
|
|
||||||
|
|
||||||
void ButtonHandler(void) {
|
|
||||||
if (TasmotaGlobal.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];
|
|
||||||
|
|
||||||
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 == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type))) {
|
|
||||||
button_present = 1;
|
|
||||||
if (Button.dual_code) {
|
|
||||||
AddLog(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 (PinUsed(GPIO_KEY1, button_index)) {
|
|
||||||
button_present = 1;
|
|
||||||
#ifdef ESP32
|
|
||||||
#ifndef CONFIG_IDF_TARGET_ESP32C3
|
|
||||||
if (bitRead(Button.touch_mask, button_index)) { // Touch
|
|
||||||
uint32_t _value = touchRead(Pin(GPIO_KEY1, button_index));
|
|
||||||
button = NOT_PRESSED;
|
|
||||||
if (_value != 0) { // Probably read-error
|
|
||||||
if (_value < TouchButton.pin_threshold) {
|
|
||||||
if (++Button.touch_hits[button_index] > TouchButton.hit_threshold) {
|
|
||||||
if (!bitRead(TouchButton.calibration, button_index+1)) {
|
|
||||||
button = PRESSED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Button.touch_hits[button_index] = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Button.touch_hits[button_index] = 0;
|
|
||||||
}
|
|
||||||
if (bitRead(TouchButton.calibration, button_index+1)) {
|
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("PLOT: %u, %u, %u,"), button_index+1, _value, Button.touch_hits[button_index]); // Button number (1..4), value, continuous hits under threshold
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
#endif // not ESP32C3
|
|
||||||
#endif // ESP32
|
|
||||||
{ // Normal button
|
|
||||||
button = (digitalRead(Pin(GPIO_KEY1, button_index)) != bitRead(Button.inverted_mask, button_index));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef USE_ADC
|
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON, button_index)) {
|
|
||||||
button_present = 1;
|
|
||||||
button = AdcGetButton(Pin(GPIO_ADC_BUTTON, button_index));
|
|
||||||
}
|
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON_INV, button_index)) {
|
|
||||||
button_present = 1;
|
|
||||||
button = AdcGetButton(Pin(GPIO_ADC_BUTTON_INV, button_index));
|
|
||||||
}
|
|
||||||
#endif // USE_ADC
|
|
||||||
if (button_present) {
|
|
||||||
XdrvMailbox.index = button_index;
|
|
||||||
XdrvMailbox.payload = button;
|
|
||||||
if (XdrvCall(FUNC_BUTTON_PRESSED)) {
|
|
||||||
// Serviced
|
|
||||||
}
|
|
||||||
#ifdef ESP8266
|
|
||||||
else if (SONOFF_4CHPRO == TasmotaGlobal.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(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(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 (!Settings->flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
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 {
|
|
||||||
MqttButtonTopic(button_index +1, 1, 0); // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // ESP8266
|
|
||||||
else {
|
|
||||||
if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) {
|
|
||||||
|
|
||||||
if (Settings->flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action,
|
|
||||||
if (!Settings->flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
AddLog(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 {
|
|
||||||
MqttButtonTopic(button_index +1, 1, 0); // SetOption73 1 - Decouple button from relay and send just mqtt topic
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Button.press_counter[button_index] = (Button.window_timer[button_index]) ? Button.press_counter[button_index] +1 : 1;
|
|
||||||
AddLog(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
|
|
||||||
}
|
|
||||||
TasmotaGlobal.blinks = 201;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
Button.hold_timer[button_index] = 0;
|
|
||||||
if (Settings->flag3.mqtt_buttons && (PRESSED == Button.last_state[button_index]) && !Button.press_counter[button_index]) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
MqttButtonTopic(button_index +1, 6, 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
|
|
||||||
MqttButtonTopic(button_index +1, 3, 1);
|
|
||||||
} else {
|
|
||||||
SendKey(KEY_BUTTON, button_index +1, POWER_HOLD); // Execute Hold command via MQTT if ButtonTopic is set
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Settings->flag.button_restrict) { // SetOption1 (0) - Control button multipress
|
|
||||||
if (Settings->param[P_HOLD_IGNORE] > 0) { // SetOption40 (0) - Do not ignore button hold
|
|
||||||
if (Button.hold_timer[button_index] > loops_per_second * Settings->param[P_HOLD_IGNORE] / 10) {
|
|
||||||
Button.hold_timer[button_index] = 0; // Reset button hold counter to stay below hold trigger
|
|
||||||
Button.press_counter[button_index] = 0; // Discard button press to disable functionality
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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 (!TasmotaGlobal.restart_flag && !Button.hold_timer[button_index] && (Button.press_counter[button_index] > 0) && (Button.press_counter[button_index] < 7)) {
|
|
||||||
|
|
||||||
bool single_press = false;
|
|
||||||
if (Button.press_counter[button_index] < 3) { // Single or Double press
|
|
||||||
#ifdef ESP8266
|
|
||||||
if ((SONOFF_DUAL_R2 == TasmotaGlobal.module_type) || (SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.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 == TasmotaGlobal.devices_present)) { // Single Button with two devices only
|
|
||||||
if (Settings->flag.button_swap) { // SetOption11 (0)
|
|
||||||
Button.press_counter[button_index] = (single_press) ? 1 : 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
XdrvMailbox.index = button_index;
|
|
||||||
XdrvMailbox.payload = Button.press_counter[button_index];
|
|
||||||
if (XdrvCall(FUNC_BUTTON_MULTI_PRESSED)) {
|
|
||||||
// Serviced
|
|
||||||
} else
|
|
||||||
|
|
||||||
#ifdef ROTARY_V1
|
|
||||||
if (!RotaryButtonPressed(button_index)) {
|
|
||||||
#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
|
|
||||||
// TasmotaGlobal.restart_flag = 1;
|
|
||||||
// }
|
|
||||||
if (!Settings->flag3.mqtt_buttons) { // SetOption73 - Detach buttons from relays and enable MQTT action state for multipress
|
|
||||||
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) { // BUTTON1 can toggle up to 5 relays if present. If a relay is not present will send out the key value (2,11,12,13 and 14) for rules
|
|
||||||
bool valid_relay = PinUsed(GPIO_REL1, Button.press_counter[button_index]-1);
|
|
||||||
#ifdef ESP8266
|
|
||||||
if ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type)) {
|
|
||||||
valid_relay = (Button.press_counter[button_index] <= TasmotaGlobal.devices_present);
|
|
||||||
}
|
|
||||||
#endif // ESP8266
|
|
||||||
if ((Button.press_counter[button_index] > 1) && valid_relay && (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(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 press start wificonfig 2
|
|
||||||
if (!Settings->flag.button_restrict) { // SetOption1 - Control button multipress
|
|
||||||
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_WIFICONFIG " 2"));
|
|
||||||
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) {
|
|
||||||
MqttButtonTopic(button_index +1, Button.press_counter[button_index], 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef ROTARY_V1
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Button.press_counter[button_index] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Button.last_state[button_index] = button;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
void MqttButtonTopic(uint8_t button_id, uint8_t action, uint8_t hold) {
|
|
||||||
char scommand[CMDSZ];
|
|
||||||
char stopic[TOPSZ];
|
|
||||||
char mqttstate[7];
|
|
||||||
|
|
||||||
SendKey(KEY_BUTTON, button_id, (hold) ? 3 : action +9);
|
|
||||||
|
|
||||||
if (!Settings->flag.hass_discovery) {
|
|
||||||
GetTextIndexed(mqttstate, sizeof(mqttstate), action, kMultiPress);
|
|
||||||
snprintf_P(scommand, sizeof(scommand), PSTR("BUTTON%d"), button_id);
|
|
||||||
GetTopic_P(stopic, STAT, TasmotaGlobal.mqtt_topic, scommand);
|
|
||||||
Response_P(S_JSON_COMMAND_SVALUE, "ACTION", (hold) ? SettingsText(SET_STATE_TXT4) : mqttstate);
|
|
||||||
MqttPublish(stopic);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void MqttButtonTopic(uint32_t button_id, uint32_t action, uint32_t hold) {
|
|
||||||
SendKey(KEY_BUTTON, button_id, (hold) ? 3 : action +9);
|
|
||||||
|
|
||||||
if (!Settings->flag.hass_discovery) { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59)
|
|
||||||
char scommand[10];
|
|
||||||
snprintf_P(scommand, sizeof(scommand), PSTR(D_JSON_BUTTON "%d"), button_id);
|
|
||||||
char mqttstate[7];
|
|
||||||
Response_P(S_JSON_SVALUE_ACTION_SVALUE, scommand, (hold) ? SettingsText(SET_STATE_TXT4) : GetTextIndexed(mqttstate, sizeof(mqttstate), action, kMultiPress));
|
|
||||||
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonLoop(void) {
|
|
||||||
if (Button.present) {
|
|
||||||
if (TimeReached(Button.debounce)) {
|
|
||||||
SetNextTimeInterval(Button.debounce, Settings->button_debounce); // ButtonDebounce (50)
|
|
||||||
ButtonHandler();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // BUTTON_V2
|
|
@ -1,536 +0,0 @@
|
|||||||
/*
|
|
||||||
support_button.ino - button support for Tasmota
|
|
||||||
|
|
||||||
Copyright (C) 2022 Federico Leoni and 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//#define BUTTON_V3
|
|
||||||
#ifdef BUTTON_V3
|
|
||||||
/*********************************************************************************************\
|
|
||||||
* Button support with input filter
|
|
||||||
*
|
|
||||||
* Inspired by (https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/master/olimex/user/user_switch2.c)
|
|
||||||
\*********************************************************************************************/
|
|
||||||
|
|
||||||
#define MAX_RELAY_BUTTON1 5 // Max number of relay controlled by BUTTON1
|
|
||||||
|
|
||||||
const uint8_t BUTTON_PROBE_INTERVAL = 10; // Time in milliseconds between button input probe
|
|
||||||
const uint8_t BUTTON_FAST_PROBE_INTERVAL = 2; // Time in milliseconds between button input probe for AC detection
|
|
||||||
const uint8_t BUTTON_AC_PERIOD = (20 + BUTTON_FAST_PROBE_INTERVAL - 1) / BUTTON_FAST_PROBE_INTERVAL; // Duration of an AC wave in probe intervals
|
|
||||||
|
|
||||||
const char kMultiPress[] PROGMEM = "|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|CLEAR|";
|
|
||||||
|
|
||||||
#include <Ticker.h>
|
|
||||||
|
|
||||||
Ticker TickerButton;
|
|
||||||
|
|
||||||
struct BUTTON {
|
|
||||||
uint32_t debounce = 0; // Button debounce timer
|
|
||||||
uint32_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup)
|
|
||||||
uint32_t pulldown_mask = 0; // key pulldown flag (1 = pulldown)
|
|
||||||
uint32_t inverted_mask = 0; // Key inverted flag (1 = inverted)
|
|
||||||
uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold
|
|
||||||
uint16_t dual_code = 0; // Sonoff dual received code
|
|
||||||
uint8_t state[MAX_KEYS] = { 0 };
|
|
||||||
uint8_t last_state[MAX_KEYS]; // Last button states
|
|
||||||
uint8_t virtual_state[MAX_KEYS]; // Virtual 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 first_change = 0;
|
|
||||||
uint8_t present = 0; // Number of buttons found flag
|
|
||||||
uint8_t mutex;
|
|
||||||
} Button;
|
|
||||||
|
|
||||||
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
|
|
||||||
struct TOUCH_BUTTON {
|
|
||||||
uint32_t touch_mask = 0; // Touch flag (1 = enabled)
|
|
||||||
uint32_t calibration = 0; // Bitfield
|
|
||||||
uint8_t hits[MAX_KEYS] = { 0 }; // Hits in a row to filter out noise
|
|
||||||
} TouchButton;
|
|
||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
|
||||||
|
|
||||||
/********************************************************************************************/
|
|
||||||
|
|
||||||
void ButtonPullupFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.no_pullup_mask, button_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonPulldownFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.pulldown_mask, button_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonInvertFlag(uint32_t button_bit) {
|
|
||||||
bitSet(Button.inverted_mask, button_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
|
|
||||||
void ButtonTouchFlag(uint32_t button_bit) {
|
|
||||||
bitSet(TouchButton.touch_mask, button_bit);
|
|
||||||
}
|
|
||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
|
||||||
|
|
||||||
/*********************************************************************************************/
|
|
||||||
|
|
||||||
void ButtonProbe(void) {
|
|
||||||
if (Button.mutex || (TasmotaGlobal.uptime < 4)) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
|
||||||
Button.mutex = 1;
|
|
||||||
|
|
||||||
uint32_t state_filter;
|
|
||||||
uint32_t first_change = Button.first_change;
|
|
||||||
uint32_t debounce_flags = Settings->button_debounce % 10;
|
|
||||||
bool force_high = (debounce_flags &1); // 51, 101, 151 etc
|
|
||||||
bool force_low = (debounce_flags &2); // 52, 102, 152 etc
|
|
||||||
bool ac_detect = (debounce_flags == 9); // 39, 49, 59 etc
|
|
||||||
|
|
||||||
if (ac_detect) {
|
|
||||||
if (Settings->button_debounce < 2 * BUTTON_AC_PERIOD * BUTTON_FAST_PROBE_INTERVAL + 9) {
|
|
||||||
state_filter = 2 * BUTTON_AC_PERIOD;
|
|
||||||
} else if (Settings->button_debounce > (0x7f - 2 * BUTTON_AC_PERIOD) * BUTTON_FAST_PROBE_INTERVAL) {
|
|
||||||
state_filter = 0x7f;
|
|
||||||
} else {
|
|
||||||
state_filter = (Settings->button_debounce - 9) / BUTTON_FAST_PROBE_INTERVAL;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state_filter = Settings->button_debounce / BUTTON_PROBE_INTERVAL; // 5, 10, 15
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < MAX_KEYS; i++) {
|
|
||||||
if (!PinUsed(GPIO_KEY1, i)) { continue; }
|
|
||||||
|
|
||||||
bool button_not_activated;
|
|
||||||
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
|
|
||||||
if (bitRead(TouchButton.touch_mask, i)) {
|
|
||||||
if (ac_detect || bitRead(TouchButton.calibration, i +1)) { continue; } // Touch is slow. Takes 21mS to read
|
|
||||||
uint32_t value = touchRead(Pin(GPIO_KEY1, i));
|
|
||||||
#ifdef SOC_TOUCH_VERSION_2
|
|
||||||
button_not_activated = (value < Settings->touch_threshold); // ESPS3 No touch = 24200, Touch > 40000
|
|
||||||
#else
|
|
||||||
button_not_activated = ((value == 0) || (value > Settings->touch_threshold)); // ESP32 No touch = 74, Touch < 40
|
|
||||||
#endif
|
|
||||||
} else
|
|
||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
|
||||||
button_not_activated = (digitalRead(Pin(GPIO_KEY1, i)) != bitRead(Button.inverted_mask, i));
|
|
||||||
|
|
||||||
if (button_not_activated) {
|
|
||||||
|
|
||||||
if (ac_detect) { // Enabled with ButtonDebounce x9
|
|
||||||
Button.state[i] |= 0x80;
|
|
||||||
if (Button.state[i] > 0x80) {
|
|
||||||
Button.state[i]--;
|
|
||||||
if (0x80 == Button.state[i]) {
|
|
||||||
Button.virtual_state[i] = 0;
|
|
||||||
Button.first_change = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (force_high) { // Enabled with ButtonDebounce x1
|
|
||||||
if (1 == Button.virtual_state[i]) {
|
|
||||||
Button.state[i] = state_filter; // With noisy input keep current state 1 unless constant 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Button.state[i] < state_filter) {
|
|
||||||
Button.state[i]++;
|
|
||||||
if (state_filter == Button.state[i]) {
|
|
||||||
Button.virtual_state[i] = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (ac_detect) { // Enabled with ButtonDebounce x9
|
|
||||||
/*
|
|
||||||
* Moes MS-104B and similar devices using an AC detection circuitry
|
|
||||||
* on their switch inputs generating an ~4 ms long low pulse every
|
|
||||||
* AC wave. We start the time measurement on the falling edge.
|
|
||||||
*
|
|
||||||
* state: bit7: previous state, bit6..0: counter
|
|
||||||
*/
|
|
||||||
if (Button.state[i] & 0x80) {
|
|
||||||
Button.state[i] &= 0x7f;
|
|
||||||
if (Button.state[i] < state_filter - 2 * BUTTON_AC_PERIOD) {
|
|
||||||
Button.state[i] += 2 * BUTTON_AC_PERIOD;
|
|
||||||
} else {
|
|
||||||
Button.state[i] = state_filter;
|
|
||||||
Button.virtual_state[i] = 1;
|
|
||||||
if (first_change) {
|
|
||||||
Button.last_state[i] = 1;
|
|
||||||
Button.first_change = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Button.state[i] > 0x00) {
|
|
||||||
Button.state[i]--;
|
|
||||||
if (0x00 == Button.state[i]) {
|
|
||||||
Button.virtual_state[i] = 0;
|
|
||||||
Button.first_change = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (force_low) { // Enabled with ButtonDebounce x2
|
|
||||||
if (0 == Button.virtual_state[i]) {
|
|
||||||
Button.state[i] = 0; // With noisy input keep current state 0 unless constant 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Button.state[i] > 0) {
|
|
||||||
Button.state[i]--;
|
|
||||||
if (0 == Button.state[i]) {
|
|
||||||
Button.virtual_state[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Button.mutex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonInit(void) {
|
|
||||||
bool ac_detect = (Settings->button_debounce % 10 == 9);
|
|
||||||
|
|
||||||
Button.present = 0;
|
|
||||||
#ifdef ESP8266
|
|
||||||
if ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type)) {
|
|
||||||
Button.present++;
|
|
||||||
}
|
|
||||||
#endif // ESP8266
|
|
||||||
for (uint32_t i = 0; i < MAX_KEYS; i++) {
|
|
||||||
Button.last_state[i] = NOT_PRESSED;
|
|
||||||
if (PinUsed(GPIO_KEY1, i)) {
|
|
||||||
Button.present++;
|
|
||||||
#ifdef ESP8266
|
|
||||||
pinMode(Pin(GPIO_KEY1, i), bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_KEY1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
|
||||||
#endif // ESP8266
|
|
||||||
#ifdef ESP32
|
|
||||||
pinMode(Pin(GPIO_KEY1, i), bitRead(Button.pulldown_mask, i) ? INPUT_PULLDOWN : bitRead(Button.no_pullup_mask, i) ? INPUT : INPUT_PULLUP);
|
|
||||||
#endif // ESP32
|
|
||||||
if (ac_detect) {
|
|
||||||
Button.state[i] = 0x80 + 2 * BUTTON_AC_PERIOD;
|
|
||||||
Button.last_state[i] = 0; // Will set later in the debouncing code
|
|
||||||
} else {
|
|
||||||
// Set global now so doesn't change the saved power state on first button check
|
|
||||||
Button.last_state[i] = (digitalRead(Pin(GPIO_KEY1, i)) != bitRead(Button.inverted_mask, i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef USE_ADC
|
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON, i) || PinUsed(GPIO_ADC_BUTTON_INV, i)) {
|
|
||||||
Button.present++;
|
|
||||||
}
|
|
||||||
#endif // USE_ADC
|
|
||||||
Button.virtual_state[i] = Button.last_state[i];
|
|
||||||
}
|
|
||||||
if (Button.present) {
|
|
||||||
Button.first_change = true;
|
|
||||||
TickerButton.attach_ms((ac_detect) ? BUTTON_FAST_PROBE_INTERVAL : BUTTON_PROBE_INTERVAL, ButtonProbe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 commands WifiConfig and Reset
|
|
||||||
* SetOption11 (0) - If set perform single press action on double press and reverse (on two relay devices only)
|
|
||||||
* SetOption13 (0) - If set act on single press only
|
|
||||||
* SetOption32 (40) - Button held for factor times longer
|
|
||||||
* SetOption40 (0) - Do not ignore button hold
|
|
||||||
* SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
\*********************************************************************************************/
|
|
||||||
|
|
||||||
void ButtonHandler(void) {
|
|
||||||
if (TasmotaGlobal.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];
|
|
||||||
|
|
||||||
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 == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type))) {
|
|
||||||
button_present = 1;
|
|
||||||
if (Button.dual_code) {
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: 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 (PinUsed(GPIO_KEY1, button_index)) {
|
|
||||||
|
|
||||||
#if defined(SOC_TOUCH_VERSION_1) || defined(SOC_TOUCH_VERSION_2)
|
|
||||||
if (bitRead(TouchButton.touch_mask, button_index) && bitRead(TouchButton.calibration, button_index +1)) { // Touch
|
|
||||||
uint32_t _value = touchRead(Pin(GPIO_KEY1, button_index));
|
|
||||||
#ifdef SOC_TOUCH_VERSION_2
|
|
||||||
if (_value > Settings->touch_threshold) { // ESPS3 No touch = 24200, Touch = 100000
|
|
||||||
#else
|
|
||||||
if ((_value > 0) && (_value < Settings->touch_threshold)) { // ESP32 No touch = 74, Touch = 20 (Probably read-error (0))
|
|
||||||
#endif
|
|
||||||
TouchButton.hits[button_index]++;
|
|
||||||
} else {
|
|
||||||
TouchButton.hits[button_index] = 0;
|
|
||||||
}
|
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("PLOT: %u, %u, %u,"), button_index +1, _value, TouchButton.hits[button_index]); // Button number (1..4), value, continuous hits under threshold
|
|
||||||
continue;
|
|
||||||
} else
|
|
||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
|
||||||
|
|
||||||
button_present = 1;
|
|
||||||
button = Button.virtual_state[button_index];
|
|
||||||
}
|
|
||||||
#ifdef USE_ADC
|
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON, button_index)) {
|
|
||||||
button_present = 1;
|
|
||||||
button = AdcGetButton(Pin(GPIO_ADC_BUTTON, button_index));
|
|
||||||
}
|
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON_INV, button_index)) {
|
|
||||||
button_present = 1;
|
|
||||||
button = AdcGetButton(Pin(GPIO_ADC_BUTTON_INV, button_index));
|
|
||||||
}
|
|
||||||
#endif // USE_ADC
|
|
||||||
if (button_present) {
|
|
||||||
XdrvMailbox.index = button_index;
|
|
||||||
XdrvMailbox.payload = button;
|
|
||||||
if (XdrvCall(FUNC_BUTTON_PRESSED)) {
|
|
||||||
// Serviced
|
|
||||||
}
|
|
||||||
#ifdef ESP8266
|
|
||||||
else if (SONOFF_4CHPRO == TasmotaGlobal.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(LOG_LEVEL_DEBUG, PSTR("BTN: Button%d level 1-0"), 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(LOG_LEVEL_DEBUG, PSTR("BTN: Button%d level 0-1"), button_index +1);
|
|
||||||
if (!Button.hold_timer[button_index]) { button_pressed = true; } // Do not allow within 1 second
|
|
||||||
}
|
|
||||||
if (button_pressed) {
|
|
||||||
if (!Settings->flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
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 {
|
|
||||||
MqttButtonTopic(button_index +1, 1, 0); // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // ESP8266
|
|
||||||
else {
|
|
||||||
if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) {
|
|
||||||
|
|
||||||
if (Settings->flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action,
|
|
||||||
if (!Settings->flag3.mqtt_buttons) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Button%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 {
|
|
||||||
MqttButtonTopic(button_index +1, 1, 0); // SetOption73 1 - Decouple button from relay and send just mqtt topic
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Button.press_counter[button_index] = (Button.window_timer[button_index]) ? Button.press_counter[button_index] +1 : 1;
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Button%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
|
|
||||||
}
|
|
||||||
TasmotaGlobal.blinks = 201;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
Button.hold_timer[button_index] = 0;
|
|
||||||
if (Settings->flag3.mqtt_buttons && (PRESSED == Button.last_state[button_index]) && !Button.press_counter[button_index]) { // SetOption73 (0) - Decouple button from relay and send just mqtt topic
|
|
||||||
MqttButtonTopic(button_index +1, 6, 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
|
|
||||||
MqttButtonTopic(button_index +1, 3, 1);
|
|
||||||
} else {
|
|
||||||
SendKey(KEY_BUTTON, button_index +1, POWER_HOLD); // Execute Hold command via MQTT if ButtonTopic is set
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Settings->flag.button_restrict) { // SetOption1 (0) - Control button multipress
|
|
||||||
if (Settings->param[P_HOLD_IGNORE] > 0) { // SetOption40 (0) - Do not ignore button hold
|
|
||||||
if (Button.hold_timer[button_index] > loops_per_second * Settings->param[P_HOLD_IGNORE] / 10) {
|
|
||||||
Button.hold_timer[button_index] = 0; // Reset button hold counter to stay below hold trigger
|
|
||||||
Button.press_counter[button_index] = 0; // Discard button press to disable functionality
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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 (!TasmotaGlobal.restart_flag && !Button.hold_timer[button_index] && (Button.press_counter[button_index] > 0) && (Button.press_counter[button_index] < 7)) {
|
|
||||||
|
|
||||||
bool single_press = false;
|
|
||||||
if (Button.press_counter[button_index] < 3) { // Single or Double press
|
|
||||||
#ifdef ESP8266
|
|
||||||
if ((SONOFF_DUAL_R2 == TasmotaGlobal.module_type) || (SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.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 == TasmotaGlobal.devices_present)) { // Single Button with two devices only
|
|
||||||
if (Settings->flag.button_swap) { // SetOption11 (0)
|
|
||||||
Button.press_counter[button_index] = (single_press) ? 1 : 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
XdrvMailbox.index = button_index;
|
|
||||||
XdrvMailbox.payload = Button.press_counter[button_index];
|
|
||||||
if (XdrvCall(FUNC_BUTTON_MULTI_PRESSED)) {
|
|
||||||
// Serviced
|
|
||||||
} else
|
|
||||||
|
|
||||||
#ifdef ROTARY_V1
|
|
||||||
if (!RotaryButtonPressed(button_index)) {
|
|
||||||
#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
|
|
||||||
// TasmotaGlobal.restart_flag = 1;
|
|
||||||
// }
|
|
||||||
if (!Settings->flag3.mqtt_buttons) { // SetOption73 - Detach buttons from relays and enable MQTT action state for multipress
|
|
||||||
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) { // BUTTON1 can toggle up to 5 relays if present. If a relay is not present will send out the key value (2,11,12,13 and 14) for rules
|
|
||||||
bool valid_relay = PinUsed(GPIO_REL1, Button.press_counter[button_index]-1);
|
|
||||||
#ifdef ESP8266
|
|
||||||
if ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type)) {
|
|
||||||
valid_relay = (Button.press_counter[button_index] <= TasmotaGlobal.devices_present);
|
|
||||||
}
|
|
||||||
#endif // ESP8266
|
|
||||||
#ifdef USE_SHELLY_PRO
|
|
||||||
if (TasmotaGlobal.gpio_optiona.shelly_pro) {
|
|
||||||
valid_relay = (Button.press_counter[button_index] <= TasmotaGlobal.devices_present);
|
|
||||||
}
|
|
||||||
#endif // USE_SHELLY_PRO
|
|
||||||
if ((Button.press_counter[button_index] > 1) && valid_relay && (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(LOG_LEVEL_DEBUG, PSTR("BTN: Relay%d found on GPIO%d"), Button.press_counter[button_index], Pin(GPIO_REL1, Button.press_counter[button_index]-1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else { // 6 press start wificonfig 2
|
|
||||||
if (!Settings->flag.button_restrict) { // SetOption1 - Control button multipress
|
|
||||||
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_WIFICONFIG " 2"));
|
|
||||||
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) {
|
|
||||||
MqttButtonTopic(button_index +1, Button.press_counter[button_index], 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef ROTARY_V1
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Button.press_counter[button_index] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Button.last_state[button_index] = button;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MqttButtonTopic(uint32_t button_id, uint32_t action, uint32_t hold) {
|
|
||||||
SendKey(KEY_BUTTON, button_id, (hold) ? 3 : action +9);
|
|
||||||
|
|
||||||
if (!Settings->flag.hass_discovery) { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59)
|
|
||||||
char scommand[10];
|
|
||||||
snprintf_P(scommand, sizeof(scommand), PSTR(D_JSON_BUTTON "%d"), button_id);
|
|
||||||
char mqttstate[7];
|
|
||||||
Response_P(S_JSON_SVALUE_ACTION_SVALUE, scommand, (hold) ? SettingsText(SET_STATE_TXT4) : GetTextIndexed(mqttstate, sizeof(mqttstate), action, kMultiPress));
|
|
||||||
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonLoop(void) {
|
|
||||||
if (Button.present) {
|
|
||||||
if (TimeReached(Button.debounce)) {
|
|
||||||
SetNextTimeInterval(Button.debounce, Settings->button_debounce); // ButtonDebounce (50)
|
|
||||||
ButtonHandler();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // BUTTON_V3
|
|
@ -42,7 +42,7 @@ struct BUTTON {
|
|||||||
uint32_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup)
|
uint32_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup)
|
||||||
uint32_t pulldown_mask = 0; // key pulldown flag (1 = pulldown)
|
uint32_t pulldown_mask = 0; // key pulldown flag (1 = pulldown)
|
||||||
uint32_t inverted_mask = 0; // Key inverted flag (1 = inverted)
|
uint32_t inverted_mask = 0; // Key inverted flag (1 = inverted)
|
||||||
uint32_t virtual_pin_used = 0; // Key used bitmask
|
uint32_t used = 0; // Key used bitmask
|
||||||
uint32_t virtual_pin = 0; // Key state bitmask
|
uint32_t virtual_pin = 0; // Key state bitmask
|
||||||
uint16_t hold_timer[MAX_KEYS_SET] = { 0 }; // Timer for button hold
|
uint16_t hold_timer[MAX_KEYS_SET] = { 0 }; // Timer for button hold
|
||||||
uint16_t dual_code = 0; // Sonoff dual received code
|
uint16_t dual_code = 0; // Sonoff dual received code
|
||||||
@ -85,14 +85,24 @@ void ButtonTouchFlag(uint32_t button_bit) {
|
|||||||
}
|
}
|
||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
||||||
|
|
||||||
bool ButtonUsed(uint32_t index) {
|
/*------------------------------------------------------------------------------------------*/
|
||||||
return (PinUsed(GPIO_KEY1, index) || bitRead(Button.virtual_pin_used, index));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ButtonSetVirtualPinState(uint32_t index, uint32_t state) {
|
void ButtonSetVirtualPinState(uint32_t index, uint32_t state) {
|
||||||
|
// Set virtual pin state to be debounced as used by early detected buttons
|
||||||
bitWrite(Button.virtual_pin, index, state);
|
bitWrite(Button.virtual_pin, index, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t ButtonLastState(uint32_t index) {
|
||||||
|
// Get last state
|
||||||
|
return Button.last_state[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
bool ButtonUsed(uint32_t index) {
|
||||||
|
return (PinUsed(GPIO_KEY1, index) || bitRead(Button.used, index));
|
||||||
|
}
|
||||||
|
|
||||||
/*********************************************************************************************/
|
/*********************************************************************************************/
|
||||||
|
|
||||||
void ButtonProbe(void) {
|
void ButtonProbe(void) {
|
||||||
@ -134,7 +144,7 @@ void ButtonProbe(void) {
|
|||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
||||||
not_activated = (digitalRead(Pin(GPIO_KEY1, i)) != bitRead(Button.inverted_mask, i));
|
not_activated = (digitalRead(Pin(GPIO_KEY1, i)) != bitRead(Button.inverted_mask, i));
|
||||||
}
|
}
|
||||||
else if (bitRead(Button.virtual_pin_used, i)) {
|
else if (bitRead(Button.used, i)) {
|
||||||
not_activated = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i));
|
not_activated = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i));
|
||||||
}
|
}
|
||||||
else { continue; }
|
else { continue; }
|
||||||
@ -220,7 +230,7 @@ void ButtonInit(void) {
|
|||||||
bool ac_detect = (Settings->button_debounce % 10 == 9);
|
bool ac_detect = (Settings->button_debounce % 10 == 9);
|
||||||
|
|
||||||
Button.present = 0;
|
Button.present = 0;
|
||||||
Button.virtual_pin_used = 0;
|
Button.used = 0;
|
||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
if ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type)) {
|
if ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type)) {
|
||||||
@ -259,7 +269,7 @@ void ButtonInit(void) {
|
|||||||
XdrvMailbox.index bit 0 = current state
|
XdrvMailbox.index bit 0 = current state
|
||||||
*/
|
*/
|
||||||
Button.present++;
|
Button.present++;
|
||||||
bitSet(Button.virtual_pin_used, i); // This pin is used
|
bitSet(Button.used, i); // This pin is used
|
||||||
bool state = (XdrvMailbox.index &1);
|
bool state = (XdrvMailbox.index &1);
|
||||||
ButtonSetVirtualPinState(i, state); // Virtual hardware pin state
|
ButtonSetVirtualPinState(i, state); // Virtual hardware pin state
|
||||||
if (!state) { ButtonInvertFlag(i); } // Set inverted flag
|
if (!state) { ButtonInvertFlag(i); } // Set inverted flag
|
||||||
@ -279,7 +289,7 @@ void ButtonInit(void) {
|
|||||||
Button.debounced_state[i] = Button.last_state[i];
|
Button.debounced_state[i] = Button.last_state[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: vPinUsed %08X, State %08X, Invert %08X"), Button.virtual_pin_used, Button.virtual_pin, Button.inverted_mask);
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: vPinUsed %08X, State %08X, Invert %08X"), Button.used, Button.virtual_pin, Button.inverted_mask);
|
||||||
|
|
||||||
if (Button.present) {
|
if (Button.present) {
|
||||||
Button.first_change = true;
|
Button.first_change = true;
|
||||||
@ -377,7 +387,7 @@ void ButtonHandler(void) {
|
|||||||
button = AdcGetButton(Pin(GPIO_ADC_BUTTON_INV, button_index));
|
button = AdcGetButton(Pin(GPIO_ADC_BUTTON_INV, button_index));
|
||||||
}
|
}
|
||||||
#endif // USE_ADC
|
#endif // USE_ADC
|
||||||
else if (bitRead(Button.virtual_pin_used, button_index)) {
|
else if (bitRead(Button.used, button_index)) {
|
||||||
button_present = 1;
|
button_present = 1;
|
||||||
button = Button.debounced_state[button_index];
|
button = Button.debounced_state[button_index];
|
||||||
}
|
}
|
||||||
|
@ -1,454 +0,0 @@
|
|||||||
/*
|
|
||||||
support_switch.ino - switch support for Tasmota
|
|
||||||
|
|
||||||
Copyright (C) 2021 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//#define SWITCH_V3
|
|
||||||
#ifdef SWITCH_V3
|
|
||||||
/*********************************************************************************************\
|
|
||||||
* Switch support with input filter
|
|
||||||
*
|
|
||||||
* Inspired by (https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/master/olimex/user/user_switch2.c)
|
|
||||||
\*********************************************************************************************/
|
|
||||||
|
|
||||||
const uint8_t SWITCH_PROBE_INTERVAL = 10; // Time in milliseconds between switch input probe
|
|
||||||
const uint8_t SWITCH_FAST_PROBE_INTERVAL = 2; // Time in milliseconds between switch input probe for AC detection
|
|
||||||
const uint8_t AC_PERIOD = (20 + SWITCH_FAST_PROBE_INTERVAL - 1) / SWITCH_FAST_PROBE_INTERVAL; // Duration of an AC wave in probe intervals
|
|
||||||
|
|
||||||
// Switch Mode definietions
|
|
||||||
#define SM_TIMER_MASK 0x3F
|
|
||||||
#define SM_NO_TIMER_MASK 0xFF
|
|
||||||
#define SM_FIRST_PRESS 0x40
|
|
||||||
#define SM_SECOND_PRESS 0x80
|
|
||||||
#define POWER_NONE 99
|
|
||||||
|
|
||||||
const char kSwitchPressStates[] PROGMEM =
|
|
||||||
"||||POWER_INCREMENT|POWER_INV|POWER_CLEAR|POWER_RELEASE|POWER_100||POWER_DELAYED";
|
|
||||||
|
|
||||||
#include <Ticker.h>
|
|
||||||
|
|
||||||
Ticker TickerSwitch;
|
|
||||||
|
|
||||||
struct SWITCH {
|
|
||||||
uint32_t debounce = 0; // Switch debounce timer
|
|
||||||
uint32_t no_pullup_mask = 0; // Switch pull-up bitmask flags
|
|
||||||
uint32_t pulldown_mask = 0; // Switch pull-down bitmask flags
|
|
||||||
uint8_t state[MAX_SWITCHES] = { 0 };
|
|
||||||
uint8_t last_state[MAX_SWITCHES]; // Last wall switch states
|
|
||||||
uint8_t hold_timer[MAX_SWITCHES] = { 0 }; // Timer for wallswitch push button hold
|
|
||||||
uint8_t virtual_state[MAX_SWITCHES]; // Virtual switch states
|
|
||||||
uint8_t first_change = 0;
|
|
||||||
uint8_t present = 0;
|
|
||||||
} Switch;
|
|
||||||
|
|
||||||
/********************************************************************************************/
|
|
||||||
|
|
||||||
void SwitchPullupFlag(uint32 switch_bit) {
|
|
||||||
bitSet(Switch.no_pullup_mask, switch_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwitchPulldownFlag(uint32 switch_bit) {
|
|
||||||
bitSet(Switch.pulldown_mask, switch_bit);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwitchSetVirtual(uint32_t index, uint32_t state) {
|
|
||||||
Switch.virtual_state[index] = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t SwitchGetVirtual(uint32_t index) {
|
|
||||||
return Switch.virtual_state[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t SwitchLastState(uint32_t index) {
|
|
||||||
return Switch.last_state[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SwitchState(uint32_t index) {
|
|
||||||
uint32_t switchmode = Settings->switchmode[index];
|
|
||||||
return ((FOLLOW_INV == switchmode) ||
|
|
||||||
(PUSHBUTTON_INV == switchmode) ||
|
|
||||||
(PUSHBUTTONHOLD_INV == switchmode) ||
|
|
||||||
(FOLLOWMULTI_INV == switchmode) ||
|
|
||||||
(PUSHHOLDMULTI_INV == switchmode) ||
|
|
||||||
(PUSHON_INV == switchmode) ||
|
|
||||||
(PUSH_IGNORE_INV == switchmode)
|
|
||||||
) ^ Switch.last_state[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************************************************************/
|
|
||||||
|
|
||||||
void SwitchProbe(void) {
|
|
||||||
if (TasmotaGlobal.uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
|
||||||
|
|
||||||
uint32_t state_filter;
|
|
||||||
uint32_t first_change = Switch.first_change;
|
|
||||||
uint32_t debounce_flags = Settings->switch_debounce % 10;
|
|
||||||
bool force_high = (debounce_flags &1); // 51, 101, 151 etc
|
|
||||||
bool force_low = (debounce_flags &2); // 52, 102, 152 etc
|
|
||||||
bool ac_detect = (debounce_flags == 9);
|
|
||||||
|
|
||||||
if (ac_detect) {
|
|
||||||
if (Settings->switch_debounce < 2 * AC_PERIOD * SWITCH_FAST_PROBE_INTERVAL + 9) {
|
|
||||||
state_filter = 2 * AC_PERIOD;
|
|
||||||
} else if (Settings->switch_debounce > (0x7f - 2 * AC_PERIOD) * SWITCH_FAST_PROBE_INTERVAL) {
|
|
||||||
state_filter = 0x7f;
|
|
||||||
} else {
|
|
||||||
state_filter = (Settings->switch_debounce - 9) / SWITCH_FAST_PROBE_INTERVAL;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state_filter = Settings->switch_debounce / SWITCH_PROBE_INTERVAL; // 5, 10, 15
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
|
||||||
if (!PinUsed(GPIO_SWT1, i)) { continue; }
|
|
||||||
|
|
||||||
// Olimex user_switch2.c code to fix 50Hz induced pulses
|
|
||||||
if (1 == digitalRead(Pin(GPIO_SWT1, i))) {
|
|
||||||
|
|
||||||
if (ac_detect) { // Enabled with SwitchDebounce x9
|
|
||||||
Switch.state[i] |= 0x80;
|
|
||||||
if (Switch.state[i] > 0x80) {
|
|
||||||
Switch.state[i]--;
|
|
||||||
if (0x80 == Switch.state[i]) {
|
|
||||||
Switch.virtual_state[i] = 0;
|
|
||||||
Switch.first_change = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (force_high) { // Enabled with SwitchDebounce x1
|
|
||||||
if (1 == Switch.virtual_state[i]) {
|
|
||||||
Switch.state[i] = state_filter; // With noisy input keep current state 1 unless constant 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Switch.state[i] < state_filter) {
|
|
||||||
Switch.state[i]++;
|
|
||||||
if (state_filter == Switch.state[i]) {
|
|
||||||
Switch.virtual_state[i] = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (ac_detect) { // Enabled with SwitchDebounce x9
|
|
||||||
/*
|
|
||||||
* Moes MS-104B and similar devices using an AC detection circuitry
|
|
||||||
* on their switch inputs generating an ~4 ms long low pulse every
|
|
||||||
* AC wave. We start the time measurement on the falling edge.
|
|
||||||
*
|
|
||||||
* state: bit7: previous state, bit6..0: counter
|
|
||||||
*/
|
|
||||||
if (Switch.state[i] & 0x80) {
|
|
||||||
Switch.state[i] &= 0x7f;
|
|
||||||
if (Switch.state[i] < state_filter - 2 * AC_PERIOD) {
|
|
||||||
Switch.state[i] += 2 * AC_PERIOD;
|
|
||||||
} else {
|
|
||||||
Switch.state[i] = state_filter;
|
|
||||||
Switch.virtual_state[i] = 1;
|
|
||||||
if (first_change) {
|
|
||||||
Switch.last_state[i] = 1;
|
|
||||||
Switch.first_change = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Switch.state[i] > 0x00) {
|
|
||||||
Switch.state[i]--;
|
|
||||||
if (0x00 == Switch.state[i]) {
|
|
||||||
Switch.virtual_state[i] = 0;
|
|
||||||
Switch.first_change = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (force_low) { // Enabled with SwitchDebounce x2
|
|
||||||
if (0 == Switch.virtual_state[i]) {
|
|
||||||
Switch.state[i] = 0; // With noisy input keep current state 0 unless constant 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Switch.state[i] > 0) {
|
|
||||||
Switch.state[i]--;
|
|
||||||
if (0 == Switch.state[i]) {
|
|
||||||
Switch.virtual_state[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwitchInit(void) {
|
|
||||||
bool ac_detect = (Settings->switch_debounce % 10 == 9);
|
|
||||||
|
|
||||||
Switch.present = 0;
|
|
||||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
|
||||||
Switch.last_state[i] = NOT_PRESSED; // Init global to virtual switch state;
|
|
||||||
if (PinUsed(GPIO_SWT1, i)) {
|
|
||||||
Switch.present++;
|
|
||||||
#ifdef ESP8266
|
|
||||||
pinMode(Pin(GPIO_SWT1, i), bitRead(Switch.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_SWT1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
|
||||||
#endif // ESP8266
|
|
||||||
#ifdef ESP32
|
|
||||||
pinMode(Pin(GPIO_SWT1, i), bitRead(Switch.pulldown_mask, i) ? INPUT_PULLDOWN : bitRead(Switch.no_pullup_mask, i) ? INPUT : INPUT_PULLUP);
|
|
||||||
#endif // ESP32
|
|
||||||
if (ac_detect) {
|
|
||||||
Switch.state[i] = 0x80 + 2 * AC_PERIOD;
|
|
||||||
Switch.last_state[i] = 0; // Will set later in the debouncing code
|
|
||||||
} else {
|
|
||||||
Switch.last_state[i] = digitalRead(Pin(GPIO_SWT1, i)); // Set global now so doesn't change the saved power state on first switch check
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Switch.virtual_state[i] = Switch.last_state[i];
|
|
||||||
}
|
|
||||||
if (Switch.present) {
|
|
||||||
Switch.first_change = true;
|
|
||||||
TickerSwitch.attach_ms((ac_detect) ? SWITCH_FAST_PROBE_INTERVAL : SWITCH_PROBE_INTERVAL, SwitchProbe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************************************************************\
|
|
||||||
* Switch handler
|
|
||||||
\*********************************************************************************************/
|
|
||||||
|
|
||||||
void SwitchHandler(uint32_t mode) {
|
|
||||||
if (TasmotaGlobal.uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
|
||||||
|
|
||||||
uint32_t loops_per_second = 1000 / Settings->switch_debounce;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
|
||||||
if (PinUsed(GPIO_SWT1, i) || (mode)) {
|
|
||||||
uint32_t button = Switch.virtual_state[i];
|
|
||||||
uint32_t switchflag = POWER_TOGGLE +1;
|
|
||||||
uint32_t mqtt_action = POWER_NONE;
|
|
||||||
uint32_t switchmode = Settings->switchmode[i];
|
|
||||||
|
|
||||||
if (Switch.hold_timer[i] & (((switchmode == PUSHHOLDMULTI) | (switchmode == PUSHHOLDMULTI_INV)) ? SM_TIMER_MASK: SM_NO_TIMER_MASK)) {
|
|
||||||
Switch.hold_timer[i]--;
|
|
||||||
if ((Switch.hold_timer[i] & SM_TIMER_MASK) == loops_per_second * Settings->param[P_HOLD_TIME] / 25) {
|
|
||||||
if ((switchmode == PUSHHOLDMULTI) | (switchmode == PUSHHOLDMULTI_INV)){
|
|
||||||
if (((switchmode == PUSHHOLDMULTI) & (NOT_PRESSED == Switch.last_state[i])) | ((switchmode == PUSHHOLDMULTI_INV) & (PRESSED == Switch.last_state[i]))) {
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT
|
|
||||||
}
|
|
||||||
else if ((Switch.hold_timer[i] & ~SM_TIMER_MASK) == SM_FIRST_PRESS) {
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_DELAYED); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_DELAYED;
|
|
||||||
Switch.hold_timer[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (0 == (Switch.hold_timer[i] & (((switchmode == PUSHHOLDMULTI) | (switchmode == PUSHHOLDMULTI_INV)) ? SM_TIMER_MASK: SM_NO_TIMER_MASK))) {
|
|
||||||
switch (switchmode) {
|
|
||||||
case TOGGLEMULTI:
|
|
||||||
switchflag = POWER_TOGGLE; // Toggle after hold
|
|
||||||
break;
|
|
||||||
case FOLLOWMULTI:
|
|
||||||
switchflag = button &1; // Follow wall switch state after hold
|
|
||||||
break;
|
|
||||||
case FOLLOWMULTI_INV:
|
|
||||||
switchflag = ~button &1; // Follow inverted wall switch state after hold
|
|
||||||
break;
|
|
||||||
case PUSHHOLDMULTI:
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
Switch.hold_timer[i] = loops_per_second * Settings->param[P_HOLD_TIME] / 25;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_INCREMENT;
|
|
||||||
} else {
|
|
||||||
Switch.hold_timer[i]= 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_CLEAR;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHHOLDMULTI_INV:
|
|
||||||
if (PRESSED == button) {
|
|
||||||
Switch.hold_timer[i] = loops_per_second * Settings->param[P_HOLD_TIME] / 25;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_INCREMENT;
|
|
||||||
} else {
|
|
||||||
Switch.hold_timer[i]= 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_CLEAR;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_HOLD); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_HOLD;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (button != Switch.last_state[i]) { // This implies if ((PRESSED == button) then (NOT_PRESSED == Switch.last_state[i]))
|
|
||||||
switch (switchmode) {
|
|
||||||
case TOGGLE:
|
|
||||||
case PUSHBUTTON_TOGGLE:
|
|
||||||
switchflag = POWER_TOGGLE; // Toggle
|
|
||||||
break;
|
|
||||||
case FOLLOW:
|
|
||||||
switchflag = button &1; // Follow wall switch state
|
|
||||||
break;
|
|
||||||
case FOLLOW_INV:
|
|
||||||
switchflag = ~button &1; // Follow inverted wall switch state
|
|
||||||
break;
|
|
||||||
case PUSHBUTTON:
|
|
||||||
if (PRESSED == button) {
|
|
||||||
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHBUTTON_INV:
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
switchflag = POWER_TOGGLE; // Toggle with releasing pushbutton from Gnd
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHBUTTONHOLD:
|
|
||||||
if (PRESSED == button) {
|
|
||||||
Switch.hold_timer[i] = loops_per_second * Settings->param[P_HOLD_TIME] / 10; // Start timer on button press
|
|
||||||
}
|
|
||||||
if ((NOT_PRESSED == button) && (Switch.hold_timer[i])) {
|
|
||||||
Switch.hold_timer[i] = 0; // Button released and hold timer not expired : stop timer...
|
|
||||||
switchflag = POWER_TOGGLE; // ...and Toggle
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHBUTTONHOLD_INV:
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
Switch.hold_timer[i] = loops_per_second * Settings->param[P_HOLD_TIME] / 10; // Start timer on button press...
|
|
||||||
}
|
|
||||||
if ((PRESSED == button) && (Switch.hold_timer[i])) {
|
|
||||||
Switch.hold_timer[i] = 0; // Button released and hold timer not expired : stop timer.
|
|
||||||
switchflag = POWER_TOGGLE; // ...and Toggle
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TOGGLEMULTI:
|
|
||||||
case FOLLOWMULTI:
|
|
||||||
case FOLLOWMULTI_INV:
|
|
||||||
if (Switch.hold_timer[i]) {
|
|
||||||
Switch.hold_timer[i] = 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_HOLD); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_HOLD;
|
|
||||||
} else {
|
|
||||||
Switch.hold_timer[i] = loops_per_second / 2; // 0.5 second multi press window
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHHOLDMULTI:
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
if ((Switch.hold_timer[i] & SM_TIMER_MASK) != 0) {
|
|
||||||
Switch.hold_timer[i] = ((Switch.hold_timer[i] & ~SM_TIMER_MASK) == SM_FIRST_PRESS) ? SM_SECOND_PRESS : 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_INV;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ((Switch.hold_timer[i] & SM_TIMER_MASK) > loops_per_second * Settings->param[P_HOLD_TIME] / 25) {
|
|
||||||
if ((Switch.hold_timer[i] & ~SM_TIMER_MASK) != SM_SECOND_PRESS) {
|
|
||||||
Switch.hold_timer[i]= SM_FIRST_PRESS;
|
|
||||||
switchflag = POWER_TOGGLE; // Toggle with pushbutton
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_100); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_100;
|
|
||||||
Switch.hold_timer[i]= 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Switch.hold_timer[i]= 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_RELEASE); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_RELEASE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Switch.hold_timer[i] = (Switch.hold_timer[i] & ~SM_TIMER_MASK) | loops_per_second * Settings->param[P_HOLD_TIME] / 10;
|
|
||||||
break;
|
|
||||||
case PUSHHOLDMULTI_INV:
|
|
||||||
if (PRESSED == button) {
|
|
||||||
if ((Switch.hold_timer[i] & SM_TIMER_MASK) != 0) {
|
|
||||||
Switch.hold_timer[i] = ((Switch.hold_timer[i] & ~SM_TIMER_MASK) == SM_FIRST_PRESS) ? SM_SECOND_PRESS : 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_INV;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ((Switch.hold_timer[i] & SM_TIMER_MASK)> loops_per_second * Settings->param[P_HOLD_TIME] / 25) {
|
|
||||||
if ((Switch.hold_timer[i] & ~SM_TIMER_MASK) != SM_SECOND_PRESS) {
|
|
||||||
Switch.hold_timer[i]= SM_FIRST_PRESS;
|
|
||||||
switchflag = POWER_TOGGLE; // Toggle with pushbutton
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_100); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_100;
|
|
||||||
Switch.hold_timer[i]= 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Switch.hold_timer[i]= 0;
|
|
||||||
SendKey(KEY_SWITCH, i +1, POWER_RELEASE); // Execute command via MQTT
|
|
||||||
mqtt_action = POWER_RELEASE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Switch.hold_timer[i] = (Switch.hold_timer[i] & ~SM_TIMER_MASK) | loops_per_second * Settings->param[P_HOLD_TIME] / 10;
|
|
||||||
break;
|
|
||||||
case PUSHON:
|
|
||||||
if (PRESSED == button) {
|
|
||||||
switchflag = POWER_ON; // Power ON with pushbutton to Gnd
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHON_INV:
|
|
||||||
if (NOT_PRESSED == button) {
|
|
||||||
switchflag = POWER_ON; // Power ON with releasing pushbutton from Gnd
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSH_IGNORE:
|
|
||||||
case PUSH_IGNORE_INV:
|
|
||||||
Switch.last_state[i] = button; // Update switch state before publishing
|
|
||||||
MqttPublishSensor();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Switch.last_state[i] = button;
|
|
||||||
}
|
|
||||||
if (switchflag <= POWER_TOGGLE) {
|
|
||||||
if (!Settings->flag5.mqtt_switches) { // SetOption114 (0) - Detach Switches from relays and enable MQTT action state for all the SwitchModes
|
|
||||||
if (!SendKey(KEY_SWITCH, i +1, switchflag)) { // Execute command via MQTT
|
|
||||||
ExecuteCommandPower(i +1, switchflag, SRC_SWITCH); // Execute command internally (if i < TasmotaGlobal.devices_present)
|
|
||||||
}
|
|
||||||
} else { mqtt_action = switchflag; }
|
|
||||||
}
|
|
||||||
if ((mqtt_action != POWER_NONE) && Settings->flag5.mqtt_switches) { // SetOption114 (0) - Detach Switches from relays and enable MQTT action state for all the SwitchModes
|
|
||||||
if (!Settings->flag.hass_discovery) { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59)
|
|
||||||
char mqtt_state_str[16];
|
|
||||||
char *mqtt_state = mqtt_state_str;
|
|
||||||
if (mqtt_action <= 3) {
|
|
||||||
if (mqtt_action != 3) { SendKey(KEY_SWITCH, i +1, mqtt_action); }
|
|
||||||
mqtt_state = SettingsText(SET_STATE_TXT1 + mqtt_action);
|
|
||||||
} else {
|
|
||||||
GetTextIndexed(mqtt_state_str, sizeof(mqtt_state_str), mqtt_action, kSwitchPressStates);
|
|
||||||
}
|
|
||||||
Response_P(S_JSON_SVALUE_ACTION_SVALUE, GetSwitchText(i).c_str(), mqtt_state);
|
|
||||||
char scommand[10];
|
|
||||||
snprintf_P(scommand, sizeof(scommand), PSTR(D_JSON_SWITCH "%d"), i +1);
|
|
||||||
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, scommand);
|
|
||||||
}
|
|
||||||
mqtt_action = POWER_NONE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SwitchLoop(void) {
|
|
||||||
if (Switch.present) {
|
|
||||||
if (TimeReached(Switch.debounce)) {
|
|
||||||
SetNextTimeInterval(Switch.debounce, Settings->switch_debounce);
|
|
||||||
SwitchHandler(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // SWITCH_V3
|
|
@ -47,14 +47,13 @@ struct SWITCH {
|
|||||||
uint32_t debounce = 0; // Switch debounce timer
|
uint32_t debounce = 0; // Switch debounce timer
|
||||||
uint32_t no_pullup_mask = 0; // Switch pull-up bitmask flags
|
uint32_t no_pullup_mask = 0; // Switch pull-up bitmask flags
|
||||||
uint32_t pulldown_mask = 0; // Switch pull-down bitmask flags
|
uint32_t pulldown_mask = 0; // Switch pull-down bitmask flags
|
||||||
uint32_t virtual_pin_used = 0; // Switch used bitmask
|
uint32_t used = 0; // Switch used bitmask
|
||||||
uint32_t virtual_pin = 0; // Switch state bitmask
|
uint32_t virtual_pin = 0; // Switch state bitmask
|
||||||
uint8_t state[MAX_SWITCHES_SET] = { 0 };
|
uint8_t state[MAX_SWITCHES_SET] = { 0 };
|
||||||
uint8_t last_state[MAX_SWITCHES_SET]; // Last wall switch states
|
uint8_t last_state[MAX_SWITCHES_SET]; // Last wall switch states
|
||||||
uint8_t hold_timer[MAX_SWITCHES_SET] = { 0 }; // Timer for wallswitch push button hold
|
uint8_t hold_timer[MAX_SWITCHES_SET] = { 0 }; // Timer for wallswitch push button hold
|
||||||
uint8_t debounced_state[MAX_SWITCHES_SET]; // Switch debounced states
|
uint8_t debounced_state[MAX_SWITCHES_SET]; // Switch debounced states
|
||||||
uint8_t first_change = 0;
|
uint8_t first_change = 0;
|
||||||
uint8_t present = 0;
|
|
||||||
bool probe_mutex;
|
bool probe_mutex;
|
||||||
} Switch;
|
} Switch;
|
||||||
|
|
||||||
@ -68,31 +67,35 @@ void SwitchPulldownFlag(uint32 switch_bit) {
|
|||||||
bitSet(Switch.pulldown_mask, switch_bit);
|
bitSet(Switch.pulldown_mask, switch_bit);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SwitchUsed(uint32_t index) {
|
/*------------------------------------------------------------------------------------------*/
|
||||||
return (PinUsed(GPIO_SWT1, index) || bitRead(Switch.virtual_pin_used, index));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preffered virtual switch support since v12.3.1.4
|
|
||||||
void SwitchSetVirtualPinState(uint32_t index, uint32_t state) {
|
void SwitchSetVirtualPinState(uint32_t index, uint32_t state) {
|
||||||
|
// Set virtual pin state to be debounced as used by early detected switches
|
||||||
bitWrite(Switch.virtual_pin, index, state);
|
bitWrite(Switch.virtual_pin, index, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy virtual switch support
|
void SwitchSetState(uint32_t index, uint32_t state) {
|
||||||
void SwitchSetVirtual(uint32_t index, uint32_t state) {
|
// Set debounced pin state to be used by late detected switches
|
||||||
// bitSet(Switch.virtual_pin_used, index);
|
bitSet(Switch.used, index); // Force use bit as call maybe late
|
||||||
Switch.debounced_state[index] = state;
|
Switch.debounced_state[index] = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy virtual switch support
|
uint8_t SwitchGetState(uint32_t index) {
|
||||||
uint8_t SwitchGetVirtual(uint32_t index) {
|
// Get current state
|
||||||
return Switch.debounced_state[index];
|
return Switch.debounced_state[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy virtual switch support
|
|
||||||
uint8_t SwitchLastState(uint32_t index) {
|
uint8_t SwitchLastState(uint32_t index) {
|
||||||
|
// Get last state
|
||||||
return Switch.last_state[index];
|
return Switch.last_state[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
bool SwitchUsed(uint32_t index) {
|
||||||
|
return bitRead(Switch.used, index);
|
||||||
|
}
|
||||||
|
|
||||||
bool SwitchState(uint32_t index) {
|
bool SwitchState(uint32_t index) {
|
||||||
uint32_t switchmode = Settings->switchmode[index];
|
uint32_t switchmode = Settings->switchmode[index];
|
||||||
return ((FOLLOW_INV == switchmode) ||
|
return ((FOLLOW_INV == switchmode) ||
|
||||||
@ -135,7 +138,7 @@ void SwitchProbe(void) {
|
|||||||
if (PinUsed(GPIO_SWT1, i)) {
|
if (PinUsed(GPIO_SWT1, i)) {
|
||||||
not_activated = digitalRead(Pin(GPIO_SWT1, i));
|
not_activated = digitalRead(Pin(GPIO_SWT1, i));
|
||||||
}
|
}
|
||||||
else if (bitRead(Switch.virtual_pin_used, i)) {
|
else if (bitRead(Switch.used, i)) {
|
||||||
not_activated = bitRead(Switch.virtual_pin, i);
|
not_activated = bitRead(Switch.virtual_pin, i);
|
||||||
}
|
}
|
||||||
else { continue; }
|
else { continue; }
|
||||||
@ -222,14 +225,11 @@ void SwitchProbe(void) {
|
|||||||
void SwitchInit(void) {
|
void SwitchInit(void) {
|
||||||
bool ac_detect = (Settings->switch_debounce % 10 == 9);
|
bool ac_detect = (Settings->switch_debounce % 10 == 9);
|
||||||
|
|
||||||
Switch.present = 0;
|
Switch.used = 0;
|
||||||
Switch.virtual_pin_used = 0;
|
|
||||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||||
Switch.last_state[i] = NOT_PRESSED; // Init global to virtual switch state;
|
Switch.last_state[i] = NOT_PRESSED; // Init global to virtual switch state;
|
||||||
bool used = false;
|
|
||||||
|
|
||||||
if (PinUsed(GPIO_SWT1, i)) {
|
if (PinUsed(GPIO_SWT1, i)) {
|
||||||
Switch.present++;
|
bitSet(Switch.used, i); // This pin is used
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
pinMode(Pin(GPIO_SWT1, i), bitRead(Switch.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_SWT1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
pinMode(Pin(GPIO_SWT1, i), bitRead(Switch.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_SWT1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
||||||
#endif // ESP8266
|
#endif // ESP8266
|
||||||
@ -237,7 +237,6 @@ void SwitchInit(void) {
|
|||||||
pinMode(Pin(GPIO_SWT1, i), bitRead(Switch.pulldown_mask, i) ? INPUT_PULLDOWN : bitRead(Switch.no_pullup_mask, i) ? INPUT : INPUT_PULLUP);
|
pinMode(Pin(GPIO_SWT1, i), bitRead(Switch.pulldown_mask, i) ? INPUT_PULLDOWN : bitRead(Switch.no_pullup_mask, i) ? INPUT : INPUT_PULLUP);
|
||||||
#endif // ESP32
|
#endif // ESP32
|
||||||
Switch.last_state[i] = digitalRead(Pin(GPIO_SWT1, i)); // Set global now so doesn't change the saved power state on first switch check
|
Switch.last_state[i] = digitalRead(Pin(GPIO_SWT1, i)); // Set global now so doesn't change the saved power state on first switch check
|
||||||
used = true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
XdrvMailbox.index = i;
|
XdrvMailbox.index = i;
|
||||||
@ -248,28 +247,25 @@ void SwitchInit(void) {
|
|||||||
At exit:
|
At exit:
|
||||||
XdrvMailbox.index bit 0 = current state
|
XdrvMailbox.index bit 0 = current state
|
||||||
*/
|
*/
|
||||||
Switch.present++;
|
bitSet(Switch.used, i); // This pin is used
|
||||||
bitSet(Switch.virtual_pin_used, i); // This pin is used
|
|
||||||
bool state = (XdrvMailbox.index &1);
|
bool state = (XdrvMailbox.index &1);
|
||||||
SwitchSetVirtualPinState(i, state); // Virtual hardware pin state
|
SwitchSetVirtualPinState(i, state); // Virtual hardware pin state
|
||||||
Switch.last_state[i] = bitRead(Switch.virtual_pin, i);
|
Switch.last_state[i] = bitRead(Switch.virtual_pin, i);
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), Switch.present, Switch.last_state[i]);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), i +1, Switch.last_state[i]);
|
||||||
|
|
||||||
used = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (used && ac_detect) {
|
if (bitRead(Switch.used, i) && ac_detect) {
|
||||||
Switch.state[i] = 0x80 + 2 * SWITCH_AC_PERIOD;
|
Switch.state[i] = 0x80 + 2 * SWITCH_AC_PERIOD;
|
||||||
Switch.last_state[i] = 0; // Will set later in the debouncing code
|
Switch.last_state[i] = 0; // Will set later in the debouncing code
|
||||||
}
|
}
|
||||||
Switch.debounced_state[i] = Switch.last_state[i];
|
Switch.debounced_state[i] = Switch.last_state[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: vPinUsed %08X, State %08X"), Switch.virtual_pin_used, Switch.virtual_pin);
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: vPinUsed %08X, State %08X"), Switch.used, Switch.virtual_pin);
|
||||||
|
|
||||||
if (Switch.present) {
|
if (Switch.used) { // Any bit set
|
||||||
Switch.first_change = true;
|
Switch.first_change = true;
|
||||||
TickerSwitch.attach_ms((ac_detect) ? SWITCH_FAST_PROBE_INTERVAL : SWITCH_PROBE_INTERVAL, SwitchProbe);
|
TickerSwitch.attach_ms((ac_detect) ? SWITCH_FAST_PROBE_INTERVAL : SWITCH_PROBE_INTERVAL, SwitchProbe);
|
||||||
}
|
}
|
||||||
@ -279,14 +275,13 @@ void SwitchInit(void) {
|
|||||||
* Switch handler
|
* Switch handler
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
void SwitchHandler(uint32_t mode) {
|
void SwitchHandler(void) {
|
||||||
if (TasmotaGlobal.uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
if (TasmotaGlobal.uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
||||||
|
|
||||||
uint32_t loops_per_second = 1000 / Settings->switch_debounce;
|
uint32_t loops_per_second = 1000 / Settings->switch_debounce;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||||
// if (PinUsed(GPIO_SWT1, i) || bitRead(Switch.virtual_pin_used, i)) {
|
if (bitRead(Switch.used, i)) {
|
||||||
if (SwitchUsed(i)) {
|
|
||||||
uint32_t button = Switch.debounced_state[i];
|
uint32_t button = Switch.debounced_state[i];
|
||||||
uint32_t switchflag = POWER_TOGGLE +1;
|
uint32_t switchflag = POWER_TOGGLE +1;
|
||||||
uint32_t mqtt_action = POWER_NONE;
|
uint32_t mqtt_action = POWER_NONE;
|
||||||
@ -497,12 +492,12 @@ void SwitchHandler(uint32_t mode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SwitchLoop(void) {
|
void SwitchLoop(void) {
|
||||||
if (Switch.present) {
|
if (Switch.used) {
|
||||||
if (TimeReached(Switch.debounce)) {
|
if (TimeReached(Switch.debounce)) {
|
||||||
SetNextTimeInterval(Switch.debounce, Settings->switch_debounce);
|
SetNextTimeInterval(Switch.debounce, Settings->switch_debounce);
|
||||||
SwitchHandler(0);
|
SwitchHandler();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // SWITCH_V3
|
#endif // SWITCH_V4
|
||||||
|
@ -5779,7 +5779,7 @@ int32_t UpdVar(char *vname, float *fvar, uint32_t mode) {
|
|||||||
return 1;
|
return 1;
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
*fvar = Button.last_state[index - 1];
|
*fvar = ButtonLastState(index - 1);
|
||||||
return 1;
|
return 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,7 @@ struct TUYA {
|
|||||||
#endif // USE_ENERGY_SENSOR
|
#endif // USE_ENERGY_SENSOR
|
||||||
char *buffer = nullptr; // Serial receive buffer
|
char *buffer = nullptr; // Serial receive buffer
|
||||||
int byte_counter = 0; // Index in serial receive buffer
|
int byte_counter = 0; // Index in serial receive buffer
|
||||||
|
uint8_t last_button;
|
||||||
bool low_power_mode = false; // Normal or Low power mode protocol
|
bool low_power_mode = false; // Normal or Low power mode protocol
|
||||||
bool send_success_next_second = false; // Second command success in low power mode
|
bool send_success_next_second = false; // Second command success in low power mode
|
||||||
uint32_t ignore_dimmer_cmd_timeout = 0; // Time until which received dimmer commands should be ignored
|
uint32_t ignore_dimmer_cmd_timeout = 0; // Time until which received dimmer commands should be ignored
|
||||||
@ -847,11 +848,10 @@ void TuyaProcessStatePacket(void) {
|
|||||||
if (Tuya.buffer[dpidStart + 4]) { PowerOff = true; }
|
if (Tuya.buffer[dpidStart + 4]) { PowerOff = true; }
|
||||||
}
|
}
|
||||||
} else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) {
|
} else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: RX Switch-%d --> MCU State: %d Current State:%d"),fnId - TUYA_MCU_FUNC_SWT1 + 1,Tuya.buffer[dpidStart + 4], SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1));
|
uint32_t switch_state = SwitchGetState(fnId - TUYA_MCU_FUNC_SWT1);
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: RX Switch-%d --> MCU State: %d Current State:%d"),fnId - TUYA_MCU_FUNC_SWT1 + 1,Tuya.buffer[dpidStart + 4], switch_state);
|
||||||
if (SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1) != Tuya.buffer[dpidStart + 4]) {
|
if (switch_state != Tuya.buffer[dpidStart + 4]) {
|
||||||
SwitchSetVirtual(fnId - TUYA_MCU_FUNC_SWT1, Tuya.buffer[dpidStart + 4]);
|
SwitchSetState(fnId - TUYA_MCU_FUNC_SWT1, Tuya.buffer[dpidStart + 4]);
|
||||||
SwitchHandler(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (PowerOff) { Tuya.ignore_dimmer_cmd_timeout = millis() + 250; }
|
if (PowerOff) { Tuya.ignore_dimmer_cmd_timeout = millis() + 250; }
|
||||||
@ -1382,14 +1382,16 @@ void TuyaSerialInput(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TuyaButtonPressed(void)
|
bool TuyaButtonPressed(void) {
|
||||||
{
|
bool result = false;
|
||||||
if (!XdrvMailbox.index && ((PRESSED == XdrvMailbox.payload) && (NOT_PRESSED == Button.last_state[XdrvMailbox.index]))) {
|
uint32_t button = XdrvMailbox.payload;
|
||||||
|
if (!XdrvMailbox.index && ((PRESSED == button) && (NOT_PRESSED == Tuya.last_button))) {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: Reset GPIO triggered"));
|
AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: Reset GPIO triggered"));
|
||||||
TuyaResetWifi();
|
TuyaResetWifi();
|
||||||
return true; // Reset GPIO served here
|
result = true; // Reset GPIO served here
|
||||||
}
|
}
|
||||||
return false; // Don't serve other buttons
|
Tuya.last_button = button;
|
||||||
|
return result; // Don't serve other buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t TuyaGetTuyaWifiState(void) {
|
uint8_t TuyaGetTuyaWifiState(void) {
|
||||||
|
@ -253,6 +253,8 @@ typedef struct TUYA_STRUCT_tag {
|
|||||||
uint8_t dimCmdEnable; // we are allowed to send a dim command - bitfield
|
uint8_t dimCmdEnable; // we are allowed to send a dim command - bitfield
|
||||||
uint8_t dimDebug; // enables a single dim debug - bitfield
|
uint8_t dimDebug; // enables a single dim debug - bitfield
|
||||||
|
|
||||||
|
uint8_t last_button;
|
||||||
|
|
||||||
int sends;
|
int sends;
|
||||||
int rxs;
|
int rxs;
|
||||||
|
|
||||||
@ -265,9 +267,8 @@ void TuyaSendState(uint8_t id, uint8_t type, uint8_t* value, int len);
|
|||||||
|
|
||||||
int init_tuya_struct() {
|
int init_tuya_struct() {
|
||||||
if (pTuya) return 0; // done already
|
if (pTuya) return 0; // done already
|
||||||
pTuya = (TUYA_STRUCT *)malloc(sizeof(TUYA_STRUCT));
|
pTuya = (TUYA_STRUCT *)calloc(sizeof(TUYA_STRUCT), 1);
|
||||||
if (!pTuya) return 0;
|
if (!pTuya) return 0;
|
||||||
memset(pTuya, 0, sizeof(TUYA_STRUCT));
|
|
||||||
strcpy(pTuya->RGBColor, "000000"); // Stores RGB Color string in Hex format
|
strcpy(pTuya->RGBColor, "000000"); // Stores RGB Color string in Hex format
|
||||||
pTuya->CTMin = 153; // Minimum CT level allowed - When SetOption82 is enabled will default to 200
|
pTuya->CTMin = 153; // Minimum CT level allowed - When SetOption82 is enabled will default to 200
|
||||||
pTuya->CTMax = 500; // Maximum CT level allowed - When SetOption82 is enabled will default to 380
|
pTuya->CTMax = 500; // Maximum CT level allowed - When SetOption82 is enabled will default to 380
|
||||||
@ -1638,11 +1639,10 @@ void TuyaProcessRxedDP(uint8_t dpid, uint8_t type, uint8_t *data, int dpDataLen)
|
|||||||
if (value) { PowerOff = true; }
|
if (value) { PowerOff = true; }
|
||||||
}
|
}
|
||||||
} else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) {
|
} else if (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("T:fn%d Switch%d --> M%d T%d"),fnId, fnId - TUYA_MCU_FUNC_SWT1 + 1, value, SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1));
|
uint32_t switch_state = SwitchGetState(fnId - TUYA_MCU_FUNC_SWT1);
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("T:fn%d Switch%d --> M%d T%d"),fnId, fnId - TUYA_MCU_FUNC_SWT1 + 1, value, switch_state);
|
||||||
if (SwitchGetVirtual(fnId - TUYA_MCU_FUNC_SWT1) != value) {
|
if (switch_state != value) {
|
||||||
SwitchSetVirtual(fnId - TUYA_MCU_FUNC_SWT1, value);
|
SwitchSetState(fnId - TUYA_MCU_FUNC_SWT1, value);
|
||||||
SwitchHandler(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//if (PowerOff) { pTuya->ignore_dimmer_cmd_timeout = millis() + 250; }
|
//if (PowerOff) { pTuya->ignore_dimmer_cmd_timeout = millis() + 250; }
|
||||||
@ -2261,14 +2261,16 @@ void TuyaSerialInput(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TuyaButtonPressed(void)
|
bool TuyaButtonPressed(void) {
|
||||||
{
|
bool result = false;
|
||||||
if (!XdrvMailbox.index && ((PRESSED == XdrvMailbox.payload) && (NOT_PRESSED == Button.last_state[XdrvMailbox.index]))) {
|
uint32_t button = XdrvMailbox.payload;
|
||||||
|
if (!XdrvMailbox.index && ((PRESSED == button) && (NOT_PRESSED == pTuya->last_button))) {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: Reset GPIO triggered"));
|
AddLog(LOG_LEVEL_DEBUG, PSTR("TYA: Reset GPIO triggered"));
|
||||||
TuyaResetWifi();
|
TuyaResetWifi();
|
||||||
return true; // Reset GPIO served here
|
result = true; // Reset GPIO served here
|
||||||
}
|
}
|
||||||
return false; // Don't serve other buttons
|
pTuya->last_button = button;
|
||||||
|
return result; // Don't serve other buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t TuyaGetTuyaWifiState(void) {
|
uint8_t TuyaGetTuyaWifiState(void) {
|
||||||
|
@ -316,7 +316,7 @@ uint8_t ThermostatInputStatus(uint8_t input_switch)
|
|||||||
bool ifId = ThermostatSwitchIdValid(input_switch);
|
bool ifId = ThermostatSwitchIdValid(input_switch);
|
||||||
uint8_t value = 0;
|
uint8_t value = 0;
|
||||||
if(ifId) {
|
if(ifId) {
|
||||||
value = SwitchGetVirtual(ifId - THERMOSTAT_INPUT_SWT1);
|
value = SwitchGetState(ifId - THERMOSTAT_INPUT_SWT1);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -588,7 +588,7 @@ extern "C" {
|
|||||||
be_newobject(vm, "list");
|
be_newobject(vm, "list");
|
||||||
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
for (uint32_t i = 0; i < MAX_SWITCHES_SET; i++) {
|
||||||
if (SwitchUsed(i)) {
|
if (SwitchUsed(i)) {
|
||||||
be_pushbool(vm, SwitchGetVirtual(i) == PRESSED);
|
be_pushbool(vm, SwitchGetState(i) == PRESSED);
|
||||||
be_data_push(vm, -2);
|
be_data_push(vm, -2);
|
||||||
be_pop(vm, 1);
|
be_pop(vm, 1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user