Nfc improvements #40
@@ -0,0 +1,3 @@
|
||||
mod nfc;
|
||||
|
||||
pub use nfc::nfc_scanner;
|
||||
@@ -0,0 +1,45 @@
|
||||
use embedded_hal_bus::i2c::AtomicDevice;
|
||||
use esp_hal::{Blocking, i2c::master::I2c, time::Instant};
|
||||
|
||||
use crate::{
|
||||
card::{decoder::split_nfc_hex, model::Card},
|
||||
drivers::nfc_pn532::NfcPn532Driver,
|
||||
navigation::navigation::CARD_DATA,
|
||||
};
|
||||
|
||||
#[embassy_executor::task]
|
||||
#[allow(
|
||||
clippy::large_stack_frames,
|
||||
reason = "ignoring this for now because it still works"
|
||||
)]
|
||||
pub async fn nfc_scanner(
|
||||
mut nfc_driver: NfcPn532Driver<AtomicDevice<'static, I2c<'static, Blocking>>>,
|
||||
) {
|
||||
loop {
|
||||
while CARD_DATA.lock().await.is_none() {
|
||||
let start = Instant::now();
|
||||
match nfc_driver.read_card_data().await {
|
||||
Ok(card_data) => {
|
||||
let Some(split_data) = split_nfc_hex(&card_data) else {
|
||||
log::error!("Failed to split NFC card data");
|
||||
continue;
|
||||
};
|
||||
CARD_DATA
|
||||
.lock()
|
||||
.await
|
||||
.replace(Card::try_from(split_data).unwrap());
|
||||
let elapsed_ms = start.elapsed().as_millis();
|
||||
log::info!(
|
||||
"NFC read duration: {} ms, card data length: {}",
|
||||
elapsed_ms,
|
||||
card_data.len()
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("NFC read failed: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
embassy_time::Timer::after(embassy_time::Duration::from_millis(1000)).await;
|
||||
}
|
||||
}
|
||||
+3
-44
@@ -7,19 +7,17 @@
|
||||
)]
|
||||
#![deny(clippy::large_stack_frames)]
|
||||
|
||||
use creaturedex::card::decoder::split_nfc_hex;
|
||||
use creaturedex::card::model::Card;
|
||||
use creaturedex::background_tasks;
|
||||
use creaturedex::drivers::i2c_bus::I2cBusPinConfiguration;
|
||||
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
|
||||
use creaturedex::drivers::secondary_oled::SecondaryDisplay;
|
||||
use creaturedex::drivers::spi_bus;
|
||||
use creaturedex::drivers::tertiary_lcd::{TertiaryDisplay, init_tertiary_lcd, write_wrapped};
|
||||
use creaturedex::navigation::inputs::Inputs;
|
||||
use creaturedex::navigation::navigation::{self, CARD_DATA};
|
||||
use creaturedex::navigation::navigation::{self};
|
||||
use creaturedex::navigation::outputs::Outputs;
|
||||
use creaturedex::network::wifi::setup_wifi;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::Instant;
|
||||
use embedded_graphics::pixelcolor::BinaryColor;
|
||||
use embedded_graphics::{
|
||||
mono_font::{MonoTextStyle, ascii::FONT_6X10},
|
||||
@@ -27,10 +25,8 @@ use embedded_graphics::{
|
||||
text::Text,
|
||||
};
|
||||
use embedded_hal_bus::i2c::AtomicDevice;
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::gpio::{Input, InputConfig, Pull};
|
||||
use esp_hal::i2c::master::I2c;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use log::error;
|
||||
|
||||
@@ -135,44 +131,7 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
log::info!("Setup complete, entering main loop");
|
||||
spawner.spawn(navigation::run(inputs, outputs).expect("run task failed"));
|
||||
spawner.spawn(nfc_driver_task(nfc_driver).expect("nfc driver task failed"));
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
#[allow(
|
||||
clippy::large_stack_frames,
|
||||
reason = "ignoring this for now because it still works"
|
||||
)]
|
||||
async fn nfc_driver_task(
|
||||
mut nfc_driver: NfcPn532Driver<AtomicDevice<'static, I2c<'static, Blocking>>>,
|
||||
) {
|
||||
loop {
|
||||
while CARD_DATA.lock().await.is_none() {
|
||||
let start = Instant::now();
|
||||
match nfc_driver.read_card_data().await {
|
||||
Ok(card_data) => {
|
||||
let Some(split_data) = split_nfc_hex(&card_data) else {
|
||||
log::error!("Failed to split NFC card data");
|
||||
continue;
|
||||
};
|
||||
CARD_DATA
|
||||
.lock()
|
||||
.await
|
||||
.replace(Card::try_from(split_data).unwrap());
|
||||
let elapsed_ms = start.elapsed().as_millis();
|
||||
log::info!(
|
||||
"NFC read duration: {} ms, card data length: {}",
|
||||
elapsed_ms,
|
||||
card_data.len()
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("NFC read failed: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
embassy_time::Timer::after(embassy_time::Duration::from_millis(1000)).await;
|
||||
}
|
||||
spawner.spawn(background_tasks::nfc_scanner(nfc_driver).expect("nfc scanner task failed"));
|
||||
}
|
||||
|
||||
fn display_shit(oled: &mut SecondaryDisplay<'static>, text: &str) {
|
||||
|
||||
@@ -243,6 +243,7 @@ where
|
||||
Err(e) => {
|
||||
error!("Parsing page failed: {e}");
|
||||
if e.status_code.is_some_and(|e| e.protocol_error) {
|
||||
// After protocol error, all subsequent reads will fail, so we can stop trying to read more pages.
|
||||
return Err("Failed to parse NFC page");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
pub mod background_tasks;
|
||||
pub mod card;
|
||||
pub mod display;
|
||||
pub mod drivers;
|
||||
|
||||
Reference in New Issue
Block a user