1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-19 04:48:12 +00:00

feat(core): make brightness an u8

[no changelog]
This commit is contained in:
Ioan Bizău 2024-07-17 16:29:05 +02:00 committed by Ioan Bizău
parent a15dd6598f
commit 6f96585d4b
12 changed files with 71 additions and 75 deletions

View File

@ -49,44 +49,44 @@ use crate::trezorhal::{
}; };
use crate::ui::constant::WIDTH; use crate::ui::constant::WIDTH;
pub fn backlight() -> u16 { pub fn backlight() -> u8 {
display::backlight(-1) as u16 display::backlight(-1) as u8
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
pub fn set_backlight(val: u16) { pub fn set_backlight(val: u8) {
display::backlight(val as i32); display::backlight(val as i32);
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
pub fn fade_backlight(target: u16) { pub fn fade_backlight(target: u8) {
const FADE_DURATION_MS: u32 = 50; const FADE_DURATION_MS: u32 = 50;
fade_backlight_duration(target, FADE_DURATION_MS); fade_backlight_duration(target, FADE_DURATION_MS);
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
pub fn fade_backlight_duration(target: u16, duration_ms: u32) { pub fn fade_backlight_duration(target: u8, duration_ms: u32) {
let target = target as i32; let target = target as i32;
let duration_ms = duration_ms as i32; let duration_ms = duration_ms as i32;
let current = backlight() as i32; let current = backlight() as i32;
for i in 0..duration_ms { for i in 0..duration_ms {
let val = i32::lerp(current, target, i as f32 / duration_ms as f32); let val = i32::lerp(current, target, i as f32 / duration_ms as f32);
set_backlight(val as u16); set_backlight(val as u8);
time::sleep(Duration::from_millis(1)); time::sleep(Duration::from_millis(1));
} }
//account for imprecise rounding //account for imprecise rounding
set_backlight(target as u16); set_backlight(target as u8);
} }
#[cfg(not(feature = "backlight"))] #[cfg(not(feature = "backlight"))]
pub fn set_backlight(_: u16) {} pub fn set_backlight(_: u8) {}
#[cfg(not(feature = "backlight"))] #[cfg(not(feature = "backlight"))]
pub fn fade_backlight(_: u16) {} pub fn fade_backlight(_: u8) {}
#[cfg(not(feature = "backlight"))] #[cfg(not(feature = "backlight"))]
pub fn fade_backlight_duration(_: u16, _: u32) {} pub fn fade_backlight_duration(_: u8, _: u32) {}
#[cfg(not(feature = "framebuffer"))] #[cfg(not(feature = "framebuffer"))]
/// Fill a whole rectangle with a specific color. /// Fill a whole rectangle with a specific color.

View File

@ -1,4 +1,4 @@
use core::sync::atomic::{AtomicU16, Ordering}; use core::sync::atomic::{AtomicU8, Ordering};
use crate::{ use crate::{
error::Error, error::Error,
@ -63,7 +63,7 @@ impl FlowState for SetBrightness {
} }
} }
static BRIGHTNESS: AtomicU16 = AtomicU16::new(0); static BRIGHTNESS: AtomicU8 = AtomicU8::new(0);
#[allow(clippy::not_unsafe_ptr_arg_deref)] #[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { pub extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
@ -72,13 +72,13 @@ pub extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *m
impl SetBrightness { impl SetBrightness {
fn new_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, Error> { fn new_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, Error> {
let current: Option<u16> = kwargs.get(Qstr::MP_QSTR_current)?.try_into_option()?; let current: Option<u8> = kwargs.get(Qstr::MP_QSTR_current)?.try_into_option()?;
let content_slider = Frame::left_aligned( let content_slider = Frame::left_aligned(
TR::brightness__title.into(), TR::brightness__title.into(),
NumberInputSliderDialog::new( NumberInputSliderDialog::new(
theme::backlight::get_backlight_min(), theme::backlight::get_backlight_min() as u16,
theme::backlight::get_backlight_max(), theme::backlight::get_backlight_max() as u16,
current.unwrap_or(theme::backlight::get_backlight_normal()), current.unwrap_or(theme::backlight::get_backlight_normal()) as u16,
), ),
) )
.with_subtitle(TR::homescreen__settings_subtitle.into()) .with_subtitle(TR::homescreen__settings_subtitle.into())
@ -87,7 +87,7 @@ impl SetBrightness {
.map(|msg| match msg { .map(|msg| match msg {
FrameMsg::Content(NumberInputSliderDialogMsg::Changed(n)) => { FrameMsg::Content(NumberInputSliderDialogMsg::Changed(n)) => {
display::backlight(n as _); display::backlight(n as _);
BRIGHTNESS.store(n, Ordering::Relaxed); BRIGHTNESS.store(n as u8, Ordering::Relaxed);
None None
} }
FrameMsg::Button(_) => Some(FlowMsg::Info), FrameMsg::Button(_) => Some(FlowMsg::Info),
@ -114,7 +114,7 @@ impl SetBrightness {
.with_swipe(SwipeDirection::Left, SwipeSettings::default()) .with_swipe(SwipeDirection::Left, SwipeSettings::default())
.map(move |msg| match msg { .map(move |msg| match msg {
FrameMsg::Content(()) => { FrameMsg::Content(()) => {
let _ = storage::set_brightness(BRIGHTNESS.load(Ordering::Relaxed) as u8); let _ = storage::set_brightness(BRIGHTNESS.load(Ordering::Relaxed));
Some(FlowMsg::Confirmed) Some(FlowMsg::Confirmed)
} }
FrameMsg::Button(_) => Some(FlowMsg::Info), FrameMsg::Button(_) => Some(FlowMsg::Info),

View File

@ -33,27 +33,27 @@ impl UIFeaturesCommon for ModelMercuryFeatures {
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_none() -> u16 { fn get_backlight_none() -> u8 {
backlight::get_backlight_none() backlight::get_backlight_none()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_normal() -> u16 { fn get_backlight_normal() -> u8 {
backlight::get_backlight_normal() backlight::get_backlight_normal()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_low() -> u16 { fn get_backlight_low() -> u8 {
backlight::get_backlight_low() backlight::get_backlight_low()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_dim() -> u16 { fn get_backlight_dim() -> u8 {
backlight::get_backlight_dim() backlight::get_backlight_dim()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_max() -> u16 { fn get_backlight_max() -> u8 {
backlight::get_backlight_max() backlight::get_backlight_max()
} }

View File

@ -10,43 +10,41 @@ const BACKLIGHT_MIN: u8 = 10;
const BACKLIGHT_MAX: u8 = 255; const BACKLIGHT_MAX: u8 = 255;
#[cfg(feature = "bootloader")] #[cfg(feature = "bootloader")]
pub fn get_backlight_normal() -> u16 { pub fn get_backlight_normal() -> u8 {
BACKLIGHT_NORMAL.into() BACKLIGHT_NORMAL
} }
#[cfg(not(feature = "bootloader"))] #[cfg(not(feature = "bootloader"))]
pub fn get_backlight_normal() -> u16 { pub fn get_backlight_normal() -> u8 {
storage::get_brightness() storage::get_brightness()
.unwrap_or(BACKLIGHT_NORMAL) .unwrap_or(BACKLIGHT_NORMAL)
.clamp(BACKLIGHT_MIN, BACKLIGHT_MAX) .clamp(BACKLIGHT_MIN, BACKLIGHT_MAX)
.into()
} }
#[cfg(feature = "bootloader")] #[cfg(feature = "bootloader")]
pub fn get_backlight_low() -> u16 { pub fn get_backlight_low() -> u8 {
BACKLIGHT_LOW.into() BACKLIGHT_LOW
} }
#[cfg(not(feature = "bootloader"))] #[cfg(not(feature = "bootloader"))]
pub fn get_backlight_low() -> u16 { pub fn get_backlight_low() -> u8 {
storage::get_brightness() storage::get_brightness()
.unwrap_or(BACKLIGHT_LOW) .unwrap_or(BACKLIGHT_LOW)
.clamp(BACKLIGHT_MIN, BACKLIGHT_LOW) .clamp(BACKLIGHT_MIN, BACKLIGHT_LOW)
.into()
} }
pub fn get_backlight_dim() -> u16 { pub fn get_backlight_dim() -> u8 {
BACKLIGHT_DIM.into() BACKLIGHT_DIM
} }
pub fn get_backlight_none() -> u16 { pub fn get_backlight_none() -> u8 {
BACKLIGHT_NONE.into() BACKLIGHT_NONE
} }
pub fn get_backlight_max() -> u16 { pub fn get_backlight_max() -> u8 {
BACKLIGHT_MAX.into() BACKLIGHT_MAX
} }
pub fn get_backlight_min() -> u16 { pub fn get_backlight_min() -> u8 {
BACKLIGHT_MIN.into() BACKLIGHT_MIN
} }

View File

@ -43,7 +43,7 @@ pub struct ButtonPage<T> {
/// Whether to pass-through right swipe to parent component. /// Whether to pass-through right swipe to parent component.
swipe_right: bool, swipe_right: bool,
/// Fade to given backlight level on next paint(). /// Fade to given backlight level on next paint().
fade: Cell<Option<u16>>, fade: Cell<Option<u8>>,
} }
impl<T> ButtonPage<T> impl<T> ButtonPage<T>

View File

@ -17,12 +17,12 @@ use super::{
pub struct SetBrightnessDialog(NumberInputSliderDialog); pub struct SetBrightnessDialog(NumberInputSliderDialog);
impl SetBrightnessDialog { impl SetBrightnessDialog {
pub fn new(current: Option<u16>) -> Self { pub fn new(current: Option<u8>) -> Self {
let current = current.unwrap_or(theme::backlight::get_backlight_normal()); let current = current.unwrap_or(theme::backlight::get_backlight_normal());
Self(NumberInputSliderDialog::new( Self(NumberInputSliderDialog::new(
theme::backlight::get_backlight_min(), theme::backlight::get_backlight_min() as u16,
theme::backlight::get_backlight_max(), theme::backlight::get_backlight_max() as u16,
current, current as u16,
)) ))
} }
} }

View File

@ -18,7 +18,7 @@ pub struct SimplePage<T> {
scrollbar: ScrollBar, scrollbar: ScrollBar,
axis: Axis, axis: Axis,
swipe_right_to_go_back: bool, swipe_right_to_go_back: bool,
fade: Cell<Option<u16>>, fade: Cell<Option<u8>>,
} }
impl<T> SimplePage<T> impl<T> SimplePage<T>

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: u16, backlight_start: u8,
backlight_end: u16, backlight_end: u8,
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 u16); display::set_backlight(value as u8);
} }
} }

View File

@ -1316,7 +1316,7 @@ extern "C" fn new_request_number(n_args: usize, args: *const Obj, kwargs: *mut M
extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| { let block = move |_args: &[Obj], kwargs: &Map| {
let current: Option<u16> = kwargs.get(Qstr::MP_QSTR_current)?.try_into_option()?; let current: Option<u8> = kwargs.get(Qstr::MP_QSTR_current)?.try_into_option()?;
let obj = LayoutObj::new(Frame::centered( let obj = LayoutObj::new(Frame::centered(
theme::label_title(), theme::label_title(),
TR::brightness__title.into(), TR::brightness__title.into(),

View File

@ -34,27 +34,27 @@ impl UIFeaturesCommon for ModelTTFeatures {
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_none() -> u16 { fn get_backlight_none() -> u8 {
backlight::get_backlight_none() backlight::get_backlight_none()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_normal() -> u16 { fn get_backlight_normal() -> u8 {
backlight::get_backlight_normal() backlight::get_backlight_normal()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_low() -> u16 { fn get_backlight_low() -> u8 {
backlight::get_backlight_low() backlight::get_backlight_low()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_dim() -> u16 { fn get_backlight_dim() -> u8 {
backlight::get_backlight_dim() backlight::get_backlight_dim()
} }
#[cfg(feature = "backlight")] #[cfg(feature = "backlight")]
fn get_backlight_max() -> u16 { fn get_backlight_max() -> u8 {
backlight::get_backlight_max() backlight::get_backlight_max()
} }

View File

@ -10,43 +10,41 @@ const BACKLIGHT_MIN: u8 = 10;
const BACKLIGHT_MAX: u8 = 255; const BACKLIGHT_MAX: u8 = 255;
#[cfg(feature = "bootloader")] #[cfg(feature = "bootloader")]
pub fn get_backlight_normal() -> u16 { pub fn get_backlight_normal() -> u8 {
BACKLIGHT_NORMAL.into() BACKLIGHT_NORMAL
} }
#[cfg(not(feature = "bootloader"))] #[cfg(not(feature = "bootloader"))]
pub fn get_backlight_normal() -> u16 { pub fn get_backlight_normal() -> u8 {
storage::get_brightness() storage::get_brightness()
.unwrap_or(BACKLIGHT_NORMAL) .unwrap_or(BACKLIGHT_NORMAL)
.clamp(BACKLIGHT_MIN, BACKLIGHT_MAX) .clamp(BACKLIGHT_MIN, BACKLIGHT_MAX)
.into()
} }
#[cfg(feature = "bootloader")] #[cfg(feature = "bootloader")]
pub fn get_backlight_low() -> u16 { pub fn get_backlight_low() -> u8 {
BACKLIGHT_LOW.into() BACKLIGHT_LOW
} }
#[cfg(not(feature = "bootloader"))] #[cfg(not(feature = "bootloader"))]
pub fn get_backlight_low() -> u16 { pub fn get_backlight_low() -> u8 {
storage::get_brightness() storage::get_brightness()
.unwrap_or(BACKLIGHT_LOW) .unwrap_or(BACKLIGHT_LOW)
.clamp(BACKLIGHT_MIN, BACKLIGHT_LOW) .clamp(BACKLIGHT_MIN, BACKLIGHT_LOW)
.into()
} }
pub fn get_backlight_dim() -> u16 { pub fn get_backlight_dim() -> u8 {
BACKLIGHT_DIM.into() BACKLIGHT_DIM
} }
pub fn get_backlight_none() -> u16 { pub fn get_backlight_none() -> u8 {
BACKLIGHT_NONE.into() BACKLIGHT_NONE
} }
pub fn get_backlight_max() -> u16 { pub fn get_backlight_max() -> u8 {
BACKLIGHT_MAX.into() BACKLIGHT_MAX
} }
pub fn get_backlight_min() -> u16 { pub fn get_backlight_min() -> u8 {
BACKLIGHT_MIN.into() BACKLIGHT_MIN
} }

View File

@ -11,19 +11,19 @@ pub trait UIFeaturesCommon {
fn fadeout() {} fn fadeout() {}
fn backlight_on() {} fn backlight_on() {}
fn get_backlight_none() -> u16 { fn get_backlight_none() -> u8 {
0 0
} }
fn get_backlight_normal() -> u16 { fn get_backlight_normal() -> u8 {
0 0
} }
fn get_backlight_low() -> u16 { fn get_backlight_low() -> u8 {
0 0
} }
fn get_backlight_dim() -> u16 { fn get_backlight_dim() -> u8 {
0 0
} }
fn get_backlight_max() -> u16 { fn get_backlight_max() -> u8 {
0 0
} }