Implemented Scan card menu
This commit was merged in pull request #29.
This commit is contained in:
+17
-13
@@ -55,7 +55,7 @@ impl pn532::CountDown for TimerWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Pn532Device<'a> = Pn532<I2CInterface<I2c<'a, esp_hal::Blocking>>, TimerWrapper, 32>;
|
||||
pub type Pn532Device<'a> = Pn532<I2CInterface<I2c<'a, esp_hal::Blocking>>, (), 32>;
|
||||
|
||||
/// PN532 NFC reader driver.
|
||||
pub struct NfcPn532Driver<'a> {
|
||||
@@ -74,16 +74,15 @@ impl<'a> NfcPn532Driver<'a> {
|
||||
.with_sda(sda)
|
||||
.with_scl(scl);
|
||||
|
||||
let pn532 = Pn532::new(I2CInterface { i2c }, TimerWrapper::new());
|
||||
let pn532 = Pn532::new_async(I2CInterface { i2c });
|
||||
Self { pn532 }
|
||||
}
|
||||
|
||||
pub fn configure_sam(&mut self, timeout: Duration) -> Result<(), ()> {
|
||||
if let Err(e) = self.pn532.process(
|
||||
pub async fn configure_sam(&mut self) -> Result<(), ()> {
|
||||
if let Err(e) = self.pn532.process_async(
|
||||
&Request::sam_configuration(SAMMode::Normal, false),
|
||||
0,
|
||||
timeout,
|
||||
) {
|
||||
).await {
|
||||
error!("Could not initialize PN532: {e:?}");
|
||||
Err(())
|
||||
} else {
|
||||
@@ -92,8 +91,8 @@ impl<'a> NfcPn532Driver<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_target(&mut self, timeout: Duration) -> Result<(), ()> {
|
||||
match self.pn532.process(&Request::INLIST_ONE_ISO_A_TARGET, 23, timeout) {
|
||||
pub async fn poll_target(&mut self) -> Result<(), ()> {
|
||||
match self.pn532.process_async(&Request::INLIST_ONE_ISO_A_TARGET, 23).await {
|
||||
Ok(uid) => {
|
||||
info!("uid = {uid:?}");
|
||||
Ok(())
|
||||
@@ -117,21 +116,26 @@ impl<'a> NfcPn532Driver<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_card_data(&mut self) -> Result<Vec<u8>, ()> {
|
||||
let ms_1000 = Duration::from_millis(1000);
|
||||
self.poll_target(Duration::from_secs(10))?;
|
||||
pub async fn read_card_data(&mut self) -> Result<Vec<u8>, ()> {
|
||||
self.poll_target().await?;
|
||||
|
||||
let mut data: Vec<u8> = Vec::with_capacity(858);
|
||||
|
||||
for i in 0x04..=230 {
|
||||
for i in 0x0B..=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 {
|
||||
let Ok(result) = self.pn532.process_async(&Request::ntag_read(i), 17).await else {
|
||||
continue;
|
||||
};
|
||||
parse_result = Self::parse_nfc_page(i, result);
|
||||
}
|
||||
let bytes = parse_result.unwrap();
|
||||
let bytes = if i == 0x0B {
|
||||
&bytes[1..]
|
||||
} else {
|
||||
&bytes
|
||||
};
|
||||
data.extend(bytes);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user