Source code for simple_menu

import buttons
import color
import display
import sys
import time
import config

TIMEOUT = 0x100
""":py:func:`~simple_menu.button_events` timeout marker."""
LONG_PRESS_MS = 1000
RETRIGGER_MS = 250

try:
    LONG_PRESS_MS = int(config.get_string("long_press_ms"))
except Exception:
    pass

try:
    RETRIGGER_MS = int(config.get_string("retrigger_ms"))
except Exception:
    pass


[docs]def button_events(timeout=None, long_press_ms=LONG_PRESS_MS, retrigger_ms=RETRIGGER_MS): """ Iterate over button presses (event-loop). This is just a helper function used internally by the menu. But you can of course use it for your own scripts as well. It works like this: .. code-block:: python import simple_menu, buttons for ev in simple_menu.button_events(): if ev == buttons.BOTTOM_LEFT: # Left pass elif ev == buttons.BOTTOM_RIGHT: # Right pass elif ev == buttons.TOP_RIGHT: # Select pass .. versionadded:: 1.4 :param float,optional timeout: Timeout after which the generator should yield in any case. If a timeout is defined, the generator will periodically yield :py:data:`simple_menu.TIMEOUT`. .. versionadded:: 1.9 :param int,optional long_press_ms: Time for long key press in ms. .. versionadded:: 1.17 :param int,optional retrigger_ms: Time for repeating key press on hold in ms. .. versionadded:: 1.17 """ yield 0 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) next_tick = time.time_ms() + timeout while True: if timeout is not None: current_time = time.time_ms() if current_time >= next_tick: next_tick += timeout yield TIMEOUT v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if v == 0: button_pressed = False 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): pass