Improved NFC reading speed
This commit is contained in:
@@ -7,53 +7,9 @@ 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,
|
||||
}
|
||||
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 34>;
|
||||
|
||||
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>,
|
||||
{
|
||||
self.start = 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 elapsed >= self.duration {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(nb::Error::WouldBlock)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 32>;
|
||||
pub const PAGES_PER_READ: usize = 4;
|
||||
|
||||
/// PN532 NFC reader driver.
|
||||
pub struct NfcPn532Driver<I2C>
|
||||
@@ -98,11 +54,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_nfc_page(iteration: u8, bytes: &[u8]) -> Result<[u8; 4], ()> {
|
||||
pub fn parse_nfc_page(iteration: u8, bytes: &[u8]) -> Result<[u8; PAGES_PER_READ * 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]);
|
||||
let slice_len = bytes.len().min(PAGES_PER_READ * 4 + 1);
|
||||
let mut arr = [0; PAGES_PER_READ * 4];
|
||||
arr[..slice_len - 1].copy_from_slice(&bytes[1..slice_len]);
|
||||
Ok(arr)
|
||||
} else {
|
||||
info!("err {iteration}: {:02x?}", bytes);
|
||||
@@ -118,10 +75,10 @@ where
|
||||
|
||||
let mut data: Vec<u8> = Vec::with_capacity(858);
|
||||
|
||||
for i in 0x0B..=230 {
|
||||
for i in (0x0B..=230).step_by(PAGES_PER_READ) {
|
||||
let mut parse_result = Err(());
|
||||
while parse_result.is_err() {
|
||||
let Ok(result) = self.pn532.process_async(&Request::ntag_read(i), 17).await else {
|
||||
let Ok(result) = self.pn532.process_async(&Request::ntag_read(i), PAGES_PER_READ * 4 + 1).await else {
|
||||
continue;
|
||||
};
|
||||
parse_result = Self::parse_nfc_page(i, result);
|
||||
@@ -134,8 +91,6 @@ where
|
||||
};
|
||||
data.extend(bytes);
|
||||
}
|
||||
|
||||
info!("PN532 full card payload read over I2C: {:02x?}", data);
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user