mirror of
https://github.com/arendst/Tasmota.git
synced 2025-04-25 23:37:16 +00:00
Merge pull request #15867 from lugerun/improve-button-debounceing
Improve button debouncing
This commit is contained in:
commit
eebf98dc59
@ -43,7 +43,9 @@ struct BUTTON {
|
|||||||
uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold
|
uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold
|
||||||
uint16_t dual_code = 0; // Sonoff dual received code
|
uint16_t dual_code = 0; // Sonoff dual received code
|
||||||
|
|
||||||
|
uint8_t debounce_phase = 0; // 0 = regular iteration; 1 = read data for debouncing only
|
||||||
uint8_t last_state[MAX_KEYS]; // Last button states
|
uint8_t last_state[MAX_KEYS]; // Last button states
|
||||||
|
uint8_t last_state_between[MAX_KEYS]; // Last button states at the half time read
|
||||||
uint8_t window_timer[MAX_KEYS] = { 0 }; // Max time between button presses to record press count
|
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 press_counter[MAX_KEYS] = { 0 }; // Number of button presses within Button.window_timer
|
||||||
|
|
||||||
@ -91,6 +93,7 @@ void ButtonInit(void) {
|
|||||||
#endif // ESP8266
|
#endif // ESP8266
|
||||||
for (uint32_t i = 0; i < MAX_KEYS; i++) {
|
for (uint32_t i = 0; i < MAX_KEYS; i++) {
|
||||||
Button.last_state[i] = NOT_PRESSED;
|
Button.last_state[i] = NOT_PRESSED;
|
||||||
|
Button.last_state_between[i] = NOT_PRESSED;
|
||||||
if (PinUsed(GPIO_KEY1, i)) {
|
if (PinUsed(GPIO_KEY1, i)) {
|
||||||
Button.present++;
|
Button.present++;
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
@ -204,6 +207,18 @@ 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
|
||||||
|
if (Button.debounce_phase == 1) {
|
||||||
|
// adjust values
|
||||||
|
Button.last_state_between[button_index] = button;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (button != Button.last_state_between[button_index]) {
|
||||||
|
// no clean signal, just keep the previous one
|
||||||
|
button = Button.last_state[button_index];
|
||||||
|
}
|
||||||
|
|
||||||
if (button_present) {
|
if (button_present) {
|
||||||
XdrvMailbox.index = button_index;
|
XdrvMailbox.index = button_index;
|
||||||
XdrvMailbox.payload = button;
|
XdrvMailbox.payload = button;
|
||||||
@ -374,6 +389,8 @@ void ButtonHandler(void) {
|
|||||||
}
|
}
|
||||||
Button.last_state[button_index] = button;
|
Button.last_state[button_index] = button;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Button.debounce_phase = (Button.debounce_phase + 1) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -409,7 +426,7 @@ void MqttButtonTopic(uint32_t button_id, uint32_t action, uint32_t hold) {
|
|||||||
void ButtonLoop(void) {
|
void ButtonLoop(void) {
|
||||||
if (Button.present) {
|
if (Button.present) {
|
||||||
if (TimeReached(Button.debounce)) {
|
if (TimeReached(Button.debounce)) {
|
||||||
SetNextTimeInterval(Button.debounce, Settings->button_debounce); // ButtonDebounce (50)
|
SetNextTimeInterval(Button.debounce, Settings->button_debounce / 2);
|
||||||
ButtonHandler();
|
ButtonHandler();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user