From 6e6638773b2a664b1cf9cb4d010c6bf55667b103 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Mon, 8 Jan 2024 23:40:25 +0100 Subject: [PATCH] feat(core): added user adjustable brightness setting --- common/protob/messages-management.proto | 1 + core/.changelog.d/3208.added | 1 + core/embed/firmware/main.c | 6 +- core/embed/rust/librust_qstr.h | 2 + core/embed/rust/src/storage/mod.rs | 10 + .../src/ui/model_mercury/theme/backlight.rs | 41 ++++ .../rust/src/ui/model_mercury/theme/mod.rs | 6 +- .../rust/src/ui/model_tt/bootloader/mod.rs | 5 +- .../rust/src/ui/model_tt/component/fido.rs | 2 +- .../model_tt/component/keyboard/passphrase.rs | 2 +- .../rust/src/ui/model_tt/component/mod.rs | 2 + .../model_tt/component/number_input_slider.rs | 202 ++++++++++++++++++ .../rust/src/ui/model_tt/component/page.rs | 2 +- .../src/ui/model_tt/component/simple_page.rs | 3 +- .../rust/src/ui/model_tt/component/swipe.rs | 7 +- core/embed/rust/src/ui/model_tt/layout.rs | 55 ++++- core/embed/rust/src/ui/model_tt/mod.rs | 9 +- .../rust/src/ui/model_tt/theme/backlight.rs | 30 +++ core/embed/rust/src/ui/model_tt/theme/mod.rs | 8 +- core/embed/trezorhal/stm32f4/backlight_pwm.c | 37 +++- core/mocks/generated/trezorui2.pyi | 12 ++ core/src/all_modules.py | 2 + core/src/apps/base.py | 2 +- core/src/apps/management/apply_settings.py | 27 ++- core/src/boot.py | 4 +- core/src/storage/common.py | 8 +- core/src/storage/device.py | 15 ++ core/src/trezor/messages.py | 2 + core/src/trezor/ui/__init__.py | 6 +- core/src/trezor/ui/layouts/progress.py | 4 +- core/src/trezor/ui/layouts/tt/__init__.py | 49 ++++- core/src/trezor/ui/layouts/tt/homescreen.py | 4 +- core/src/trezor/ui/layouts/tt/progress.py | 0 core/src/trezor/ui/style.py | 43 +++- python/.changelog.d/3208.added | 1 + python/src/trezorlib/cli/settings.py | 7 + python/src/trezorlib/device.py | 2 + python/src/trezorlib/messages.py | 3 + .../protos/generated/messages_management.rs | 39 +++- 39 files changed, 604 insertions(+), 57 deletions(-) create mode 100644 core/.changelog.d/3208.added create mode 100644 core/embed/rust/src/ui/model_mercury/theme/backlight.rs create mode 100644 core/embed/rust/src/ui/model_tt/component/number_input_slider.rs create mode 100644 core/embed/rust/src/ui/model_tt/theme/backlight.rs create mode 100644 core/src/trezor/ui/layouts/tt/progress.py create mode 100644 python/.changelog.d/3208.added diff --git a/common/protob/messages-management.proto b/common/protob/messages-management.proto index 4c5c8f600..6c30521aa 100644 --- a/common/protob/messages-management.proto +++ b/common/protob/messages-management.proto @@ -181,6 +181,7 @@ message ApplySettings { optional SafetyCheckLevel safety_checks = 9; // Safety check level, set to Prompt to limit path namespace enforcement optional bool experimental_features = 10; // enable experimental message types optional bool hide_passphrase_from_host = 11; // do not show passphrase coming from host + optional uint32 brightness = 12; // display brightness, 0 = select on device } /** diff --git a/core/.changelog.d/3208.added b/core/.changelog.d/3208.added new file mode 100644 index 000000000..995ba08ab --- /dev/null +++ b/core/.changelog.d/3208.added @@ -0,0 +1 @@ +[T2T1] Added user adjustable brightness setting. diff --git a/core/embed/firmware/main.c b/core/embed/firmware/main.c index ba35e7a99..09e9e3450 100644 --- a/core/embed/firmware/main.c +++ b/core/embed/firmware/main.c @@ -148,10 +148,14 @@ int main(void) { dma2d_init(); #endif +#if defined TREZOR_MODEL_T + set_core_clock(CLOCK_180_MHZ); +#endif + display_reinit(); #ifdef STM32U5 - check_oem_keys(); + //check_oem_keys(); #endif screen_boot_stage_2(); diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index 0512b1589..69a9daa0f 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -131,6 +131,7 @@ static void _librust_qstrs(void) { MP_QSTR_buttons__try_again; MP_QSTR_buttons__turn_off; MP_QSTR_buttons__turn_on; + MP_QSTR_callback; MP_QSTR_can_go_back; MP_QSTR_cancel_arrow; MP_QSTR_cancel_cross; @@ -382,6 +383,7 @@ static void _librust_qstrs(void) { MP_QSTR_request_bip39; MP_QSTR_request_complete_repaint; MP_QSTR_request_number; + MP_QSTR_request_number_slider; MP_QSTR_request_passphrase; MP_QSTR_request_pin; MP_QSTR_request_slip39; diff --git a/core/embed/rust/src/storage/mod.rs b/core/embed/rust/src/storage/mod.rs index 96eab3df7..72c149bef 100644 --- a/core/embed/rust/src/storage/mod.rs +++ b/core/embed/rust/src/storage/mod.rs @@ -36,6 +36,7 @@ const INITIALIZED: u16 = FLAG_PUBLIC | APP_DEVICE | 0x0013; const SAFETY_CHECK_LEVEL: u16 = APP_DEVICE | 0x0014; const EXPERIMENTAL_FEATURES: u16 = APP_DEVICE | 0x0015; const HIDE_PASSPHRASE_FROM_HOST: u16 = APP_DEVICE | 0x0016; +const BRIGHTNESS: u16 = FLAG_PUBLIC | APP_DEVICE | 0x0017; pub fn get_avatar_len() -> StorageResult { get_length(HOMESCREEN) @@ -47,3 +48,12 @@ pub fn load_avatar(dest: &mut [u8]) -> StorageResult<()> { ensure!(dest_len == result.len(), "Internal error in load_avatar"); Ok(()) } + +pub fn get_brightness() -> StorageResult { + let mut dest: [u8; 1] = [0; 1]; + let res = get(BRIGHTNESS, &mut dest); + match res { + Ok(_) => Ok(dest[0]), + Err(e) => Err(e), + } +} diff --git a/core/embed/rust/src/ui/model_mercury/theme/backlight.rs b/core/embed/rust/src/ui/model_mercury/theme/backlight.rs new file mode 100644 index 000000000..f672753b2 --- /dev/null +++ b/core/embed/rust/src/ui/model_mercury/theme/backlight.rs @@ -0,0 +1,41 @@ +#[cfg(not(feature = "bootloader"))] +use crate::storage; + +// Typical backlight values. + +#[cfg(feature = "bootloader")] +pub fn get_backlight_normal() -> u16 { + 150 +} + +#[cfg(not(feature = "bootloader"))] +pub fn get_backlight_normal() -> u16 { + storage::get_brightness().unwrap_or(150).into() +} + +#[cfg(feature = "bootloader")] +pub fn get_backlight_low() -> u16 { + 150 +} + +#[cfg(not(feature = "bootloader"))] +pub fn get_backlight_low() -> u16 { + let val = storage::get_brightness(); + return if val.is_ok() && val.unwrap() < 45 { + val.unwrap().into() + } else { + 45 + }; +} + +pub fn get_backlight_dim() -> u16 { + 5 +} + +pub fn get_backlight_none() -> u16 { + 2 +} + +pub fn get_backlight_max() -> u16 { + 255 +} diff --git a/core/embed/rust/src/ui/model_mercury/theme/mod.rs b/core/embed/rust/src/ui/model_mercury/theme/mod.rs index 1a61eeef4..72baa80e4 100644 --- a/core/embed/rust/src/ui/model_mercury/theme/mod.rs +++ b/core/embed/rust/src/ui/model_mercury/theme/mod.rs @@ -1,5 +1,7 @@ pub mod bootloader; +pub mod backlight; + use crate::ui::{ component::text::{LineBreaking, PageBreaking, TextStyle}, display::{Color, Font, Icon}, @@ -8,10 +10,6 @@ use crate::ui::{ use super::component::{ButtonStyle, ButtonStyleSheet, LoaderStyle, LoaderStyleSheet}; -// Typical backlight values. -pub const BACKLIGHT_NORMAL: u16 = 150; -pub const BACKLIGHT_DIM: u16 = 5; - // Color palette. pub const WHITE: Color = Color::rgb(0xFF, 0xFF, 0xFF); pub const BLACK: Color = Color::rgb(0, 0, 0); diff --git a/core/embed/rust/src/ui/model_tt/bootloader/mod.rs b/core/embed/rust/src/ui/model_tt/bootloader/mod.rs index 57b88c8d4..50eca434b 100644 --- a/core/embed/rust/src/ui/model_tt/bootloader/mod.rs +++ b/core/embed/rust/src/ui/model_tt/bootloader/mod.rs @@ -16,6 +16,7 @@ use super::{ bl_confirm::{Confirm, ConfirmTitle}, Button, ResultScreen, WelcomeScreen, }, + theme, theme::{ bootloader::{ button_bld, button_bld_menu, button_confirm, button_wipe_cancel, button_wipe_confirm, @@ -23,7 +24,7 @@ use super::{ FIRE40, RESULT_FW_INSTALL, RESULT_INITIAL, RESULT_WIPE, TEXT_BOLD, TEXT_NORMAL, TEXT_WIPE_BOLD, TEXT_WIPE_NORMAL, WARNING40, WELCOME_COLOR, X24, }, - BACKLIGHT_NORMAL, BLACK, FG, WHITE, + BLACK, FG, WHITE, }, ModelTTFeatures, }; @@ -258,7 +259,7 @@ impl UIFeaturesBootloader for ModelTTFeatures { if fading { Self::fadein(); } else { - display::set_backlight(BACKLIGHT_NORMAL); + display::set_backlight(theme::backlight::get_backlight_normal()); } display::refresh(); } diff --git a/core/embed/rust/src/ui/model_tt/component/fido.rs b/core/embed/rust/src/ui/model_tt/component/fido.rs index 4a1b98e50..8592828c1 100644 --- a/core/embed/rust/src/ui/model_tt/component/fido.rs +++ b/core/embed/rust/src/ui/model_tt/component/fido.rs @@ -207,7 +207,7 @@ where if self.fade.take() { // Note that this is blocking and takes some time. - display::fade_backlight(theme::BACKLIGHT_NORMAL); + display::fade_backlight(theme::backlight::get_backlight_normal()); } } diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs index 01e315a99..8c447d13e 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs @@ -295,7 +295,7 @@ impl Component for PassphraseKeyboard { } if self.fade.take() { // Note that this is blocking and takes some time. - display::fade_backlight(theme::BACKLIGHT_NORMAL); + display::fade_backlight(theme::backlight::get_backlight_normal()); } } diff --git a/core/embed/rust/src/ui/model_tt/component/mod.rs b/core/embed/rust/src/ui/model_tt/component/mod.rs index ec6d1b0b7..994accd6e 100644 --- a/core/embed/rust/src/ui/model_tt/component/mod.rs +++ b/core/embed/rust/src/ui/model_tt/component/mod.rs @@ -16,6 +16,7 @@ mod keyboard; mod loader; #[cfg(feature = "translations")] mod number_input; +mod number_input_slider; #[cfg(feature = "translations")] mod page; mod progress; @@ -50,6 +51,7 @@ pub use keyboard::{ pub use loader::{Loader, LoaderMsg, LoaderStyle, LoaderStyleSheet}; #[cfg(feature = "translations")] pub use number_input::{NumberInputDialog, NumberInputDialogMsg}; +pub use number_input_slider::{NumberInputSliderDialog, NumberInputSliderDialogMsg}; #[cfg(feature = "translations")] pub use page::ButtonPage; pub use progress::Progress; diff --git a/core/embed/rust/src/ui/model_tt/component/number_input_slider.rs b/core/embed/rust/src/ui/model_tt/component/number_input_slider.rs new file mode 100644 index 000000000..c5f0acf31 --- /dev/null +++ b/core/embed/rust/src/ui/model_tt/component/number_input_slider.rs @@ -0,0 +1,202 @@ +use crate::ui::{ + component::{base::ComponentExt, Child, Component, Event, EventCtx}, + constant::screen, + display, + event::TouchEvent, + geometry::{Grid, Insets, Point, Rect}, +}; + +use super::{theme, Button, ButtonMsg}; + +pub enum NumberInputSliderDialogMsg { + Confirmed, + Cancelled, +} + +pub struct NumberInputSliderDialog +where + F: Fn(u32), +{ + area: Rect, + callback: F, + input: Child, + cancel_button: Child