diff --git a/pycardium/modules/py/simple_menu.py b/pycardium/modules/py/simple_menu.py index 0ce10992b5324f55c0da93c4c8b13c889f30b32f..5432afcd6353140f7e52459678f34d9804fdc243 100644 --- a/pycardium/modules/py/simple_menu.py +++ b/pycardium/modules/py/simple_menu.py @@ -8,8 +8,7 @@ import config TIMEOUT = 0x100 """:py:func:`~simple_menu.button_events` timeout marker.""" - -def button_events(timeout=None): +def button_events(timeout=None, long_press_ms = 1000, retrigger_ms = 250): """ Iterate over button presses (event-loop). @@ -44,6 +43,8 @@ def button_events(timeout=None): v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) button_pressed = True if v != 0 else False + t0 = time.time_ms() + still_pressed = False if timeout is not None: timeout = int(timeout * 1000) @@ -60,18 +61,28 @@ def button_events(timeout=None): if v == 0: button_pressed = False - - if not button_pressed and v & buttons.BOTTOM_LEFT != 0: - button_pressed = True - yield buttons.BOTTOM_LEFT - - if not button_pressed and v & buttons.BOTTOM_RIGHT != 0: - button_pressed = True - yield buttons.BOTTOM_RIGHT - - if not button_pressed and v & buttons.TOP_RIGHT != 0: - button_pressed = True - yield buttons.TOP_RIGHT + still_pressed = False + else: + if not button_pressed: + button_pressed = True + t0 = time.time_ms() + + if v & buttons.BOTTOM_LEFT: + yield buttons.BOTTOM_LEFT + + if v & buttons.BOTTOM_RIGHT: + yield buttons.BOTTOM_RIGHT + + if v & buttons.TOP_RIGHT: + yield buttons.TOP_RIGHT + + if not still_pressed and long_press_ms and time.time_ms() - t0 <= long_press_ms: + pass + else: + if retrigger_ms and time.time_ms() - t0 > retrigger_ms: + button_pressed = False + still_pressed = True + class _ExitMenuException(Exception): @@ -305,14 +316,14 @@ class Menu: self.disp.update() - def run(self, long_press_ms = 1000): + def run(self, long_press_ms = 1000, retrigger_ms = 250): """Start the event-loop.""" try: timeout = self.scroll_speed if self.timeout is not None and self.timeout < self.scroll_speed: timeout = self.timeout - for ev in button_events(timeout): + for ev in button_events(timeout, long_press_ms, retrigger_ms): if ev == buttons.BOTTOM_RIGHT: self.select_time = time.time_ms() self.draw_menu(-8)