From d612037eda24b860b12023e1f54099e15b3f008f Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 3 May 2016 18:35:11 +0200 Subject: [PATCH] proof of concept for waiting for events --- src/apps/playground/__init__.py | 20 +++++++++++++------ src/trezor/loop.py | 35 ++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/apps/playground/__init__.py b/src/apps/playground/__init__.py index a71fdeee..2b569802 100644 --- a/src/apps/playground/__init__.py +++ b/src/apps/playground/__init__.py @@ -2,6 +2,7 @@ from trezor import loop from trezor import ui from trezor.utils import unimport_func + def layout_tap_to_confirm(address, amount, currency): ui.display.bar(0, 0, 240, 40, ui.GREEN) @@ -14,15 +15,20 @@ def layout_tap_to_confirm(address, amount, currency): ui.display.text(10, 160, address[18:], ui.MONO, ui.BLACK, ui.WHITE) f = open('apps/playground/tap_64.toig', 'rb') - _background = ui.WHITE - - def func(foreground): - ui.display.text(68, 212, 'TAP TO CONFIRM', ui.BOLD, foreground, _background) + bg = ui.WHITE + def func(fg): + ui.display.text(68, 212, 'TAP TO CONFIRM', ui.BOLD, fg, bg) f.seek(0) - ui.display.icon(3, 170, f.read(), _background, foreground) + ui.display.icon(3, 170, f.read(), bg, fg) + + animation = ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000) + + yield loop.Wait([ + animation, + loop.EVT_TSTART, + ]) - yield from ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000) @unimport_func def zprava(): @@ -43,10 +49,12 @@ def zprava(): # m2 = GetAddress.load(BytesIO(data)) # print(m2.__dict__) + def dispatch(): # Callback for HID messages print("Dispatch playground") + def boot(): # Initilize app on boot time. # This should hookup HID message types dispatcher() wants to receive. diff --git a/src/trezor/loop.py b/src/trezor/loop.py index 1b4262ca..9f06a50b 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -10,7 +10,7 @@ EVT_TMOVE = const(-2) EVT_TEND = const(-3) EVT_MSG = const(-4) -evt_handlers = { +event_handlers = { EVT_TSTART: None, EVT_TMOVE: None, EVT_TEND: None, @@ -101,19 +101,27 @@ def run_forever(start_gens): if event: # Run interrupt handler - log.info(__name__, "Received data: %s", event) - continue + event_id, *args = event + event_id = -event_id + gen = event_handlers.get(event_id, None) + event_handlers[event_id] = None + if not gen: + log.info(__name__, 'No handler for event: %s', event) + continue + if not args: + args = None else: if time_queue: # Run something from the time queue _, gen = heappop(time_queue) + args = None else: # Sleep again delay = delay_max continue try: - ret = gen.send(None) + ret = gen.send(args) except StopIteration as e: log.debug(__name__, '%s finished', gen) @@ -123,13 +131,16 @@ def run_forever(start_gens): log.exception(__name__, e) continue - if isinstance(ret, int): - if ret >= 0: - # Sleep until ret, call us later - __call_at(ret, gen) - else: - # Wait for event - raise NotImplementedError() + if isinstance(ret, int) and ret >= 0: + # Sleep until ret, call us later + __call_at(ret, gen) + + elif isinstance(ret, int) and ret in event_handlers: + # Wait for event + if event_handlers[ret]: + raise Exception('Already waiting for %s: %s' % + (ret, event_handlers[ret])) + event_handlers[ret] = gen elif isinstance(ret, Wait): # Register the origin generator as a waiting callback @@ -140,4 +151,4 @@ def run_forever(start_gens): __call_at(None, gen) else: - raise Exception("Unhandled result %s by %s" % (ret, gen)) + raise Exception('Unhandled result %s by %s' % (ret, gen))