diff --git a/src/apps/homescreen/layout_homescreen.py b/src/apps/homescreen/layout_homescreen.py index 1c23436661..93097e8464 100644 --- a/src/apps/homescreen/layout_homescreen.py +++ b/src/apps/homescreen/layout_homescreen.py @@ -1,3 +1,5 @@ +import utime + from trezor import ui from trezor import loop from trezor import layout @@ -6,8 +8,8 @@ from trezor import utils def layout_homescreen(): print("Homescreen layout!") - from apps import playground - loop.call_later(5 * 1000000, layout.change(playground.layout_tap_to_confirm('1BitkeyP2nDd5oa64x7AjvBbbwST54W5Zmx2', 110.126967, 'BTC'))) + # 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) diff --git a/src/trezor/loop.py b/src/trezor/loop.py index 3321375365..22dd88427d 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -18,10 +18,51 @@ evt_handlers = { EVT_TSTART: None, EVT_MSG: None, } 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) + for gen in start_gens: + __call_at(None, gen) + while True: if time_queue: @@ -30,7 +71,7 @@ def run_forever(): else: delay = delay_max - event = wait_for_event(delay) + event = __wait_for_event(delay) if event: # run interrupt handler @@ -49,52 +90,17 @@ def run_forever(): if isinstance(ret, type_gen): # generator, run it and call us asap - call_at(None, ret) - call_at(None, gen, *args) + __call_at(None, ret) + __call_at(None, gen, *args) elif isinstance(ret, int): if ret >= 0: - # sleep in microseconds, call us later - call_at(utime.ticks_us() + ret, gen, *args) + # sleep until ret, call us later + __call_at(ret, gen, *args) else: # wait for event raise NotImplementedError() elif ret is None: # just call us asap - 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 + __call_at(None, gen, *args) diff --git a/src/trezor/main.py b/src/trezor/main.py index a1b08af42f..fb674af188 100644 --- a/src/trezor/main.py +++ b/src/trezor/main.py @@ -2,13 +2,17 @@ import sys sys.path.append('lib') import gc +import log + +log.level = log.INFO from trezor import loop from trezor import layout + def perf_info_debug(): 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_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" % \ (mem_alloc, gc.mem_alloc(), delay_avg, delay_last, ', '.join(queue))) - yield loop.Sleep(1000000) + yield loop.sleep(1000000) + def perf_info(): while True: gc.collect() print("mem_alloc: %d" % gc.mem_alloc()) - yield loop.Sleep(1000000) + yield loop.sleep(1000000) + def run(main_layout): if __debug__: - loop.call_soon(perf_info_debug()) + perf_info_gen = perf_info_debug() else: - loop.call_soon(perf_info()) + perf_info_gen = perf_info() - loop.call_soon(layout.set_main(main_layout)) - - loop.run_forever() - loop.close() + loop.run_forever([ + perf_info_gen, + layout.set_main(main_layout), + ]) diff --git a/src/trezor/ui.py b/src/trezor/ui.py index 88e3faf748..2a2aeed9e7 100644 --- a/src/trezor/ui.py +++ b/src/trezor/ui.py @@ -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) c = blend(ca, cb, y) func(c) - yield loop.Sleep(delay) + yield loop.sleep(delay)