diff --git a/src/playground/__init__.py b/src/playground/__init__.py index 2f5c4e0ab..245134070 100644 --- a/src/playground/__init__.py +++ b/src/playground/__init__.py @@ -91,7 +91,7 @@ def tap_to_confirm(address, amount, currency): f.seek(0) ui.display.icon(3, 170, f.read(), _background, foreground) - yield from ui.animate_pulse(func) # , DELAY=10000) + yield from ui.animate_pulse(func, ui.BLACK, ui.GREY) # , delay=10000) ''' def on_read(): @@ -130,8 +130,7 @@ def homescreen(): def func(foreground): f.seek(0) ui.display.icon(0, 0, f.read(), foreground, ui.BLACK) - - yield from ui.animate_pulse(func, SPEED=400000, BASE_COLOR=(0xff, 0xff, 0xff), MIN_COLOR=0xaa, MAX_COLOR=0xff) + yield from ui.animate_pulse(func, ui.WHITE, ui.GREY, speed=400000) def run(): ui.touch.start(lambda x, y: print('touch start %d %d\n' % (x, y))) diff --git a/src/trezor/ui.py b/src/trezor/ui.py index d15638537..4b648323e 100644 --- a/src/trezor/ui.py +++ b/src/trezor/ui.py @@ -38,14 +38,20 @@ NORMAL = const(1) BOLD = const(2) -def animate_pulse(func, SPEED=200000, DELAY=30000, BASE_COLOR=(0x00, 0x00, 0x00), MIN_COLOR=0x00, MAX_COLOR=0x80): - while True: - y = 1 + math.sin(utime.ticks_us() / SPEED) +def lerpi(a: int, b: int, t: float) -> int: + return int(a + t * (b - a)) + - # Normalize color from interval 0:2 to MIN_COLOR:MAX_COLOR - col = int((MAX_COLOR - MIN_COLOR) / 2 * y) + MIN_COLOR - foreground = rgbcolor(BASE_COLOR[0] + col, BASE_COLOR[1] + col, BASE_COLOR[2] + col) +def blend(ca: int, cb: int, t: float) -> int: + return rgbcolor(lerpi((ca >> 8) & 0xF8, (cb >> 8) & 0xF8, t), + lerpi((ca >> 3) & 0xFC, (cb >> 3) & 0xFC, t), + lerpi((ca << 3) & 0xF8, (cb << 3) & 0xF8, t)) - func(foreground) - yield loop.Sleep(DELAY) +def animate_pulse(func, ca, cb, speed=200000, delay=30000): + while True: + # normalize sin from interval -1:1 to 0:1 + y = 0.5 + 0.5 * math.sin(utime.ticks_us() / speed) + c = blend(ca, cb, y) + func(c) + yield loop.Sleep(delay)