diff --git a/libs/bao1x-hal/src/sh1107.rs b/libs/bao1x-hal/src/sh1107.rs index ddfb0d4ef..c31472913 100644 --- a/libs/bao1x-hal/src/sh1107.rs +++ b/libs/bao1x-hal/src/sh1107.rs @@ -512,6 +512,56 @@ impl<'a> Oled128x128<'a> { crate::board::assert_periph_reset(self.iox, false); self.powerdown = false; } + + #[cfg(feature = "std")] + pub fn render_bitmap(&mut self, bitmap: ux_api::service::api::BaosecBitmap) { + const W: isize = WIDTH as _; + const H: isize = LINES as _; + + // Clamp bounding box to valid bitmap dimensions. + // If br > 128, truncate. If tl < 0, clamp to 0. + let src_x0 = bitmap.bounding_box.tl.x.max(0); + let src_y0 = bitmap.bounding_box.tl.y.max(0); + let src_x1 = bitmap.bounding_box.br.x.min(W); + let src_y1 = bitmap.bounding_box.br.y.min(H); + + for src_y in src_y0..src_y1 { + // Map this bitmap row to its destination row in the framebuffer. + let dst_y = src_y + bitmap.top_left.y; + + // Skip rows that land outside the framebuffer. + if dst_y < 0 || dst_y >= H { + continue; + } + + for src_x in src_x0..src_x1 { + // Map this bitmap column to its destination column. + let dst_x = src_x + bitmap.top_left.x; + + // Skip columns that land outside the framebuffer. + if dst_x < 0 || dst_x >= W { + continue; + } + + // --- read source bit --- + let src_linear = (src_x + src_y * W) as usize; + let src_word = src_linear >> 5; // / 32 + let src_shift = src_linear & 0x1F; // % 32 + let on = (bitmap.bits[src_word] >> src_shift) & 1; + + // --- write destination bit --- + let dst_linear = (dst_x + dst_y * W) as usize; + let dst_word = dst_linear >> 5; + let dst_shift = dst_linear & 0x1F; + + if on != 0 { + self.buffer[dst_word] |= 1 << dst_shift; + } else { + self.buffer[dst_word] &= !(1 << dst_shift); + } + } + } + } } impl<'a> FrameBuffer for Oled128x128<'a> { diff --git a/libs/ux-api/src/service/api.rs b/libs/ux-api/src/service/api.rs index 958bbd370..24ba52b47 100644 --- a/libs/ux-api/src/service/api.rs +++ b/libs/ux-api/src/service/api.rs @@ -107,6 +107,9 @@ pub enum GfxOpcode { PowerDown, #[cfg(feature = "board-baosec")] PowerUp, + #[cfg(feature = "board-baosec")] + /// This call is specific and highly optimized to the display on Baosec + BaosecBitmap, /// Gutter for invalid calls InvalidCall, @@ -154,3 +157,14 @@ pub struct KeyboardRegistration { pub server_name: String, pub listener_op_id: usize, } + +#[cfg(feature = "board-baosec")] +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)] +pub struct BaosecBitmap { + // exactly enough for a 128x128 black and white display = 2048 bytes + pub bits: [u32; 512], + // top left corner of the bitmap + pub top_left: crate::minigfx::Point, + // bounding box of the bitmap - if we want only a portion of the bitmap to be drawn + pub bounding_box: crate::minigfx::Rectangle, +} diff --git a/libs/ux-api/src/service/gfx.rs b/libs/ux-api/src/service/gfx.rs index 47141c237..409392f7a 100644 --- a/libs/ux-api/src/service/gfx.rs +++ b/libs/ux-api/src/service/gfx.rs @@ -769,6 +769,29 @@ impl Gfx { } } + #[cfg(feature = "board-baosec")] + pub fn bitmap( + &self, + bitmap: &[u32], + top_left: Option, + bounding_box: Option, + ) -> Result<(), xous::Error> { + let mut bmp = BaosecBitmap { + bits: [0u32; 512], + top_left: top_left.unwrap_or(Point::new(0, 0)), + bounding_box: bounding_box.unwrap_or(Rectangle { + tl: Point::new(0, 0), + // br is *exclusive* + br: Point::new(128, 128), + style: DrawStyle { fill_color: None, stroke_color: None, stroke_width: 0 }, + }), + }; + bmp.bits[..bitmap.len().min(512)].copy_from_slice(&bitmap[..bitmap.len().min(512)]); + let mut buf = Buffer::into_buf(bmp).unwrap(); + buf.lend_mut(self.conn, GfxOpcode::BaosecBitmap.to_u32().unwrap())?; + Ok(()) + } + #[cfg(feature = "hosted-baosec")] pub fn acquire_qr(&self) -> Result { let dummy = "otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30".to_string(); diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 21b099c7f..36b85e9cc 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -861,6 +861,12 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { scalar.arg1 = 1; } } + #[cfg(feature = "board-baosec")] + GfxOpcode::BaosecBitmap => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let bitmap = buffer.to_original::().unwrap(); + display.render_bitmap(bitmap); + } GfxOpcode::Quit => break, _ => { // This is perfectly normal because not all opcodes are handled by all platforms.