diff --git a/services/bao-video/src/gfx.rs b/services/bao-video/src/gfx.rs index 89c732cfb..ef845ac05 100644 --- a/services/bao-video/src/gfx.rs +++ b/services/bao-video/src/gfx.rs @@ -26,65 +26,71 @@ fn char_offset(c: char) -> isize { fallback } -pub fn msg<'a>(fb: &mut dyn FrameBuffer, text: &'a str, ll_pos: Point, fg: ColorNative, bg: ColorNative) { - let mut ll_pos = ll_pos.clone(); - // this routine is adapted from the embedded graphics crate https://docs.rs/embedded-graphics/0.7.1/embedded_graphics/ +pub fn msg<'a>( + fb: &mut dyn FrameBuffer, + text: &'a str, + ll_pos: Point, + fg: ColorNative, + bg: ColorNative, + upside_down: bool, + screen_size: Point, // e.g. Point::new(SCREEN_WIDTH, SCREEN_HEIGHT); ignored when !upside_down +) { + let mut ll_pos = ll_pos; // no need for .clone() on Copy types let char_per_row = FONT_IMAGE_WIDTH / CHAR_WIDTH; - let mut idx = 0; + let mut idx: isize = 0; + for current_char in text.chars() { - let mut char_walk_x = 0; - let mut char_walk_y = 0; + let cur_byte = current_char as u8; + let is_cr = cur_byte == 0x0d; + let is_lf = cur_byte == 0x0a; + let is_visible = !is_cr && !is_lf; + // -- Per-character constants (hoisted out of the pixel loop) ---------- + let char_offset = char_offset(current_char); + let row = char_offset / char_per_row; + let char_x = (char_offset - (row * char_per_row)) * CHAR_WIDTH; + let char_y = row * CHAR_HEIGHT; + // Base bit index in the font bitmap for the top-left of this glyph + let base_bitmap_idx = char_x + FONT_IMAGE_WIDTH * char_y; + // Screen X of the left edge of this character + let char_screen_x = ll_pos.x + CHAR_WIDTH * idx; + + // -- Pixel walk ------------------------------------------------------- + let mut char_walk_x: isize = 0; + let mut char_walk_y: isize = 0; loop { - // Char _code_ offset from first char, most often a space - // E.g. first char = ' ' (32), target char = '!' (33), offset = 33 - 32 = 1 - let char_offset = char_offset(current_char); - let row = char_offset / char_per_row; + if is_visible { + let bitmap_bit_idx = base_bitmap_idx + char_walk_x + char_walk_y * FONT_IMAGE_WIDTH; + let bitmap_byte = (bitmap_bit_idx / 8) as usize; + let bitmap_bit = bitmap_bit_idx as u8 & 7 ^ 7; + let color = if FONT_IMAGE[bitmap_byte] & (1 << bitmap_bit) != 0 { fg } else { bg }; - // Top left corner of character, in pixels - let char_x = (char_offset - (row * char_per_row)) * CHAR_WIDTH; - let char_y = row * CHAR_HEIGHT; + let px = char_screen_x + char_walk_x; + let py = ll_pos.y + char_walk_y; - // Bit index - // = X pixel offset for char - // + Character row offset (row 0 = 0, row 1 = (192 * 8) = 1536) - // + X offset for the pixel block that comprises this char - // + Y offset for pixel block - let bitmap_bit_index = - char_x + (FONT_IMAGE_WIDTH * char_y) + char_walk_x + (char_walk_y * FONT_IMAGE_WIDTH); + // For upside-down: mirror the screen position. + let draw_point = if upside_down { + Point::new(screen_size.x - 1 - px, screen_size.y - 1 - py) + } else { + Point::new(px, py) + }; - let bitmap_byte = bitmap_bit_index / 8; - let bitmap_bit = 7 - (bitmap_bit_index % 8); - - let color = if FONT_IMAGE[bitmap_byte as usize] & (1 << bitmap_bit) != 0 { fg } else { bg }; - - let x = ll_pos.x + CHAR_WIDTH * idx + char_walk_x; - let y = ll_pos.y + char_walk_y; - - // draw color at x, y - if (current_char as u8 != 0xd) && (current_char as u8 != 0xa) { - // don't draw CRLF specials - fb.put_pixel(Point::new(x, y), color); + fb.put_pixel(draw_point, color); } char_walk_x += 1; - if char_walk_x >= CHAR_WIDTH { char_walk_x = 0; char_walk_y += 1; - - // Done with this char, move on to the next one if char_walk_y >= CHAR_HEIGHT { - if current_char as u8 == 0xd { - // '\n' + if is_cr { ll_pos.y += CHAR_HEIGHT; - } else if current_char as u8 == 0xa { - // '\r' + } else if is_lf { ll_pos.x = LEFT_MARGIN; + idx = 0; } else { idx += 1; } - break; } } diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 948e36995..722e92ba2 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -70,14 +70,11 @@ use xous_ipc::Buffer; pub const IMAGE_WIDTH: usize = 256; pub const IMAGE_HEIGHT: usize = 240; -// Next steps for performance improvement: -// -// Improve qr::mapping -> point_from_hv_lines such that we're not just deriving the HV -// lines from the the edges of the finder regions, we're also using the very edge of -// the whole QR code itself to guide the line. This will improve the intersection point -// so that we can accurately hit the "fourth corner". At the moment it's sort of a -// luck of the draw if the interpolation hits exactly right, or if we're roughly a module -// off from ideal, which causes the data around that point to be interpreted incorrectly. +#[derive(PartialEq, Eq, Clone, Copy)] +enum DisplayOrientation { + Normal, + UpsideDown, +} #[cfg(feature = "b64-export")] #[allow(dead_code)] @@ -412,6 +409,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { // ---- main loop variables let screen_clip = Rectangle::new(Point::new(0, 0), display.screen_size()); + let screen_size = display.screen_size(); // make a copy so the borrow checker doesn't complain // this will kick the hardware into the QR code scanning routine automatically. Eventually // this needs to be turned into a call that can invoke and abort the QR code scanning. @@ -433,6 +431,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { let mut bw_thresh: u8 = 128; let mut qr_request: Option = None; let mut kbd_listeners: Vec<(CID, usize)> = Vec::new(); + let mut orientation = DisplayOrientation::Normal; loop { if !is_panic.load(Ordering::Relaxed) { xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); @@ -505,12 +504,18 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { 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); + // fix orientation if it's upside down + if orientation == DisplayOrientation::UpsideDown { + display + .flip_vertical(false) + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)) + } + // now start acquisition + cam.capture_async(); } // if qr_request is already pending, ignore any new acquisition requests } @@ -526,6 +531,11 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { ) }; response.replace(acquisition).unwrap(); + if orientation == DisplayOrientation::UpsideDown { + display + .flip_vertical(true) + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)) + } display .pop() .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); @@ -609,6 +619,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Point::new(0, 0), Mono::White.into(), Mono::Black.into(), + orientation == DisplayOrientation::UpsideDown, + screen_size, ); display .draw() @@ -632,6 +644,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Point::new(0, 0), Mono::White.into(), Mono::Black.into(), + orientation == DisplayOrientation::UpsideDown, + screen_size, ); display.draw().unwrap_or_else(|_| { display_timeout_handler(&udma_global, &mut display) @@ -663,6 +677,11 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { ) }; response.replace(acquisition).unwrap(); + if orientation == DisplayOrientation::UpsideDown { + display.flip_vertical(true).unwrap_or_else(|_| { + display_timeout_handler(&udma_global, &mut display) + }) + } #[cfg(not(feature = "hosted-baosec"))] hal.set_preemption(true); continue; @@ -675,6 +694,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Point::new(0, 0), Mono::White.into(), Mono::Black.into(), + orientation == DisplayOrientation::UpsideDown, + screen_size, ); gfx::msg( &mut display, @@ -682,6 +703,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Point::new(0, 64), Mono::White.into(), Mono::Black.into(), + orientation == DisplayOrientation::UpsideDown, + screen_size, ); } } @@ -693,6 +716,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Point::new(0, 0), Mono::White.into(), Mono::Black.into(), + orientation == DisplayOrientation::UpsideDown, + screen_size, ); } } @@ -704,6 +729,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { Point::new(0, 0), Mono::White.into(), Mono::Black.into(), + orientation == DisplayOrientation::UpsideDown, + screen_size, ); } @@ -910,9 +937,20 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! { #[cfg(feature = "board-baosec")] GfxOpcode::FlipScreen => { if let Some(scalar) = msg.body.scalar_message_mut() { - display - .flip_vertical(scalar.arg1 != 0) - .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)) + log::info!("gfx flip"); + if scalar.arg1 != 0 { + orientation = DisplayOrientation::UpsideDown; + } else { + orientation = DisplayOrientation::Normal; + } + if qr_request.is_none() { + display + .flip_vertical(scalar.arg1 != 0) + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); + display + .redraw() + .unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display)); + } } } GfxOpcode::Quit => break,