From 539420cac8004b5a6ccb81ae5ab5a01ced516527 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Fri, 23 Jun 2023 12:07:18 +0200 Subject: [PATCH] refactor(core): make backlight/fading optional [no changelog] --- core/SConscript.firmware | 7 +++- core/SConscript.unix | 7 +++- .../extmod/modtrezorutils/modtrezorutils.c | 6 ++++ core/embed/lib/display.c | 2 ++ core/embed/rust/Cargo.toml | 2 ++ core/embed/rust/src/ui/display/mod.rs | 12 +++++++ core/embed/trezorhal/boards/trezor_t.h | 1 + core/embed/unix/board-unix.h | 1 + core/mocks/generated/trezorutils.pyi | 1 + core/site_scons/boards/trezor_t.py | 2 ++ .../site_tools/micropython/__init__.py | 2 ++ core/src/trezor/ui/__init__.py | 34 ++++++++++--------- core/src/trezor/utils.py | 1 + 13 files changed, 60 insertions(+), 18 deletions(-) diff --git a/core/SConscript.firmware b/core/SConscript.firmware index 5fe2812d19..85f28fc80d 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -695,7 +695,12 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/bitcoin/sign_tx/zcash_v4.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/enums/Zcash*.py')) - source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY) + source_mpy = env.FrozenModule( + source=SOURCE_PY, + source_dir=SOURCE_PY_DIR, + bitcoin_only=BITCOIN_ONLY, + backlight='backlight' in FEATURES_AVAILABLE + ) source_mpyc = env.FrozenCFile( target='frozen_mpy.c', source=source_mpy, qstr_header=qstr_preprocessed) diff --git a/core/SConscript.unix b/core/SConscript.unix index b188b4c6a4..0de462803d 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -690,7 +690,12 @@ if FROZEN: SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'apps/bitcoin/sign_tx/zcash_v4.py')) SOURCE_PY.extend(Glob(SOURCE_PY_DIR + 'trezor/enums/Zcash*.py')) - source_mpy = env.FrozenModule(source=SOURCE_PY, source_dir=SOURCE_PY_DIR, bitcoin_only=BITCOIN_ONLY) + source_mpy = env.FrozenModule( + source=SOURCE_PY, + source_dir=SOURCE_PY_DIR, + bitcoin_only=BITCOIN_ONLY, + backlight='backlight' in TREZOR_MODEL in ('T',) + ) source_mpyc = env.FrozenCFile( target='frozen_mpy.c', source=source_mpy, qstr_header=qstr_preprocessed) diff --git a/core/embed/extmod/modtrezorutils/modtrezorutils.c b/core/embed/extmod/modtrezorutils/modtrezorutils.c index 0eecb3e5f0..2fabdcded1 100644 --- a/core/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/core/embed/extmod/modtrezorutils/modtrezorutils.c @@ -259,6 +259,7 @@ STATIC mp_obj_str_t mod_trezorutils_revision_obj = { /// VERSION_MINOR: int /// VERSION_PATCH: int /// USE_SD_CARD: bool +/// USE_BACKLIGHT: bool /// MODEL: str /// INTERNAL_MODEL: str /// EMULATOR: bool @@ -289,6 +290,11 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { {MP_ROM_QSTR(MP_QSTR_USE_SD_CARD), mp_const_true}, #else {MP_ROM_QSTR(MP_QSTR_USE_SD_CARD), mp_const_false}, +#endif +#ifdef USE_SD_CARD + {MP_ROM_QSTR(MP_QSTR_USE_BACKLIGHT), mp_const_true}, +#else + {MP_ROM_QSTR(MP_QSTR_USE_BACKLIGHT), mp_const_false}, #endif {MP_ROM_QSTR(MP_QSTR_MODEL), MP_ROM_QSTR(MODEL_NAME_QSTR)}, {MP_ROM_QSTR(MP_QSTR_INTERNAL_MODEL), diff --git a/core/embed/lib/display.c b/core/embed/lib/display.c index 3241091350..71eede34cd 100644 --- a/core/embed/lib/display.c +++ b/core/embed/lib/display.c @@ -567,11 +567,13 @@ void display_offset(int set_xy[2], int *get_x, int *get_y) { } void display_fade(int start, int end, int delay) { +#ifdef USE_BACKLIGHT for (int i = 0; i < 100; i++) { display_backlight(start + i * (end - start) / 100); hal_delay(delay / 100); } display_backlight(end); +#endif } #define UTF8_IS_CONT(ch) (((ch)&0xC0) == 0x80) diff --git a/core/embed/rust/Cargo.toml b/core/embed/rust/Cargo.toml index 4ac1755584..bb34304e3d 100644 --- a/core/embed/rust/Cargo.toml +++ b/core/embed/rust/Cargo.toml @@ -27,6 +27,7 @@ debug = ["ui_debug"] sbu = [] sd_card = [] rgb_led = [] +backlight = [] test = [ "button", "cc", @@ -37,6 +38,7 @@ test = [ "ui", "dma2d", "touch", + "backlight" ] [lib] diff --git a/core/embed/rust/src/ui/display/mod.rs b/core/embed/rust/src/ui/display/mod.rs index 51e7f4425f..67a891d6d7 100644 --- a/core/embed/rust/src/ui/display/mod.rs +++ b/core/embed/rust/src/ui/display/mod.rs @@ -39,15 +39,18 @@ pub fn backlight() -> u16 { display::backlight(-1) as u16 } +#[cfg(feature = "backlight")] pub fn set_backlight(val: u16) { display::backlight(val as i32); } +#[cfg(feature = "backlight")] pub fn fade_backlight(target: u16) { const FADE_DURATION_MS: u32 = 50; fade_backlight_duration(target, FADE_DURATION_MS); } +#[cfg(feature = "backlight")] pub fn fade_backlight_duration(target: u16, duration_ms: u32) { let target = target as i32; let duration_ms = duration_ms as i32; @@ -62,6 +65,15 @@ pub fn fade_backlight_duration(target: u16, duration_ms: u32) { set_backlight(target as u16); } +#[cfg(not(feature = "backlight"))] +pub fn set_backlight(_: u16) {} + +#[cfg(not(feature = "backlight"))] +pub fn fade_backlight(_: u16) {} + +#[cfg(not(feature = "backlight"))] +pub fn fade_backlight_duration(_: u16, _: u32) {} + /// Fill a whole rectangle with a specific color. pub fn rect_fill(r: Rect, fg_color: Color) { display::bar(r.x0, r.y0, r.width(), r.height(), fg_color.into()); diff --git a/core/embed/trezorhal/boards/trezor_t.h b/core/embed/trezorhal/boards/trezor_t.h index 6abd082a77..d4630bb988 100644 --- a/core/embed/trezorhal/boards/trezor_t.h +++ b/core/embed/trezorhal/boards/trezor_t.h @@ -9,6 +9,7 @@ #define USE_TOUCH 1 #define USE_SBU 1 #define USE_RGB_COLORS 1 +#define USE_BACKLIGHT 1 #define USE_DISP_I8080_8BIT_DW 1 #define I2C_COUNT 1 diff --git a/core/embed/unix/board-unix.h b/core/embed/unix/board-unix.h index 0af4b14037..99fa6b8aee 100644 --- a/core/embed/unix/board-unix.h +++ b/core/embed/unix/board-unix.h @@ -6,6 +6,7 @@ #define USE_SD_CARD 1 #define USE_SBU 1 #define USE_RGB_COLORS 1 +#define USE_BACKLIGHT 1 #endif #ifdef TREZOR_MODEL_1 diff --git a/core/mocks/generated/trezorutils.pyi b/core/mocks/generated/trezorutils.pyi index 28c7250876..913d958410 100644 --- a/core/mocks/generated/trezorutils.pyi +++ b/core/mocks/generated/trezorutils.pyi @@ -84,6 +84,7 @@ VERSION_MAJOR: int VERSION_MINOR: int VERSION_PATCH: int USE_SD_CARD: bool +USE_BACKLIGHT: bool MODEL: str INTERNAL_MODEL: str EMULATOR: bool diff --git a/core/site_scons/boards/trezor_t.py b/core/site_scons/boards/trezor_t.py index ee5f934d6d..07cfcd9870 100644 --- a/core/site_scons/boards/trezor_t.py +++ b/core/site_scons/boards/trezor_t.py @@ -26,6 +26,8 @@ def configure( sources += [f'embed/trezorhal/displays/panels/lx154a2411.c', ] sources += [f'embed/trezorhal/displays/panels/lx154a2422.c', ] + features_available.append("backlight") + if "input" in features_wanted: sources += ["embed/trezorhal/i2c.c"] sources += ["embed/trezorhal/touch/touch.c"] diff --git a/core/site_scons/site_tools/micropython/__init__.py b/core/site_scons/site_tools/micropython/__init__.py index f5dc01e2be..b39fc29470 100644 --- a/core/site_scons/site_tools/micropython/__init__.py +++ b/core/site_scons/site_tools/micropython/__init__.py @@ -31,9 +31,11 @@ def generate(env): # replace "utils.BITCOIN_ONLY" with literal constant (True/False) # so the compiler can optimize out the things we don't want btc_only = env['bitcoin_only'] == '1' + backlight = env['backlight'] interim = f"{target[:-4]}.i" # replace .mpy with .i sed_scripts = " ".join([ rf"-e 's/utils\.BITCOIN_ONLY/{btc_only}/g'", + rf"-e 's/utils\.USE_BACKLIGHT/{backlight}/g'", r"-e 's/if TYPE_CHECKING/if False/'", r"-e 's/import typing/# \0/'", r"-e '/from typing import (/,/^\s*)/ {s/^/# /}'", diff --git a/core/src/trezor/ui/__init__.py b/core/src/trezor/ui/__init__.py index 59ddcde710..fabfaa6467 100644 --- a/core/src/trezor/ui/__init__.py +++ b/core/src/trezor/ui/__init__.py @@ -66,29 +66,31 @@ async def _alert(count: int) -> None: def alert(count: int = 3) -> None: - global _alert_in_progress - if _alert_in_progress: - return + if utils.USE_BACKLIGHT: + global _alert_in_progress + if _alert_in_progress: + return - _alert_in_progress = True - loop.schedule(_alert(count)) + _alert_in_progress = True + loop.schedule(_alert(count)) def backlight_fade(val: int, delay: int = 14000, step: int = 15) -> None: - if __debug__: - if utils.DISABLE_ANIMATION: + if utils.USE_BACKLIGHT: + if __debug__: + if utils.DISABLE_ANIMATION: + display.backlight(val) + return + current = display.backlight() + if current < 0: display.backlight(val) return - current = display.backlight() - if current < 0: + elif current > val: + step = -step + for i in range(current, val, step): + display.backlight(i) + utime.sleep_us(delay) display.backlight(val) - return - elif current > val: - step = -step - for i in range(current, val, step): - display.backlight(i) - utime.sleep_us(delay) - display.backlight(val) # Component events. Should be different from `io.TOUCH_*` events. diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index 243405fa8b..d40cb16df5 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -6,6 +6,7 @@ from trezorutils import ( # noqa: F401 INTERNAL_MODEL, MODEL, SCM_REVISION, + USE_BACKLIGHT, USE_SD_CARD, VERSION_MAJOR, VERSION_MINOR,