mirror of
https://github.com/arendst/Tasmota.git
synced 2025-04-25 23:37:16 +00:00
Refactor buttons and switches Pt.2
This commit is contained in:
parent
e0584b2157
commit
dab80f9d29
@ -53,7 +53,6 @@ struct BUTTON {
|
|||||||
uint8_t press_counter[MAX_KEYS_SET] = { 0 }; // Number of button presses within Button.window_timer
|
uint8_t press_counter[MAX_KEYS_SET] = { 0 }; // Number of button presses within Button.window_timer
|
||||||
uint8_t dual_receive_count = 0; // Sonoff dual input flag
|
uint8_t dual_receive_count = 0; // Sonoff dual input flag
|
||||||
uint8_t first_change = 0;
|
uint8_t first_change = 0;
|
||||||
uint8_t present = 0; // Number of buttons found flag
|
|
||||||
bool probe_mutex;
|
bool probe_mutex;
|
||||||
} Button;
|
} Button;
|
||||||
|
|
||||||
@ -92,6 +91,11 @@ void ButtonSetVirtualPinState(uint32_t index, uint32_t state) {
|
|||||||
bitWrite(Button.virtual_pin, index, state);
|
bitWrite(Button.virtual_pin, index, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t ButtonGetState(uint32_t index) {
|
||||||
|
// Get current state
|
||||||
|
return Button.debounced_state[index];
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t ButtonLastState(uint32_t index) {
|
uint8_t ButtonLastState(uint32_t index) {
|
||||||
// Get last state
|
// Get last state
|
||||||
return Button.last_state[index];
|
return Button.last_state[index];
|
||||||
@ -228,22 +232,16 @@ void ButtonProbe(void) {
|
|||||||
|
|
||||||
void ButtonInit(void) {
|
void ButtonInit(void) {
|
||||||
bool ac_detect = (Settings->button_debounce % 10 == 9);
|
bool ac_detect = (Settings->button_debounce % 10 == 9);
|
||||||
|
|
||||||
Button.present = 0;
|
|
||||||
Button.used = 0;
|
Button.used = 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_SET; i++) {
|
for (uint32_t i = 0; i < MAX_KEYS_SET; i++) {
|
||||||
Button.last_state[i] = NOT_PRESSED;
|
Button.last_state[i] = NOT_PRESSED;
|
||||||
bool used = false;
|
#ifdef ESP8266
|
||||||
|
if ((0 == i) && ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type))) {
|
||||||
|
bitSet(Button.used, i); // This pin is used
|
||||||
|
} else
|
||||||
|
#endif // ESP8266
|
||||||
if (PinUsed(GPIO_KEY1, i)) {
|
if (PinUsed(GPIO_KEY1, i)) {
|
||||||
Button.present++;
|
bitSet(Button.used, i); // This pin is used
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
pinMode(Pin(GPIO_KEY1, i), bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_KEY1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
pinMode(Pin(GPIO_KEY1, i), bitRead(Button.no_pullup_mask, i) ? INPUT : ((16 == Pin(GPIO_KEY1, i)) ? INPUT_PULLDOWN_16 : INPUT_PULLUP));
|
||||||
#endif // ESP8266
|
#endif // ESP8266
|
||||||
@ -252,11 +250,14 @@ void ButtonInit(void) {
|
|||||||
#endif // ESP32
|
#endif // ESP32
|
||||||
// Set global now so doesn't change the saved power state on first button check
|
// 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));
|
Button.last_state[i] = (digitalRead(Pin(GPIO_KEY1, i)) != bitRead(Button.inverted_mask, i));
|
||||||
used = true;
|
if (ac_detect) {
|
||||||
|
Button.state[i] = 0x80 + 2 * BUTTON_AC_PERIOD;
|
||||||
|
Button.last_state[i] = 0; // Will set later in the debouncing code
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#ifdef USE_ADC
|
#ifdef USE_ADC
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON, i) || PinUsed(GPIO_ADC_BUTTON_INV, i)) {
|
else if (PinUsed(GPIO_ADC_BUTTON, i) || PinUsed(GPIO_ADC_BUTTON_INV, i)) {
|
||||||
Button.present++;
|
bitSet(Button.used, i); // This pin is used
|
||||||
}
|
}
|
||||||
#endif // USE_ADC
|
#endif // USE_ADC
|
||||||
else {
|
else {
|
||||||
@ -268,7 +269,6 @@ void ButtonInit(void) {
|
|||||||
At exit:
|
At exit:
|
||||||
XdrvMailbox.index bit 0 = current state
|
XdrvMailbox.index bit 0 = current state
|
||||||
*/
|
*/
|
||||||
Button.present++;
|
|
||||||
bitSet(Button.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
|
||||||
@ -276,22 +276,15 @@ void ButtonInit(void) {
|
|||||||
// last_state[i] must be 1 to indicate no button pressed
|
// last_state[i] must be 1 to indicate no button pressed
|
||||||
Button.last_state[i] = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i));
|
Button.last_state[i] = (bitRead(Button.virtual_pin, i) != bitRead(Button.inverted_mask, i));
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Add vButton%d, State %d"), Button.present, Button.last_state[i]);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Add vButton%d, State %d"), i +1, Button.last_state[i]);
|
||||||
|
|
||||||
used = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (used && ac_detect) {
|
|
||||||
Button.state[i] = 0x80 + 2 * BUTTON_AC_PERIOD;
|
|
||||||
Button.last_state[i] = 0; // Will set later in the debouncing code
|
|
||||||
}
|
|
||||||
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.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.used) { // Any bit set
|
||||||
Button.first_change = true;
|
Button.first_change = true;
|
||||||
TickerButton.attach_ms((ac_detect) ? BUTTON_FAST_PROBE_INTERVAL : BUTTON_PROBE_INTERVAL, ButtonProbe);
|
TickerButton.attach_ms((ac_detect) ? BUTTON_FAST_PROBE_INTERVAL : BUTTON_PROBE_INTERVAL, ButtonProbe);
|
||||||
}
|
}
|
||||||
@ -338,12 +331,12 @@ void ButtonHandler(void) {
|
|||||||
char scmnd[20];
|
char scmnd[20];
|
||||||
|
|
||||||
for (uint32_t button_index = 0; button_index < MAX_KEYS_SET; button_index++) {
|
for (uint32_t button_index = 0; button_index < MAX_KEYS_SET; button_index++) {
|
||||||
uint8_t button = NOT_PRESSED;
|
if (!bitRead(Button.used, button_index)) { return; }
|
||||||
uint8_t button_present = 0;
|
|
||||||
|
uint8_t button = Button.debounced_state[button_index];
|
||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
if (!button_index && ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type))) {
|
if (!button_index && ((SONOFF_DUAL == TasmotaGlobal.module_type) || (CH4 == TasmotaGlobal.module_type))) {
|
||||||
button_present = 1;
|
|
||||||
if (Button.dual_code) {
|
if (Button.dual_code) {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Code %04X"), Button.dual_code);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("BTN: Code %04X"), Button.dual_code);
|
||||||
button = PRESSED;
|
button = PRESSED;
|
||||||
@ -352,6 +345,8 @@ void ButtonHandler(void) {
|
|||||||
hold_time_extent = 1;
|
hold_time_extent = 1;
|
||||||
}
|
}
|
||||||
Button.dual_code = 0;
|
Button.dual_code = 0;
|
||||||
|
} else {
|
||||||
|
button = NOT_PRESSED;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
#endif // ESP8266
|
#endif // ESP8266
|
||||||
@ -371,28 +366,19 @@ void ButtonHandler(void) {
|
|||||||
}
|
}
|
||||||
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
|
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;
|
continue;
|
||||||
} else
|
}
|
||||||
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
#endif // ESP32 SOC_TOUCH_VERSION_1 or SOC_TOUCH_VERSION_2
|
||||||
|
|
||||||
button_present = 1;
|
|
||||||
button = Button.debounced_state[button_index];
|
|
||||||
}
|
}
|
||||||
#ifdef USE_ADC
|
#ifdef USE_ADC
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON, button_index)) {
|
else if (PinUsed(GPIO_ADC_BUTTON, button_index)) {
|
||||||
button_present = 1;
|
|
||||||
button = AdcGetButton(Pin(GPIO_ADC_BUTTON, button_index));
|
button = AdcGetButton(Pin(GPIO_ADC_BUTTON, button_index));
|
||||||
}
|
}
|
||||||
else if (PinUsed(GPIO_ADC_BUTTON_INV, button_index)) {
|
else if (PinUsed(GPIO_ADC_BUTTON_INV, button_index)) {
|
||||||
button_present = 1;
|
|
||||||
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.used, button_index)) {
|
|
||||||
button_present = 1;
|
|
||||||
button = Button.debounced_state[button_index];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (button_present) {
|
|
||||||
XdrvMailbox.index = button_index;
|
XdrvMailbox.index = button_index;
|
||||||
XdrvMailbox.payload = button;
|
XdrvMailbox.payload = button;
|
||||||
XdrvMailbox.command_code = Button.last_state[button_index];
|
XdrvMailbox.command_code = Button.last_state[button_index];
|
||||||
@ -498,7 +484,7 @@ void ButtonHandler(void) {
|
|||||||
#endif // ESP8266
|
#endif // ESP8266
|
||||||
{
|
{
|
||||||
single_press = (Settings->flag.button_swap +1 == Button.press_counter[button_index]); // SetOption11 (0)
|
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 ((1 == Button.used) && (2 == TasmotaGlobal.devices_present)) { // Single Button with two devices only
|
||||||
if (Settings->flag.button_swap) { // SetOption11 (0)
|
if (Settings->flag.button_swap) { // SetOption11 (0)
|
||||||
Button.press_counter[button_index] = (single_press) ? 1 : 2;
|
Button.press_counter[button_index] = (single_press) ? 1 : 2;
|
||||||
}
|
}
|
||||||
@ -558,7 +544,6 @@ void ButtonHandler(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Button.last_state[button_index] = button;
|
Button.last_state[button_index] = button;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -576,7 +561,7 @@ void MqttButtonTopic(uint32_t button_id, uint32_t action, uint32_t hold) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ButtonLoop(void) {
|
void ButtonLoop(void) {
|
||||||
if (Button.present) {
|
if (Button.used) {
|
||||||
if (TimeReached(Button.debounce)) {
|
if (TimeReached(Button.debounce)) {
|
||||||
SetNextTimeInterval(Button.debounce, Settings->button_debounce); // ButtonDebounce (50)
|
SetNextTimeInterval(Button.debounce, Settings->button_debounce); // ButtonDebounce (50)
|
||||||
ButtonHandler();
|
ButtonHandler();
|
||||||
|
@ -76,7 +76,10 @@ void SwitchSetVirtualPinState(uint32_t index, uint32_t state) {
|
|||||||
|
|
||||||
void SwitchSetState(uint32_t index, uint32_t state) {
|
void SwitchSetState(uint32_t index, uint32_t state) {
|
||||||
// Set debounced pin state to be used by late detected switches
|
// Set debounced pin state to be used by late detected switches
|
||||||
bitSet(Switch.used, index); // Force use bit as call maybe late
|
if (!bitRead(Switch.used, index)) {
|
||||||
|
bitSet(Switch.used, index);
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), index +1, state);
|
||||||
|
}
|
||||||
Switch.debounced_state[index] = state;
|
Switch.debounced_state[index] = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,16 +221,14 @@ void SwitchProbe(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Switch.probe_mutex = false;
|
Switch.probe_mutex = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwitchInit(void) {
|
void SwitchInit(void) {
|
||||||
bool ac_detect = (Settings->switch_debounce % 10 == 9);
|
bool ac_detect = (Settings->switch_debounce % 10 == 9);
|
||||||
|
|
||||||
Switch.used = 0;
|
Switch.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;
|
||||||
if (PinUsed(GPIO_SWT1, i)) {
|
if (PinUsed(GPIO_SWT1, i)) {
|
||||||
bitSet(Switch.used, i); // This pin is used
|
bitSet(Switch.used, i); // This pin is used
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
@ -237,6 +238,10 @@ 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
|
||||||
|
if (ac_detect) {
|
||||||
|
Switch.state[i] = 0x80 + 2 * SWITCH_AC_PERIOD;
|
||||||
|
Switch.last_state[i] = 0; // Will set later in the debouncing code
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
XdrvMailbox.index = i;
|
XdrvMailbox.index = i;
|
||||||
@ -255,11 +260,6 @@ void SwitchInit(void) {
|
|||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), i +1, Switch.last_state[i]);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SWT: Add vSwitch%d, State %d"), i +1, Switch.last_state[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bitRead(Switch.used, i) && ac_detect) {
|
|
||||||
Switch.state[i] = 0x80 + 2 * SWITCH_AC_PERIOD;
|
|
||||||
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,7 +281,8 @@ void SwitchHandler(void) {
|
|||||||
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 (bitRead(Switch.used, i)) {
|
if (!bitRead(Switch.used, i)) { return; }
|
||||||
|
|
||||||
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;
|
||||||
@ -489,7 +490,6 @@ void SwitchHandler(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SwitchLoop(void) {
|
void SwitchLoop(void) {
|
||||||
if (Switch.used) {
|
if (Switch.used) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user