Change button driver making it modular

Change button driver making it modular
This commit is contained in:
Theo Arends 2019-01-07 12:38:47 +01:00
parent 3d31dd1457
commit 95f39be2ef
4 changed files with 243 additions and 184 deletions

View File

@ -3,6 +3,7 @@
* Add relay status functionality to LED2 when configured leaving LED1 for (wifi/mqtt) status indication
* Add no pull-up control to Shelly 2 module (default is pull-up, change GPIO2 to Switch3n for no pull-up) (#4841)
* Add 4 seconds startup delay to button control (#4829)
* Change button driver making it modular
*
* 6.4.1.6 20190105
* Add commands PowerCal, VoltageCal and CurrentCal for HLW8012, HJL01 and BL0937 based energy sensors

View File

@ -108,7 +108,6 @@ unsigned long state_250msecond = 0; // State 250msecond timer
unsigned long pulse_timer[MAX_PULSETIMERS] = { 0 }; // Power off timer
unsigned long blink_timer = 0; // Power cycle timer
unsigned long backlog_delay = 0; // Command backlog delay
unsigned long button_debounce = 0; // Button debounce timer
power_t power = 0; // Current copy of Settings.power
power_t blink_power; // Blink power state
power_t blink_mask = 0; // Blink relay active mask
@ -129,12 +128,10 @@ uint32_t global_update = 0; // Timestamp of last global temperat
float global_temperature = 0; // Provide a global temperature to be used by some sensors
float global_humidity = 0; // Provide a global humidity to be used by some sensors
char *ota_url; // OTA url string pointer
uint16_t dual_button_code = 0; // Sonoff dual received code
uint16_t mqtt_cmnd_publish = 0; // ignore flag for publish command
uint16_t blink_counter = 0; // Number of blink cycles
uint16_t seriallog_timer = 0; // Timer to disable Seriallog
uint16_t syslog_timer = 0; // Timer to re-enable syslog_level
uint16_t holdbutton[MAX_KEYS] = { 0 }; // Timer for button hold
int16_t save_data_counter; // Counter and flag for config save to Flash
RulesBitfield rules_flag; // Rule state flags (16 bits)
uint8_t serial_local = 0; // Handle serial locally;
@ -149,9 +146,6 @@ uint8_t sleep; // Current copy of Settings.sleep
uint8_t stop_flash_rotate = 0; // Allow flash configuration rotation
uint8_t blinkstate = 0; // LED state
uint8_t blinkspeed = 1; // LED blink rate
uint8_t lastbutton[MAX_KEYS] = { NOT_PRESSED, NOT_PRESSED, NOT_PRESSED, NOT_PRESSED }; // Last button states
uint8_t multiwindow[MAX_KEYS] = { 0 }; // Max time between button presses to record press count
uint8_t multipress[MAX_KEYS] = { 0 }; // Number of button presses within multiwindow
uint8_t pin[GPIO_MAX]; // Possible pin configurations
uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off))
uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted)
@ -164,7 +158,6 @@ uint8_t soft_spi_flg = 0; // Software SPI configured
uint8_t light_type = 0; // Light types
uint8_t ntp_force_sync = 0; // Force NTP sync
byte serial_in_byte; // Received byte
byte dual_hex_code = 0; // Sonoff dual input flag
byte ota_retry_counter = OTA_ATTEMPTS; // OTA retry counter
byte web_log_index = 1; // Index in Web log buffer (should never be 0)
byte reset_web_log_flag = 0; // Reset web console log
@ -1744,154 +1737,6 @@ void PerformEverySecond(void)
if ((3 == RtcTime.minute) && !latest_uptime_flag) latest_uptime_flag = true;
}
/*********************************************************************************************\
* Button handler with single press only or multi-press and hold on all buttons
\*********************************************************************************************/
void ButtonHandler(void)
{
if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
uint8_t button = NOT_PRESSED;
uint8_t button_present = 0;
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;
char scmnd[20];
uint8_t maxdev = (devices_present > MAX_KEYS) ? MAX_KEYS : devices_present;
for (byte button_index = 0; button_index < maxdev; button_index++) {
button = NOT_PRESSED;
button_present = 0;
if (!button_index && ((SONOFF_DUAL == Settings.module) || (CH4 == Settings.module))) {
button_present = 1;
if (dual_button_code) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " " D_CODE " %04X"), dual_button_code);
AddLog(LOG_LEVEL_DEBUG);
button = PRESSED;
if (0xF500 == dual_button_code) { // Button hold
holdbutton[button_index] = (loops_per_second * Settings.param[P_HOLD_TIME] / 10) -1;
hold_time_extent = 1;
}
dual_button_code = 0;
}
} else {
if (pin[GPIO_KEY1 +button_index] < 99) {
button_present = 1;
button = digitalRead(pin[GPIO_KEY1 +button_index]);
}
}
if (button_present) {
XdrvMailbox.index = button_index;
XdrvMailbox.payload = button;
if (XdrvCall(FUNC_BUTTON_PRESSED)) {
// Serviced
}
else if (SONOFF_4CHPRO == Settings.module) {
if (holdbutton[button_index]) { holdbutton[button_index]--; }
boolean button_pressed = false;
if ((PRESSED == button) && (NOT_PRESSED == lastbutton[button_index])) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_10), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
holdbutton[button_index] = loops_per_second;
button_pressed = true;
}
if ((NOT_PRESSED == button) && (PRESSED == lastbutton[button_index])) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_01), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
if (!holdbutton[button_index]) { button_pressed = true; } // Do not allow within 1 second
}
if (button_pressed) {
if (!SendKey(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
}
}
}
else {
if ((PRESSED == button) && (NOT_PRESSED == lastbutton[button_index])) {
if (Settings.flag.button_single) { // Allow only single button press for immediate action
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
if (!SendKey(0, 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 {
multipress[button_index] = (multiwindow[button_index]) ? multipress[button_index] +1 : 1;
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_MULTI_PRESS " %d"), button_index +1, multipress[button_index]);
AddLog(LOG_LEVEL_DEBUG);
multiwindow[button_index] = loops_per_second / 2; // 0.5 second multi press window
}
blinks = 201;
}
if (NOT_PRESSED == button) {
holdbutton[button_index] = 0;
} else {
holdbutton[button_index]++;
if (Settings.flag.button_single) { // Allow only single button press for immediate action
if (holdbutton[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // Button held for factor times longer
// Settings.flag.button_single = 0;
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_SETOPTION "13 0")); // Disable single press only
ExecuteCommand(scmnd, SRC_BUTTON);
}
} else {
if (Settings.flag.button_restrict) { // Button restriction
if (holdbutton[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // Button hold
multipress[button_index] = 0;
SendKey(0, button_index +1, 3); // Execute Hold command via MQTT if ButtonTopic is set
}
} else {
if (holdbutton[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // Button held for factor times longer
multipress[button_index] = 0;
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_RESET " 1"));
ExecuteCommand(scmnd, SRC_BUTTON);
}
}
}
}
if (!Settings.flag.button_single) { // Allow multi-press
if (multiwindow[button_index]) {
multiwindow[button_index]--;
} else {
if (!restart_flag && !holdbutton[button_index] && (multipress[button_index] > 0) && (multipress[button_index] < MAX_BUTTON_COMMANDS +3)) {
boolean single_press = false;
if (multipress[button_index] < 3) { // Single or Double press
if ((SONOFF_DUAL_R2 == Settings.module) || (SONOFF_DUAL == Settings.module) || (CH4 == Settings.module)) {
single_press = true;
} else {
single_press = (Settings.flag.button_swap +1 == multipress[button_index]);
multipress[button_index] = 1;
}
}
if (single_press && SendKey(0, button_index + multipress[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
// Success
} else {
if (multipress[button_index] < 3) { // Single or Double press
if (WifiState() > WIFI_RESTART) { // WPSconfig, Smartconfig or Wifimanager active
restart_flag = 1;
} else {
ExecuteCommandPower(button_index + multipress[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
}
} else { // 3 - 7 press
if (!Settings.flag.button_restrict) {
snprintf_P(scmnd, sizeof(scmnd), kCommands[multipress[button_index] -3]);
ExecuteCommand(scmnd, SRC_BUTTON);
}
}
}
multipress[button_index] = 0;
}
}
}
}
}
lastbutton[button_index] = button;
}
}
/*********************************************************************************************\
* State loops
\*********************************************************************************************/
@ -2224,22 +2069,7 @@ void SerialInput(void)
* Sonoff dual and ch4 19200 baud serial interface
\*-------------------------------------------------------------------------------------------*/
if ((SONOFF_DUAL == Settings.module) || (CH4 == Settings.module)) {
if (dual_hex_code) {
dual_hex_code--;
if (dual_hex_code) {
dual_button_code = (dual_button_code << 8) | serial_in_byte;
serial_in_byte = 0;
} else {
if (serial_in_byte != 0xA1) {
dual_button_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;
dual_button_code = 0;
dual_hex_code = 3;
}
serial_in_byte = ButtonSerial(serial_in_byte);
}
/*-------------------------------------------------------------------------------------------*/
@ -2328,7 +2158,6 @@ void SerialInput(void)
void GpioInit(void)
{
uint8_t mpin;
uint8_t key_no_pullup = 0;
if (Settings.module >= MAXMODULE) {
Settings.module = MODULE;
@ -2366,7 +2195,7 @@ void GpioInit(void)
mpin -= (GPIO_SWT1_NP - GPIO_SWT1);
}
else if ((mpin >= GPIO_KEY1_NP) && (mpin < (GPIO_KEY1_NP + MAX_KEYS))) {
bitSet(key_no_pullup, mpin - GPIO_KEY1_NP);
ButtonPullupFlag(mpin - GPIO_KEY1_NP);
mpin -= (GPIO_KEY1_NP - GPIO_KEY1);
}
else if ((mpin >= GPIO_REL1_INV) && (mpin < (GPIO_REL1_INV + MAX_RELAYS))) {
@ -2483,11 +2312,6 @@ void GpioInit(void)
}
}
for (byte i = 0; i < MAX_KEYS; i++) {
if (pin[GPIO_KEY1 +i] < 99) {
pinMode(pin[GPIO_KEY1 +i], (16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : bitRead(key_no_pullup, i) ? INPUT : INPUT_PULLUP);
}
}
for (byte i = 0; i < MAX_LEDS; i++) {
if (pin[GPIO_LED1 +i] < 99) {
pinMode(pin[GPIO_LED1 +i], OUTPUT);
@ -2495,6 +2319,7 @@ void GpioInit(void)
}
}
ButtonInit();
SwitchInit();
#ifdef USE_WS2812
@ -2680,11 +2505,7 @@ void loop(void)
OsWatchLoop();
if (TimeReached(button_debounce)) {
SetNextTimeInterval(button_debounce, Settings.button_debounce);
ButtonHandler();
}
ButtonLoop();
SwitchLoop();
if (TimeReached(state_50msecond)) {

236
sonoff/support_button.ino Normal file
View File

@ -0,0 +1,236 @@
/*
support_button.ino - button support for Sonoff-Tasmota
Copyright (C) 2019 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_V1
#ifdef BUTTON_V1
/*********************************************************************************************\
* Button support
\*********************************************************************************************/
unsigned long button_debounce = 0; // Button debounce timer
uint16_t holdbutton[MAX_KEYS] = { 0 }; // Timer for button hold
uint16_t dual_button_code = 0; // Sonoff dual received code
uint8_t lastbutton[MAX_KEYS] = { NOT_PRESSED, NOT_PRESSED, NOT_PRESSED, NOT_PRESSED }; // Last button states
uint8_t multiwindow[MAX_KEYS] = { 0 }; // Max time between button presses to record press count
uint8_t multipress[MAX_KEYS] = { 0 }; // Number of button presses within multiwindow
uint8_t dual_hex_code = 0; // Sonoff dual input flag
uint8_t key_no_pullup = 0;
uint8_t buttons_found = 0;
/********************************************************************************************/
void ButtonPullupFlag(uint8 button_bit)
{
bitSet(key_no_pullup, button_bit);
}
void ButtonInit(void)
{
buttons_found = 0;
for (byte i = 0; i < MAX_KEYS; i++) {
if (pin[GPIO_KEY1 +i] < 99) {
buttons_found++;
pinMode(pin[GPIO_KEY1 +i], (16 == pin[GPIO_KEY1 +i]) ? INPUT_PULLDOWN_16 : bitRead(key_no_pullup, i) ? INPUT : INPUT_PULLUP);
}
}
}
byte ButtonSerial(byte serial_in_byte)
{
if (dual_hex_code) {
dual_hex_code--;
if (dual_hex_code) {
dual_button_code = (dual_button_code << 8) | serial_in_byte;
serial_in_byte = 0;
} else {
if (serial_in_byte != 0xA1) {
dual_button_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;
dual_button_code = 0;
dual_hex_code = 3;
}
return serial_in_byte;
}
/*********************************************************************************************\
* Button handler with single press only or multi-press and hold on all buttons
\*********************************************************************************************/
void ButtonHandler(void)
{
if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
uint8_t button = NOT_PRESSED;
uint8_t button_present = 0;
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;
char scmnd[20];
uint8_t maxdev = (devices_present > MAX_KEYS) ? MAX_KEYS : devices_present;
for (byte button_index = 0; button_index < maxdev; button_index++) {
button = NOT_PRESSED;
button_present = 0;
if (!button_index && ((SONOFF_DUAL == Settings.module) || (CH4 == Settings.module))) {
button_present = 1;
if (dual_button_code) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON " " D_CODE " %04X"), dual_button_code);
AddLog(LOG_LEVEL_DEBUG);
button = PRESSED;
if (0xF500 == dual_button_code) { // Button hold
holdbutton[button_index] = (loops_per_second * Settings.param[P_HOLD_TIME] / 10) -1;
hold_time_extent = 1;
}
dual_button_code = 0;
}
} else {
if (pin[GPIO_KEY1 +button_index] < 99) {
button_present = 1;
button = digitalRead(pin[GPIO_KEY1 +button_index]);
}
}
if (button_present) {
XdrvMailbox.index = button_index;
XdrvMailbox.payload = button;
if (XdrvCall(FUNC_BUTTON_PRESSED)) {
// Serviced
}
else if (SONOFF_4CHPRO == Settings.module) {
if (holdbutton[button_index]) { holdbutton[button_index]--; }
boolean button_pressed = false;
if ((PRESSED == button) && (NOT_PRESSED == lastbutton[button_index])) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_10), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
holdbutton[button_index] = loops_per_second;
button_pressed = true;
}
if ((NOT_PRESSED == button) && (PRESSED == lastbutton[button_index])) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_LEVEL_01), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
if (!holdbutton[button_index]) { button_pressed = true; } // Do not allow within 1 second
}
if (button_pressed) {
if (!SendKey(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
}
}
}
else {
if ((PRESSED == button) && (NOT_PRESSED == lastbutton[button_index])) {
if (Settings.flag.button_single) { // Allow only single button press for immediate action
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
if (!SendKey(0, 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 {
multipress[button_index] = (multiwindow[button_index]) ? multipress[button_index] +1 : 1;
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_MULTI_PRESS " %d"), button_index +1, multipress[button_index]);
AddLog(LOG_LEVEL_DEBUG);
multiwindow[button_index] = loops_per_second / 2; // 0.5 second multi press window
}
blinks = 201;
}
if (NOT_PRESSED == button) {
holdbutton[button_index] = 0;
} else {
holdbutton[button_index]++;
if (Settings.flag.button_single) { // Allow only single button press for immediate action
if (holdbutton[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // Button held for factor times longer
// Settings.flag.button_single = 0;
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_SETOPTION "13 0")); // Disable single press only
ExecuteCommand(scmnd, SRC_BUTTON);
}
} else {
if (Settings.flag.button_restrict) { // Button restriction
if (holdbutton[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // Button hold
multipress[button_index] = 0;
SendKey(0, button_index +1, 3); // Execute Hold command via MQTT if ButtonTopic is set
}
} else {
if (holdbutton[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // Button held for factor times longer
multipress[button_index] = 0;
snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_RESET " 1"));
ExecuteCommand(scmnd, SRC_BUTTON);
}
}
}
}
if (!Settings.flag.button_single) { // Allow multi-press
if (multiwindow[button_index]) {
multiwindow[button_index]--;
} else {
if (!restart_flag && !holdbutton[button_index] && (multipress[button_index] > 0) && (multipress[button_index] < MAX_BUTTON_COMMANDS +3)) {
boolean single_press = false;
if (multipress[button_index] < 3) { // Single or Double press
if ((SONOFF_DUAL_R2 == Settings.module) || (SONOFF_DUAL == Settings.module) || (CH4 == Settings.module)) {
single_press = true;
} else {
single_press = (Settings.flag.button_swap +1 == multipress[button_index]);
multipress[button_index] = 1;
}
}
if (single_press && SendKey(0, button_index + multipress[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
// Success
} else {
if (multipress[button_index] < 3) { // Single or Double press
if (WifiState() > WIFI_RESTART) { // WPSconfig, Smartconfig or Wifimanager active
restart_flag = 1;
} else {
ExecuteCommandPower(button_index + multipress[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
}
} else { // 3 - 7 press
if (!Settings.flag.button_restrict) {
snprintf_P(scmnd, sizeof(scmnd), kCommands[multipress[button_index] -3]);
ExecuteCommand(scmnd, SRC_BUTTON);
}
}
}
multipress[button_index] = 0;
}
}
}
}
}
lastbutton[button_index] = button;
}
}
void ButtonLoop(void)
{
if (buttons_found) {
if (TimeReached(button_debounce)) {
SetNextTimeInterval(button_debounce, Settings.button_debounce);
ButtonHandler();
}
}
}
#endif // BUTTON_V1

View File

@ -133,6 +133,8 @@ void SwitchInit(void)
void SwitchHandler(byte mode)
{
if (uptime < 4) { return; } // Block GPIO for 4 seconds after poweron to workaround Wemos D1 / Obi RTS circuit
uint8_t button = NOT_PRESSED;
uint8_t switchflag;
uint16_t loops_per_second = 1000 / Settings.switch_debounce;
@ -140,7 +142,6 @@ void SwitchHandler(byte mode)
for (byte i = 0; i < MAX_SWITCHES; i++) {
if ((pin[GPIO_SWT1 +i] < 99) || (mode)) {
if (holdwallswitch[i]) {
holdwallswitch[i]--;
if (0 == holdwallswitch[i]) {