From 9f84e52ea336140a64742fa856973caf9ca9d059 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 17 May 2016 15:13:32 +0200 Subject: [PATCH] add example of swipe TODO: simplify TODO: fix bug with black color in oriented modes --- src/apps/playground/__init__.py | 88 ++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/src/apps/playground/__init__.py b/src/apps/playground/__init__.py index 7d6d712f7..bc645e894 100644 --- a/src/apps/playground/__init__.py +++ b/src/apps/playground/__init__.py @@ -1,3 +1,4 @@ +import utime from trezor import loop from trezor import ui from trezor.utils import unimport_func @@ -149,6 +150,73 @@ class PinDialog(): return result +SWIPE_DISTANCE_THRESHOLD = const(20) # Min pixels in the primary direction +SWIPE_VELOCITY_THRESHOLD = const(200) # Min pixels/second +SWIPE_RATIO_THRESHOLD = const(30) # Max ratio secondary to primary direction in % + +SWIPE_UP = const(180) +SWIPE_DOWN = const(0) +SWIPE_LEFT = const(90) +SWIPE_RIGHT = const(270) + + +class Swipe(): + + def __init__(self, area=None): + self.area = area or (0, 0, 240, 240) + self.start_pos = None + self.start_time = 0 + self.end_pos = None + self.end_time = 0 + + def send(self, event, pos): + + if event is loop.TOUCH_START and in_area(pos, self.area): + self.start_time = utime.time() + self.start_pos = pos + + elif event is loop.TOUCH_END and self.start_pos is not None: + self.end_time = utime.time() + self.end_pos = pos + td = self.end_time - self.start_time + pdx = self.end_pos[0] - self.start_pos[0] + pdy = self.end_pos[1] - self.start_pos[1] + pdxa = abs(pdx) + pdya = abs(pdy) + if pdxa > pdya: + # Horizontal direction + velx = pdx / td + velxa = abs(velx) + ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100 + print('velxa', velxa, 'pdxa', pdxa, 'ratio', ratio) + if (velxa >= SWIPE_VELOCITY_THRESHOLD + and pdxa >= SWIPE_DISTANCE_THRESHOLD + and ratio <= SWIPE_RATIO_THRESHOLD): + return SWIPE_RIGHT if pdx > 0 else SWIPE_LEFT + else: + # Vertical direction + vely = pdy / td + velya = abs(vely) + ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 + print('velya', velya, 'pdya', pdya, 'ratio', ratio) + if (velya >= SWIPE_VELOCITY_THRESHOLD + and pdya >= SWIPE_DISTANCE_THRESHOLD + and ratio <= SWIPE_RATIO_THRESHOLD): + return SWIPE_DOWN if pdy > 0 else SWIPE_UP + # No swipe, reset the state + self.start_pos = None + self.start_time = 0 + self.end_pos = None + self.end_time = 0 + + def wait(self): + while True: + event, *pos = yield loop.Select(loop.TOUCH_START, loop.TOUCH_END) + result = self.send(event, pos) + if result is not None: + return result + + def layout_tap_to_confirm(address, amount, currency): # ui.display.bar(0, 0, 240, 40, ui.GREEN) @@ -171,14 +239,22 @@ def layout_tap_to_confirm(address, amount, currency): # animation = ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000) - pin_dialog = PinDialog() - pin_result = yield from pin_dialog.wait_for_result() + # pin_dialog = PinDialog() + # pin_result = yield from pin_dialog.wait_for_result() + + # if pin_result is PIN_CONFIRMED: + # print('PIN confirmed:', pin_dialog.pin) + + # elif pin_result is PIN_CANCELLED: + # print('PIN CANCELLED, go home') - if pin_result is PIN_CONFIRMED: - print('PIN confirmed:', pin_dialog.pin) + degrees = 0 - elif pin_result is PIN_CANCELLED: - print('PIN CANCELLED, go home') + while True: + ui.display.bar(0, 0, 240, 240, ui.BLACK) + ui.display.text_center(120, 130, 'HELLO WORLD!', ui.NORMAL, ui.WHITE, ui.BLACK) + degrees = yield from Swipe().wait() + ui.display.orientation(degrees) @unimport_func