From 61482d07958c06362c59696fd1d88c2eb83c1cfa Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Wed, 1 Jun 2022 11:49:57 +0200 Subject: [PATCH] feat(core/rust): expose raw pixel access to rust --- core/.changelog.d/2297.added | 1 + .../embed/extmod/modtrezorui/display-stm32_1.h | 7 ++++--- .../embed/extmod/modtrezorui/display-stm32_R.h | 7 ++++--- .../embed/extmod/modtrezorui/display-stm32_T.h | 5 +++-- core/embed/extmod/modtrezorui/display-unix.h | 7 ++++--- core/embed/extmod/modtrezorui/display.c | 2 ++ core/embed/rust/src/trezorhal/display.rs | 18 ++++++++++++++++++ core/embed/rust/src/ui/display.rs | 17 +++++++++++++++++ 8 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 core/.changelog.d/2297.added diff --git a/core/.changelog.d/2297.added b/core/.changelog.d/2297.added new file mode 100644 index 000000000..1ca22a4af --- /dev/null +++ b/core/.changelog.d/2297.added @@ -0,0 +1 @@ +Expose raw pixel access to Rust diff --git a/core/embed/extmod/modtrezorui/display-stm32_1.h b/core/embed/extmod/modtrezorui/display-stm32_1.h index 34ff1a054..7b838c9ac 100644 --- a/core/embed/extmod/modtrezorui/display-stm32_1.h +++ b/core/embed/extmod/modtrezorui/display-stm32_1.h @@ -68,7 +68,7 @@ static struct { static bool pixeldata_dirty = true; -void PIXELDATA(uint16_t c) { +void display_pixeldata(uint16_t c) { if (PIXELWINDOW.pos.x <= PIXELWINDOW.end.x && PIXELWINDOW.pos.y <= PIXELWINDOW.end.y) { // set to white if highest bits of all R, G, B values are set to 1 @@ -89,12 +89,13 @@ void PIXELDATA(uint16_t c) { } } +#define PIXELDATA(c) display_pixeldata(c) + static void display_reset_state() {} void PIXELDATA_DIRTY() { pixeldata_dirty = true; } -static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, - uint16_t y1) { +void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { PIXELWINDOW.start.x = x0; PIXELWINDOW.start.y = y0; PIXELWINDOW.end.x = x1; diff --git a/core/embed/extmod/modtrezorui/display-stm32_R.h b/core/embed/extmod/modtrezorui/display-stm32_R.h index ce14b5abe..33e7c798b 100644 --- a/core/embed/extmod/modtrezorui/display-stm32_R.h +++ b/core/embed/extmod/modtrezorui/display-stm32_R.h @@ -57,7 +57,7 @@ static void display_set_page_and_col(uint8_t page, uint8_t col) { } } -void PIXELDATA(uint16_t c) { +void display_pixeldata(uint16_t c) { uint8_t data = DISPLAY_STATE.RAM[DISPLAY_STATE.row / 8][DISPLAY_STATE.col]; uint8_t bit = 1 << (DISPLAY_STATE.row % 8); @@ -93,6 +93,8 @@ void PIXELDATA(uint16_t c) { } } +#define PIXELDATA(c) display_pixeldata(c) + static void display_reset_state(void) { memset(DISPLAY_STATE.RAM, 0, sizeof(DISPLAY_STATE.RAM)); DISPLAY_STATE.row = 0; @@ -115,8 +117,7 @@ static void display_unsleep(void) { CMD(0xAF); // Display ON } -static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, - uint16_t y1) { +void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { if (x1 >= DISPLAY_RESX) { x1 = DISPLAY_RESX - 1; } diff --git a/core/embed/extmod/modtrezorui/display-stm32_T.h b/core/embed/extmod/modtrezorui/display-stm32_T.h index 815c0dd13..35abfddf5 100644 --- a/core/embed/extmod/modtrezorui/display-stm32_T.h +++ b/core/embed/extmod/modtrezorui/display-stm32_T.h @@ -56,6 +56,8 @@ const volatile uint8_t DISPLAY_ST7789V_INVERT_COLORS = 0; // of ILI9341V datasheet #define DISPLAY_ID_ILI9341V 0x009341U +void display_pixeldata(uint16_t c) { PIXELDATA(c); } + static uint32_t read_display_id(uint8_t command) { volatile uint8_t c = 0; uint32_t id = 0; @@ -117,8 +119,7 @@ static void display_unsleep(void) { static struct { uint16_t x, y; } BUFFER_OFFSET; -static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, - uint16_t y1) { +void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { x0 += BUFFER_OFFSET.x; x1 += BUFFER_OFFSET.x; y0 += BUFFER_OFFSET.y; diff --git a/core/embed/extmod/modtrezorui/display-unix.h b/core/embed/extmod/modtrezorui/display-unix.h index 7a72e2f65..a8ccd2920 100644 --- a/core/embed/extmod/modtrezorui/display-unix.h +++ b/core/embed/extmod/modtrezorui/display-unix.h @@ -75,7 +75,7 @@ static struct { // noop on unix, display is refreshed every loop step #define PIXELDATA_DIRTY() -void PIXELDATA(uint16_t c) { +void display_pixeldata(uint16_t c) { #if defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R // set to white if highest bits of all R, G, B values are set to 1 // bin(10000 100000 10000) = hex(0x8410) @@ -98,6 +98,8 @@ void PIXELDATA(uint16_t c) { } } +#define PIXELDATA(c) display_pixeldata(c) + static void display_reset_state() {} void display_init_seq(void) {} @@ -208,8 +210,7 @@ void display_init(void) { #endif } -static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, - uint16_t y1) { +void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { if (!RENDERER) { display_init(); } diff --git a/core/embed/extmod/modtrezorui/display.c b/core/embed/extmod/modtrezorui/display.c index fe368acd9..db2c69cdc 100644 --- a/core/embed/extmod/modtrezorui/display.c +++ b/core/embed/extmod/modtrezorui/display.c @@ -989,3 +989,5 @@ void display_utf8_substr(const char *buf_start, size_t buf_len, int char_off, *out_start = buf_start + i_start; *out_len = i - i_start; } + +void display_pixeldata_dirty(void) { PIXELDATA_DIRTY(); } diff --git a/core/embed/rust/src/trezorhal/display.rs b/core/embed/rust/src/trezorhal/display.rs index 08cb446c3..d2831a42b 100644 --- a/core/embed/rust/src/trezorhal/display.rs +++ b/core/embed/rust/src/trezorhal/display.rs @@ -107,3 +107,21 @@ pub fn loader( ); } } + +pub fn pixeldata(c: u16) { + unsafe { + ffi::display_pixeldata(c); + } +} + +pub fn pixeldata_dirty() { + unsafe { + ffi::display_pixeldata_dirty(); + } +} + +pub fn set_window(x0: u16, y0: u16, x1: u16, y1: u16) { + unsafe { + ffi::display_set_window(x0, y0, x1, y1); + } +} diff --git a/core/embed/rust/src/ui/display.rs b/core/embed/rust/src/ui/display.rs index 49f68cc73..74d0bbf34 100644 --- a/core/embed/rust/src/ui/display.rs +++ b/core/embed/rust/src/ui/display.rs @@ -183,6 +183,23 @@ pub fn text_right(baseline: Point, text: &str, font: Font, fg_color: Color, bg_c ); } +pub fn pixeldata(color: Color) { + display::pixeldata(color.into()); +} + +pub fn pixeldata_dirty() { + display::pixeldata_dirty(); +} + +pub fn set_window(window: Rect) { + display::set_window( + window.x0 as u16, + window.y0 as u16, + window.x1 as u16 - 1, + window.y1 as u16 - 1, + ); +} + #[derive(Copy, Clone, PartialEq, Eq)] pub struct Font(i32);