menu_keys: Reschedule long-click timer on each click

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-03-15 12:15:15 -04:00
parent 7d8ade74e8
commit 8d9566b945
1 changed files with 15 additions and 20 deletions

View File

@ -11,6 +11,7 @@ TIMER_DELAY = .200
class MenuKeys:
def __init__(self, config, callback):
self.printer = config.get_printer()
self.reactor = self.printer.get_reactor()
self.callback = callback
# buttons
self.encoder_pins = config.get('encoder_pins', None)
@ -30,7 +31,6 @@ class MenuKeys:
'analog_range_down_pin', None)
self.analog_range_kill_pin = config.get(
'analog_range_kill_pin', None)
self._last_click_press = 0
self.analog_pullup = config.getfloat(
'analog_pullup_resistor', 4700., above=0.)
self._encoder_fast_rate = config.getfloat(
@ -120,18 +120,14 @@ class MenuKeys:
else:
self.buttons.register_button_push(
self.kill_pin, self.kill_callback)
# start timer
reactor = self.printer.get_reactor()
reactor.register_timer(self.timer_event, reactor.NOW)
# long click timer
self.is_short_click = False
self.click_timer = self.reactor.register_timer(self.long_click_event)
def timer_event(self, eventtime):
# check long press
if (self._last_click_press > 0 and (
eventtime - self._last_click_press) >= LONG_PRESS_DURATION):
# long click
self._last_click_press = 0
def long_click_event(self, eventtime):
self.is_short_click = False
self.callback('long_click', eventtime)
return eventtime + TIMER_DELAY
return self.reactor.NEVER
# buttons & encoder callbacks
def encoder_cw_callback(self, eventtime):
@ -153,13 +149,12 @@ class MenuKeys:
self.callback('down', eventtime)
def click_callback(self, eventtime, state):
if self.click_pin:
if state:
self._last_click_press = eventtime
elif self._last_click_press > 0:
if (eventtime - self._last_click_press) < LONG_PRESS_DURATION:
# short click
self._last_click_press = 0
self.is_short_click = True
self.reactor.update_timer(self.click_timer,
eventtime + LONG_PRESS_DURATION)
elif self.is_short_click:
self.reactor.update_timer(self.click_timer, self.reactor.NEVER)
self.callback('click', eventtime)
def back_callback(self, eventtime):