Skip to content
Snippets Groups Projects
Commit 20234d4c authored by schneider's avatar schneider
Browse files

Merge branch 'Pixtxa-master-patch-27969' into 'master'

Simple Menu

See merge request !433
parents 7955f13e 65e9a005
No related branches found
No related tags found
1 merge request!433Simple Menu
Pipeline #5001 passed
......@@ -50,4 +50,8 @@ Option name Type Description
``ble_log_enable`` Boolean Activate HCI level logging of BLE data. Creates a new btsnoop compatible log file named ``ble.log`` in the ``logs`` folder after each boot if BLE is activated. Keeps the last 10 files.
------------------ ---------- -----------
``right_scroll`` Boolean Use both right buttons to scroll up and down. Lower left button is SELECT.
------------------ ---------- -----------
``long_press_ms`` Integer Defines the timespan for a long key press in milliseconds.
------------------ ---------- -----------
``retrigger_ms`` Integer Defines the timespan for repeating key presses when a key is hold in milliseconds.
================== ========== ===========
......@@ -6,10 +6,23 @@ import time
import config
TIMEOUT = 0x100
LONG_PRESS_MS = 1000
RETRIGGER_MS = 250
try:
LONG_PRESS_MS = int(config.get_string("long_press_ms"))
except OSError:
pass
try:
RETRIGGER_MS = int(config.get_string("retrigger_ms"))
except OSError:
pass
""":py:func:`~simple_menu.button_events` timeout marker."""
def button_events(timeout=None):
def button_events(timeout=None, long_press_ms=LONG_PRESS_MS, retrigger_ms=RETRIGGER_MS):
"""
Iterate over button presses (event-loop).
......@@ -39,11 +52,19 @@ def button_events(timeout=None):
: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)
......@@ -60,18 +81,31 @@ 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):
......@@ -198,6 +232,9 @@ class Menu:
right_scroll_str = config.get_string("right_scroll")
if right_scroll_str.lower() in ["true", "1"]:
right_buttons_scroll = True
if right_scroll_str.lower() in ["false", "0"]:
right_buttons_scroll = False
except OSError:
right_buttons_scroll = self.right_buttons_scroll
......@@ -305,14 +342,21 @@ class Menu:
self.disp.update()
def run(self):
"""Start the event-loop."""
def run(self, long_press_ms=LONG_PRESS_MS, retrigger_ms=RETRIGGER_MS):
"""
Start the event-loop.
: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
"""
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)
......@@ -336,10 +380,10 @@ class Menu:
print("Exception during menu.on_scroll():")
sys.print_exception(e)
elif ev == self.button_select:
t0 = time.time()
t0 = time.time_ms()
long_press = False
while buttons.read(buttons.TOP_RIGHT) > 0:
if time.time() - t0 > 1:
while buttons.read(self.button_select) > 0:
if time.time_ms() - t0 > long_press_ms:
long_press = True
break
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment