mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-05-25 18:28:47 +00:00
refactor(core): simplify fade_backlight_duration
- IIUC, casting to and from `i32` can be removed. - `timer.elapsed()` can be evaluated once per loop iteration. [no changelog]
This commit is contained in:
parent
dabc5546b5
commit
d38b52c0c4
@ -41,25 +41,27 @@ pub fn fade_backlight(target: u8) {
|
|||||||
|
|
||||||
#[cfg(feature = "backlight")]
|
#[cfg(feature = "backlight")]
|
||||||
pub fn fade_backlight_duration(target: u8, duration_ms: u32) {
|
pub fn fade_backlight_duration(target: u8, duration_ms: u32) {
|
||||||
let target = target as i32;
|
let current = backlight();
|
||||||
let current = backlight() as i32;
|
|
||||||
let duration = Duration::from_millis(duration_ms);
|
let duration = Duration::from_millis(duration_ms);
|
||||||
|
|
||||||
if animation_disabled() {
|
if animation_disabled() {
|
||||||
set_backlight(target as u8);
|
set_backlight(target);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let timer = Stopwatch::new_started();
|
let timer = Stopwatch::new_started();
|
||||||
|
|
||||||
while timer.elapsed() < duration {
|
loop {
|
||||||
let elapsed = timer.elapsed();
|
let elapsed = timer.elapsed();
|
||||||
let val = i32::lerp(current, target, elapsed / duration);
|
if elapsed >= duration {
|
||||||
set_backlight(val as u8);
|
break;
|
||||||
|
}
|
||||||
|
let val = u8::lerp(current, target, elapsed / duration);
|
||||||
|
set_backlight(val);
|
||||||
time::sleep(Duration::from_millis(1));
|
time::sleep(Duration::from_millis(1));
|
||||||
}
|
}
|
||||||
//account for imprecise rounding
|
//account for imprecise rounding
|
||||||
set_backlight(target as u8);
|
set_backlight(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "backlight"))]
|
#[cfg(not(feature = "backlight"))]
|
||||||
|
Loading…
Reference in New Issue
Block a user