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."""


[docs]def button_events(timeout=None): """ 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 """ yield 0 v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) button_pressed = True if v != 0 else 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 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
class _ExitMenuException(Exception): pass