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.
This commit is contained in:
bunnie 2025-12-07 18:58:33 +08:00
parent 4b3369a767
commit ebb7e8dfae
5 changed files with 104 additions and 11 deletions

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,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");
*/
}
}

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