working nfc reading, but finnicky

This commit is contained in:
2026-07-31 22:28:07 +02:00
parent 744a844f75
commit c8ea94a3ca
+43 -21
View File
@@ -9,6 +9,7 @@
use core::convert::Infallible;
use alloc::vec::Vec;
use display_interface_spi::SPIInterface;
use embedded_graphics::Drawable;
use embedded_graphics::mono_font::MonoTextStyle;
@@ -112,7 +113,7 @@ fn main() -> ! {
)
.unwrap();
display.clear(Rgb565::BLACK).unwrap();
// display.clear(Rgb565::BLACK).unwrap();
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
Text::new("Hello, ESP32!", Point::new(20, 100), style)
@@ -129,7 +130,7 @@ fn main() -> ! {
time::Rate,
};
let config = I2cConfig::default().with_frequency(Rate::from_khz(100));
let config = I2cConfig::default().with_frequency(Rate::from_khz(20));
let i2c = I2c::new(peripherals.I2C0, config)
.expect("Failed to init I2C")
.with_sda(sda)
@@ -139,27 +140,48 @@ fn main() -> ! {
let ms_50 = Duration::from_millis(50);
let ms_1000 = Duration::from_millis(1000);
let delay_start = Instant::now();
while delay_start.elapsed() < Duration::from_millis(500) {}
let mut data: Vec<u8> = Vec::with_capacity(858);
if let Err(e) = pn532.process(
&Request::sam_configuration(SAMMode::Normal, false),
0,
Duration::from_millis(5000),
) {
error!("Could not initialize PN532: {e:?}")
} else {
info!("Successfully init'ed PN532");
}
loop {
info!("Hello world!");
if let Err(e) = pn532.process(
&Request::sam_configuration(SAMMode::Normal, false),
0,
ms_1000,
match pn532.process(
&Request::INLIST_ONE_ISO_A_TARGET,
23,
Duration::from_secs(10),
) {
error!("Could not initialize PN532: {e:?}")
} else {
info!("Successfully init'ed PN532");
}
if let Ok(uid) = pn532.process(&Request::INLIST_ONE_ISO_A_TARGET, 7, ms_1000) {
info!("uid = {uid:?}");
let result = pn532.process(&Request::ntag_read(10), 17, ms_1000).unwrap();
// if result[0] == 0x00 {
info!("page 10: {:?}", &result[1..5]);
// }
}
else {
info!("No NFC card found.");
Ok(uid) => {
info!("uid = {uid:?}");
for i in (4..=231) {
let result = pn532.process(&Request::ntag_read(i), 17, ms_1000).unwrap();
let success = result.first().is_some_and(|x| *x == 0);
if success {
// info!("page {i}: {:02x?}", result);
data.extend(&result[1..5]);
} else {
info!("err {i}: {:02x?}", result);
}
}
info!("Full data: {data:02x?}");
data.clear();
}
Err(e) => {
info!("No NFC card found: {e:?}");
}
}
let delay_start = Instant::now();
@@ -194,7 +216,7 @@ impl pn532::CountDown for TimerWrapper {
T: Into<Self::Time>,
{
// without this debug log, it works worse: stuck in NFC card search without ever returning.
self.start = dbg!(Instant::now());
self.start = Instant::now();
self.duration = timeout.into();
self.counter = 0;
}
@@ -203,7 +225,7 @@ impl pn532::CountDown for TimerWrapper {
let elapsed = self.start.elapsed();
self.counter += 1;
if self.counter.is_multiple_of(1) {
info!("Elapsed: {elapsed}");
// info!("Elapsed: {elapsed}");
}
if elapsed >= self.duration {
Ok(())