card storage: make more robust, handle cards that are already seen, implement in nfc read

This commit is contained in:
2026-08-26 20:47:17 +00:00
parent 2cd18c4ce1
commit 1f1bdda35b
2 changed files with 138 additions and 53 deletions
+19 -12
View File
@@ -16,7 +16,7 @@ use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
use creaturedex::navigation::inputs::Inputs;
use creaturedex::navigation::navigation::{self, CARD_DATA};
use creaturedex::network::wifi::setup_wifi;
use creaturedex::storage::cardstore::CardStore;
use creaturedex::storage::cardstore::{CardStore, CardStoreError};
use creaturedex::storage::store::Store;
use embassy_executor::Spawner;
use embassy_time::Instant;
@@ -32,6 +32,7 @@ use esp_hal::Blocking;
use esp_hal::clock::CpuClock;
use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull};
use esp_hal::i2c::master::{Config as I2cConfig, I2c};
use esp_hal::rtc_cntl::Rtc;
use esp_hal::timer::timg::TimerGroup;
use log::error;
@@ -79,16 +80,10 @@ async fn main(spawner: Spawner) {
peripherals.GPIO12, // shared dc
)));
let _secondary_oled_cs_2 = Output::new(
peripherals.GPIO14,
Level::High,
OutputConfig::default(),
);
let _secondary_oled_cs_3 = Output::new(
peripherals.GPIO21,
Level::High,
OutputConfig::default(),
);
let _secondary_oled_cs_2 =
Output::new(peripherals.GPIO14, Level::High, OutputConfig::default());
let _secondary_oled_cs_3 =
Output::new(peripherals.GPIO21, Level::High, OutputConfig::default());
let config = I2cConfig::default().with_frequency(esp_hal::time::Rate::from_khz(400));
let i2c = match I2c::new(peripherals.I2C0, config) {
@@ -112,6 +107,8 @@ async fn main(spawner: Spawner) {
let store = Store::new(peripherals.FLASH).expect("Could not initialize Flash Store");
let card_store = CardStore::new(store).expect("Could not initialize Card Store");
let mut rtc = Rtc::new(peripherals.LPWR);
//setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await;
let mut nfc_driver = NfcPn532Driver::new(AtomicDevice::new(shared_i2c));
@@ -135,12 +132,14 @@ async fn main(spawner: Spawner) {
log::info!("Setup complete, entering main loop");
spawner.spawn(navigation::run(inputs, display).expect("run task failed"));
spawner.spawn(nfc_driver_task(nfc_driver).expect("nfc driver task failed"));
spawner.spawn(nfc_driver_task(nfc_driver, card_store, rtc).expect("nfc driver task failed"));
}
#[embassy_executor::task]
async fn nfc_driver_task(
mut nfc_driver: NfcPn532Driver<AtomicDevice<'static, I2c<'static, Blocking>>>,
mut card_store: CardStore,
rtc: Rtc<'static>,
) {
loop {
while CARD_DATA.lock().await.is_none() {
@@ -155,6 +154,14 @@ async fn nfc_driver_task(
.lock()
.await
.replace(Card::try_from(split_data).expect("Invalid card data"));
match card_store.save_raw_card(split_data, rtc.current_time_us()) {
Ok(()) => {}
Err(error) => {
log::error!("Error storing card in flash: {:?}", error)
}
}
let elapsed_ms = start.elapsed().as_millis();
log::info!(
"NFC read duration: {} ms, card data length: {}",