1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-27 16:48:09 +00:00

trezor/ui/swipe: improve dimming

This commit is contained in:
Jan Pochyla 2017-09-26 11:54:07 +02:00
parent f2e53ab2eb
commit 305d4d9cc5
3 changed files with 61 additions and 34 deletions

View File

@ -3,10 +3,10 @@ from trezor.utils import unimport
async def swipe_to_rotate(): async def swipe_to_rotate():
from trezor.ui.swipe import Swipe from trezor.ui.swipe import Swipe, degrees
degrees = await Swipe(absolute=True) swipe = await Swipe(absolute=True)
ui.display.orientation(degrees) ui.display.orientation(degrees(swipe))
async def dim_screen(): async def dim_screen():

View File

@ -1,14 +1,20 @@
from micropython import const from micropython import const
from trezor import loop, ui 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): async def change_page(page, page_count):
while True: while True:
s = await Swipe() if page == 0:
if s == SWIPE_UP and page < page_count - 1: 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 return page + 1 # scroll down
elif s == SWIPE_DOWN and page > 0: elif s == SWIPE_DOWN:
return page - 1 # scroll up return page - 1 # scroll up
@ -25,7 +31,7 @@ async def paginate(render_page, page_count, page=0, *args):
async def animate_swipe(): async def animate_swipe():
time_delay = const(30000) time_delay = const(40000)
draw_delay = const(200000) draw_delay = const(200000)
sleep = loop.sleep(time_delay) sleep = loop.sleep(time_delay)

View File

@ -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_VELOCITY_THRESHOLD = const(200) # Min pixels per second
_SWIPE_RATIO_THRESHOLD = const(30) # Max ratio or directions in % _SWIPE_RATIO_THRESHOLD = const(30) # Max ratio or directions in %
SWIPE_UP = const(180) SWIPE_UP = const(0x01)
SWIPE_DOWN = const(0) SWIPE_DOWN = const(0x02)
SWIPE_LEFT = const(90) SWIPE_LEFT = const(0x04)
SWIPE_RIGHT = const(270) 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): 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.area = area or (0, 0, ui.SCREEN, ui.SCREEN)
self.absolute = absolute self.absolute = absolute
self.directions = directions
self.start_pos = None self.start_pos = None
self.start_time = 0 self.start_time = 0
self.light = None
self.light_origin = None self.light_origin = None
self.light_target = ui.BACKLIGHT_NONE self.light_target = ui.BACKLIGHT_NONE
@ -30,7 +46,6 @@ class Swipe(ui.Widget):
temp_time = utime.ticks_ms() / 1000 temp_time = utime.ticks_ms() / 1000
# primary now for fading purposes
if event == io.TOUCH_MOVE and self.start_pos is not None: if event == io.TOUCH_MOVE and self.start_pos is not None:
pdx = pos[0] - self.start_pos[0] pdx = pos[0] - self.start_pos[0]
pdy = pos[1] - self.start_pos[1] pdy = pos[1] - self.start_pos[1]
@ -38,25 +53,25 @@ class Swipe(ui.Widget):
pdxa = abs(pdx) pdxa = abs(pdx)
pdya = abs(pdy) pdya = abs(pdy)
if pdxa < pdya and pdy < 0: if pdxa > pdya and self.directions & SWIPE_HORIZONTAL:
# check if its vertical scroll up # Horizontal direction
velya = abs(pdy / td) if td > 0 else 1 if (pdx > 0 and self.directions & SWIPE_RIGHT) or (pdx < 0 and self.directions & SWIPE_LEFT):
ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 ui.display.backlight(ui.lerpi(
if (velya >= _SWIPE_VELOCITY_THRESHOLD and self.light_origin,
pdya >= _SWIPE_DISTANCE_THRESHOLD and self.light_target,
ratio <= _SWIPE_RATIO_THRESHOLD): pdxa / 120 if pdxa < 120 else 1))
light = ui.display.backlight() elif pdxa < pdya and self.directions & SWIPE_VERTICAL:
if light > self.light_target: # Vertical direction
light -= 5 if (pdy > 0 and self.directions & SWIPE_DOWN) or (pdy < 0 and self.directions & SWIPE_UP):
ui.display.backlight(light) ui.display.backlight(ui.lerpi(
else: self.light_origin,
ui.display.backlight(self.light_target) self.light_target,
pdya / 120 if pdya < 120 else 1))
elif event == io.TOUCH_START and contains(self.area, pos): elif event == io.TOUCH_START and contains(self.area, pos):
self.start_time = temp_time self.start_time = temp_time
self.start_pos = pos self.start_pos = pos
self.light_origin = ui.BACKLIGHT_NORMAL self.light_origin = ui.BACKLIGHT_NORMAL
ui.display.backlight(self.light_origin)
elif event == io.TOUCH_END and self.start_pos is not None: elif event == io.TOUCH_END and self.start_pos is not None:
td = temp_time - self.start_time td = temp_time - self.start_time
@ -64,24 +79,30 @@ class Swipe(ui.Widget):
pdy = pos[1] - self.start_pos[1] pdy = pos[1] - self.start_pos[1]
pdxa = abs(pdx) pdxa = abs(pdx)
pdya = abs(pdy) pdya = abs(pdy)
if pdxa > pdya: if pdxa > pdya and self.directions & SWIPE_HORIZONTAL:
# Horizontal direction # Horizontal direction
velxa = abs(pdx / td) velxa = abs(pdx / td)
ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100 ratio = int(pdya / pdxa * 100) if pdxa > 0 else 100
if (velxa >= _SWIPE_VELOCITY_THRESHOLD and if (velxa >= _SWIPE_VELOCITY_THRESHOLD and
pdxa >= _SWIPE_DISTANCE_THRESHOLD and pdxa >= _SWIPE_DISTANCE_THRESHOLD and
ratio <= _SWIPE_RATIO_THRESHOLD): ratio <= _SWIPE_RATIO_THRESHOLD):
return SWIPE_RIGHT if pdx > 0 else SWIPE_LEFT if pdx > 0 and self.directions & SWIPE_RIGHT:
else: return SWIPE_RIGHT
elif pdx < 0 and self.directions & SWIPE_LEFT:
return SWIPE_LEFT
elif pdxa < pdya and self.directions & SWIPE_VERTICAL:
# Vertical direction # Vertical direction
velya = abs(pdy / td) velya = abs(pdy / td)
ratio = int(pdxa / pdya * 100) if pdya > 0 else 100 ratio = int(pdxa / pdya * 100) if pdya > 0 else 100
if (velya >= _SWIPE_VELOCITY_THRESHOLD and if (velya >= _SWIPE_VELOCITY_THRESHOLD and
pdya >= _SWIPE_DISTANCE_THRESHOLD and pdya >= _SWIPE_DISTANCE_THRESHOLD and
ratio <= _SWIPE_RATIO_THRESHOLD): ratio <= _SWIPE_RATIO_THRESHOLD):
if pdy < 0: if pdy > 0 and self.directions & SWIPE_DOWN:
ui.display.backlight(self.light_origin) self.light = None
return SWIPE_DOWN if pdy > 0 else SWIPE_UP return SWIPE_DOWN
elif pdy < 0 and self.directions & SWIPE_UP:
self.light = None
return SWIPE_UP
# No swipe, reset the state # No swipe, reset the state
self.start_pos = None self.start_pos = None
self.start_time = 0 self.start_time = 0