implement card rendering with new card type and menu
This commit was merged in pull request #28.
This commit is contained in:
+71
-55
@@ -1,8 +1,3 @@
|
||||
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;
|
||||
use alloc::{format, string::String};
|
||||
use embedded_graphics::mono_font::MonoTextStyle;
|
||||
@@ -14,81 +9,102 @@ use embedded_graphics::text::Text;
|
||||
use embedded_graphics::{Drawable, geometry::Size, primitives::Rectangle};
|
||||
use embedded_graphics_core::draw_target::DrawTarget;
|
||||
|
||||
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);
|
||||
use core::future;
|
||||
|
||||
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);
|
||||
use crate::{
|
||||
card::model::{Card, Palette},
|
||||
display::primary_lcd::PrimaryLcdDisplay,
|
||||
navigation::navigation::{Action, Navigable},
|
||||
views::view::View,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CardView {
|
||||
pub card: Card,
|
||||
}
|
||||
|
||||
impl Navigable for CardView {
|
||||
fn display(
|
||||
&self,
|
||||
display: &mut PrimaryLcdDisplay<'static>,
|
||||
) -> impl core::future::Future<Output = ()> + Send {
|
||||
display.clear(Rgb565::BLACK).unwrap();
|
||||
|
||||
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 bg_color = self.card.cardtype.clone().into();
|
||||
|
||||
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
|
||||
),
|
||||
&format!("Name: {}", self.card.name),
|
||||
Point::new(20, 240),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!("Type: {}", self.card.cardtype),
|
||||
Point::new(20, 250),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!("Event: {}", decode_known_event(nfc_payload.event_encoding)),
|
||||
&format!("Event: {}", self.card.event),
|
||||
Point::new(20, 260),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!(
|
||||
"UUID: {}",
|
||||
nfc_payload
|
||||
.card_uuid
|
||||
.iter()
|
||||
.map(|b| format!("{:02X}", b))
|
||||
.collect::<String>()
|
||||
),
|
||||
&format!("UUID: {}", self.card.uuid),
|
||||
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();
|
||||
Text::new(
|
||||
&format!("Trait1: {}", self.card.trait1),
|
||||
Point::new(20, 280),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!("Trait2: {}", self.card.trait2),
|
||||
Point::new(20, 290),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!("Trait3: {}", self.card.trait3),
|
||||
Point::new(20, 300),
|
||||
style,
|
||||
)
|
||||
.draw(display)
|
||||
.unwrap();
|
||||
Text::new(
|
||||
&format!("Secret: {}", self.card.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()
|
||||
);
|
||||
let palette: Palette = self.card.cardtype.clone().into();
|
||||
render_sprite_onto_ili9341(display, self.card.sprite.data.as_slice(), &palette);
|
||||
|
||||
core::future::ready(())
|
||||
}
|
||||
|
||||
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = View> + Send {
|
||||
let new_menu = match input {
|
||||
Action::Ok => View::Card(self.clone()),
|
||||
_ => View::Card(self.clone()),
|
||||
};
|
||||
|
||||
future::ready(new_menu)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
use alloc::vec;
|
||||
use core::future;
|
||||
|
||||
use crate::card::model::Card;
|
||||
use crate::card::{decoder::split_nfc_hex, mock::MOCK_PAYLOAD, mock::MOCK_PAYLOAD2};
|
||||
use crate::display::primary_lcd::PrimaryLcdDisplay;
|
||||
|
||||
use crate::views::card_view::CardView;
|
||||
use crate::views::journal_view::JournalView;
|
||||
use crate::views::{menu_item, settings_menu::SettingsMenu, view::View};
|
||||
|
||||
@@ -33,6 +36,9 @@ impl Navigable for MainMenu {
|
||||
}
|
||||
|
||||
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = View> + Send {
|
||||
let card_mock1 = Card::try_from(split_nfc_hex(&MOCK_PAYLOAD).unwrap()).unwrap();
|
||||
let card_mock2 = Card::try_from(split_nfc_hex(&MOCK_PAYLOAD2).unwrap()).unwrap();
|
||||
|
||||
let new_menu = match input {
|
||||
Action::Up => {
|
||||
let new_selected = if (self.selected - 1) < 0 {
|
||||
@@ -51,8 +57,9 @@ impl Navigable for MainMenu {
|
||||
})
|
||||
}
|
||||
Action::Ok => match self.selected {
|
||||
0 => View::Card(CardView { card: card_mock1 }),
|
||||
1 => View::Journal(JournalView {
|
||||
cards: vec![],
|
||||
cards: vec![card_mock1, card_mock2],
|
||||
selected_card: 0,
|
||||
}),
|
||||
2 => View::Settings(SettingsMenu { selected: 0 }),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::views::card_view::CardView;
|
||||
use crate::views::{
|
||||
journal_view::JournalView, main_menu::MainMenu, scan_menu::ScanMenu,
|
||||
settings_menu::SettingsMenu,
|
||||
@@ -11,6 +12,7 @@ pub enum View {
|
||||
Scan(ScanMenu),
|
||||
Settings(SettingsMenu),
|
||||
Journal(JournalView),
|
||||
Card(CardView),
|
||||
}
|
||||
|
||||
impl Navigable for View {
|
||||
@@ -24,6 +26,7 @@ impl Navigable for View {
|
||||
View::Scan(scan_menu) => scan_menu.display(display).await,
|
||||
View::Settings(settings_menu) => settings_menu.display(display).await,
|
||||
View::Journal(journal_view) => journal_view.display(display).await,
|
||||
View::Card(card_view) => card_view.display(display).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +41,7 @@ impl Navigable for View {
|
||||
View::Scan(scan_menu) => scan_menu.handle_input(input).await,
|
||||
View::Settings(settings_menu) => settings_menu.handle_input(input).await,
|
||||
View::Journal(journal_view) => journal_view.handle_input(input).await,
|
||||
View::Card(card_view) => card_view.handle_input(input).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user