From c97db79d142b649566e303f896ebd0fc5a2d7236 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Sat, 30 Apr 2016 15:57:08 +0200 Subject: [PATCH] WIP wait fix --- src/apps/homescreen/layout_homescreen.py | 49 ++++++++--------- src/trezor/layout.py | 2 +- src/trezor/loop.py | 67 +++++++++++++++++------- 3 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/apps/homescreen/layout_homescreen.py b/src/apps/homescreen/layout_homescreen.py index 861a216f7..d95768691 100644 --- a/src/apps/homescreen/layout_homescreen.py +++ b/src/apps/homescreen/layout_homescreen.py @@ -1,34 +1,29 @@ -import utime - from trezor import ui from trezor import loop -from trezor import layout -from trezor import utils + def layout_homescreen(): print("Homescreen layout!") + # ui.display.bar(0, 0, 240, 240, ui.WHITE) + + # f = open('apps/homescreen/trezor.toig', 'r') + + # def func(foreground): + # f.seek(0) + # ui.display.icon(0, 0, f.read(), foreground, ui.BLACK) + + # animation = ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) + + timeout = loop.sleep(1000 * 1000) + + yield loop.wait([timeout]) + print('back to layout') + + # try: + # print(animation.throw(StopIteration())) + # except: + # pass + # from apps import playground - # loop.call_at(utime.ticks_us() + 5 * 1000000, layout.change(playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC'))) - - ui.display.bar(0, 0, 240, 240, ui.WHITE) - - f = open('apps/homescreen/trezor.toig', 'r') - - def func(foreground): - f.seek(0) - ui.display.icon(0, 0, f.read(), foreground, ui.BLACK) - - animation = ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) - - timeout = loop.sleep(3 * 1000000) - - yield from loop.wait([timeout, animation]) - - try: - print(animation.throw(StopIteration())) - except: - pass - - from apps import playground - return playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC') + # return playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC') diff --git a/src/trezor/layout.py b/src/trezor/layout.py index a6bf92714..0d5c6c122 100644 --- a/src/trezor/layout.py +++ b/src/trezor/layout.py @@ -1,8 +1,8 @@ import sys import utime -from . import utils from . import log +from . import utils _new_layout = None _current_layout = None diff --git a/src/trezor/loop.py b/src/trezor/loop.py index a8b936658..25a64a6f1 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -11,10 +11,14 @@ EVT_TMOVE = const(-2) EVT_TEND = const(-3) EVT_MSG = const(-4) -evt_handlers = { EVT_TSTART: None, - EVT_TMOVE: None, - EVT_TEND: None, - EVT_MSG: None, } +DO_NOTHING = const(-5) + +evt_handlers = { + EVT_TSTART: None, + EVT_TMOVE: None, + EVT_TEND: None, + EVT_MSG: None, +} time_queue = [] if __debug__: @@ -32,30 +36,47 @@ def __call_at(time, gen): time = utime.ticks_us() heappush(time_queue, (time, gen)) -def __wait_gen(gen, cb): - if isinstance(gen, int): - ret = yield gen - else: +def __wait_for_gen(gen, cb): + if isinstance(gen, type_gen): ret = yield from gen + else: + ret = yield gen + try: + cb.throw(StopIteration) + except Exception as e: + log.info(__name__, '__wait_gen throw raised %s', e) - cb.throw(StopIteration()) + log.info(__name__, '__wait_gen returning %s', ret) -def __wait_cb(): - while True: +class __Wait(): + pass + + +def __wait_callback(call_after): + # TODO: rewrite as instance of __Wait instead of generator + delegate = yield + received = 0 + while received < call_after: try: - yield sleep(1000000) + yield except StopIteration: - break + received += 1 + __call_at(None, delegate) -def wait(gens): - cb = __wait_cb() - cb.send(None) +def wait_for_first(gens): + cb = __wait_callback(1) for g in gens: - __call_at(None, __wait_gen(g, cb)) + __call_at(None, __wait_for_gen(g, cb)) + return cb + +def wait_for_all(gens): + cb = __wait_callback(len(gens)) + for g in gens: + __call_at(None, __wait_for_gen(g, cb)) return cb @@ -99,9 +120,12 @@ def run_forever(start_gens): try: ret = gen.send(None) + except StopIteration as e: + log.info(__name__, '%s ended', gen) # gen ended, forget it and go on continue + except Exception as e: log.exception(__name__, e) continue @@ -114,9 +138,14 @@ def run_forever(start_gens): # wait for event raise NotImplementedError() + elif isinstance(ret, type_gen): + log.info(__name__, 'Scheduling %s -> %s', gen, ret) + ret.send(None) + ret.send(gen) + elif ret is None: # just call us asap __call_at(None, gen) - else: - raise Exception("Unhandled result %s" % gen) + else: + raise Exception("Unhandled result %s by %s" % (ret, gen))