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

hide loop.call_at, use yield or run_forever

This commit is contained in:
Jan Pochyla 2016-04-29 21:48:59 +02:00 committed by Pavol Rusnak
parent 4eb87b2fd8
commit ac28a41d96
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 68 additions and 54 deletions

View File

@ -1,3 +1,5 @@
import utime
from trezor import ui from trezor import ui
from trezor import loop from trezor import loop
from trezor import layout from trezor import layout
@ -6,8 +8,8 @@ from trezor import utils
def layout_homescreen(): def layout_homescreen():
print("Homescreen layout!") print("Homescreen layout!")
from apps import playground # from apps import playground
loop.call_later(5 * 1000000, layout.change(playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC'))) # 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) ui.display.bar(0, 0, 240, 240, ui.WHITE)

View File

@ -18,10 +18,51 @@ evt_handlers = { EVT_TSTART: None,
EVT_MSG: None, } EVT_MSG: None, }
time_queue = [] time_queue = []
if __debug__:
# For performance stats
import array
log_delay_pos = 0
log_delay_rb_len = const(10)
log_delay_rb = array.array('i', [0] * log_delay_rb_len)
def run_forever():
def __wait_for_event(timeout_us):
if __debug__:
# Adding delay to ring buffer for performance stats
global log_delay_pos
global log_delay_rb
global log_delay_rb_len
log_delay_rb[log_delay_pos] = timeout_us
log_delay_pos = (log_delay_pos + 1) % log_delay_rb_len
event = msg.select(timeout_us)
if event:
# print('msg:', m)
# utime.sleep_us(10000)
if event[0] == 2:
ui.display.bar(event[1], event[2], 2, 2, ui.BLACK)
return event
def __call_at(time, gen, *args):
if __debug__:
log.debug("Scheduling %s", (time, gen, args))
if not time:
time = utime.ticks_us()
heappush(time_queue, (time, gen, args))
def sleep(us):
return utime.ticks_us() + us
def run_forever(start_gens):
delay_max = const(1000000) delay_max = const(1000000)
for gen in start_gens:
__call_at(None, gen)
while True: while True:
if time_queue: if time_queue:
@ -30,7 +71,7 @@ def run_forever():
else: else:
delay = delay_max delay = delay_max
event = wait_for_event(delay) event = __wait_for_event(delay)
if event: if event:
# run interrupt handler # run interrupt handler
@ -49,52 +90,17 @@ def run_forever():
if isinstance(ret, type_gen): if isinstance(ret, type_gen):
# generator, run it and call us asap # generator, run it and call us asap
call_at(None, ret) __call_at(None, ret)
call_at(None, gen, *args) __call_at(None, gen, *args)
elif isinstance(ret, int): elif isinstance(ret, int):
if ret >= 0: if ret >= 0:
# sleep in microseconds, call us later # sleep until ret, call us later
call_at(utime.ticks_us() + ret, gen, *args) __call_at(ret, gen, *args)
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, *args)
def call_at(time, gen, *args):
if __debug__:
log.debug("Scheduling %s", (time, gen, args))
if not time:
time = utime.ticks_us()
heappush(time_queue, (time, gen, args))
if __debug__:
# For performance stats
import array
log_delay_pos = 0
log_delay_rb_len = const(10)
log_delay_rb = array.array('i', [0] * log_delay_rb_len)
def wait_for_event(timeout_us):
if __debug__:
# Adding delay to ring buffer for performance stats
global log_delay_pos
global log_delay_rb
global log_delay_rb_len
log_delay_rb[log_delay_pos] = timeout_us
log_delay_pos = (log_delay_pos + 1) % log_delay_rb_len
event = msg.select(timeout_us)
if event:
# print('msg:', m)
# utime.sleep_us(10000)
if event[0] == 2:
ui.display.bar(event[1], event[2], 2, 2, ui.BLACK)
return event

View File

@ -2,13 +2,17 @@ import sys
sys.path.append('lib') sys.path.append('lib')
import gc import gc
import log
log.level = log.INFO
from trezor import loop from trezor import loop
from trezor import layout from trezor import layout
def perf_info_debug(): def perf_info_debug():
while True: while True:
queue = [str(x[2]).split("'")[1] for x in loop.q] queue = [str(x[1]).split("'")[1] for x in loop.time_queue]
delay_avg = sum(loop.log_delay_rb) / loop.log_delay_rb_len delay_avg = sum(loop.log_delay_rb) / loop.log_delay_rb_len
delay_last = loop.log_delay_rb[loop.log_delay_pos] delay_last = loop.log_delay_rb[loop.log_delay_pos]
@ -18,21 +22,23 @@ def perf_info_debug():
print("mem_alloc: %s/%s, delay_avg: %d, delay_last: %d, queue: %s" % \ print("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)
def perf_info(): def perf_info():
while True: while True:
gc.collect() gc.collect()
print("mem_alloc: %d" % gc.mem_alloc()) print("mem_alloc: %d" % gc.mem_alloc())
yield loop.Sleep(1000000) yield loop.sleep(1000000)
def run(main_layout): def run(main_layout):
if __debug__: if __debug__:
loop.call_soon(perf_info_debug()) perf_info_gen = perf_info_debug()
else: else:
loop.call_soon(perf_info()) perf_info_gen = perf_info()
loop.call_soon(layout.set_main(main_layout)) loop.run_forever([
perf_info_gen,
loop.run_forever() layout.set_main(main_layout),
loop.close() ])

View File

@ -53,4 +53,4 @@ def animate_pulse(func, ca, cb, speed=200000, delay=30000):
y = 0.5 + 0.5 * math.sin(utime.ticks_us() / speed) y = 0.5 + 0.5 * math.sin(utime.ticks_us() / speed)
c = blend(ca, cb, y) c = blend(ca, cb, y)
func(c) func(c)
yield loop.Sleep(delay) yield loop.sleep(delay)