From b864ad58b267ba2abfc31f737115b1085a9bb3a1 Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 8 Jun 2022 16:07:46 +0200 Subject: [PATCH] feat(core/rust): cleaner backlight functions --- core/embed/rust/src/ui/display/mod.rs | 41 +++++++++++-------- .../rust/src/ui/model_tt/component/page.rs | 2 +- .../rust/src/ui/model_tt/component/swipe.rs | 6 +-- core/embed/rust/src/ui/model_tt/theme.rs | 10 ++--- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/core/embed/rust/src/ui/display/mod.rs b/core/embed/rust/src/ui/display/mod.rs index fcd668d085..c7512d8e5d 100644 --- a/core/embed/rust/src/ui/display/mod.rs +++ b/core/embed/rust/src/ui/display/mod.rs @@ -31,30 +31,31 @@ pub use crate::ui::display::toif::Icon; #[cfg(any(feature = "model_tt", feature = "model_tr"))] pub use loader::{loader, loader_indeterminate, LOADER_MAX, LOADER_MIN}; -pub fn backlight() -> i32 { - display::backlight(-1) +pub fn backlight() -> u16 { + display::backlight(-1) as u16 } -pub fn set_backlight(val: i32) { - display::backlight(val); +pub fn set_backlight(val: u16) { + display::backlight(val as i32); } -pub fn fade_backlight(target: i32) { - const BACKLIGHT_DELAY: Duration = Duration::from_millis(14); - const BACKLIGHT_STEP: usize = 15; +pub fn fade_backlight(target: u16) { + const FADE_DURATION_MS: u32 = 50; + fade_backlight_duration(target, FADE_DURATION_MS); +} - let current = backlight(); - if current < target { - for val in (current..target).step_by(BACKLIGHT_STEP) { - set_backlight(val); - time::sleep(BACKLIGHT_DELAY); - } - } else { - for val in (target..current).rev().step_by(BACKLIGHT_STEP) { - set_backlight(val); - time::sleep(BACKLIGHT_DELAY); - } +pub fn fade_backlight_duration(target: u16, duration_ms: u32) { + let target = target as i32; + let duration_ms = duration_ms as i32; + let current = backlight() as i32; + + for i in 0..duration_ms { + let val = i32::lerp(current, target, i as f32 / duration_ms as f32); + set_backlight(val as u16); + time::sleep(Duration::from_millis(1)); } + //account for imprecise rounding + set_backlight(target as u16); } pub fn rect_fill(r: Rect, fg_color: Color) { @@ -1094,6 +1095,10 @@ impl Color { Self(r | g | b) } + pub const fn alpha(bg: Color, alpha: u16) -> Self { + Self::rgba(bg, 0xFF, 0xFF, 0xFF, alpha) + } + pub const fn r(self) -> u8 { (self.0 >> 8) as u8 & 0xF8 } diff --git a/core/embed/rust/src/ui/model_tt/component/page.rs b/core/embed/rust/src/ui/model_tt/component/page.rs index 8ba49e6b59..d7d7090489 100644 --- a/core/embed/rust/src/ui/model_tt/component/page.rs +++ b/core/embed/rust/src/ui/model_tt/component/page.rs @@ -21,7 +21,7 @@ pub struct SwipePage { scrollbar: ScrollBar, hint: Label<&'static str>, button_back: Option>, - fade: Option, + fade: Option, } impl SwipePage diff --git a/core/embed/rust/src/ui/model_tt/component/swipe.rs b/core/embed/rust/src/ui/model_tt/component/swipe.rs index 3a97373235..b43a2fd22c 100644 --- a/core/embed/rust/src/ui/model_tt/component/swipe.rs +++ b/core/embed/rust/src/ui/model_tt/component/swipe.rs @@ -20,8 +20,8 @@ pub struct Swipe { pub allow_down: bool, pub allow_left: bool, pub allow_right: bool, - backlight_start: i32, - backlight_end: i32, + backlight_start: u16, + backlight_end: u16, origin: Option, } @@ -82,7 +82,7 @@ impl Swipe { let start = self.backlight_start as f32; let end = self.backlight_end as f32; let value = start + ratio * (end - start); - display::set_backlight(value as i32); + display::set_backlight(value as u16); } } diff --git a/core/embed/rust/src/ui/model_tt/theme.rs b/core/embed/rust/src/ui/model_tt/theme.rs index 7af2479aa5..d262d5f16b 100644 --- a/core/embed/rust/src/ui/model_tt/theme.rs +++ b/core/embed/rust/src/ui/model_tt/theme.rs @@ -17,11 +17,11 @@ use num_traits::FromPrimitive; pub const ERASE_HOLD_DURATION: Duration = Duration::from_millis(1500); // Typical backlight values. -pub const BACKLIGHT_NORMAL: i32 = 150; -pub const BACKLIGHT_LOW: i32 = 45; -pub const BACKLIGHT_DIM: i32 = 5; -pub const BACKLIGHT_NONE: i32 = 2; -pub const BACKLIGHT_MAX: i32 = 255; +pub const BACKLIGHT_NORMAL: u16 = 150; +pub const BACKLIGHT_LOW: u16 = 45; +pub const BACKLIGHT_DIM: u16 = 5; +pub const BACKLIGHT_NONE: u16 = 2; +pub const BACKLIGHT_MAX: u16 = 255; // Color palette. pub const WHITE: Color = Color::rgb(0xFF, 0xFF, 0xFF);