2018-12-28 15:35:19 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
support_switch.ino - switch support for Tasmota
|
2018-12-28 15:35:19 +00:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Theo Arends
|
2018-12-28 15:35:19 +00:00
|
|
|
|
|
|
|
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_V2
|
|
|
|
#ifdef SWITCH_V2
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Switch support with input filter
|
|
|
|
*
|
|
|
|
* Inspired by (https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/master/olimex/user/user_switch2.c)
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-03-31 10:59:04 +01:00
|
|
|
const uint8_t SWITCH_PROBE_INTERVAL = 10; // Time in milliseconds between switch input probe
|
2018-12-28 15:35:19 +00:00
|
|
|
|
|
|
|
#include <Ticker.h>
|
|
|
|
|
|
|
|
Ticker TickerSwitch;
|
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
struct SWITCH {
|
|
|
|
unsigned long debounce = 0; // Switch debounce timer
|
|
|
|
uint16_t no_pullup_mask = 0; // Switch pull-up 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 present = 0;
|
|
|
|
} Switch;
|
2018-12-28 15:35:19 +00:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void SwitchPullupFlag(uint16 switch_bit)
|
|
|
|
{
|
2019-08-17 15:19:58 +01:00
|
|
|
bitSet(Switch.no_pullup_mask, switch_bit);
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:53:35 +00:00
|
|
|
void SwitchSetVirtual(uint32_t index, uint8_t state)
|
2018-12-28 15:35:19 +00:00
|
|
|
{
|
2020-02-06 13:53:35 +00:00
|
|
|
Switch.virtual_state[index] = state;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:53:35 +00:00
|
|
|
uint8_t SwitchGetVirtual(uint32_t index)
|
2018-12-28 15:35:19 +00:00
|
|
|
{
|
2020-02-06 13:53:35 +00:00
|
|
|
return Switch.virtual_state[index];
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:53:35 +00:00
|
|
|
uint8_t SwitchLastState(uint32_t index)
|
2018-12-28 15:35:19 +00:00
|
|
|
{
|
2020-02-06 13:53:35 +00:00
|
|
|
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)
|
|
|
|
) ^ Switch.last_state[index];
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
void SwitchProbe(void)
|
|
|
|
{
|
2019-01-07 09:15:00 +00:00
|
|
|
if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
|
|
|
|
2018-12-30 13:04:32 +00:00
|
|
|
uint8_t state_filter = Settings.switch_debounce / SWITCH_PROBE_INTERVAL; // 5, 10, 15
|
|
|
|
uint8_t force_high = (Settings.switch_debounce % 50) &1; // 51, 101, 151 etc
|
|
|
|
uint8_t force_low = (Settings.switch_debounce % 50) &2; // 52, 102, 152 etc
|
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
2018-12-28 15:35:19 +00:00
|
|
|
if (pin[GPIO_SWT1 +i] < 99) {
|
2019-01-07 09:15:00 +00:00
|
|
|
// Olimex user_switch2.c code to fix 50Hz induced pulses
|
|
|
|
if (1 == digitalRead(pin[GPIO_SWT1 +i])) {
|
|
|
|
|
|
|
|
if (force_high) { // Enabled with SwitchDebounce x1
|
2019-08-17 15:19:58 +01:00
|
|
|
if (1 == Switch.virtual_state[i]) {
|
|
|
|
Switch.state[i] = state_filter; // With noisy input keep current state 1 unless constant 0
|
2018-12-30 13:04:32 +00:00
|
|
|
}
|
2019-01-07 09:15:00 +00:00
|
|
|
}
|
2018-12-30 13:04:32 +00:00
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Switch.state[i] < state_filter) {
|
|
|
|
Switch.state[i]++;
|
|
|
|
if (state_filter == Switch.state[i]) {
|
|
|
|
Switch.virtual_state[i] = 1;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2019-01-07 09:15:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-30 13:04:32 +00:00
|
|
|
|
2019-01-07 09:15:00 +00:00
|
|
|
if (force_low) { // Enabled with SwitchDebounce x2
|
2019-08-17 15:19:58 +01:00
|
|
|
if (0 == Switch.virtual_state[i]) {
|
|
|
|
Switch.state[i] = 0; // With noisy input keep current state 0 unless constant 1
|
2018-12-30 13:04:32 +00:00
|
|
|
}
|
2019-01-07 09:15:00 +00:00
|
|
|
}
|
2018-12-30 13:04:32 +00:00
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Switch.state[i] > 0) {
|
|
|
|
Switch.state[i]--;
|
|
|
|
if (0 == Switch.state[i]) {
|
|
|
|
Switch.virtual_state[i] = 0;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 13:04:32 +00:00
|
|
|
TickerSwitch.attach_ms(SWITCH_PROBE_INTERVAL, SwitchProbe); // Re-arm as core 2.3.0 does only support ONCE mode
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchInit(void)
|
|
|
|
{
|
2019-08-17 15:19:58 +01:00
|
|
|
Switch.present = 0;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
2019-08-17 15:19:58 +01:00
|
|
|
Switch.last_state[i] = 1; // Init global to virtual switch state;
|
2018-12-28 15:35:19 +00:00
|
|
|
if (pin[GPIO_SWT1 +i] < 99) {
|
2019-08-17 15:19:58 +01:00
|
|
|
Switch.present++;
|
|
|
|
pinMode(pin[GPIO_SWT1 +i], bitRead(Switch.no_pullup_mask, i) ? INPUT : ((16 == pin[GPIO_SWT1 +i]) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
|
|
|
Switch.last_state[i] = digitalRead(pin[GPIO_SWT1 +i]); // Set global now so doesn't change the saved power state on first switch check
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
Switch.virtual_state[i] = Switch.last_state[i];
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Switch.present) { TickerSwitch.attach_ms(SWITCH_PROBE_INTERVAL, SwitchProbe); }
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Switch handler
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void SwitchHandler(uint8_t mode)
|
2018-12-28 15:35:19 +00:00
|
|
|
{
|
2019-01-07 11:38:47 +00:00
|
|
|
if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
|
|
|
|
|
2018-12-28 15:35:19 +00:00
|
|
|
uint16_t loops_per_second = 1000 / Settings.switch_debounce;
|
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < MAX_SWITCHES; i++) {
|
2018-12-28 15:35:19 +00:00
|
|
|
if ((pin[GPIO_SWT1 +i] < 99) || (mode)) {
|
2020-01-15 12:05:00 +00:00
|
|
|
uint8_t button = Switch.virtual_state[i];
|
|
|
|
uint8_t switchflag = POWER_TOGGLE +1;
|
2018-12-28 15:35:19 +00:00
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Switch.hold_timer[i]) {
|
|
|
|
Switch.hold_timer[i]--;
|
|
|
|
if (0 == Switch.hold_timer[i]) {
|
2020-01-15 12:05:00 +00:00
|
|
|
|
|
|
|
switch (Settings.switchmode[i]) {
|
|
|
|
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;
|
2020-01-25 10:22:28 +00:00
|
|
|
case PUSHHOLDMULTI:
|
2020-01-28 14:10:23 +00:00
|
|
|
if (NOT_PRESSED == button){
|
2020-01-25 10:22:28 +00:00
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 25;
|
2020-01-28 14:10:23 +00:00
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT
|
2020-01-25 10:22:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT
|
2020-02-06 13:53:35 +00:00
|
|
|
break;
|
|
|
|
case PUSHHOLDMULTI_INV:
|
2020-01-28 14:10:23 +00:00
|
|
|
if (PRESSED == button){
|
2020-01-25 10:22:28 +00:00
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 25;
|
2020-01-28 14:10:23 +00:00
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_INCREMENT); // Execute command via MQTT
|
2020-01-25 10:22:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_CLEAR); // Execute command via MQTT
|
2020-02-06 13:53:35 +00:00
|
|
|
break;
|
2020-01-15 12:05:00 +00:00
|
|
|
default:
|
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_HOLD); // Execute command via MQTT
|
|
|
|
break;
|
|
|
|
}
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:10:23 +00:00
|
|
|
// enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, TOGGLEMULTI, FOLLOWMULTI, FOLLOWMULTI_INV, PUSHHOLDMULTI, PUSHHOLDMULTI_INV, MAX_SWITCH_OPTION};
|
2018-12-28 15:35:19 +00:00
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
if (button != Switch.last_state[i]) {
|
2018-12-28 15:35:19 +00:00
|
|
|
switch (Settings.switchmode[i]) {
|
|
|
|
case TOGGLE:
|
2020-01-15 12:05:00 +00:00
|
|
|
case PUSHBUTTON_TOGGLE:
|
2019-09-04 11:20:04 +01:00
|
|
|
switchflag = POWER_TOGGLE; // Toggle
|
2018-12-28 15:35:19 +00:00
|
|
|
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:
|
2019-08-17 15:19:58 +01:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) {
|
2019-09-04 11:20:04 +01:00
|
|
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PUSHBUTTON_INV:
|
2019-08-17 15:19:58 +01:00
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) {
|
2019-09-04 11:20:04 +01:00
|
|
|
switchflag = POWER_TOGGLE; // Toggle with releasing pushbutton from Gnd
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PUSHBUTTONHOLD:
|
2019-08-17 15:19:58 +01:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) {
|
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) {
|
|
|
|
Switch.hold_timer[i] = 0;
|
2019-09-04 11:20:04 +01:00
|
|
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PUSHBUTTONHOLD_INV:
|
2019-08-17 15:19:58 +01:00
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) {
|
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) {
|
|
|
|
Switch.hold_timer[i] = 0;
|
2019-09-04 11:20:04 +01:00
|
|
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-01-15 12:05:00 +00:00
|
|
|
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
|
|
|
|
} else {
|
|
|
|
Switch.hold_timer[i] = loops_per_second / 2; // 0.5 second multi press window
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2020-01-15 12:05:00 +00:00
|
|
|
break;
|
2020-01-25 10:22:28 +00:00
|
|
|
case PUSHHOLDMULTI:
|
2020-01-28 14:10:23 +00:00
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) {
|
2020-01-25 10:22:28 +00:00
|
|
|
if(Switch.hold_timer[i]!=0)
|
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT
|
2020-02-06 13:53:35 +00:00
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10;
|
2020-01-25 10:22:28 +00:00
|
|
|
}
|
2020-01-28 14:10:23 +00:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) {
|
2020-01-25 10:22:28 +00:00
|
|
|
if(Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25)
|
2020-02-06 13:53:35 +00:00
|
|
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton
|
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10;
|
|
|
|
}
|
2020-01-25 10:22:28 +00:00
|
|
|
break;
|
|
|
|
case PUSHHOLDMULTI_INV:
|
2020-01-28 14:10:23 +00:00
|
|
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) {
|
2020-01-25 10:22:28 +00:00
|
|
|
if(Switch.hold_timer[i]!=0)
|
2020-02-06 13:53:35 +00:00
|
|
|
SendKey(KEY_SWITCH, i +1, POWER_INV); // Execute command via MQTT
|
2020-01-25 10:22:28 +00:00
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10;
|
|
|
|
}
|
2020-01-28 14:10:23 +00:00
|
|
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) {
|
2020-01-25 10:22:28 +00:00
|
|
|
if(Switch.hold_timer[i] > loops_per_second * Settings.param[P_HOLD_TIME] / 25)
|
|
|
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton
|
2020-02-06 13:53:35 +00:00
|
|
|
Switch.hold_timer[i] = loops_per_second * Settings.param[P_HOLD_TIME] / 10;
|
2020-01-25 10:22:28 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
Switch.last_state[i] = button;
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
2020-01-15 12:05:00 +00:00
|
|
|
if (switchflag <= POWER_TOGGLE) {
|
|
|
|
if (!SendKey(KEY_SWITCH, i +1, switchflag)) { // Execute command via MQTT
|
|
|
|
ExecuteCommandPower(i +1, switchflag, SRC_SWITCH); // Execute command internally (if i < devices_present)
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 15:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchLoop(void)
|
|
|
|
{
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Switch.present) {
|
|
|
|
if (TimeReached(Switch.debounce)) {
|
|
|
|
SetNextTimeInterval(Switch.debounce, Settings.switch_debounce);
|
2018-12-28 15:35:19 +00:00
|
|
|
SwitchHandler(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SWITCH_V2
|