Implemented basic loop.wait()

pull/25/head
slush0 8 years ago committed by Pavol Rusnak
parent 6a5f16207c
commit b0d9a4b884
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -19,4 +19,16 @@ def layout_homescreen():
f.seek(0)
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')

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

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

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

Loading…
Cancel
Save