Added updated NFC implementation into refactored structure
This commit was merged in pull request #25.
This commit is contained in:
+1
-1
@@ -94,7 +94,7 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
|
||||
loop {
|
||||
if let Some(card_data) = nfc_driver.read_card(timeout) {
|
||||
if let Ok(card_data) = nfc_driver.read_card_data() {
|
||||
log::info!("NFC Card Read: {:?}", card_data);
|
||||
}
|
||||
|
||||
|
||||
+47
-19
@@ -1,3 +1,4 @@
|
||||
use alloc::vec::Vec;
|
||||
use core::convert::Infallible;
|
||||
use esp_hal::gpio::interconnect::{PeripheralInput, PeripheralOutput};
|
||||
use esp_hal::i2c::master::{Config as I2cConfig, I2c};
|
||||
@@ -38,8 +39,7 @@ impl pn532::CountDown for TimerWrapper {
|
||||
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.start = Instant::now();
|
||||
self.duration = timeout.into();
|
||||
self.counter = 0;
|
||||
}
|
||||
@@ -47,9 +47,6 @@ impl pn532::CountDown for TimerWrapper {
|
||||
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 {
|
||||
@@ -71,7 +68,7 @@ impl<'a> NfcPn532Driver<'a> {
|
||||
SDA: PeripheralInput<'static> + PeripheralOutput<'static>,
|
||||
SCL: PeripheralInput<'static> + PeripheralOutput<'static>,
|
||||
{
|
||||
let config = I2cConfig::default().with_frequency(Rate::from_khz(100));
|
||||
let config = I2cConfig::default().with_frequency(Rate::from_khz(20));
|
||||
let i2c = I2c::new(i2c0, config)
|
||||
.expect("Failed to init I2C")
|
||||
.with_sda(sda)
|
||||
@@ -95,24 +92,55 @@ impl<'a> NfcPn532Driver<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
pub fn poll_target(&mut self, timeout: Duration) -> Result<(), ()> {
|
||||
match self.pn532.process(&Request::INLIST_ONE_ISO_A_TARGET, 23, timeout) {
|
||||
Ok(uid) => {
|
||||
info!("uid = {uid:?}");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
info!("No NFC card found: {e:?}");
|
||||
Err(())
|
||||
}
|
||||
} else {
|
||||
info!("No NFC card found.");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_nfc_page(iteration: u8, bytes: &[u8]) -> Result<[u8; 4], ()> {
|
||||
let success = bytes.first().is_some_and(|x| *x == 0);
|
||||
if success {
|
||||
let mut arr = [0; 4];
|
||||
arr.copy_from_slice(&bytes[1..5]);
|
||||
Ok(arr)
|
||||
} else {
|
||||
info!("err {iteration}: {:02x?}", bytes);
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_card_data(&mut self) -> Result<Vec<u8>, ()> {
|
||||
let ms_1000 = Duration::from_millis(1000);
|
||||
self.poll_target(Duration::from_secs(10))?;
|
||||
|
||||
let mut data: Vec<u8> = Vec::with_capacity(858);
|
||||
|
||||
for i in 0x04..=230 {
|
||||
let mut parse_result = Err(());
|
||||
while parse_result.is_err() {
|
||||
let Ok(result) = self.pn532.process(&Request::ntag_read(i), 17, ms_1000) else {
|
||||
continue;
|
||||
};
|
||||
parse_result = Self::parse_nfc_page(i, result);
|
||||
}
|
||||
let bytes = parse_result.unwrap();
|
||||
data.extend(bytes);
|
||||
}
|
||||
|
||||
info!("Full data: {data:02x?}");
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub fn device_mut(&mut self) -> &mut Pn532Device<'a> {
|
||||
&mut self.pn532
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user