Moved nfc and wifi into dedicated file
This commit is contained in:
+62
-1
@@ -1 +1,62 @@
|
||||
// Display sprite module
|
||||
use embedded_graphics::{geometry::Size, pixelcolor::Rgb565, primitives::Rectangle, prelude::Point};
|
||||
use embedded_graphics_core::draw_target::DrawTarget;
|
||||
use crate::display::palette::Rgb;
|
||||
|
||||
pub fn render_sprite_onto_ili9341<D>(ili9341: &mut D, sprite: &[u8], palette: &[Rgb; 16])
|
||||
where
|
||||
D: DrawTarget<Color = Rgb565>,
|
||||
{
|
||||
let pixel_count = sprite.len() * 2;
|
||||
if pixel_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
// OPTIMIZATION 1: Pre-compute the 16-color palette to Rgb565 once.
|
||||
let mut palette565 = [Rgb565::new(0, 0, 0); 16];
|
||||
for (i, Rgb(r, g, b)) in palette.iter().enumerate() {
|
||||
palette565[i] = Rgb565::new(*r >> 3, *g >> 2, *b >> 3);
|
||||
}
|
||||
|
||||
let mut src_width = 1usize;
|
||||
while src_width * src_width < pixel_count {
|
||||
src_width += 1;
|
||||
}
|
||||
|
||||
let target_width = 240usize;
|
||||
let scale = core::cmp::max(1, target_width / src_width);
|
||||
|
||||
for y in 0..src_width {
|
||||
for x in 0..src_width {
|
||||
let pixel_index = y * src_width + x;
|
||||
if pixel_index >= pixel_count {
|
||||
break;
|
||||
}
|
||||
|
||||
// Bitwise operations are slightly faster than division/modulo
|
||||
let byte_index = pixel_index >> 1;
|
||||
let pixel_byte = sprite[byte_index];
|
||||
let color_index = if pixel_index.is_multiple_of(2) {
|
||||
(pixel_byte >> 4) & 0x0F
|
||||
} else {
|
||||
pixel_byte & 0x0F
|
||||
};
|
||||
|
||||
let color = palette565[color_index as usize];
|
||||
|
||||
let start_x = (x * scale) as i32;
|
||||
let start_y = (y * scale) as i32;
|
||||
let end_x = core::cmp::min(start_x + scale as i32, target_width as i32);
|
||||
let end_y = core::cmp::min(start_y + scale as i32, target_width as i32);
|
||||
|
||||
let width = u32::try_from(end_x - start_x).unwrap();
|
||||
let height = u32::try_from(end_y - start_y).unwrap();
|
||||
|
||||
let area = Rectangle::new(
|
||||
Point::new(start_x.try_into().unwrap(), start_y.try_into().unwrap()),
|
||||
Size::new(width, height),
|
||||
);
|
||||
|
||||
let _ = ili9341.fill_solid(&area, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,98 @@
|
||||
// Card view module
|
||||
use alloc::{format, string::String};
|
||||
use embedded_graphics::mono_font::MonoTextStyle;
|
||||
use embedded_graphics::mono_font::ascii::FONT_6X10;
|
||||
use embedded_graphics::pixelcolor::Rgb565;
|
||||
use embedded_graphics::prelude::Point;
|
||||
use embedded_graphics::prelude::RgbColor;
|
||||
use embedded_graphics::text::Text;
|
||||
use embedded_graphics::{Drawable, geometry::Size, primitives::Rectangle};
|
||||
use embedded_graphics_core::draw_target::DrawTarget;
|
||||
use crate::card::decoder::{decode_known_event, decode_packed_card_text, decode_secret, split_nfc_hex};
|
||||
use crate::card::model::NfcPayload;
|
||||
use crate::display::palette::{PALETTES, TYPE_STYLES};
|
||||
use crate::display::sprite::render_sprite_onto_ili9341;
|
||||
|
||||
pub fn render_nfc_payload<D>(display: &mut D, payload: &[u8; 858])
|
||||
where
|
||||
D: DrawTarget<Color = Rgb565>,
|
||||
D::Error: core::fmt::Debug,
|
||||
{
|
||||
let nfc_payload: Option<NfcPayload<'_>> = split_nfc_hex(payload);
|
||||
|
||||
if let Some(nfc_payload) = nfc_payload {
|
||||
let (name, trait1, trait2, trait3) = decode_packed_card_text(nfc_payload.packed_card_text);
|
||||
let secret = decode_secret(nfc_payload.secret);
|
||||
|
||||
let background = Rectangle::new(
|
||||
Point::new(0, 0),
|
||||
Size::new(240, 320),
|
||||
);
|
||||
let bg_color: Rgb565 = TYPE_STYLES[(nfc_payload.card_type - 1) as usize]
|
||||
.primary
|
||||
.into_rgb565();
|
||||
|
||||
let _ = display.fill_solid(&background, bg_color);
|
||||
|
||||
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
||||
Text::new(&format!("Name: {}", name), Point::new(20, 240), style)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!(
|
||||
"Type: {}",
|
||||
TYPE_STYLES[(nfc_payload.card_type - 1) as usize].name
|
||||
),
|
||||
Point::new(20, 250),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!(
|
||||
"Event: {}",
|
||||
decode_known_event(nfc_payload.event_encoding).unwrap_or("Unknown")
|
||||
),
|
||||
Point::new(20, 260),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!(
|
||||
"UUID: {}",
|
||||
nfc_payload
|
||||
.card_uuid
|
||||
.iter()
|
||||
.map(|b| format!("{:02X}", b))
|
||||
.collect::<String>()
|
||||
),
|
||||
Point::new(20, 270),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(&format!("Trait1: {}", trait1), Point::new(20, 280), style)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(&format!("Trait2: {}", trait2), Point::new(20, 290), style)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(&format!("Trait3: {}", trait3), Point::new(20, 300), style)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(&format!("Secret: {}", secret), Point::new(20, 310), style)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
|
||||
render_sprite_onto_ili9341(
|
||||
display,
|
||||
nfc_payload.sprite,
|
||||
&PALETTES[(nfc_payload.card_type - 1) as usize],
|
||||
);
|
||||
} else {
|
||||
log::error!(
|
||||
"NFC payload too short to decode packed text: {} bytes",
|
||||
payload.len()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user