Added NFC implementation into refactored structure
This commit is contained in:
Generated
+11
@@ -320,6 +320,7 @@ dependencies = [
|
|||||||
"esp-rtos",
|
"esp-rtos",
|
||||||
"ili9341",
|
"ili9341",
|
||||||
"log",
|
"log",
|
||||||
|
"pn532",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2584,6 +2585,16 @@ version = "0.2.17"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pn532"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ce656b2f9335bedbd8806597f6f78e644a955366673cd9280a0fb67a0d7b35a1"
|
||||||
|
dependencies = [
|
||||||
|
"embedded-hal 1.0.0",
|
||||||
|
"nb 1.1.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "portable-atomic"
|
name = "portable-atomic"
|
||||||
version = "1.14.0"
|
version = "1.14.0"
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ embedded-hal-bus = "0.3.0"
|
|||||||
embedded-graphics = "0.8.2"
|
embedded-graphics = "0.8.2"
|
||||||
embedded-graphics-core = "0.4.1"
|
embedded-graphics-core = "0.4.1"
|
||||||
base64 = { version = "0.22", default-features = false, features = ["alloc"] }
|
base64 = { version = "0.22", default-features = false, features = ["alloc"] }
|
||||||
|
pn532 = "0.5.0"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# For fine tuning these settings, please refer to https://doc.rust-lang.org/cargo/reference/profiles.html
|
# For fine tuning these settings, please refer to https://doc.rust-lang.org/cargo/reference/profiles.html
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
use creaturedex::card::mock::{MOCK_PAYLOAD, MOCK_PAYLOAD2};
|
use creaturedex::card::mock::{MOCK_PAYLOAD, MOCK_PAYLOAD2};
|
||||||
use creaturedex::display::primary_lcd::init_primary_lcd;
|
use creaturedex::display::primary_lcd::init_primary_lcd;
|
||||||
use creaturedex::display::views::card_view::render_nfc_payload;
|
use creaturedex::display::views::card_view::render_nfc_payload;
|
||||||
|
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
|
||||||
use creaturedex::navigation::input::init_navigation_button;
|
use creaturedex::navigation::input::init_navigation_button;
|
||||||
use creaturedex::navigation::state::NavigationState;
|
use creaturedex::navigation::state::NavigationState;
|
||||||
use creaturedex::network::wifi::setup_wifi;
|
use creaturedex::network::wifi::setup_wifi;
|
||||||
@@ -18,6 +19,7 @@ use embassy_time::Timer;
|
|||||||
use embedded_graphics::draw_target::DrawTarget;
|
use embedded_graphics::draw_target::DrawTarget;
|
||||||
use embedded_graphics::pixelcolor::Rgb565;
|
use embedded_graphics::pixelcolor::Rgb565;
|
||||||
use embedded_graphics::prelude::RgbColor;
|
use embedded_graphics::prelude::RgbColor;
|
||||||
|
use esp_hal::time::Duration;
|
||||||
use esp_hal::clock::CpuClock;
|
use esp_hal::clock::CpuClock;
|
||||||
use esp_hal::delay::Delay;
|
use esp_hal::delay::Delay;
|
||||||
use esp_hal::timer::timg::TimerGroup;
|
use esp_hal::timer::timg::TimerGroup;
|
||||||
@@ -83,7 +85,19 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
let mut nav_state = NavigationState::new();
|
let mut nav_state = NavigationState::new();
|
||||||
|
|
||||||
|
let mut nfc_driver = NfcPn532Driver::new(peripherals.I2C0, peripherals.GPIO17, peripherals.GPIO18);
|
||||||
|
let timeout = Duration::from_millis(1000);
|
||||||
|
if nfc_driver.configure_sam(timeout).is_ok() {
|
||||||
|
log::info!("NFC SAM configuration successful");
|
||||||
|
} else {
|
||||||
|
log::error!("NFC SAM configuration failed");
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
if let Some(card_data) = nfc_driver.read_card(timeout) {
|
||||||
|
log::info!("NFC Card Read: {:?}", card_data);
|
||||||
|
}
|
||||||
|
|
||||||
if button1.is_low() {
|
if button1.is_low() {
|
||||||
log::info!("Button pressed!");
|
log::info!("Button pressed!");
|
||||||
display.clear(Rgb565::BLACK).unwrap();
|
display.clear(Rgb565::BLACK).unwrap();
|
||||||
|
|||||||
+118
-1
@@ -1 +1,118 @@
|
|||||||
// Drivers nfc_pn532 module
|
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 log::{error, info};
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
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>,
|
||||||
|
{
|
||||||
|
// without this debug log, it works worse: stuck in NFC card search without ever returning.
|
||||||
|
self.start = esp_println::dbg!(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 self.counter.is_multiple_of(1) {
|
||||||
|
info!("Elapsed: {elapsed}");
|
||||||
|
}
|
||||||
|
if elapsed >= self.duration {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(nb::Error::WouldBlock)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Pn532Device<'a> = Pn532<I2CInterface<I2c<'a, esp_hal::Blocking>>, TimerWrapper, 32>;
|
||||||
|
|
||||||
|
/// PN532 NFC reader driver.
|
||||||
|
pub struct NfcPn532Driver<'a> {
|
||||||
|
pn532: Pn532Device<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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(100));
|
||||||
|
let i2c = I2c::new(i2c0, config)
|
||||||
|
.expect("Failed to init I2C")
|
||||||
|
.with_sda(sda)
|
||||||
|
.with_scl(scl);
|
||||||
|
|
||||||
|
let pn532 = Pn532::new(I2CInterface { i2c }, TimerWrapper::new());
|
||||||
|
Self { pn532 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn configure_sam(&mut self, timeout: Duration) -> Result<(), ()> {
|
||||||
|
if let Err(e) = self.pn532.process(
|
||||||
|
&Request::sam_configuration(SAMMode::Normal, false),
|
||||||
|
0,
|
||||||
|
timeout,
|
||||||
|
) {
|
||||||
|
error!("Could not initialize PN532: {e:?}");
|
||||||
|
Err(())
|
||||||
|
} else {
|
||||||
|
info!("Successfully init'ed PN532");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!("No NFC card found.");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn device_mut(&mut self) -> &mut Pn532Device<'a> {
|
||||||
|
&mut self.pn532
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user