use super::constant; use crate::{ error::Error, time::Duration, trezorhal::{display, qr, time}, ui::lerp::Lerp, }; use core::slice; use super::geometry::{Offset, Point, Rect}; pub fn backlight() -> i32 { display::backlight(-1) } pub fn set_backlight(val: i32) { display::backlight(val); } pub fn fade_backlight(target: i32) { const BACKLIGHT_DELAY: Duration = Duration::from_millis(14); const BACKLIGHT_STEP: usize = 15; let current = backlight(); if current < target { for val in (current..target).step_by(BACKLIGHT_STEP) { set_backlight(val); time::sleep(BACKLIGHT_DELAY); } } else { for val in (target..current).rev().step_by(BACKLIGHT_STEP) { set_backlight(val); time::sleep(BACKLIGHT_DELAY); } } } pub fn rect_fill(r: Rect, fg_color: Color) { display::bar(r.x0, r.y0, r.width(), r.height(), fg_color.into()); } pub fn rect_stroke(r: Rect, fg_color: Color) { display::bar(r.x0, r.y0, r.width(), 1, fg_color.into()); display::bar(r.x0, r.y0 + r.height() - 1, r.width(), 1, fg_color.into()); display::bar(r.x0, r.y0, 1, r.height(), fg_color.into()); display::bar(r.x0 + r.width() - 1, r.y0, 1, r.height(), fg_color.into()); } pub fn rect_fill_rounded(r: Rect, fg_color: Color, bg_color: Color, radius: u8) { assert!([2, 4, 8, 16].iter().any(|allowed| radius == *allowed)); display::bar_radius( r.x0, r.y0, r.width(), r.height(), fg_color.into(), bg_color.into(), radius, ); } /// NOTE: Cannot start at odd x-coordinate. In this case icon is shifted 1px /// left. pub fn icon_top_left(top_left: Point, data: &[u8], fg_color: Color, bg_color: Color) { let toif_info = display::toif_info(data).unwrap(); assert!(toif_info.grayscale); display::icon( top_left.x, top_left.y, toif_info.width.into(), toif_info.height.into(), &data[12..], // Skip TOIF header. fg_color.into(), bg_color.into(), ); } pub fn icon(center: Point, data: &[u8], fg_color: Color, bg_color: Color) { let toif_info = display::toif_info(data).unwrap(); assert!(toif_info.grayscale); let r = Rect::from_center_and_size( center, Offset::new(toif_info.width.into(), toif_info.height.into()), ); display::icon( r.x0, r.y0, r.width(), r.height(), &data[12..], // Skip TOIF header. fg_color.into(), bg_color.into(), ); } pub fn image(center: Point, data: &[u8]) { let toif_info = display::toif_info(data).unwrap(); assert!(!toif_info.grayscale); let r = Rect::from_center_and_size( center, Offset::new(toif_info.width.into(), toif_info.height.into()), ); display::image( r.x0, r.y0, r.width(), r.height(), &data[12..], // Skip TOIF header. ); } pub fn toif_info(data: &[u8]) -> Option<(Offset, bool)> { if let Ok(info) = display::toif_info(data) { Some(( Offset::new(info.width.into(), info.height.into()), info.grayscale, )) } else { None } } // Used on T1 only. pub fn rect_fill_rounded1(r: Rect, fg_color: Color, bg_color: Color) { display::bar(r.x0, r.y0, r.width(), r.height(), fg_color.into()); let corners = [ r.top_left(), r.top_right() - Offset::x(1), r.bottom_right() - Offset::uniform(1), r.bottom_left() - Offset::y(1), ]; for p in corners.iter() { display::bar(p.x, p.y, 1, 1, bg_color.into()); } } #[derive(Copy, Clone, PartialEq, Eq)] pub struct TextOverlay<'a> { area: Rect, text: &'a str, font: Font, } impl<'a> TextOverlay<'a> { pub fn new(text: &'a str, font: Font) -> Self { let area = Rect::zero(); Self { area, text, font } } pub fn place(&mut self, baseline: Point) { let text_width = self.font.text_width(self.text); let text_height = self.font.text_height(); let text_area_start = baseline + Offset::new(-(text_width / 2), -text_height); let text_area_end = baseline + Offset::new(text_width / 2, 0); let area = Rect::new(text_area_start, text_area_end); self.area = area; } pub fn get_pixel(&self, underlying: Color, fg: Color, p: Point) -> Color { if !self.area.contains(p) { return underlying; } let mut tot_adv = 0; let p_rel = Point::new(p.x - self.area.x0, p.y - self.area.y0); for g in self.text.bytes().filter_map(|c| self.font.get_glyph(c)) { let char_area = Rect::new( Point::new(tot_adv + g.bearing_x, g.height - g.bearing_y), Point::new(tot_adv + g.bearing_x + g.width, g.bearing_y), ); tot_adv += g.adv; if !char_area.contains(p_rel) { continue; } let p_inner = p_rel - char_area.top_left(); let overlay_data = g.get_pixel_data(p_inner); return Color::lerp(underlying, fg, overlay_data as f32 / 15_f32); } underlying } } /// Gets a color of a pixel on `p` coordinates of rounded rectangle with corner /// radius 2 fn rect_rounded2_get_pixel( p: Offset, size: Offset, colortable: [Color; 16], fill: bool, line_width: i32, ) -> Color { let border = (p.x >= 0 && p.x < line_width) || ((p.x >= size.x - line_width) && p.x <= (size.x - 1)) || (p.y >= 0 && p.y < line_width) || ((p.y >= size.y - line_width) && p.y <= (size.y - 1)); let corner_lim = 2 * line_width; let corner_inner = line_width; let corner_all = ((p.x > size.x - (corner_lim + 1)) || p.x < corner_lim) && (p.y < corner_lim || p.y > size.y - (corner_lim + 1)); let corner = corner_all && (p.y >= corner_inner) && (p.x >= corner_inner) && (p.y <= size.y - (corner_inner + 1)) && (p.x <= size.x - (corner_inner + 1)); let corner_out = corner_all && !corner; if (border || corner || fill) && !corner_out { colortable[15] } else { colortable[0] } } /// Draws a rounded rectangle with corner radius 2, partially filled /// according to `fill_from` and `fill_to` arguments. /// Optionally draws a text inside the rectangle and adjusts its color to match /// the fill. The coordinates of the text are specified in the TextOverlay /// struct. pub fn bar_with_text_and_fill( area: Rect, overlay: Option, fg_color: Color, bg_color: Color, fill_from: i32, fill_to: i32, ) { let r = area.translate(get_offset()); let clamped = r.clamp(constant::screen()); let colortable = get_color_table(fg_color, bg_color); set_window(clamped); for y_c in clamped.y0..clamped.y1 { for x_c in clamped.x0..clamped.x1 { let p = Point::new(x_c, y_c); let r_offset = p - r.top_left(); let filled = (r_offset.x >= fill_from && fill_from >= 0 && (r_offset.x <= fill_to || fill_to < fill_from)) || (r_offset.x < fill_to && fill_to >= 0); let underlying_color = rect_rounded2_get_pixel(r_offset, r.size(), colortable, filled, 1); let final_color = overlay.map_or(underlying_color, |o| { let text_color = if filled { bg_color } else { fg_color }; o.get_pixel(underlying_color, text_color, p) }); pixeldata(final_color); } } pixeldata_dirty(); } // Used on T1 only. pub fn dotted_line(start: Point, width: i32, color: Color) { for x in (start.x..width).step_by(2) { display::bar(x, start.y, 1, 1, color.into()); } } pub const LOADER_MIN: u16 = 0; pub const LOADER_MAX: u16 = 1000; pub fn loader( progress: u16, y_offset: i32, fg_color: Color, bg_color: Color, icon: Option<(&[u8], Color)>, ) { display::loader( progress, false, y_offset, fg_color.into(), bg_color.into(), icon.map(|i| i.0), icon.map(|i| i.1.into()).unwrap_or(0), ); } pub fn loader_indeterminate( progress: u16, y_offset: i32, fg_color: Color, bg_color: Color, icon: Option<(&[u8], Color)>, ) { display::loader( progress, true, y_offset, fg_color.into(), bg_color.into(), icon.map(|i| i.0), icon.map(|i| i.1.into()).unwrap_or(0), ); } pub fn qrcode(center: Point, data: &str, max_size: u32, case_sensitive: bool) -> Result<(), Error> { qr::render_qrcode(center.x, center.y, data, max_size, case_sensitive) } pub fn text(baseline: Point, text: &str, font: Font, fg_color: Color, bg_color: Color) { display::text( baseline.x, baseline.y, text, font.0, fg_color.into(), bg_color.into(), ); } pub fn text_center(baseline: Point, text: &str, font: Font, fg_color: Color, bg_color: Color) { let w = font.text_width(text); display::text( baseline.x - w / 2, baseline.y, text, font.0, fg_color.into(), bg_color.into(), ); } pub fn text_right(baseline: Point, text: &str, font: Font, fg_color: Color, bg_color: Color) { let w = font.text_width(text); display::text( baseline.x - w, baseline.y, text, font.0, fg_color.into(), bg_color.into(), ); } #[inline(always)] pub fn pixeldata(color: Color) { display::pixeldata(color.into()); } pub fn pixeldata_dirty() { display::pixeldata_dirty(); } pub fn get_offset() -> Offset { let offset = display::get_offset(); Offset::new(offset.0, offset.1) } 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, ); } pub fn get_color_table(fg_color: Color, bg_color: Color) -> [Color; 16] { let mut table: [Color; 16] = [Color::from_u16(0); 16]; for (i, item) in table.iter_mut().enumerate() { *item = Color::lerp(bg_color, fg_color, i as f32 / 15_f32); } table } pub struct Glyph { pub width: i32, pub height: i32, pub adv: i32, pub bearing_x: i32, pub bearing_y: i32, data: &'static [u8], } impl Glyph { /// Construct a `Glyph` from a raw pointer. /// /// # Safety /// /// This function is unsafe because the caller has to guarantee that `data` /// is pointing to a memory containing a valid glyph data, that is: /// - contains valid glyph metadata /// - data has appropriate size /// - data must have static lifetime pub unsafe fn load(data: *const u8) -> Self { unsafe { let width = *data.offset(0) as i32; let height = *data.offset(1) as i32; let data_bits = constant::FONT_BPP * width * height; let data_bytes = if data_bits % 8 == 0 { data_bits / 8 } else { (data_bits / 8) + 1 }; Glyph { width, height, adv: *data.offset(2) as i32, bearing_x: *data.offset(3) as i32, bearing_y: *data.offset(4) as i32, data: slice::from_raw_parts(data.offset(5), data_bytes as usize), } } } pub fn print(&self, pos: Point, colortable: [Color; 16]) -> i32 { let bearing = Offset::new(self.bearing_x, -self.bearing_y); let size = Offset::new(self.width, self.height); let pos_adj = pos + bearing; let r = Rect::from_top_left_and_size(pos_adj, size); let area = r.translate(get_offset()); let window = area.clamp(constant::screen()); set_window(window); for y in window.y0..window.y1 { for x in window.x0..window.x1 { let p = Point::new(x, y); let r = p - pos_adj; let c = self.get_pixel_data(r); pixeldata(colortable[c as usize]); } } self.adv } pub fn unpack_bpp1(&self, a: i32) -> u8 { let c_data = self.data[(a / 8) as usize]; ((c_data >> (7 - (a % 8))) & 0x01) * 15 } pub fn unpack_bpp2(&self, a: i32) -> u8 { let c_data = self.data[(a / 4) as usize]; ((c_data >> (6 - (a % 4) * 2)) & 0x03) * 5 } pub fn unpack_bpp4(&self, a: i32) -> u8 { let c_data = self.data[(a / 2) as usize]; (c_data >> (4 - (a % 2) * 4)) & 0x0F } pub fn unpack_bpp8(&self, a: i32) -> u8 { let c_data = self.data[a as usize]; c_data >> 4 } pub fn get_pixel_data(&self, p: Offset) -> u8 { let a = p.x + p.y * self.width; match constant::FONT_BPP { 1 => self.unpack_bpp1(a), 2 => self.unpack_bpp2(a), 4 => self.unpack_bpp4(a), 8 => self.unpack_bpp8(a), _ => 0, } } } #[derive(Copy, Clone, PartialEq, Eq)] pub struct Font(i32); impl Font { pub const fn new(id: i32) -> Self { Self(id) } pub fn text_width(self, text: &str) -> i32 { display::text_width(text, self.0) } pub fn char_width(self, ch: char) -> i32 { display::char_width(ch, self.0) } pub fn text_height(self) -> i32 { display::text_height(self.0) } pub fn line_height(self) -> i32 { constant::LINE_SPACE + self.text_height() } pub fn get_glyph(self, char_byte: u8) -> Option { let gl_data = display::get_char_glyph(char_byte, self.0); if gl_data.is_null() { return None; } unsafe { Some(Glyph::load(gl_data)) } } pub fn display_text(self, text: &str, baseline: Point, fg_color: Color, bg_color: Color) { let colortable = get_color_table(fg_color, bg_color); let mut adv_total = 0; for c in text.bytes() { let g = self.get_glyph(c); if let Some(gly) = g { let adv = gly.print(baseline + Offset::new(adv_total, 0), colortable); adv_total += adv; } } } } #[derive(Copy, Clone, PartialEq, Eq)] pub struct Color(u16); impl Color { pub const fn from_u16(val: u16) -> Self { Self(val) } pub const fn rgb(r: u8, g: u8, b: u8) -> Self { let r = (r as u16 & 0xF8) << 8; let g = (g as u16 & 0xFC) << 3; let b = (b as u16 & 0xF8) >> 3; Self(r | g | b) } pub const fn r(self) -> u8 { (self.0 >> 8) as u8 & 0xF8 } pub const fn g(self) -> u8 { (self.0 >> 3) as u8 & 0xFC } pub const fn b(self) -> u8 { (self.0 << 3) as u8 & 0xF8 } pub fn to_u16(self) -> u16 { self.0 } pub fn negate(self) -> Self { Self(!self.0) } } impl Lerp for Color { fn lerp(a: Self, b: Self, t: f32) -> Self { let r = u8::lerp(a.r(), b.r(), t); let g = u8::lerp(a.g(), b.g(), t); let b = u8::lerp(a.b(), b.b(), t); Color::rgb(r, g, b) } } impl From for Color { fn from(val: u16) -> Self { Self(val) } } impl From for u16 { fn from(val: Color) -> Self { val.to_u16() } }