aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/buttons.py
diff options
context:
space:
mode:
authorGareth Farrington <gareth@waves.ky>2025-03-20 16:55:33 -0700
committerGitHub <noreply@github.com>2025-03-20 19:55:33 -0400
commit272e815522b0bc8e0806e052b73a5cc1af979cd7 (patch)
tree8cbf07cad71d2113a55c45d53900ac3ad15ba8e2 /klippy/extras/buttons.py
parent06d65ef5ac139e2c84e7381c190ee2b1e4ec4237 (diff)
downloadkutter-272e815522b0bc8e0806e052b73a5cc1af979cd7.tar.gz
kutter-272e815522b0bc8e0806e052b73a5cc1af979cd7.tar.xz
kutter-272e815522b0bc8e0806e052b73a5cc1af979cd7.zip
buttons: Debounce gcode_button and filament_switch_sensor (#6848)
Add `debounce_delay` config option which sets the debounce time, defaults to 0 Signed-off-by: Gareth Farrington <gareth@waves.ky>
Diffstat (limited to 'klippy/extras/buttons.py')
-rw-r--r--klippy/extras/buttons.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/klippy/extras/buttons.py b/klippy/extras/buttons.py
index daa998a9..4f012f51 100644
--- a/klippy/extras/buttons.py
+++ b/klippy/extras/buttons.py
@@ -244,6 +244,33 @@ class HalfStepRotaryEncoder(BaseRotaryEncoder):
BaseRotaryEncoder.R_START | BaseRotaryEncoder.R_DIR_CCW),
)
+class DebounceButton:
+ def __init__(self, config, button_action):
+ self.printer = config.get_printer()
+ self.reactor = self.printer.get_reactor()
+ self.button_action = button_action
+ self.debounce_delay = config.getfloat('debounce_delay', 0., minval=0.)
+ self.logical_state = None
+ self.physical_state = None
+ self.latest_eventtime = None
+ def button_handler(self, eventtime, state):
+ self.physical_state = state
+ self.latest_eventtime = eventtime
+ # if there would be no state transition, ignore the event:
+ if self.logical_state == self.physical_state:
+ return
+ trigger_time = eventtime + self.debounce_delay
+ self.reactor.register_callback(self._debounce_event, trigger_time)
+ def _debounce_event(self, eventtime):
+ # if there would be no state transition, ignore the event:
+ if self.logical_state == self.physical_state:
+ return
+ # if there were more recent events, they supersede this one:
+ if (eventtime - self.debounce_delay) < self.latest_eventtime:
+ return
+ # enact state transition and trigger action
+ self.logical_state = self.physical_state
+ self.button_action(self.latest_eventtime, self.logical_state)
######################################################################
# Button registration code
@@ -261,6 +288,14 @@ class PrinterButtons:
self.adc_buttons[pin] = adc_buttons = MCU_ADC_buttons(
self.printer, pin, pullup)
adc_buttons.setup_button(min_val, max_val, callback)
+ def register_debounce_button(self, pin, callback, config):
+ debounce = DebounceButton(config, callback)
+ return self.register_buttons([pin], debounce.button_handler)
+ def register_debounce_adc_button(self, pin, min_val, max_val, pullup
+ , callback, config):
+ debounce = DebounceButton(config, callback)
+ return self.register_adc_button(pin, min_val. min_val, max_val, pullup
+ , debounce.button_handler)
def register_adc_button_push(self, pin, min_val, max_val, pullup, callback):
def helper(eventtime, state, callback=callback):
if state: