Implemented tertiary LCD display

This commit is contained in:
2026-08-22 18:25:48 +02:00
parent b44ae59c8b
commit 07c5210d7b
7 changed files with 251 additions and 119 deletions
+23 -27
View File
@@ -1,9 +1,7 @@
use alloc::vec::Vec;
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 embedded_hal::i2c::I2c;
use esp_hal::time::{Duration, Instant};
use log::{error, info};
use pn532::i2c::I2CInterface;
use pn532::requests::SAMMode;
@@ -55,25 +53,21 @@ impl pn532::CountDown for TimerWrapper {
}
}
pub type Pn532Device<'a> = Pn532<I2CInterface<I2c<'a, esp_hal::Blocking>>, (), 32>;
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 32>;
/// PN532 NFC reader driver.
pub struct NfcPn532Driver<'a> {
pn532: Pn532Device<'a>,
pub struct NfcPn532Driver<I2C>
where
I2C: I2c,
{
pn532: Pn532Device<I2C>,
}
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(20));
let i2c = I2c::new(i2c0, config)
.expect("Failed to init I2C")
.with_sda(sda)
.with_scl(scl);
impl<I2C> NfcPn532Driver<I2C>
where
I2C: I2c,
{
pub fn new(i2c: I2C) -> Self {
let pn532 = Pn532::new_async(I2CInterface { i2c });
Self { pn532 }
}
@@ -83,10 +77,10 @@ impl<'a> NfcPn532Driver<'a> {
&Request::sam_configuration(SAMMode::Normal, false),
0,
).await {
error!("Could not initialize PN532: {e:?}");
error!("PN532 SAM configuration failed over I2C: {e:?}");
Err(())
} else {
info!("Successfully init'ed PN532");
info!("PN532 SAM configuration successful on shared I2C bus");
Ok(())
}
}
@@ -94,11 +88,11 @@ impl<'a> NfcPn532Driver<'a> {
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:?}");
info!("PN532 detected NFC target UID: {uid:?}");
Ok(())
}
Err(e) => {
info!("No NFC card found: {e:?}");
info!("No NFC card found on shared I2C bus: {e:?}");
Err(())
}
}
@@ -117,12 +111,14 @@ impl<'a> NfcPn532Driver<'a> {
}
pub async fn read_card_data(&mut self) -> Result<Vec<u8>, ()> {
self.poll_target().await?;
if let Err(e) = self.poll_target().await {
error!("PN532 I2C target poll failed before reading card data: {e:?}");
return Err(e);
}
let mut data: Vec<u8> = Vec::with_capacity(858);
for i in 0x0B..=230 {
let mut parse_result = Err(());
while parse_result.is_err() {
let Ok(result) = self.pn532.process_async(&Request::ntag_read(i), 17).await else {
@@ -139,11 +135,11 @@ impl<'a> NfcPn532Driver<'a> {
data.extend(bytes);
}
info!("Full data: {data:02x?}");
info!("PN532 full card payload read over I2C: {:02x?}", data);
Ok(data)
}
pub fn device_mut(&mut self) -> &mut Pn532Device<'a> {
pub fn device_mut(&mut self) -> &mut Pn532Device<I2C> {
&mut self.pn532
}
}