1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

Reworked wait into class

This commit is contained in:
slush0 2016-05-02 16:06:08 +02:00 committed by Pavol Rusnak
parent b7b57ae53e
commit 06515321cf
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 37 additions and 49 deletions

View File

@ -7,17 +7,17 @@ def layout_homescreen():
# ui.display.bar(0, 0, 240, 240, ui.WHITE) # ui.display.bar(0, 0, 240, 240, ui.WHITE)
# f = open('apps/homescreen/trezor.toig', 'r') f = open('apps/homescreen/trezor.toig', 'r')
# def func(foreground): def func(foreground):
# f.seek(0) f.seek(0)
# ui.display.icon(0, 0, f.read(), foreground, ui.BLACK) ui.display.icon(0, 0, f.read(), foreground, ui.BLACK)
# animation = ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) animation = ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000)
timeout = loop.sleep(1000 * 1000) timeout = loop.sleep(5000 * 1000)
yield loop.wait([timeout]) yield loop.Wait([animation, timeout])
print('back to layout') print('back to layout')
# try: # try:
@ -25,5 +25,5 @@ def layout_homescreen():
# except: # except:
# pass # pass
# from apps import playground from apps import playground
# return playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC') return playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC')

View File

@ -36,49 +36,38 @@ def __call_at(time, gen):
time = utime.ticks_us() time = utime.ticks_us()
heappush(time_queue, (time, gen)) heappush(time_queue, (time, gen))
class Wait():
def __init__(self, gens, wait_for=1, exit_others=True):
self.wait_for = wait_for
self.exit_others = exit_others
self.received = 0
self.callback = None
self.gens = gens
def __wait_for_gen(gen, cb): for g in gens:
if isinstance(gen, type_gen): __call_at(None, self._wait(g))
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)
log.info(__name__, '__wait_gen returning %s', ret) def _wait(self, gen):
if isinstance(gen, type_gen):
ret = yield from gen
else:
ret = yield gen
self.finish(gen, ret)
class __Wait(): def finish(self, gen, result):
pass self.received += 1
if self.received >= self.wait_for:
__call_at(None, self.callback)
self.callback = None
def __wait_callback(call_after): if self.exit_others:
# TODO: rewrite as instance of __Wait instead of generator for g in self.gens:
delegate = yield try:
received = 0 g.throw(StopIteration())
while received < call_after: except:
try: pass
yield
except StopIteration:
received += 1
__call_at(None, delegate)
def wait_for_first(gens):
cb = __wait_callback(1)
for g in gens:
__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
def sleep(us): def sleep(us):
return utime.ticks_us() + us return utime.ticks_us() + us
@ -138,10 +127,9 @@ def run_forever(start_gens):
# wait for event # wait for event
raise NotImplementedError() raise NotImplementedError()
elif isinstance(ret, type_gen): elif isinstance(ret, Wait):
log.info(__name__, 'Scheduling %s -> %s', gen, ret) log.info(__name__, 'Scheduling %s -> %s', gen, ret)
ret.send(None) ret.callback = gen
ret.send(gen)
elif ret is None: elif ret is None:
# just call us asap # just call us asap