1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-03 12:00:59 +00:00

WIP: experiment with pin dialog

Mostly to try out current design of the event loop and the event
multiplexing.  It works, but the pin_widget code is not really something
extra.
This commit is contained in:
Jan Pochyla 2016-05-09 13:48:29 +02:00 committed by Pavol Rusnak
parent f24dd6b42f
commit ef22adb2b7
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -5,13 +5,13 @@ from trezor.utils import unimport_func
def multiplex_touch_events(gens): def multiplex_touch_events(gens):
while True: while True:
e, (x, y) = yield loop.Wait([ event = yield loop.Wait([
loop.TOUCH_START, loop.TOUCH_START,
loop.TOUCH_MOVE, loop.TOUCH_MOVE,
loop.TOUCH_END, loop.TOUCH_END,
]) ])
for gen in gens: for gen in gens:
gen.send((e, (x, y))) gen.send(event)
def in_area(pos, area): def in_area(pos, area):
@ -20,76 +20,191 @@ def in_area(pos, area):
return ax <= x <= ax + aw and ay <= y <= ay + ah return ax <= x <= ax + aw and ay <= y <= ay + ah
def click_in(area, enter, leave): def click_in(area, click=None, enter=None, leave=None):
while True: while True:
e, pos = yield e, pos = yield
if e is not loop.TOUCH_START or not in_area(pos, area): if e is not loop.TOUCH_START or not in_area(pos, area):
continue continue
inside = True inside = True
enter() if enter:
enter()
while True: while True:
e, pos = yield e, pos = yield
if e is loop.TOUCH_MOVE: if e is loop.TOUCH_MOVE:
if in_area(pos, area): if in_area(pos, area):
if not inside: if not inside:
enter() if enter:
enter()
inside = True inside = True
else: else:
if inside: if inside:
leave() if leave:
leave()
inside = False inside = False
elif e is loop.TOUCH_END: elif e is loop.TOUCH_END:
if in_area(pos, area): if in_area(pos, area):
return if click:
click()
else:
return
else: else:
break break
def layout_tap_to_confirm(address, amount, currency): default_button = {
'bg-color': ui.WHITE,
'fg-color': ui.BLACK,
'text-style': ui.NORMAL,
}
default_button_active = {
'bg-color': ui.GREY,
'fg-color': ui.BLACK,
'text-style': ui.BOLD,
}
cancel_button = {
'bg-color': ui.blend(ui.BLACK, ui.RED, 0.3),
'fg-color': ui.RED,
'text-style': ui.NORMAL,
}
cancel_button_active = {
'bg-color': ui.RED,
'fg-color': ui.WHITE,
'text-style': ui.BOLD,
}
confirm_button = {
'bg-color': ui.blend(ui.BLACK, ui.GREEN, 0.3),
'fg-color': ui.GREEN,
'text-style': ui.NORMAL,
}
confirm_button_active = {
'bg-color': ui.GREEN,
'fg-color': ui.WHITE,
'text-style': ui.BOLD,
}
ui.display.bar(0, 0, 240, 40, ui.GREEN)
ui.display.bar(0, 40, 240, 200, ui.WHITE)
ui.display.text(10, 28, 'Sending', ui.BOLD, ui.WHITE, ui.GREEN) def render_button(area, text, style):
ui.display.text(10, 80, '%f %s' % (amount, currency), ui.BOLD, ui.BLACK, ui.WHITE) ax, ay, aw, ah = area
ui.display.text(10, 110, 'to this address:', ui.NORMAL, ui.BLACK, ui.WHITE) tx = ax + aw // 2
ui.display.text(10, 140, address[:18], ui.MONO, ui.BLACK, ui.WHITE) ty = ay + ah - 5
ui.display.text(10, 160, address[18:], ui.MONO, ui.BLACK, ui.WHITE) ui.display.bar(ax, ay, aw, ah, style['bg-color'])
ui.display.text_center(tx, ty, text,
style['text-style'],
style['fg-color'],
style['bg-color'])
f = open('apps/playground/tap_64.toig', 'rb')
bg = ui.WHITE def button_widget(area, text,
style = ui.NORMAL click=None,
style=default_button,
active_style=default_button_active):
def enter(): def enter():
nonlocal style render_button(area, text, active_style)
style = ui.BOLD
def leave(): def leave():
nonlocal style render_button(area, text, style)
style = ui.NORMAL
def func(fg): def _click():
ui.display.text(68, 212, 'TAP TO CONFIRM', style, fg, bg) leave()
f.seek(0) click()
ui.display.icon(3, 170, f.read(), bg, fg)
animation = ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000) render_button(area, text, style)
click_up = click_in((0, 0, 240, 40), enter, leave) return click_in(area, _click, enter, leave)
click_down = click_in((0, 200, 240, 40), enter, leave)
next(click_down)
next(click_up)
yield loop.Wait(( def pin_widget():
animation,
multiplex_touch_events(( pin = ''
click_down, width = const(80)
click_up, height = const(60)
)), margin = const(5)
def digit_area(d):
x = ((d - 1) % 3) * width
y = ((d - 1) // 3) * height
return (
x + margin,
y + margin,
width - margin,
height - margin
)
def append_digit(d):
pin += str(d)
print('PIN so far: ', pin)
def digit_widget(digit):
nonlocal pin
area = digit_area(digit)
button = button_widget(
area,
str(digit),
lambda: append_digit(digit))
return button
digits = (1, 2, 3, 4, 5, 6, 7, 8, 9)
buttons = [digit_widget(d) for d in digits]
def cancel():
raise StopIteration(None)
def confirm():
raise StopIteration(pin)
cancel_widget = button_widget((0, 240 - 60, 120, 60), 'Cancel',
click=cancel,
style=cancel_button,
active_style=cancel_button_active)
confirm_widget = button_widget((120, 240 - 60, 120, 60), 'Confirm',
click=confirm,
style=confirm_button,
active_style=confirm_button_active)
buttons.append(cancel_widget)
buttons.append(confirm_widget)
for but in buttons:
next(but)
return buttons
def layout_tap_to_confirm(address, amount, currency):
# ui.display.bar(0, 0, 240, 40, ui.GREEN)
# ui.display.bar(0, 40, 240, 200, ui.WHITE)
# ui.display.text(10, 28, 'Sending', ui.BOLD, ui.WHITE, ui.GREEN)
# ui.display.text(10, 80, '%f %s' % (amount, currency), ui.BOLD, ui.BLACK, ui.WHITE)
# ui.display.text(10, 110, 'to this address:', ui.NORMAL, ui.BLACK, ui.WHITE)
# ui.display.text(10, 140, address[:18], ui.MONO, ui.BLACK, ui.WHITE)
# ui.display.text(10, 160, address[18:], ui.MONO, ui.BLACK, ui.WHITE)
# f = open('apps/playground/tap_64.toig', 'rb')
# bg = ui.WHITE
# style = ui.NORMAL
# def func(fg):
# ui.display.text(68, 212, 'TAP TO CONFIRM', style, fg, bg)
# f.seek(0)
# ui.display.icon(3, 170, f.read(), bg, fg)
# animation = ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000)
# button = button_widget((20, 20, 210, 60), 'HELLO WORLD!')
# next(button)
pin = pin_widget()
result = yield loop.Wait((
# animation,
multiplex_touch_events(pin),
)) ))
print(result)
@unimport_func @unimport_func
def zprava(): def zprava():