From ebb7e8dfae62b052c6848a607ec834aedc6b8983 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 18:58:33 +0800 Subject: [PATCH 1/9] refactor QR code notification implementation Previous implementation sent the QR code as draw commands. This is inefficient as it would require hundreds of draw commands to be sent per QR code rendered. This implementation codes the QR code as a bool and sends it in a single memory message, and implements a QR code renderer on the receiver side. The QR code rendering is also cleaned up so that the QR code is always centered and takes the full screen, to give the best conditions for someone scanning the code from another device. --- libs/ux-api/src/minigfx/handlers.rs | 66 +++++++++++++++++++++++++ libs/ux-api/src/service/api.rs | 8 +++ libs/ux-api/src/service/gfx.rs | 14 ++++++ libs/ux-api/src/widgets/notification.rs | 25 ++++++---- services/modals/src/main.rs | 2 +- 5 files changed, 104 insertions(+), 11 deletions(-) diff --git a/libs/ux-api/src/minigfx/handlers.rs b/libs/ux-api/src/minigfx/handlers.rs index 6e22798c0..c8c3d6845 100644 --- a/libs/ux-api/src/minigfx/handlers.rs +++ b/libs/ux-api/src/minigfx/handlers.rs @@ -7,6 +7,7 @@ use xous_ipc::Buffer; use super::FrameBuffer; use super::*; +use crate::service::api::QrRender; use crate::wordwrap::*; pub fn draw_clip_object(display: &mut T, msg: &mut xous::envelope::Envelope) { @@ -400,3 +401,68 @@ pub fn draw_text_view(display: &mut T, msg: &mut xous::envelope: // pack our data back into the buffer to return buffer.replace(tv).unwrap(); } + +pub fn render_qr( + display: &mut T, + screen_clip: Option, + msg: &mut xous::envelope::Envelope, +) { + if msg.body.has_memory() { + let screensize = Rectangle::new( + Point::new(0, 0), + Point::new(crate::platform::WIDTH as _, crate::platform::HEIGHT as _), + ); + let buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; + let qr_render = buffer.to_original::().unwrap(); + log::debug!( + "qr_render width {}: {:?}", + qr_render.width, + &qr_render.modules[..32.min(qr_render.modules.len())] + ); + const QUIET_MODULES: isize = 1; + let window_width = screen_clip.unwrap_or(screensize).width() as isize; + let total_width = qr_render.width as isize + QUIET_MODULES * 2; + let module_width = (window_width / total_width).max(1) as isize; + let left_edge = if module_width * total_width < window_width { + (window_width - (module_width * total_width)) as isize / 2 + } else { + 0 + }; + + const USE_TOP_LEFT: bool = false; + let tl = if USE_TOP_LEFT { qr_render.top_left } else { Point::new(0, 0) }; + + // draw a white background over the rendering area + let r = Rectangle::new_with_style( + Point::from((left_edge, tl.y)), + screen_clip.unwrap_or(screensize).br, + DrawStyle::new(PixelColor::Light, PixelColor::Light, 1), + ); + op::rectangle(display, r, screen_clip.into(), false); + + // now raster in the modules + let mut line_top_left = + Point::new(left_edge + module_width * QUIET_MODULES, tl.y + module_width * QUIET_MODULES); + let dark = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); + for line in qr_render.modules.chunks(qr_render.width) { + for (i, &module) in line.iter().enumerate() { + if module { + // dark color: render a square + let m = Rectangle::new_coords_with_style( + line_top_left.x + i as isize * module_width, + line_top_left.y, + line_top_left.x + (i + 1) as isize * module_width, + line_top_left.y + module_width, + dark, + ); + op::rectangle(display, m, screen_clip.into(), false); + } else { + // light color: don't render, background is already light + } + } + line_top_left += Point::new(0, module_width); + } + } else { + panic!("Incorrect message type for RenderQr") + } +} diff --git a/libs/ux-api/src/service/api.rs b/libs/ux-api/src/service/api.rs index 209fe6478..ff15d7007 100644 --- a/libs/ux-api/src/service/api.rs +++ b/libs/ux-api/src/service/api.rs @@ -98,6 +98,7 @@ pub enum GfxOpcode { UnclippedObjectList, /// V2 API for getting filtered keyboard events FilteredKeyboardListener, + RenderQr, #[cfg(feature = "board-baosec")] AcquireQr, @@ -137,6 +138,13 @@ pub struct QrAcquisition { pub meta: Option, } +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct QrRender { + pub width: usize, + pub top_left: crate::minigfx::Point, + pub modules: Vec, +} + // this structure is used to register a keyboard listener. #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)] pub struct KeyboardRegistration { diff --git a/libs/ux-api/src/service/gfx.rs b/libs/ux-api/src/service/gfx.rs index 00c9b7d26..f078565b7 100644 --- a/libs/ux-api/src/service/gfx.rs +++ b/libs/ux-api/src/service/gfx.rs @@ -729,6 +729,20 @@ impl Gfx { buf.lend(self.conn, GfxOpcode::UnclippedObjectList.to_u32().unwrap()).map(|_| ()) } + /// On small screens, `top_left` is ignored and the code takes the whole screen, always + pub fn render_qr( + &self, + qr_stream: &Vec, + qr_width: usize, + top_left: Point, + ) -> Result<(), xous::Error> { + let qr_render = QrRender { width: qr_width, top_left, modules: qr_stream.to_owned() }; + let mut buf = Buffer::into_buf(qr_render).unwrap(); + buf.lend_mut(self.conn, GfxOpcode::RenderQr.to_u32().unwrap())?; + // do nothing with the response - function just blocks until the QR code is done rendering + Ok(()) + } + #[cfg(feature = "board-baosec")] pub fn acquire_qr(&self) -> Result { let acquisition = QrAcquisition { content: None, meta: None }; diff --git a/libs/ux-api/src/widgets/notification.rs b/libs/ux-api/src/widgets/notification.rs index 0452fe2d9..7f65f594f 100644 --- a/libs/ux-api/src/widgets/notification.rs +++ b/libs/ux-api/src/widgets/notification.rs @@ -4,7 +4,7 @@ use locales::t; use qrcode::{Color, QrCode}; use super::*; -use crate::minigfx::op::{HEIGHT, WIDTH}; +use crate::minigfx::op::HEIGHT; use crate::minigfx::*; use crate::service::api::*; use crate::service::gfx::Gfx; @@ -52,14 +52,14 @@ impl Notification { Err(_e) => QrCode::new(t!("notification.qrcode.error", locales::LANG)).unwrap(), }; self.qrwidth = qrcode.width(); - log::info!("qrcode {}x{} : {} bytes ", self.qrwidth, self.qrwidth, setting.len()); - self.qrcode = Vec::new(); - for color in qrcode.to_colors().iter() { - match color { - Color::Dark => self.qrcode.push(true), - Color::Light => self.qrcode.push(false), - } - } + self.qrcode = qrcode.into_colors().into_iter().map(|c| c != Color::Light).collect(); + log::info!( + "qrcode {}x{} : {} bytes, {} modules", + self.qrwidth, + self.qrwidth, + setting.len(), + self.qrcode.len() + ); } None => { self.qrcode = Vec::new(); @@ -99,7 +99,11 @@ impl Notification { self.gfx.draw_textview(&mut tv).expect("couldn't post tv"); } - fn draw_qrcode(&self, at_height: isize, modal: &Modal) { + fn draw_qrcode(&self, at_height: isize, _modal: &Modal) { + self.gfx + .render_qr(&self.qrcode, self.qrwidth, Point { x: 0, y: at_height }) + .expect("couldn't render QR"); + /* // calculate pixel size of each module in the qrcode let qrcode_modules: isize = self.qrwidth.try_into().unwrap(); let modules: isize = qrcode_modules + 2 * QUIET_MODULES; @@ -147,6 +151,7 @@ impl Notification { module.translate(step); } self.gfx.draw_object_list(obj_list).expect("couldn't draw qrcode module"); + */ } } diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index 3b6f099cb..645361eda 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -520,7 +520,7 @@ fn wrapped_main() -> ! { renderer_modal.modify( Some(ActionType::Notification(notification)), Some(text), - false, + text.len() == 0, None, true, Some(DEFAULT_STYLE), From 4e0b67878ae54d120f64f0981242043daa509887 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 19:00:29 +0800 Subject: [PATCH 2/9] remove old code --- libs/ux-api/src/widgets/notification.rs | 49 ------------------------- 1 file changed, 49 deletions(-) diff --git a/libs/ux-api/src/widgets/notification.rs b/libs/ux-api/src/widgets/notification.rs index 7f65f594f..c196bc5f2 100644 --- a/libs/ux-api/src/widgets/notification.rs +++ b/libs/ux-api/src/widgets/notification.rs @@ -103,55 +103,6 @@ impl Notification { self.gfx .render_qr(&self.qrcode, self.qrwidth, Point { x: 0, y: at_height }) .expect("couldn't render QR"); - /* - // calculate pixel size of each module in the qrcode - let qrcode_modules: isize = self.qrwidth.try_into().unwrap(); - let modules: isize = qrcode_modules + 2 * QUIET_MODULES; - let canvas_width = modal.canvas_width - 2 * modal.margin; - let mod_size_px: isize = canvas_width / modules; - let qrcode_width_px = qrcode_modules * mod_size_px; - let quiet_px: isize = (canvas_width - qrcode_width_px) / 2; - - // Iterate thru qrcode and stamp each square module like a typewriter - let black = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); - let top = at_height + quiet_px; - - // draw a white background over the whole screen - self.gfx - .draw_rectangle(Rectangle::new_with_style( - Point::new(0, at_height), - Point::new(WIDTH, HEIGHT), - DrawStyle::new(PixelColor::Light, PixelColor::Light, 1), - )) - .unwrap(); - - let left = modal.margin + quiet_px; - let right = left + qrcode_modules * mod_size_px; - let mut module = - Rectangle::new_with_style(Point::new(0, 0), Point::new(mod_size_px - 1, mod_size_px - 1), black); - let step = Point::new(mod_size_px, 0); - let cr_lf = Point::new(-qrcode_modules * mod_size_px, mod_size_px); - let mut j: isize; - let mut obj_list = ObjectList::new(); - module.translate(Point::new(right, top)); - for (i, stamp) in self.qrcode.iter().enumerate() { - j = i.try_into().unwrap(); - if j % qrcode_modules == 0 { - module.translate(cr_lf); - } - if *stamp { - if obj_list.push(ClipObjectType::Rect(module.clone())).is_err() { - // the capacity of the list has exceeded the capacity of the buffer, send it and make a - // new one - self.gfx.draw_object_list(obj_list).expect("couldn't draw qrcode module"); - obj_list = ObjectList::new(); - obj_list.push(ClipObjectType::Rect(module.clone())).unwrap(); - }; - } - module.translate(step); - } - self.gfx.draw_object_list(obj_list).expect("couldn't draw qrcode module"); - */ } } From 6ecaa36d981c426c0a5d20f37a7578d16e2b8893 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 19:00:44 +0800 Subject: [PATCH 3/9] cleanup warning --- services/bao-console/src/cmds/aes_cmd.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/services/bao-console/src/cmds/aes_cmd.rs b/services/bao-console/src/cmds/aes_cmd.rs index 84a1377c9..c4f3c5951 100644 --- a/services/bao-console/src/cmds/aes_cmd.rs +++ b/services/bao-console/src/cmds/aes_cmd.rs @@ -127,6 +127,7 @@ pub(crate) enum BenchOp { } #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +#[allow(dead_code)] pub(crate) enum BenchResult { AesHwDone, AesSwDone, From c813aa1b92c6a492554eb7e9ee703ae64a0b698f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 19:01:00 +0800 Subject: [PATCH 4/9] add hook on bao-video to handle the qr_render case --- services/bao-video/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 46188e2db..9f16e3607 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -702,6 +702,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { } // no failure if it's not } + GfxOpcode::RenderQr => { + minigfx::handlers::render_qr(&mut display, screen_clip.into(), msg); + } GfxOpcode::Quit => break, _ => { // This is perfectly normal because not all opcodes are handled by all platforms. From c3a0a50c9ed0bbb97b2da6b66d13d433b5ac2534 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 19:01:14 +0800 Subject: [PATCH 5/9] add test routine for QR code generation/scan a 40-byte message can be encoded in a version 3 code with medium ECC. This is pretty optimal because it allows 4 pixels per module in the QR code, which is a nice size for viewing. base45 is added as a dependency to the project. Base45 is weird but apparently it's the most efficient way to code binary data into a QR code. 40 bytes (320 bits) is enough to exchange a 256-bit key plus 64 bits of sideband data. I think this is going to be enough for basic device-to-device handshaking. --- Cargo.lock | 7 ++++++ services/bao-console/Cargo.toml | 4 ++++ services/bao-console/src/cmds/test.rs | 32 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 65e418870..f94d128b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,6 +360,7 @@ dependencies = [ "bao1x-emu", "bao1x-hal", "bao1x-hal-service", + "base45", "bytemuck", "chrono", "log", @@ -612,6 +613,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23ce669cd6c8588f79e15cf450314f9638f967fc5770ff1c7c1deb0925ea7cfa" +[[package]] +name = "base45" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3103f6d6af63726e86b026ac66769fc578ea38aba33f409e5855a4def97a564" + [[package]] name = "base64" version = "0.5.2" diff --git a/services/bao-console/Cargo.toml b/services/bao-console/Cargo.toml index 3ea4729c1..75a793921 100644 --- a/services/bao-console/Cargo.toml +++ b/services/bao-console/Cargo.toml @@ -37,6 +37,9 @@ aes = { path = "../aes", default-features = false, optional = true, features = [ # time testing chrono = "0.4.33" +# QR code encoding +base45 = "3.1.0" + # hid bringup xous-usb-hid = { git = "https://github.com/betrusted-io/xous-usb-hid.git", branch = "main", optional = true } @@ -50,6 +53,7 @@ board-baosec = [ "usb-bao1x/board-baosec", "pddb/board-baosec", "xous-swapper", + "ux-api/board-baosec", ] hosted-baosec = [ "modals/hosted-baosec", diff --git a/services/bao-console/src/cmds/test.rs b/services/bao-console/src/cmds/test.rs index d36574836..67131458d 100644 --- a/services/bao-console/src/cmds/test.rs +++ b/services/bao-console/src/cmds/test.rs @@ -128,6 +128,38 @@ impl<'a> ShellCmdApi<'a> for Test { "keepon" => { todo!("Fix this to use DCDC2 for keepon (as per baosec v2)"); } + "qrshow" => { + let modals = modals::Modals::new(&_env.xns).unwrap(); + let mut test_data = [0u8; 40]; + for (i, d) in test_data.iter_mut().enumerate() { + *d = i as u8; + } + let encoded = base45::encode(&test_data); + modals.show_notification("", Some(&encoded)).ok(); + } + "qrget" => { + let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap(); + match gfx.acquire_qr() { + Ok(qr_data) => { + if let Some(meta) = qr_data.meta { + log::info!("QR code metadata: {}", meta); + } + if let Some(coded) = qr_data.content { + match base45::decode(&coded) { + Ok(data) => { + log::info!("Recovered: {:x?}", data); + } + Err(e) => { + log::info!("Base45 decode err: {:?}", e); + } + } + } + } + Err(e) => { + log::info!("QR error: {:?}", e); + } + } + } _ => { write!(ret, "{}", helpstring).unwrap(); } From 9ade5a4a0d6eee1315ee5f6174a6b337487f040d Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 19:08:06 +0800 Subject: [PATCH 6/9] add notes about coding capacity: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 40 bytes gives 320 bits which fits nicely into a version 3 code, which allows 4 pixels per module rendering. if we have to move to 3 pixels per module, the next code up that optimally uses the full screen is version 6. This would give 102 bytes of transfer in a single scan. The equation for capacity is: `binary_bytes = floor((alphanumeric_capacity / 3) × 2)` where `alphameric_capacity` is the capacity of the QR code version per spec lookup table. --- services/bao-console/src/cmds/test.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/services/bao-console/src/cmds/test.rs b/services/bao-console/src/cmds/test.rs index 67131458d..bd4128add 100644 --- a/services/bao-console/src/cmds/test.rs +++ b/services/bao-console/src/cmds/test.rs @@ -129,6 +129,17 @@ impl<'a> ShellCmdApi<'a> for Test { todo!("Fix this to use DCDC2 for keepon (as per baosec v2)"); } "qrshow" => { + // note that 40 bytes gives 320 bits which fits nicely into a version 3 code, + // which allows 4 pixels per module rendering. + // if we have to move to 3 pixels per module, the next code up that optimally + // uses the full screen is version 6. This would give 102 bytes of transfer + // in a single scan. + // + // The equation for capacity is: + // `binary_bytes = floor((alphanumeric_capacity / 3) × 2)` + // + // where `alphameric_capacity` is the capacity of the QR code version + // per spec lookup table. let modals = modals::Modals::new(&_env.xns).unwrap(); let mut test_data = [0u8; 40]; for (i, d) in test_data.iter_mut().enumerate() { From c3757248a891be790845a152147627d15b455b12 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Dec 2025 20:52:46 +0800 Subject: [PATCH 7/9] fix hosted mode --- services/bao-console/src/cmds/test.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/bao-console/src/cmds/test.rs b/services/bao-console/src/cmds/test.rs index bd4128add..9f87091c8 100644 --- a/services/bao-console/src/cmds/test.rs +++ b/services/bao-console/src/cmds/test.rs @@ -128,6 +128,7 @@ impl<'a> ShellCmdApi<'a> for Test { "keepon" => { todo!("Fix this to use DCDC2 for keepon (as per baosec v2)"); } + #[cfg(feature = "board-baosec")] "qrshow" => { // note that 40 bytes gives 320 bits which fits nicely into a version 3 code, // which allows 4 pixels per module rendering. @@ -148,6 +149,7 @@ impl<'a> ShellCmdApi<'a> for Test { let encoded = base45::encode(&test_data); modals.show_notification("", Some(&encoded)).ok(); } + #[cfg(feature = "board-baosec")] "qrget" => { let gfx = ux_api::service::gfx::Gfx::new(&_env.xns).unwrap(); match gfx.acquire_qr() { From 772c8189081ce04a90fdb881d17a7e84eb9b1682 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Dec 2025 01:09:12 +0800 Subject: [PATCH 8/9] reduce power from camera --- services/bao-video/src/main.rs | 125 ++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 48 deletions(-) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 9f16e3607..06e93cd99 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -206,17 +206,30 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { #[cfg(not(feature = "hosted-baosec"))] kbd.register_listener(SERVER_NAME_GFX, GfxOpcode::KeyPress.to_u32().unwrap() as usize); + #[cfg(not(feature = "hosted-baosec"))] + let mut timer = { + let timer_range = xous::map_memory( + xous::MemoryAddress::new(utra::pwm::HW_PWM_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map PWM range"); + utralib::CSR::new(timer_range.as_ptr() as usize as *mut u32) + }; + + udma_global.udma_clock_config(PeriphId::Cam, true); // ---- camera initialization #[cfg(not(feature = "hosted-baosec"))] - { + let (mut cam, cam_pdwn) = { // wait for other inits to finish so we can do this roughly atomically tt.sleep_ms(1000).ok(); // setup camera power match bao1x_hal::axp2101::Axp2101::new(&mut i2c) { Ok(mut pmic) => { - pmic.set_ldo(&mut i2c, Some(2.85), bao1x_hal::axp2101::WhichLdo::Bldo2).unwrap(); pmic.set_dcdc(&mut i2c, Some((1.8, false)), bao1x_hal::axp2101::WhichDcDc::Dcdc5).unwrap(); + pmic.set_ldo(&mut i2c, Some(2.85), bao1x_hal::axp2101::WhichLdo::Bldo2).unwrap(); } Err(e) => { log::error!("Couldn't setup regulators for camera, camera will be non-functional: {:?}", e); @@ -245,22 +258,12 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Some(IoxEnable::Disable), Some(IoxDriveStrength::Drive8mA), ); - let timer_range = xous::map_memory( - xous::MemoryAddress::new(utra::pwm::HW_PWM_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map PWM range"); - let mut timer = utralib::CSR::new(timer_range.as_ptr() as usize as *mut u32); timer.wo(utra::pwm::REG_CH_EN, 1); timer.rmwf(utra::pwm::REG_TIM0_CFG_R_TIMER0_SAW, 1); timer.rmwf(utra::pwm::REG_TIM0_CH0_TH_R_TIMER0_CH0_TH, 0); timer.rmwf(utra::pwm::REG_TIM0_CH0_TH_R_TIMER0_CH0_MODE, 3); - let pwm = timer_range.as_mut_ptr() as *mut u32; - unsafe { pwm.add(2).write_volatile(0) }; // for some reason the register extraction didn't get this register... - timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1); - log::info!("PWM running on PA0"); + unsafe { timer.base().add(2).write_volatile(0) }; // for some reason the register extraction didn't get this register... + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 0); /* // register debug for i in 0..12 { println!("0x{:2x}: 0x{:08x}", i, unsafe { pwm.add(i).read_volatile() }) @@ -269,25 +272,40 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { */ // setup camera pins - let (cam_pdwn_bnk, cam_pdwn_pin) = bao1x_hal::board::setup_camera_pins(&iox); - // disable camera powerdown - iox.set_gpio_pin_value(cam_pdwn_bnk, cam_pdwn_pin, IoxValue::Low); - } - udma_global.udma_clock_config(PeriphId::Cam, true); - // this is safe because we turned on the clocks before calling it - let mut cam = unsafe { Gc2145::new().expect("couldn't allocate camera") }; + let cam_pdwn = bao1x_hal::board::setup_camera_pins(&iox); + // this is safe because we turned on the clocks before calling it + let mut cam = unsafe { Gc2145::new().expect("couldn't allocate camera") }; - tt.sleep_ms(100).ok(); + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1); + tt.sleep_ms(2).ok(); // wait for camera to clock-up + iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low); - let (pid, mid) = cam.read_id(&mut i2c); - log::info!("Camera pid {:x}, mid {:x}", pid, mid); - cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240); - tt.sleep_ms(1).ok(); + // power up the camera + // starts MCLK + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1); + tt.sleep_ms(2).ok(); // wait for camera to clock-up + // bring camera out of powerdown + iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low); + tt.sleep_ms(3).ok(); // wait for camera to power-up + let (pid, mid) = cam.read_id(&mut i2c); + log::info!("Camera pid {:x}, mid {:x}", pid, mid); + cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240); + tt.sleep_ms(1).ok(); - let (cols, _rows) = cam.resolution(); - let border = (cols - IMAGE_WIDTH) / 2; - cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT)); - log::info!("320x240 resolution setup with 256x240 slicing"); + let (cols, _rows) = cam.resolution(); + let border = (cols - IMAGE_WIDTH) / 2; + cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT)); + tt.sleep_ms(2).ok(); + + // power down the camera, now that all the internal registers have been set up + // assert PWWDN + iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::High); + // stop MCLK + tt.sleep_ms(2).ok(); + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 0); + + (cam, cam_pdwn) + }; #[cfg(not(feature = "hosted-baosec"))] let cid = xous::connect(sid).unwrap(); // self-connection always succeeds @@ -324,17 +342,15 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { #[cfg(feature = "autotest")] { log::info!("initiating auto test"); - cam.capture_async(); - // cam_irq serves as a preemption timer source, every time it fires a different - // task can run after cam_irq is handled. - hal.set_preemption(false); + let acquisition = QrAcquisition { content: None, meta: None }; + let mut buf = Buffer::into_buf(acquisition).unwrap(); + buf.lend_mut(cid, GfxOpcode::AcquireQr.to_u32().unwrap()).ok(); } #[cfg(feature = "no-gam")] let modals = modals::Modals::new(&xns).unwrap(); let mut modal_queue = VecDeque::::new(); let mut frames = 0; let mut frame = [0u8; IMAGE_WIDTH * IMAGE_HEIGHT]; - let mut decode_success; let mut msg_opt = None; #[cfg(feature = "gfx-testing")] testing::tests(); @@ -356,6 +372,23 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { // this will defer response until later qr_request = msg_opt.take(); + // power up the camera + // starts MCLK + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 1); + tt.sleep_ms(2).ok(); // wait for camera to clock-up + // bring camera out of powerdown + iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::Low); + tt.sleep_ms(3).ok(); // wait for camera to power-up + let (pid, mid) = cam.read_id(&mut i2c); + log::info!("Camera pid {:x}, mid {:x}", pid, mid); + cam.init(&mut i2c, bao1x_api::camera::Resolution::Res320x240); + tt.sleep_ms(1).ok(); + + let (cols, _rows) = cam.resolution(); + let border = (cols - IMAGE_WIDTH) / 2; + cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT)); + log::info!("320x240 resolution setup with 256x240 slicing"); + // decode dummy data - what this does is load the swapped out QR decoding logic, thus // improving the latency of the decoder on the "first hit". The sole purpose of this // is to improve the user experience during scanning. @@ -427,6 +460,13 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { GfxOpcode::CamIrq => { if qr_request.is_some() { cam.capture_async(); + } else { + // power down the camera, now that the request is done + // assert PWWDN + iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::High); + // stop MCLK + tt.sleep_ms(2).ok(); + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 0); } // copy the camera data to our FB @@ -450,7 +490,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { frames += 1; let mut candidates = Vec::::new(); - decode_success = false; log::debug!("------------- SEARCH {} -----------", frames); let _finder_width = qr::find_finders(&mut candidates, &frame, bw_thresh, IMAGE_WIDTH) as isize; @@ -459,24 +498,18 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { if candidates.len() == 3 { gfx::msg( &mut display, - "Searching...", + "Decoding...", Point::new(0, 0), Mono::White.into(), Mono::Black.into(), ); + display.draw(); let mut img = rqrr::PreparedImage::prepare_from_greyscale(IMAGE_WIDTH, IMAGE_HEIGHT, |x, y| { frame[y * IMAGE_WIDTH + x] }); let grids = img.detect_grids(); if grids.len() == 1 { - gfx::msg( - &mut display, - "Decoding...", - Point::new(0, 0), - Mono::White.into(), - Mono::Black.into(), - ); match grids[0].decode() { Ok((meta, content)) => { gfx::msg( @@ -505,7 +538,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { } else { log::info!("meta: {:?}", meta); log::info!("************ {} ***********", content); - decode_success = true; gfx::msg( &mut display, &format!("{:?}", meta), @@ -545,9 +577,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { } display.draw(); - if decode_success { - tt.sleep_ms(1500).ok(); - } // clear the front buffer display.clear(); From c9d513599ce03b087e6cf2c84be8c3e871c11113 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Dec 2025 01:41:44 +0800 Subject: [PATCH 9/9] fix hosted mode --- services/bao-video/src/main.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 06e93cd99..edaf425bf 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -28,7 +28,6 @@ use bao1x_api::*; use bao1x_emu::{ camera::Gc2145, display::{MainThreadToken, Mono, Oled128x128, claim_main_thread}, - i2c::I2c, udma::UdmaGlobal, }; // breadcrumb to future self: @@ -173,6 +172,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { // ---- basic hardware setup let iox = IoxHal::new(); let udma_global = UdmaGlobal::new(); + #[cfg(not(feature = "hosted-baosec"))] let mut i2c = I2c::new(); #[allow(unused_variables)] #[cfg(not(feature = "hosted-baosec"))] @@ -306,6 +306,9 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { (cam, cam_pdwn) }; + #[cfg(feature = "hosted-baosec")] + // unused dummy object + let mut cam = unsafe { Gc2145::new().expect("couldn't allocate camera") }; #[cfg(not(feature = "hosted-baosec"))] let cid = xous::connect(sid).unwrap(); // self-connection always succeeds @@ -461,12 +464,15 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { if qr_request.is_some() { cam.capture_async(); } else { - // power down the camera, now that the request is done - // assert PWWDN - iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::High); - // stop MCLK - tt.sleep_ms(2).ok(); - timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 0); + #[cfg(not(feature = "hosted-baosec"))] + { + // power down the camera, now that the request is done + // assert PWWDN + iox.set_gpio_pin_value(cam_pdwn.0, cam_pdwn.1, IoxValue::High); + // stop MCLK + tt.sleep_ms(2).ok(); + timer.rmwf(utra::pwm::REG_TIM0_CMD_R_TIMER0_START, 0); + } } // copy the camera data to our FB