From 312859ba716210747f007416faff06bc13008030 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 8 Aug 2016 14:53:42 +0200 Subject: [PATCH] add experimental animation with `async for` --- src/apps/homescreen/layout_homescreen.py | 11 ++++++++--- src/trezor/ui/__init__.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/apps/homescreen/layout_homescreen.py b/src/apps/homescreen/layout_homescreen.py index e43689ac7a..624e9fdb6f 100644 --- a/src/apps/homescreen/layout_homescreen.py +++ b/src/apps/homescreen/layout_homescreen.py @@ -10,9 +10,14 @@ async def swipe_to_rotate(): async def animate_logo(): - def func(foreground): - ui.display.icon(0, 0, res.load('apps/homescreen/res/trezor.toig'), foreground, ui.BLACK) - await ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) + # def func(foreground): + # ui.display.icon(0, 0, res.load( + # 'apps/homescreen/res/trezor.toig'), foreground, ui.BLACK) + # await ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) + + async for fg in ui.pulse_animation(ui.WHITE, ui.GREY, speed=400000): + icon = res.load('apps/homescreen/res/trezor.toig') + ui.display.icon(0, 0, icon, fg, ui.BLACK) @unimport_gen diff --git a/src/trezor/ui/__init__.py b/src/trezor/ui/__init__.py index 261293dd59..45f771e03f 100644 --- a/src/trezor/ui/__init__.py +++ b/src/trezor/ui/__init__.py @@ -76,6 +76,25 @@ def animate_pulse(func, ca, cb, speed=200000, delay=30000): yield loop.Sleep(delay) +class pulse_animation: + + def __init__(self, color_a, color_b, speed=200000, delay=30000): + self.color_a = color_a + self.color_b = color_b + self.speed = speed + self.delay = delay + + async def __aiter__(self): + return self + + async def __anext__(self): + # normalize sin from interval -1:1 to 0:1 + y = 0.5 + 0.5 * math.sin(utime.ticks_us() / self.speed) + c = blend(self.color_a, self.color_b, y) + await loop.Sleep(self.delay) + return c + + def rotate_coords(pos: tuple) -> tuple: r = display.orientation() if r == 0: