1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +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"))]
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
}

View File

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

View File

@ -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<Point>,
}
@ -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);
}
}

View File

@ -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);