Merge pull request #752 from betrusted-io/qr-refactor

Qr refactor
This commit is contained in:
bunnie 2025-12-07 20:49:45 +03:00 committed by GitHub
commit 28232ff999
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 247 additions and 108 deletions

7
Cargo.lock generated
View file

@ -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"

View file

@ -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<T: FrameBuffer>(display: &mut T, msg: &mut xous::envelope::Envelope) {
@ -400,3 +401,68 @@ pub fn draw_text_view<T: FrameBuffer>(display: &mut T, msg: &mut xous::envelope:
// pack our data back into the buffer to return
buffer.replace(tv).unwrap();
}
pub fn render_qr<T: FrameBuffer>(
display: &mut T,
screen_clip: Option<Rectangle>,
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::<QrRender, _>().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")
}
}

View file

@ -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<String>,
}
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct QrRender {
pub width: usize,
pub top_left: crate::minigfx::Point,
pub modules: Vec<bool>,
}
// this structure is used to register a keyboard listener.
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone)]
pub struct KeyboardRegistration {

View file

@ -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<bool>,
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<QrAcquisition, xous::Error> {
let acquisition = QrAcquisition { content: None, meta: None };

View file

@ -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,54 +99,10 @@ impl Notification {
self.gfx.draw_textview(&mut tv).expect("couldn't post tv");
}
fn draw_qrcode(&self, at_height: isize, modal: &Modal) {
// 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
fn draw_qrcode(&self, at_height: isize, _modal: &Modal) {
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");
.render_qr(&self.qrcode, self.qrwidth, Point { x: 0, y: at_height })
.expect("couldn't render QR");
}
}

View file

@ -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",

View file

@ -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,

View file

@ -128,6 +128,51 @@ 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.
// 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() {
*d = i as u8;
}
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() {
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();
}

View file

@ -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"))]
@ -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,26 +272,44 @@ 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 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") };
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);
// 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));
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(feature = "hosted-baosec")]
// unused dummy object
let mut cam = unsafe { Gc2145::new().expect("couldn't allocate camera") };
tt.sleep_ms(100).ok();
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");
#[cfg(not(feature = "hosted-baosec"))]
let cid = xous::connect(sid).unwrap(); // self-connection always succeeds
@ -324,17 +345,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::<Sender>::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 +375,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 +463,16 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
GfxOpcode::CamIrq => {
if qr_request.is_some() {
cam.capture_async();
} else {
#[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
@ -450,7 +496,6 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
frames += 1;
let mut candidates = Vec::<Point>::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 +504,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 +544,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 +583,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();
@ -702,6 +737,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.

View file

@ -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),