From 305d4d9cc5161d3dfdd5e5ef674da60a3e0e0b59 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 26 Sep 2017 11:54:07 +0200 Subject: [PATCH] trezor/ui/swipe: improve dimming --- src/apps/homescreen/homescreen.py | 6 +-- src/trezor/ui/scroll.py | 16 ++++--- src/trezor/ui/swipe.py | 73 ++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/apps/homescreen/homescreen.py b/src/apps/homescreen/homescreen.py index 7c99242e39..09641961b0 100644 --- a/src/apps/homescreen/homescreen.py +++ b/src/apps/homescreen/homescreen.py @@ -3,10 +3,10 @@ from trezor.utils import unimport async def swipe_to_rotate(): - from trezor.ui.swipe import Swipe + from trezor.ui.swipe import Swipe, degrees - degrees = await Swipe(absolute=True) - ui.display.orientation(degrees) + swipe = await Swipe(absolute=True) + ui.display.orientation(degrees(swipe)) async def dim_screen(): diff --git a/src/trezor/ui/scroll.py b/src/trezor/ui/scroll.py index 4d0f657ae2..aa5f3b17e5 100644 --- a/src/trezor/ui/scroll.py +++ b/src/trezor/ui/scroll.py @@ -1,14 +1,20 @@ from micropython import const from trezor import loop, ui -from .swipe import Swipe, SWIPE_UP, SWIPE_DOWN +from .swipe import Swipe, SWIPE_UP, SWIPE_DOWN, SWIPE_VERTICAL async def change_page(page, page_count): while True: - s = await Swipe() - if s == SWIPE_UP and page < page_count - 1: + if page == 0: + d = SWIPE_UP + elif page == page_count - 1: + d = SWIPE_DOWN + else: + d = SWIPE_VERTICAL + s = await Swipe(directions=d) + if s == SWIPE_UP: return page + 1 # scroll down - elif s == SWIPE_DOWN and page > 0: + elif s == SWIPE_DOWN: return page - 1 # scroll up @@ -25,7 +31,7 @@ async def paginate(render_page, page_count, page=0, *args): async def animate_swipe(): - time_delay = const(30000) + time_delay = const(40000) draw_delay = const(200000) sleep = loop.sleep(time_delay) diff --git a/src/trezor/ui/swipe.py b/src/trezor/ui/swipe.py index 3712f7eee7..b6465c95f9 100644 --- a/src/trezor/ui/swipe.py +++ b/src/trezor/ui/swipe.py @@ -7,19 +7,35 @@ _SWIPE_DISTANCE_THRESHOLD = const(20) # Min pixels in the primary direction _SWIPE_VELOCITY_THRESHOLD = const(200) # Min pixels per second _SWIPE_RATIO_THRESHOLD = const(30) # Max ratio or directions in % -SWIPE_UP = const(180) -SWIPE_DOWN = const(0) -SWIPE_LEFT = const(90) -SWIPE_RIGHT = const(270) +SWIPE_UP = const(0x01) +SWIPE_DOWN = const(0x02) +SWIPE_LEFT = const(0x04) +SWIPE_RIGHT = const(0x08) +SWIPE_VERTICAL = const(SWIPE_UP | SWIPE_DOWN) +SWIPE_HORIZONTAL = const(SWIPE_LEFT | SWIPE_RIGHT) +SWIPE_ALL = const(SWIPE_VERTICAL | SWIPE_HORIZONTAL) + + +def degrees(swipe: int) -> int: + if swipe == SWIPE_UP: + return 180 + elif swipe == SWIPE_DOWN: + return 0 + elif swipe == SWIPE_LEFT: + return 90 + elif swipe == SWIPE_RIGHT: + return 270 class Swipe(ui.Widget): - def __init__(self, area=None, absolute=False): + def __init__(self, area=None, absolute=False, directions=SWIPE_ALL): self.area = area or (0, 0, ui.SCREEN, ui.SCREEN) self.absolute = absolute + self.directions = directions self.start_pos = None self.start_time = 0 + self.light = None self.light_origin = None self.light_target = ui.BACKLIGHT_NONE @@ -30,7 +46,6 @@ class Swipe(ui.Widget): temp_time = utime.ticks_ms() / 1000 - # primary now for fading purposes if event == io.TOUCH_MOVE and self.start_pos is not None: pdx = pos[0] - self.start_pos[0] pdy = pos[1] - self.start_pos[1] @@ -38,25 +53,25 @@ class Swipe(ui.Widget): pdxa = abs(pdx) pdya = abs(pdy) - if pdxa < pdya and pdy < 0: - # check if its vertical scroll up - velya = abs(pdy / td) if td > 0 else 1 - ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 - if (velya >= _SWIPE_VELOCITY_THRESHOLD and - pdya >= _SWIPE_DISTANCE_THRESHOLD and - ratio <= _SWIPE_RATIO_THRESHOLD): - light = ui.display.backlight() - if light > self.light_target: - light -= 5 - ui.display.backlight(light) - else: - ui.display.backlight(self.light_target) + if pdxa > pdya and self.directions & SWIPE_HORIZONTAL: + # Horizontal direction + if (pdx > 0 and self.directions & SWIPE_RIGHT) or (pdx < 0 and self.directions & SWIPE_LEFT): + ui.display.backlight(ui.lerpi( + self.light_origin, + self.light_target, + pdxa / 120 if pdxa < 120 else 1)) + elif pdxa < pdya and self.directions & SWIPE_VERTICAL: + # Vertical direction + if (pdy > 0 and self.directions & SWIPE_DOWN) or (pdy < 0 and self.directions & SWIPE_UP): + ui.display.backlight(ui.lerpi( + self.light_origin, + self.light_target, + pdya / 120 if pdya < 120 else 1)) elif event == io.TOUCH_START and contains(self.area, pos): self.start_time = temp_time self.start_pos = pos self.light_origin = ui.BACKLIGHT_NORMAL - ui.display.backlight(self.light_origin) elif event == io.TOUCH_END and self.start_pos is not None: td = temp_time - self.start_time @@ -64,24 +79,30 @@ class Swipe(ui.Widget): pdy = pos[1] - self.start_pos[1] pdxa = abs(pdx) pdya = abs(pdy) - if pdxa > pdya: + if pdxa > pdya and self.directions & SWIPE_HORIZONTAL: # Horizontal direction velxa = abs(pdx / td) ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100 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: + if pdx > 0 and self.directions & SWIPE_RIGHT: + return SWIPE_RIGHT + elif pdx < 0 and self.directions & SWIPE_LEFT: + return SWIPE_LEFT + elif pdxa < pdya and self.directions & SWIPE_VERTICAL: # Vertical direction velya = abs(pdy / td) ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 if (velya >= _SWIPE_VELOCITY_THRESHOLD and pdya >= _SWIPE_DISTANCE_THRESHOLD and ratio <= _SWIPE_RATIO_THRESHOLD): - if pdy < 0: - ui.display.backlight(self.light_origin) - return SWIPE_DOWN if pdy > 0 else SWIPE_UP + if pdy > 0 and self.directions & SWIPE_DOWN: + self.light = None + return SWIPE_DOWN + elif pdy < 0 and self.directions & SWIPE_UP: + self.light = None + return SWIPE_UP # No swipe, reset the state self.start_pos = None self.start_time = 0