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