diff --git a/services/bao-video/Cargo.toml b/services/bao-video/Cargo.toml index b9001f681..8f3648d4a 100644 --- a/services/bao-video/Cargo.toml +++ b/services/bao-video/Cargo.toml @@ -56,7 +56,6 @@ hosted-baosec = ["bao1x-emu", "ux-api/hosted-baosec", "no-gam"] autotest = [] bao1x = ["utralib/bao1x", "blitstr2/bao1x"] -decongest-udma = [] b64-export = ["base64"] default = [ # "b64-export", diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 9efd24735..c3762e31f 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -11,6 +11,7 @@ mod gfx; #[cfg(feature = "board-baosec")] mod panic; mod qr; +mod qr_warmup; #[cfg(feature = "gfx-testing")] mod testing; use std::{ @@ -197,6 +198,10 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { panic::panic_handler_thread(is_panic.clone(), panic_display); } + // respond to keyboard presses - needed to abort QR code mode + let kbd = bao1x_api::keyboard::Keyboard::new(&xns).unwrap(); + kbd.register_listener(SERVER_NAME_GFX, GfxOpcode::KeyPress.to_u32().unwrap() as usize); + // ---- camera initialization #[cfg(not(feature = "hosted-baosec"))] { @@ -280,9 +285,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { cam.set_slicing((border, 0), (cols - border, IMAGE_HEIGHT)); log::info!("320x240 resolution setup with 256x240 slicing"); - #[cfg(feature = "decongest-udma")] - log::info!("Decongest udma option enabled."); - #[cfg(not(feature = "hosted-baosec"))] let cid = xous::connect(sid).unwrap(); // self-connection always succeeds @@ -333,6 +335,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { #[cfg(feature = "gfx-testing")] testing::tests(); let mut bw_thresh: u8 = 128; + let mut qr_request: Option = None; loop { if !is_panic.load(Ordering::Relaxed) { xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); @@ -341,9 +344,66 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(GfxOpcode::InvalidCall); log::debug!("{:?}", opcode); match opcode { + GfxOpcode::AcquireQr => { + if qr_request.is_none() { + display.stash(); // save a copy of the UI + // this will defer response until later + qr_request = msg_opt.take(); + + // 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. + let mut img = rqrr::PreparedImage::prepare_from_bitmap( + bao1x_hal::sh1107::COLUMN as _, + bao1x_hal::sh1107::ROW as _, + |x, y| { + let bitnum = x + y * bao1x_hal::sh1107::COLUMN as usize; + // true is `black` + crate::qr_warmup::BITMAP[bitnum / 32] & 1 << (bitnum % 32) != 0 + }, + ); + let grids = img.detect_grids(); + if grids.len() == 1 { + match grids[0].decode() { + Ok((_meta, data)) => { + log::info!("warmed up decoder with {}", data); + } + Err(e) => { + log::error!("Test image failed to decode! {:?}", e) + } + } + } else { + log::error!("test image failed to decode, this shouldn't happen!"); + } + + // now start acquisition + cam.capture_async(); + // turning off preemption makes camera acquisition smoother; the OS will naturally try + // to schedule other tasks between camera frames after each + // CamIrq interrupt + hal.set_preemption(false); + } + // if qr_request is already pending, ignore any new acquisition requests + } + GfxOpcode::KeyPress => { + // any key press will abort QR acquisition by taking the qr_request. If it's already None, + // the keypress is just ignored. + if let Some(mut envelope) = qr_request.take() { + let acquisition = QrAcquisition { content: None, meta: None }; + let mut response = unsafe { + xous_ipc::Buffer::from_memory_message_mut( + envelope.body.memory_message_mut().unwrap(), + ) + }; + response.replace(acquisition).unwrap(); + display.pop(); + hal.set_preemption(true); + } + } GfxOpcode::CamIrq => { - #[cfg(not(feature = "decongest-udma"))] - cam.capture_async(); + if qr_request.is_some() { + cam.capture_async(); + } // copy the camera data to our FB let fb: &[u32] = cam.rx_buf(); @@ -375,7 +435,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { if candidates.len() == 3 { gfx::msg( &mut display, - "Decoding...", + "Searching...", Point::new(0, 0), Mono::White.into(), Mono::Black.into(), @@ -386,25 +446,56 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { }); 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)) => { - log::info!("meta: {:?}", meta); - log::info!("************ {} ***********", content); - decode_success = true; gfx::msg( &mut display, - &format!("{:?}", meta), + "Success!", Point::new(0, 0), Mono::White.into(), Mono::Black.into(), ); - gfx::msg( - &mut display, - &format!("{:?}", content), - Point::new(0, 64), - Mono::White.into(), - Mono::Black.into(), - ); + // this take will cause the QR response to be routed to the sender since + // the Message `Drop`s. It will also cause the sampling of the camera to + // stop on the next frame. + if let Some(mut envelope) = qr_request.take() { + let metadata = format!("{:?}", meta); + let acquisition = + QrAcquisition { content: Some(content), meta: Some(metadata) }; + let mut response = unsafe { + xous_ipc::Buffer::from_memory_message_mut( + envelope.body.memory_message_mut().unwrap(), + ) + }; + response.replace(acquisition).unwrap(); + display.pop(); + hal.set_preemption(true); + } else { + log::info!("meta: {:?}", meta); + log::info!("************ {} ***********", content); + decode_success = true; + gfx::msg( + &mut display, + &format!("{:?}", meta), + Point::new(0, 0), + Mono::White.into(), + Mono::Black.into(), + ); + gfx::msg( + &mut display, + &format!("{:?}", content), + Point::new(0, 64), + Mono::White.into(), + Mono::Black.into(), + ); + } } Err(e) => { log::info!("{:?}", e); @@ -435,48 +526,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { // clear the front buffer display.clear(); - - // re-initiate the capture. This is done at the bottom of the loop because UDMA - // congestion leads to system instability. When this problem is solved, we would - // actually want to re-initiate the capture immediately (or leave it on continuous mode) - // to allow capture to process concurrently with the code. However, there is a bug - // in the SPIM block that prevents proper usage with high bus contention that should - // be fixed in bao1x. - #[cfg(feature = "decongest-udma")] - { - const TIMEOUT_MS: u64 = 100; - let start = tt.elapsed_ms(); - let mut now = tt.elapsed_ms(); - // this is required because if we initiate the capture in the middle - // of a frame, we get an offset result. This should be fixed by DAR-704 - // on bao1x if the pull request is accepted; in which case, we can just rely - // on setting bit 30 of the CFG_GLOBAL register which will cause any - // RX start request to align to the beginning of a frame automatically. - while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High - && ((now - start) < TIMEOUT_MS) - { - now = tt.elapsed_ms(); - } - if now - start >= TIMEOUT_MS { - log::info!("Timeout before capture_async()!"); - } - cam.capture_async(); - } - - // this is no longer the case because we're relying on interrupts to wake us up. - /* - // wait for the transfer to finish - let start = tt.elapsed_ms(); - let mut now = tt.elapsed_ms(); - use bao1x_hal::udma::Udma; - while cam.udma_busy(bao1x_hal::udma::Bank::Rx) && ((now - start) < TIMEOUT_MS) { - now = tt.elapsed_ms(); - // busy-wait to get better time resolution on when the frame ends - } - if now - start >= TIMEOUT_MS { - log::info!("Timeout before rx_buf()!"); - } - */ } GfxOpcode::InvalidCall => { log::error!("Invalid call to bao video server: {:?}", msg); @@ -535,11 +584,15 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { minigfx::handlers::draw_text_view(&mut display, msg); } GfxOpcode::Flush => { - log::trace!("***gfx flush*** redraw##"); - display.redraw(); + if qr_request.is_none() { + log::trace!("***gfx flush*** redraw##"); + display.redraw(); + } } GfxOpcode::Clear => { - display.clear(); + if qr_request.is_none() { + display.clear(); + } } GfxOpcode::Line => { minigfx::handlers::line(&mut display, screen_clip.into(), msg); diff --git a/services/bao-video/src/qr_warmup.rs b/services/bao-video/src/qr_warmup.rs new file mode 100644 index 000000000..4be63e138 --- /dev/null +++ b/services/bao-video/src/qr_warmup.rs @@ -0,0 +1,132 @@ +#![cfg_attr(rustfmt, rustfmt_skip)] +pub const BITMAP: [u32; 512] = [ + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0xffffc000, 0xc0007fff, 0xffff007f, 0x0001ffff, + 0xffffc000, 0xe0007fff, 0xffff007f, 0x0001ffff, + 0xffffc000, 0xe0007fff, 0xffff007f, 0x0001ffff, + 0xffffc000, 0xc0007fff, 0xffff007f, 0x0001ffff, + 0x7777c000, 0xe0007f77, 0xddde005b, 0x0001fddd, + 0x0007c000, 0x01f07800, 0x001f0f80, 0x0001e000, + 0x0007c000, 0x01f07c00, 0x001f0780, 0x0001f000, + 0x0007c000, 0x00f07800, 0x001f0f80, 0x0001e000, + 0x0007c000, 0x01f07c00, 0x001f0780, 0x0001f000, + 0x5507c000, 0x01f07815, 0x541f0f80, 0x0001e055, + 0xff07c000, 0x00f07c3f, 0xfc1f0780, 0x0001f0ff, + 0xff87c000, 0x01f0783f, 0xfc1f0f80, 0x0001e0ff, + 0xff07c000, 0x01f07c3f, 0xfe1f0780, 0x0001f0ff, + 0xff07c000, 0x00f0f83f, 0xfc1f0fc0, 0x0001e0ff, + 0xff878000, 0x01f07c3f, 0xfc1e0780, 0x0001f0ff, + 0xff07c000, 0x01f0783f, 0xfe1f0f80, 0x0001e0ff, + 0xff07c000, 0x00f07c3f, 0xfc1f0780, 0x0001f0ff, + 0xff87c000, 0x01f0781f, 0xfc1f0f80, 0x0001e0ff, + 0xff07c000, 0x01f07c3f, 0xfe1f0780, 0x0001f0ff, + 0xff07c000, 0xe0f0783f, 0xfc1f0fff, 0x0001e0ff, + 0xff87c000, 0xe1f0fc3f, 0xfc1f07ff, 0x0001f0ff, + 0xff07c000, 0xe1f0783f, 0xfe1e0fff, 0x0001e0ff, + 0xff878000, 0xe0f07c3f, 0xfc1f07ff, 0x0001f0ff, + 0xdb07c000, 0xc1f07816, 0xb41f07ff, 0x0001e055, + 0x0007c000, 0xe1f07c00, 0x001f0003, 0x0001f000, + 0x0007c000, 0xe0f07800, 0x001f0003, 0x0001e000, + 0x0007c000, 0xe1f07c00, 0x001f0003, 0x0001f000, + 0x0007c000, 0xc1f0f800, 0x001f0003, 0x0001e000, + 0xaaafc000, 0xe0f07eda, 0x5b5f0503, 0x0001fb55, + 0xffffc000, 0xe1f07fff, 0xffff0f83, 0x0001ffff, + 0xffffc000, 0xe1f07fff, 0xffff0783, 0x0001ffff, + 0xffffc000, 0xe0f07fff, 0xffff0f83, 0x0001ffff, + 0xffffc000, 0xe1f07fff, 0xffff0783, 0x0001ffff, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x04000000, 0x00044440, 0x44200024, 0x00000444, + 0x1f800000, 0xffffffc0, 0xffe0f87f, 0x00001fff, + 0x0f000000, 0xffffffc0, 0xffe0f07f, 0x00000fff, + 0x0f800000, 0xffffffc0, 0xffe0f87f, 0x00001fff, + 0x1f000000, 0xffffffc0, 0xffe0f07f, 0x00000fff, + 0xaa800000, 0xffffd7ea, 0xeb6ada7f, 0x0000ad6f, + 0xf0000000, 0xffff83ff, 0x801f07ff, 0x0001f007, + 0xf0000000, 0xffff87ff, 0xc01f0fff, 0x0001e007, + 0xf0000000, 0xffff83ff, 0xc01f07ff, 0x0001f007, + 0xe0000000, 0xffff87ff, 0xc01f0fff, 0x0001e007, + 0xf07f8000, 0xe21ff43f, 0x001f0783, 0x0001f0f4, + 0xf0ffc000, 0xe00ff83f, 0x001f0f83, 0x0001e0f8, + 0xf0ffc000, 0xe00ffc3f, 0x001f0783, 0x0001f0f8, + 0xf07fc000, 0xe00ff83f, 0x001f0f83, 0x0001e0f8, + 0xf0ff8000, 0xc00ffc3f, 0x001e07c3, 0x0001f078, + 0xfff80000, 0x01f00001, 0xfc00f87c, 0x0001e007, + 0xfff80000, 0x01f00001, 0xfe00f07c, 0x0001f007, + 0xfff80000, 0x00f00001, 0xfc00f87c, 0x0001e007, + 0xfff80000, 0x01f00001, 0xfe00f07c, 0x0001f007, + 0xdff88000, 0x40f00001, 0xf800f87c, 0x0001e007, + 0x0fffc000, 0xe00ffc00, 0x0000ffff, 0x0001f000, + 0x0fffc000, 0xe00ff800, 0x0000ffff, 0x0001e000, + 0x0fffc000, 0xe00ffc00, 0x0000ffff, 0x0001f000, + 0x1fffc000, 0xe00ffc00, 0x0000ffff, 0x0001e000, + 0x052a4000, 0x15555000, 0x01a0ffc9, 0x00015b00, + 0x00000000, 0x1ff00000, 0x03e0ff80, 0x00000f00, + 0x00000000, 0x3ff00000, 0x03e0ff80, 0x00001f00, + 0x00000000, 0x1ff00000, 0x01e0ff80, 0x00000f00, + 0x00000000, 0x1ff00000, 0x03e0ff80, 0x00001f00, + 0xffffc000, 0xfff07fff, 0xffe0ffff, 0x0001e007, + 0xffffc000, 0xfff07fff, 0xffe0ffff, 0x0001f007, + 0xffffc000, 0xfff07fff, 0xffe0ffff, 0x0001e007, + 0xffffc000, 0xfff07fff, 0xffe0ffff, 0x0001f007, + 0xbbbfc000, 0xfff07fbb, 0xf7e2ffff, 0x0001e007, + 0x00078000, 0x1ff07800, 0x01ff0780, 0x00000000, + 0x0007c000, 0x1ff07c00, 0x03ff0f80, 0x00000000, + 0x0007c000, 0x1ff07800, 0x03ff0780, 0x00000000, + 0x0007c000, 0x1ff0fc00, 0x01ff0f80, 0x00000000, + 0x9407c000, 0x5ad07812, 0x17f52581, 0x0000a000, + 0xff87c000, 0xe0007c3f, 0x7fe0f803, 0x0001f000, + 0xff07c000, 0xe000783f, 0x3fe0f003, 0x0001e000, + 0xff878000, 0xe0007c3f, 0x3fe0f803, 0x0001f000, + 0xff07c000, 0xe000783f, 0x7fe0f003, 0x0001e000, + 0xff07c000, 0x00007c3f, 0x001ffffc, 0x00004000, + 0xff87c000, 0x0000783f, 0x001ffffc, 0x00000000, + 0xff07c000, 0x00007c3f, 0x001ffffc, 0x00000000, + 0xff87c000, 0x0000781f, 0x001ffffc, 0x00000000, + 0xff07c000, 0x00007c3f, 0x001ffffc, 0x00000000, + 0xff07c000, 0x1ff0783f, 0x001ff000, 0x0001fff8, + 0xff878000, 0x3ff07c3f, 0x001ff800, 0x0001fff8, + 0xff07c000, 0x1ff0783f, 0x001ff000, 0x0001fff8, + 0xff87c000, 0x1ff07c3f, 0x001ff800, 0x0001fff8, + 0xee07c000, 0x5ff07816, 0x081fb000, 0x0001fff0, + 0x0007c000, 0xfe007c00, 0x7e1f0f83, 0x00000000, + 0x0007c000, 0xfe007800, 0x3c1f0783, 0x00000000, + 0x0007c000, 0xff007c00, 0x3c1f0f83, 0x00000000, + 0x0007c000, 0xfe007800, 0x3e1f0783, 0x00000000, + 0x55af8000, 0xea007eb5, 0xd41f0577, 0x00014056, + 0xffffc000, 0xe0007fff, 0xc01f007f, 0x0001f0ff, + 0xffffc000, 0xe0007fff, 0xc01f007f, 0x0001e0ff, + 0xffffc000, 0xe0007fff, 0x801f007f, 0x0001f0ff, + 0xffffc000, 0xe0007fff, 0xc01f007f, 0x0001e0ff, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + +];