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

Playground

This commit is contained in:
slush0 2016-04-01 17:17:31 +02:00 committed by Pavol Rusnak
parent dc20152afe
commit a42b654eba
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 37 additions and 26 deletions

View File

@ -12,6 +12,7 @@ class EventLoop:
def __init__(self): def __init__(self):
self.q = [] self.q = []
self.cnt = 0 self.cnt = 0
self.last_sleep = 0 # For performance stats
#self.button_cb = None #self.button_cb = None
''' '''
@ -25,14 +26,13 @@ class EventLoop:
self.call_at(0, callback, *args) self.call_at(0, callback, *args)
def call_later(self, delay, callback, *args): def call_later(self, delay, callback, *args):
self.call_at(utime.time() + delay, callback, *args) self.call_at(utime.ticks_us() + delay * 1000000, callback, *args)
def call_at(self, time, callback, *args): def call_at(self, time, callback, *args):
# Including self.cnt is a workaround per heapq docs # Including self.cnt is a workaround per heapq docs
if __debug__: if __debug__:
log.debug("Scheduling %s", (time, self.cnt, callback, args)) log.debug("Scheduling %s", (time, self.cnt, callback, args))
uheapq.heappush(self.q, (time, self.cnt, callback, args)) uheapq.heappush(self.q, (time, self.cnt, callback, args))
# print(self.q)
self.cnt += 1 self.cnt += 1
def wait(self, delay): def wait(self, delay):
@ -40,7 +40,8 @@ class EventLoop:
# with IO scheduling # with IO scheduling
if __debug__: if __debug__:
log.debug("Sleeping for: %s", delay) log.debug("Sleeping for: %s", delay)
utime.sleep(delay) self.last_sleep = delay / 1000000.
utime.sleep(delay / 1000000.)
def run_forever(self): def run_forever(self):
while True: while True:
@ -49,7 +50,7 @@ class EventLoop:
if __debug__: if __debug__:
log.debug("Next coroutine to run: %s", (t, cnt, cb, args)) log.debug("Next coroutine to run: %s", (t, cnt, cb, args))
# __main__.mem_info() # __main__.mem_info()
tnow = utime.time() tnow = utime.ticks_us()
delay = t - tnow delay = t - tnow
if delay > 0: if delay > 0:
self.wait(delay) self.wait(delay)
@ -58,7 +59,9 @@ class EventLoop:
# Assuming IO completion scheduled some tasks # Assuming IO completion scheduled some tasks
continue continue
if callable(cb): if callable(cb):
cb(*args) ret = cb(*args)
if __debug__ and isinstance(ret, type_gen):
log.warning("Callback produced generator, which will never run.")
else: else:
delay = 0 delay = 0
try: try:
@ -167,11 +170,3 @@ def get_event_loop():
if _event_loop is None: if _event_loop is None:
_event_loop = _event_loop_class() _event_loop = _event_loop_class()
return _event_loop return _event_loop
def sleep(secs):
yield Sleep(secs)
'''
def coroutine(f):
return f
'''

View File

@ -2,7 +2,7 @@
import sys import sys
sys.path.append('lib') sys.path.append('lib')
import logging import utime
import math import math
import gc import gc
@ -11,27 +11,33 @@ from trezor import ui
from .import utils from .import utils
logging.basicConfig(level=logging.INFO) if __debug__:
import logging
logging.basicConfig(level=logging.INFO)
loop = core.get_event_loop() loop = core.get_event_loop()
def meminfo(): def perf_info():
mem_free = gc.mem_free() mem_free = gc.mem_free()
collected = gc.collect() # gc.collect()
print("free_mem: %s/%s, collect: %s" % (mem_free, gc.mem_free(), collected)) print("free_mem: %s/%s, last_sleep: %.06f" % \
loop.call_later(1, meminfo) (mem_free, gc.mem_free(), loop.last_sleep))
loop.call_later(1, perf_info)
def animate(): def animate():
col = 0 col = 0
f = open('../assets/lock.toi', 'r') # hawcons gesture
f = open('playground/tap_64.toi', 'r')
while True: while True:
col %= 0xff col %= 0xff
col += 0x0f col += 0x0f
ui.display.icon(190, 170, f.read(), utils.rgb2color(col, 0, 0), 0xffff) ui.display.icon(170, 170, f.read(), 0xffff, utils.rgb2color(col, 0, 0))
# ui.display.icon(100, 100, f.read(), 0xffff, utils.rgb2color(col, 0, 0))
f.seek(0) f.seek(0)
yield from core.sleep(0.5) yield core.Sleep(0.5)
sec = 0 sec = 0
event = None event = None
@ -65,6 +71,9 @@ def tap_to_confirm():
MAX_COLOR = 0xB0 MAX_COLOR = 0xB0
_background = utils.rgb2color(255, 255, 255) _background = utils.rgb2color(255, 255, 255)
f = open('playground/tap_64.toi', 'r')
x = math.pi x = math.pi
while True: while True:
x += STEP_X x += STEP_X
@ -72,21 +81,28 @@ def tap_to_confirm():
x -= 2 * math.pi x -= 2 * math.pi
y = 1 + math.sin(x) y = 1 + math.sin(x)
# ui.display.bar(0, 170, 240, 70, _background)
# Normalize color from interval 0:2 to MIN_COLOR:MAX_COLOR # Normalize color from interval 0:2 to MIN_COLOR:MAX_COLOR
col = int((MAX_COLOR - MIN_COLOR) / 2 * y) + MIN_COLOR col = int((MAX_COLOR - MIN_COLOR) / 2 * y) + MIN_COLOR
foreground = utils.rgb2color(BASE_COLOR[0] + col, BASE_COLOR[1] + col, BASE_COLOR[2] + col) foreground = utils.rgb2color(BASE_COLOR[0] + col, BASE_COLOR[1] + col, BASE_COLOR[2] + col)
ui.display.text(10, 220, 'TAP TO CONFIRM', 2, foreground, _background) ui.display.text(68, 212, 'TAP TO CONFIRM', 2, foreground, _background)
yield from core.sleep(DELAY) f.seek(0)
ui.display.icon(3, 170, f.read(), _background, foreground)
# ui.display.icon(165, 50, f.read(), _background, foreground)
yield core.Sleep(DELAY)
def run(): def run():
# sekunda(3) # sekunda(3)
# loop.call_soon(wait_for()) # loop.call_soon(wait_for())
loop.call_soon(meminfo) loop.call_soon(perf_info)
loop.call_soon(tap_to_confirm()) loop.call_soon(tap_to_confirm())
loop.call_soon(animate()) # loop.call_soon(animate())
loop.run_forever() loop.run_forever()
loop.close() loop.close()