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 ( self.callback('long_click', eventtime)
eventtime - self._last_click_press) >= LONG_PRESS_DURATION): return self.reactor.NEVER
# long click
self._last_click_press = 0
self.callback('long_click', eventtime)
return eventtime + TIMER_DELAY
# buttons & encoder callbacks # buttons & encoder callbacks
def encoder_cw_callback(self, eventtime): def encoder_cw_callback(self, eventtime):
@ -153,14 +149,13 @@ 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.is_short_click = True
self._last_click_press = eventtime self.reactor.update_timer(self.click_timer,
elif self._last_click_press > 0: eventtime + LONG_PRESS_DURATION)
if (eventtime - self._last_click_press) < LONG_PRESS_DURATION: elif self.is_short_click:
# short click self.reactor.update_timer(self.click_timer, self.reactor.NEVER)
self._last_click_press = 0 self.callback('click', eventtime)
self.callback('click', eventtime)
def back_callback(self, eventtime): def back_callback(self, eventtime):
if self.back_pin: if self.back_pin: