mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
fix orientation flipping during QR scan mode
turns out you don't just flip everything all the time! when you flip the board upside down, the camera also flips. so then you end up with a flipped image which is super-confusing. however, the overlay text needs to flip. so this adds the cases to handle that.
This commit is contained in:
parent
0d2c7233d6
commit
2ec739e54e
2 changed files with 97 additions and 53 deletions
|
|
@ -26,65 +26,71 @@ fn char_offset(c: char) -> isize {
|
||||||
fallback
|
fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn msg<'a>(fb: &mut dyn FrameBuffer, text: &'a str, ll_pos: Point, fg: ColorNative, bg: ColorNative) {
|
pub fn msg<'a>(
|
||||||
let mut ll_pos = ll_pos.clone();
|
fb: &mut dyn FrameBuffer,
|
||||||
// this routine is adapted from the embedded graphics crate https://docs.rs/embedded-graphics/0.7.1/embedded_graphics/
|
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 char_per_row = FONT_IMAGE_WIDTH / CHAR_WIDTH;
|
||||||
let mut idx = 0;
|
let mut idx: isize = 0;
|
||||||
|
|
||||||
for current_char in text.chars() {
|
for current_char in text.chars() {
|
||||||
let mut char_walk_x = 0;
|
let cur_byte = current_char as u8;
|
||||||
let mut char_walk_y = 0;
|
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 {
|
loop {
|
||||||
// Char _code_ offset from first char, most often a space
|
if is_visible {
|
||||||
// E.g. first char = ' ' (32), target char = '!' (33), offset = 33 - 32 = 1
|
let bitmap_bit_idx = base_bitmap_idx + char_walk_x + char_walk_y * FONT_IMAGE_WIDTH;
|
||||||
let char_offset = char_offset(current_char);
|
let bitmap_byte = (bitmap_bit_idx / 8) as usize;
|
||||||
let row = char_offset / char_per_row;
|
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 px = char_screen_x + char_walk_x;
|
||||||
let char_x = (char_offset - (row * char_per_row)) * CHAR_WIDTH;
|
let py = ll_pos.y + char_walk_y;
|
||||||
let char_y = row * CHAR_HEIGHT;
|
|
||||||
|
|
||||||
// Bit index
|
// For upside-down: mirror the screen position.
|
||||||
// = X pixel offset for char
|
let draw_point = if upside_down {
|
||||||
// + Character row offset (row 0 = 0, row 1 = (192 * 8) = 1536)
|
Point::new(screen_size.x - 1 - px, screen_size.y - 1 - py)
|
||||||
// + X offset for the pixel block that comprises this char
|
} else {
|
||||||
// + Y offset for pixel block
|
Point::new(px, py)
|
||||||
let bitmap_bit_index =
|
};
|
||||||
char_x + (FONT_IMAGE_WIDTH * char_y) + char_walk_x + (char_walk_y * FONT_IMAGE_WIDTH);
|
|
||||||
|
|
||||||
let bitmap_byte = bitmap_bit_index / 8;
|
fb.put_pixel(draw_point, color);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char_walk_x += 1;
|
char_walk_x += 1;
|
||||||
|
|
||||||
if char_walk_x >= CHAR_WIDTH {
|
if char_walk_x >= CHAR_WIDTH {
|
||||||
char_walk_x = 0;
|
char_walk_x = 0;
|
||||||
char_walk_y += 1;
|
char_walk_y += 1;
|
||||||
|
|
||||||
// Done with this char, move on to the next one
|
|
||||||
if char_walk_y >= CHAR_HEIGHT {
|
if char_walk_y >= CHAR_HEIGHT {
|
||||||
if current_char as u8 == 0xd {
|
if is_cr {
|
||||||
// '\n'
|
|
||||||
ll_pos.y += CHAR_HEIGHT;
|
ll_pos.y += CHAR_HEIGHT;
|
||||||
} else if current_char as u8 == 0xa {
|
} else if is_lf {
|
||||||
// '\r'
|
|
||||||
ll_pos.x = LEFT_MARGIN;
|
ll_pos.x = LEFT_MARGIN;
|
||||||
|
idx = 0;
|
||||||
} else {
|
} else {
|
||||||
idx += 1;
|
idx += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,14 +70,11 @@ use xous_ipc::Buffer;
|
||||||
pub const IMAGE_WIDTH: usize = 256;
|
pub const IMAGE_WIDTH: usize = 256;
|
||||||
pub const IMAGE_HEIGHT: usize = 240;
|
pub const IMAGE_HEIGHT: usize = 240;
|
||||||
|
|
||||||
// Next steps for performance improvement:
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||||
//
|
enum DisplayOrientation {
|
||||||
// Improve qr::mapping -> point_from_hv_lines such that we're not just deriving the HV
|
Normal,
|
||||||
// lines from the the edges of the finder regions, we're also using the very edge of
|
UpsideDown,
|
||||||
// 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.
|
|
||||||
|
|
||||||
#[cfg(feature = "b64-export")]
|
#[cfg(feature = "b64-export")]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
@ -412,6 +409,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
|
|
||||||
// ---- main loop variables
|
// ---- main loop variables
|
||||||
let screen_clip = Rectangle::new(Point::new(0, 0), display.screen_size());
|
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 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.
|
// 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 bw_thresh: u8 = 128;
|
||||||
let mut qr_request: Option<xous::MessageEnvelope> = None;
|
let mut qr_request: Option<xous::MessageEnvelope> = None;
|
||||||
let mut kbd_listeners: Vec<(CID, usize)> = Vec::new();
|
let mut kbd_listeners: Vec<(CID, usize)> = Vec::new();
|
||||||
|
let mut orientation = DisplayOrientation::Normal;
|
||||||
loop {
|
loop {
|
||||||
if !is_panic.load(Ordering::Relaxed) {
|
if !is_panic.load(Ordering::Relaxed) {
|
||||||
xous::reply_and_receive_next(sid, &mut msg_opt).unwrap();
|
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!");
|
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
|
// turning off preemption makes camera acquisition smoother; the OS will naturally try
|
||||||
// to schedule other tasks between camera frames after each
|
// to schedule other tasks between camera frames after each
|
||||||
// CamIrq interrupt
|
// CamIrq interrupt
|
||||||
hal.set_preemption(false);
|
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
|
// 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();
|
response.replace(acquisition).unwrap();
|
||||||
|
if orientation == DisplayOrientation::UpsideDown {
|
||||||
|
display
|
||||||
|
.flip_vertical(true)
|
||||||
|
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display))
|
||||||
|
}
|
||||||
display
|
display
|
||||||
.pop()
|
.pop()
|
||||||
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display));
|
.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),
|
Point::new(0, 0),
|
||||||
Mono::White.into(),
|
Mono::White.into(),
|
||||||
Mono::Black.into(),
|
Mono::Black.into(),
|
||||||
|
orientation == DisplayOrientation::UpsideDown,
|
||||||
|
screen_size,
|
||||||
);
|
);
|
||||||
display
|
display
|
||||||
.draw()
|
.draw()
|
||||||
|
|
@ -632,6 +644,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
Point::new(0, 0),
|
Point::new(0, 0),
|
||||||
Mono::White.into(),
|
Mono::White.into(),
|
||||||
Mono::Black.into(),
|
Mono::Black.into(),
|
||||||
|
orientation == DisplayOrientation::UpsideDown,
|
||||||
|
screen_size,
|
||||||
);
|
);
|
||||||
display.draw().unwrap_or_else(|_| {
|
display.draw().unwrap_or_else(|_| {
|
||||||
display_timeout_handler(&udma_global, &mut display)
|
display_timeout_handler(&udma_global, &mut display)
|
||||||
|
|
@ -663,6 +677,11 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
response.replace(acquisition).unwrap();
|
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"))]
|
#[cfg(not(feature = "hosted-baosec"))]
|
||||||
hal.set_preemption(true);
|
hal.set_preemption(true);
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -675,6 +694,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
Point::new(0, 0),
|
Point::new(0, 0),
|
||||||
Mono::White.into(),
|
Mono::White.into(),
|
||||||
Mono::Black.into(),
|
Mono::Black.into(),
|
||||||
|
orientation == DisplayOrientation::UpsideDown,
|
||||||
|
screen_size,
|
||||||
);
|
);
|
||||||
gfx::msg(
|
gfx::msg(
|
||||||
&mut display,
|
&mut display,
|
||||||
|
|
@ -682,6 +703,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
|
||||||
Point::new(0, 64),
|
Point::new(0, 64),
|
||||||
Mono::White.into(),
|
Mono::White.into(),
|
||||||
Mono::Black.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),
|
Point::new(0, 0),
|
||||||
Mono::White.into(),
|
Mono::White.into(),
|
||||||
Mono::Black.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),
|
Point::new(0, 0),
|
||||||
Mono::White.into(),
|
Mono::White.into(),
|
||||||
Mono::Black.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")]
|
#[cfg(feature = "board-baosec")]
|
||||||
GfxOpcode::FlipScreen => {
|
GfxOpcode::FlipScreen => {
|
||||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||||
display
|
log::info!("gfx flip");
|
||||||
.flip_vertical(scalar.arg1 != 0)
|
if scalar.arg1 != 0 {
|
||||||
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display))
|
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,
|
GfxOpcode::Quit => break,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue