1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-10 15:30:55 +00:00

feat(core/rust): cleaner backlight functions

This commit is contained in:
matejcik 2022-06-08 16:07:46 +02:00 committed by Martin Milata
parent 9b9443ead7
commit b864ad58b2
4 changed files with 32 additions and 27 deletions

View File

@ -31,30 +31,31 @@ pub use crate::ui::display::toif::Icon;
#[cfg(any(feature = "model_tt", feature = "model_tr"))] #[cfg(any(feature = "model_tt", feature = "model_tr"))]
pub use loader::{loader, loader_indeterminate, LOADER_MAX, LOADER_MIN}; pub use loader::{loader, loader_indeterminate, LOADER_MAX, LOADER_MIN};
pub fn backlight() -> i32 { pub fn backlight() -> u16 {
display::backlight(-1) display::backlight(-1) as u16
} }
pub fn set_backlight(val: i32) { pub fn set_backlight(val: u16) {
display::backlight(val); display::backlight(val as i32);
} }
pub fn fade_backlight(target: i32) { pub fn fade_backlight(target: u16) {
const BACKLIGHT_DELAY: Duration = Duration::from_millis(14); const FADE_DURATION_MS: u32 = 50;
const BACKLIGHT_STEP: usize = 15; fade_backlight_duration(target, FADE_DURATION_MS);
}
let current = backlight(); pub fn fade_backlight_duration(target: u16, duration_ms: u32) {
if current < target { let target = target as i32;
for val in (current..target).step_by(BACKLIGHT_STEP) { let duration_ms = duration_ms as i32;
set_backlight(val); let current = backlight() as i32;
time::sleep(BACKLIGHT_DELAY);
} for i in 0..duration_ms {
} else { let val = i32::lerp(current, target, i as f32 / duration_ms as f32);
for val in (target..current).rev().step_by(BACKLIGHT_STEP) { set_backlight(val as u16);
set_backlight(val); time::sleep(Duration::from_millis(1));
time::sleep(BACKLIGHT_DELAY);
}
} }
//account for imprecise rounding
set_backlight(target as u16);
} }
pub fn rect_fill(r: Rect, fg_color: Color) { pub fn rect_fill(r: Rect, fg_color: Color) {
@ -1094,6 +1095,10 @@ impl Color {
Self(r | g | b) 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 { pub const fn r(self) -> u8 {
(self.0 >> 8) as u8 & 0xF8 (self.0 >> 8) as u8 & 0xF8
} }

View File

@ -21,7 +21,7 @@ pub struct SwipePage<T, U> {
scrollbar: ScrollBar, scrollbar: ScrollBar,
hint: Label<&'static str>, hint: Label<&'static str>,
button_back: Option<Button<&'static str>>, button_back: Option<Button<&'static str>>,
fade: Option<i32>, fade: Option<u16>,
} }
impl<T, U> SwipePage<T, U> impl<T, U> SwipePage<T, U>

View File

@ -20,8 +20,8 @@ pub struct Swipe {
pub allow_down: bool, pub allow_down: bool,
pub allow_left: bool, pub allow_left: bool,
pub allow_right: bool, pub allow_right: bool,
backlight_start: i32, backlight_start: u16,
backlight_end: i32, backlight_end: u16,
origin: Option<Point>, origin: Option<Point>,
} }
@ -82,7 +82,7 @@ impl Swipe {
let start = self.backlight_start as f32; let start = self.backlight_start as f32;
let end = self.backlight_end as f32; let end = self.backlight_end as f32;
let value = start + ratio * (end - start); let value = start + ratio * (end - start);
display::set_backlight(value as i32); display::set_backlight(value as u16);
} }
} }

View File

@ -17,11 +17,11 @@ use num_traits::FromPrimitive;
pub const ERASE_HOLD_DURATION: Duration = Duration::from_millis(1500); pub const ERASE_HOLD_DURATION: Duration = Duration::from_millis(1500);
// Typical backlight values. // Typical backlight values.
pub const BACKLIGHT_NORMAL: i32 = 150; pub const BACKLIGHT_NORMAL: u16 = 150;
pub const BACKLIGHT_LOW: i32 = 45; pub const BACKLIGHT_LOW: u16 = 45;
pub const BACKLIGHT_DIM: i32 = 5; pub const BACKLIGHT_DIM: u16 = 5;
pub const BACKLIGHT_NONE: i32 = 2; pub const BACKLIGHT_NONE: u16 = 2;
pub const BACKLIGHT_MAX: i32 = 255; pub const BACKLIGHT_MAX: u16 = 255;
// Color palette. // Color palette.
pub const WHITE: Color = Color::rgb(0xFF, 0xFF, 0xFF); pub const WHITE: Color = Color::rgb(0xFF, 0xFF, 0xFF);