Added NFC implementation into refactored structure
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
use creaturedex::card::mock::{MOCK_PAYLOAD, MOCK_PAYLOAD2};
|
||||
use creaturedex::display::primary_lcd::init_primary_lcd;
|
||||
use creaturedex::display::views::card_view::render_nfc_payload;
|
||||
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
|
||||
use creaturedex::navigation::input::init_navigation_button;
|
||||
use creaturedex::navigation::state::NavigationState;
|
||||
use creaturedex::network::wifi::setup_wifi;
|
||||
@@ -18,6 +19,7 @@ use embassy_time::Timer;
|
||||
use embedded_graphics::draw_target::DrawTarget;
|
||||
use embedded_graphics::pixelcolor::Rgb565;
|
||||
use embedded_graphics::prelude::RgbColor;
|
||||
use esp_hal::time::Duration;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
@@ -83,7 +85,19 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
let mut nav_state = NavigationState::new();
|
||||
|
||||
let mut nfc_driver = NfcPn532Driver::new(peripherals.I2C0, peripherals.GPIO17, peripherals.GPIO18);
|
||||
let timeout = Duration::from_millis(1000);
|
||||
if nfc_driver.configure_sam(timeout).is_ok() {
|
||||
log::info!("NFC SAM configuration successful");
|
||||
} else {
|
||||
log::error!("NFC SAM configuration failed");
|
||||
}
|
||||
|
||||
loop {
|
||||
if let Some(card_data) = nfc_driver.read_card(timeout) {
|
||||
log::info!("NFC Card Read: {:?}", card_data);
|
||||
}
|
||||
|
||||
if button1.is_low() {
|
||||
log::info!("Button pressed!");
|
||||
display.clear(Rgb565::BLACK).unwrap();
|
||||
|
||||
+118
-1
@@ -1 +1,118 @@
|
||||
// Drivers nfc_pn532 module
|
||||
use core::convert::Infallible;
|
||||
use esp_hal::gpio::interconnect::{PeripheralInput, PeripheralOutput};
|
||||
use esp_hal::i2c::master::{Config as I2cConfig, I2c};
|
||||
use esp_hal::peripherals::I2C0;
|
||||
use esp_hal::time::{Duration, Instant, Rate};
|
||||
use log::{error, info};
|
||||
use pn532::i2c::I2CInterface;
|
||||
use pn532::requests::SAMMode;
|
||||
use pn532::{nb, Pn532, Request};
|
||||
|
||||
/// Timer implementation required by `pn532::CountDown`.
|
||||
pub struct TimerWrapper {
|
||||
start: Instant,
|
||||
duration: Duration,
|
||||
counter: u32,
|
||||
}
|
||||
|
||||
impl TimerWrapper {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
start: Instant::now(),
|
||||
duration: Duration::ZERO,
|
||||
counter: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TimerWrapper {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl pn532::CountDown for TimerWrapper {
|
||||
type Time = Duration;
|
||||
|
||||
fn start<T>(&mut self, timeout: T)
|
||||
where
|
||||
T: Into<Self::Time>,
|
||||
{
|
||||
// without this debug log, it works worse: stuck in NFC card search without ever returning.
|
||||
self.start = esp_println::dbg!(Instant::now());
|
||||
self.duration = timeout.into();
|
||||
self.counter = 0;
|
||||
}
|
||||
|
||||
fn wait(&mut self) -> nb::Result<(), Infallible> {
|
||||
let elapsed = self.start.elapsed();
|
||||
self.counter += 1;
|
||||
if self.counter.is_multiple_of(1) {
|
||||
info!("Elapsed: {elapsed}");
|
||||
}
|
||||
if elapsed >= self.duration {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(nb::Error::WouldBlock)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Pn532Device<'a> = Pn532<I2CInterface<I2c<'a, esp_hal::Blocking>>, TimerWrapper, 32>;
|
||||
|
||||
/// PN532 NFC reader driver.
|
||||
pub struct NfcPn532Driver<'a> {
|
||||
pn532: Pn532Device<'a>,
|
||||
}
|
||||
|
||||
impl<'a> NfcPn532Driver<'a> {
|
||||
pub fn new<SDA, SCL>(i2c0: I2C0<'static>, sda: SDA, scl: SCL) -> Self
|
||||
where
|
||||
SDA: PeripheralInput<'static> + PeripheralOutput<'static>,
|
||||
SCL: PeripheralInput<'static> + PeripheralOutput<'static>,
|
||||
{
|
||||
let config = I2cConfig::default().with_frequency(Rate::from_khz(100));
|
||||
let i2c = I2c::new(i2c0, config)
|
||||
.expect("Failed to init I2C")
|
||||
.with_sda(sda)
|
||||
.with_scl(scl);
|
||||
|
||||
let pn532 = Pn532::new(I2CInterface { i2c }, TimerWrapper::new());
|
||||
Self { pn532 }
|
||||
}
|
||||
|
||||
pub fn configure_sam(&mut self, timeout: Duration) -> Result<(), ()> {
|
||||
if let Err(e) = self.pn532.process(
|
||||
&Request::sam_configuration(SAMMode::Normal, false),
|
||||
0,
|
||||
timeout,
|
||||
) {
|
||||
error!("Could not initialize PN532: {e:?}");
|
||||
Err(())
|
||||
} else {
|
||||
info!("Successfully init'ed PN532");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_card(&mut self, timeout: Duration) -> Option<[u8; 4]> {
|
||||
if let Ok(uid) = self.pn532.process(&Request::INLIST_ONE_ISO_A_TARGET, 7, timeout) {
|
||||
info!("uid = {uid:?}");
|
||||
if let Ok(result) = self.pn532.process(&Request::ntag_read(10), 17, timeout) {
|
||||
info!("page 10: {:?}", &result[1..5]);
|
||||
let mut data = [0u8; 4];
|
||||
data.copy_from_slice(&result[1..5]);
|
||||
Some(data)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
info!("No NFC card found.");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn device_mut(&mut self) -> &mut Pn532Device<'a> {
|
||||
&mut self.pn532
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user