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

Implemented basic loop.wait()

This commit is contained in:
slush0 2016-04-30 01:20:57 +02:00 committed by Pavol Rusnak
parent 6a5f16207c
commit b0d9a4b884
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 74 additions and 26 deletions

View File

@ -19,4 +19,16 @@ def layout_homescreen():
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)
yield from ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) 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')

View File

@ -1,5 +1,8 @@
import sys import sys
import utime import utime
from . import utils
import log
_new_layout = None _new_layout = None
_current_layout = None _current_layout = None
@ -7,7 +10,7 @@ _current_layout = None
def change(layout): def change(layout):
global _new_layout global _new_layout
print("Changing layout to %s" % layout) log.debug(__name__, "Changing layout to %s", layout)
_new_layout = layout _new_layout = layout
yield _current_layout.throw(StopIteration()) yield _current_layout.throw(StopIteration())
@ -30,12 +33,12 @@ def set_main(main_layout):
continue continue
if _new_layout != None: if _new_layout != None:
print("Switching to new layout %s" % _new_layout) log.info(__name__, "Switching to new layout %s", _new_layout)
_current_layout = _new_layout _current_layout = _new_layout
_new_layout = None _new_layout = None
elif not callable(_current_layout): elif type(_current_layout) != utils.type_gen:
print("Switching to main layout %s" % main_layout) log.info(__name__, "Switching to main layout %s", main_layout)
_current_layout = main_layout _current_layout = main_layout()
else: else:
print("Switching to proposed layout %s" % _current_layout) log.info(__name__, "Switching to proposed layout %s", _current_layout)

View File

@ -1,4 +1,5 @@
import utime import utime
import sys
from uheapq import heappop, heappush from uheapq import heappop, heappush
from .utils import type_gen from .utils import type_gen
@ -11,6 +12,8 @@ EVT_TMOVE = const(-2)
EVT_TEND = const(-3) EVT_TEND = const(-3)
EVT_MSG = const(-4) EVT_MSG = const(-4)
DO_NOTHING = const(-5)
evt_handlers = { EVT_TSTART: None, evt_handlers = { EVT_TSTART: None,
EVT_TMOVE: None, EVT_TMOVE: None,
EVT_TEND: None, EVT_TEND: None,
@ -43,13 +46,39 @@ def __wait_for_event(timeout_us):
return event return event
def __call_at(time, gen, *args): def __call_at(time, gen):
if __debug__: if __debug__:
log.debug(__name__, 'Scheduling %s %s %s', time, gen, args) log.debug(__name__, 'Scheduling %s %s', time, gen)
if not time: if not time:
time = utime.ticks_us() time = utime.ticks_us()
heappush(time_queue, (time, gen, args)) heappush(time_queue, (time, gen))
def __wait_gen(gen, cb):
if isinstance(gen, int):
ret = yield gen
else:
ret = yield from gen
cb.throw(StopIteration())
def __wait_cb():
while True:
try:
yield sleep(1000000)
except StopIteration:
break
def wait(gens):
cb = __wait_cb()
cb.send(None)
for g in gens:
__call_at(None, __wait_gen(g, cb))
return cb
def sleep(us): def sleep(us):
@ -65,7 +94,7 @@ def run_forever(start_gens):
while True: while True:
if time_queue: if time_queue:
t, _, _ = time_queue[0] t, _ = time_queue[0]
delay = t - utime.ticks_us() delay = t - utime.ticks_us()
else: else:
delay = delay_max delay = delay_max
@ -77,29 +106,33 @@ def run_forever(start_gens):
raise NotImplementedError() raise NotImplementedError()
else: else:
# run something from the time queue # run something from the time queue
_, gen, args = heappop(time_queue) _, gen = heappop(time_queue)
try: try:
if not args: ret = gen.send(None)
args = (None,)
ret = gen.send(*args)
except StopIteration as e: except StopIteration as e:
# gen ended, forget it and go on # gen ended, forget it and go on
continue continue
except Exception as e:
# FIXME
log.error(__name__, str(e))
sys.print_exception(e)
# log.exception(__name__, e)
continue
if isinstance(ret, type_gen): if isinstance(ret, int):
# generator, run it and call us asap
__call_at(None, ret)
__call_at(None, gen, *args)
elif isinstance(ret, int):
if ret >= 0: if ret >= 0:
# sleep until ret, call us later # sleep until ret, call us later
__call_at(ret, gen, *args) __call_at(ret, gen)
elif ret == DO_NOTHING:
print("Removing gen from time queue")
else: else:
# wait for event # wait for event
raise NotImplementedError() raise NotImplementedError()
elif ret is None: elif ret is None:
# just call us asap # just call us asap
__call_at(None, gen, *args) __call_at(None, gen)
else:
raise Exception("Unhandled result %s" % gen)

View File

@ -19,8 +19,8 @@ def perf_info_debug():
mem_alloc = gc.mem_alloc() mem_alloc = gc.mem_alloc()
gc.collect() gc.collect()
print("mem_alloc: %s/%s, delay_avg: %d, delay_last: %d, queue: %s" % \ log.info(__name__, "mem_alloc: %s/%s, delay_avg: %d, delay_last: %d, queue: %s",
(mem_alloc, gc.mem_alloc(), delay_avg, delay_last, ', '.join(queue))) mem_alloc, gc.mem_alloc(), delay_avg, delay_last, ', '.join(queue))
yield loop.sleep(1000000) yield loop.sleep(1000000)
@ -28,7 +28,7 @@ def perf_info_debug():
def perf_info(): def perf_info():
while True: while True:
gc.collect() gc.collect()
print("mem_alloc: %d" % gc.mem_alloc()) log.info(__name__, "mem_alloc: %d", gc.mem_alloc())
yield loop.sleep(1000000) yield loop.sleep(1000000)