Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8770490949
|
||
|
|
c74c8afbd5
|
||
|
|
3bf8b4f027
|
+15
-11
@@ -7,13 +7,13 @@
|
|||||||
)]
|
)]
|
||||||
#![deny(clippy::large_stack_frames)]
|
#![deny(clippy::large_stack_frames)]
|
||||||
|
|
||||||
|
use alloc::boxed::Box;
|
||||||
use creaturedex::card::decoder::split_nfc_hex;
|
use creaturedex::card::decoder::split_nfc_hex;
|
||||||
use creaturedex::card::model::Card;
|
use creaturedex::card::model::Card;
|
||||||
use creaturedex::drivers::i2c_bus::I2cBusPinConfiguration;
|
|
||||||
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
|
|
||||||
use creaturedex::drivers::secondary_oled::SecondaryDisplay;
|
use creaturedex::drivers::secondary_oled::SecondaryDisplay;
|
||||||
use creaturedex::drivers::spi_bus;
|
use creaturedex::drivers::spi_bus;
|
||||||
use creaturedex::drivers::tertiary_lcd::{TertiaryDisplay, init_tertiary_lcd, write_wrapped};
|
use creaturedex::drivers::tertiary_lcd::{TertiaryDisplay, init_tertiary_lcd, write_wrapped};
|
||||||
|
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
|
||||||
use creaturedex::navigation::inputs::Inputs;
|
use creaturedex::navigation::inputs::Inputs;
|
||||||
use creaturedex::navigation::navigation::{self, CARD_DATA};
|
use creaturedex::navigation::navigation::{self, CARD_DATA};
|
||||||
use creaturedex::navigation::outputs::Outputs;
|
use creaturedex::navigation::outputs::Outputs;
|
||||||
@@ -27,10 +27,11 @@ use embedded_graphics::{
|
|||||||
text::Text,
|
text::Text,
|
||||||
};
|
};
|
||||||
use embedded_hal_bus::i2c::AtomicDevice;
|
use embedded_hal_bus::i2c::AtomicDevice;
|
||||||
|
use embedded_hal_bus::util::AtomicCell;
|
||||||
use esp_hal::Blocking;
|
use esp_hal::Blocking;
|
||||||
use esp_hal::clock::CpuClock;
|
use esp_hal::clock::CpuClock;
|
||||||
use esp_hal::gpio::{Input, InputConfig, Pull};
|
use esp_hal::gpio::{Input, InputConfig, Pull};
|
||||||
use esp_hal::i2c::master::I2c;
|
use esp_hal::i2c::master::{Config as I2cConfig, I2c};
|
||||||
use esp_hal::timer::timg::TimerGroup;
|
use esp_hal::timer::timg::TimerGroup;
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
@@ -81,15 +82,18 @@ async fn main(spawner: Spawner) {
|
|||||||
}
|
}
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Initialize the shared I2C bus using the refactored i2c_bus module.
|
let config = I2cConfig::default().with_frequency(esp_hal::time::Rate::from_khz(400));
|
||||||
let shared_i2c = {
|
let i2c = match I2c::new(peripherals.I2C0, config) {
|
||||||
let config = I2cBusPinConfiguration {
|
Ok(bus) => bus
|
||||||
i2c_peripheral: peripherals.I2C0,
|
.with_sda(peripherals.GPIO17)
|
||||||
scl: peripherals.GPIO18,
|
.with_scl(peripherals.GPIO18),
|
||||||
sda: peripherals.GPIO17,
|
Err(e) => {
|
||||||
};
|
log::error!("Shared I2C bus initialization failed for GPIO17/GPIO18: {e:?}");
|
||||||
config.build()
|
panic!("Shared I2C bus initialization failed");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
let shared_i2c = Box::leak(Box::new(AtomicCell::new(i2c)));
|
||||||
|
log::info!("Shared I2C bus ready on GPIO17 (SDA) and GPIO18 (SCL)");
|
||||||
|
|
||||||
let mut lcd: TertiaryDisplay = init_tertiary_lcd(AtomicDevice::new(shared_i2c));
|
let mut lcd: TertiaryDisplay = init_tertiary_lcd(AtomicDevice::new(shared_i2c));
|
||||||
if let Err(e) = write_wrapped(&mut lcd, "Testing more than 16 chars what happens now?") {
|
if let Err(e) = write_wrapped(&mut lcd, "Testing more than 16 chars what happens now?") {
|
||||||
|
|||||||
+20
-63
@@ -1,5 +1,4 @@
|
|||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::error::Error;
|
|
||||||
use embedded_hal::i2c::I2c;
|
use embedded_hal::i2c::I2c;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use pn532::i2c::I2CInterface;
|
use pn532::i2c::I2CInterface;
|
||||||
@@ -9,7 +8,6 @@ use pn532::{Pn532, Request};
|
|||||||
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 34>;
|
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 34>;
|
||||||
|
|
||||||
pub const PAGES_PER_READ: usize = 4;
|
pub const PAGES_PER_READ: usize = 4;
|
||||||
pub const BYTES_PER_READ: usize = PAGES_PER_READ * 4;
|
|
||||||
|
|
||||||
/// PN532 NFC reader driver.
|
/// PN532 NFC reader driver.
|
||||||
pub struct NfcPn532Driver<I2C>
|
pub struct NfcPn532Driver<I2C>
|
||||||
@@ -19,35 +17,6 @@ where
|
|||||||
pn532: Pn532Device<I2C>,
|
pn532: Pn532Device<I2C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct PageParseError<const N: usize> {
|
|
||||||
iteration: u8,
|
|
||||||
bytes: [u8; N],
|
|
||||||
status_code: Option<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> Error for PageParseError<N> {}
|
|
||||||
|
|
||||||
impl<const N: usize> core::fmt::Display for PageParseError<N> {
|
|
||||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
||||||
let PageParseError {
|
|
||||||
iteration,
|
|
||||||
bytes,
|
|
||||||
status_code,
|
|
||||||
} = self;
|
|
||||||
match status_code {
|
|
||||||
Some(code) => write!(
|
|
||||||
f,
|
|
||||||
"NFC pages parse error in iteration {iteration} with error code 0x{code:02x}: {bytes:02x?}"
|
|
||||||
),
|
|
||||||
None => write!(
|
|
||||||
f,
|
|
||||||
"No data available for NFC page in iteration {iteration}: {bytes:02x?}"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I2C> NfcPn532Driver<I2C>
|
impl<I2C> NfcPn532Driver<I2C>
|
||||||
where
|
where
|
||||||
I2C: I2c,
|
I2C: I2c,
|
||||||
@@ -88,21 +57,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_nfc_page(
|
pub fn parse_nfc_page(iteration: u8, bytes: &[u8]) -> Result<[u8; PAGES_PER_READ * 4], ()> {
|
||||||
iteration: u8,
|
let success = bytes.first().is_some_and(|x| *x == 0);
|
||||||
bytes: &[u8],
|
if success {
|
||||||
) -> Result<[u8; BYTES_PER_READ], PageParseError<BYTES_PER_READ>> {
|
let slice_len = bytes.len().min(PAGES_PER_READ * 4 + 1);
|
||||||
let slice_len = bytes.len().min(BYTES_PER_READ + 1);
|
let mut arr = [0; PAGES_PER_READ * 4];
|
||||||
let mut arr = [0; BYTES_PER_READ];
|
arr[..slice_len - 1].copy_from_slice(&bytes[1..slice_len]);
|
||||||
arr[..slice_len - 1].copy_from_slice(&bytes[1..slice_len]);
|
Ok(arr)
|
||||||
|
} else {
|
||||||
match bytes.first() {
|
info!("err {iteration}: {:02x?}", bytes);
|
||||||
Some(0x00) => Ok(arr),
|
Err(())
|
||||||
error => Err(PageParseError {
|
|
||||||
iteration,
|
|
||||||
bytes: arr,
|
|
||||||
status_code: error.copied(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,25 +79,18 @@ where
|
|||||||
let mut data: Vec<u8> = Vec::with_capacity(858);
|
let mut data: Vec<u8> = Vec::with_capacity(858);
|
||||||
|
|
||||||
for i in (0x0B..=230).step_by(PAGES_PER_READ) {
|
for i in (0x0B..=230).step_by(PAGES_PER_READ) {
|
||||||
let bytes = loop {
|
let mut parse_result = Err(());
|
||||||
let read = self
|
while parse_result.is_err() {
|
||||||
|
let Ok(result) = self
|
||||||
.pn532
|
.pn532
|
||||||
.process_async(&Request::ntag_read(i), BYTES_PER_READ + 1)
|
.process_async(&Request::ntag_read(i), PAGES_PER_READ * 4 + 1)
|
||||||
.await;
|
.await
|
||||||
|
else {
|
||||||
let bytes = match read {
|
continue;
|
||||||
Ok(bytes) => bytes,
|
|
||||||
Err(e) => {
|
|
||||||
error!("Reading page {i} failed: {e:?}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
parse_result = Self::parse_nfc_page(i, result);
|
||||||
match Self::parse_nfc_page(i, bytes) {
|
}
|
||||||
Ok(data) => break data,
|
let bytes = parse_result.unwrap();
|
||||||
Err(e) => error!("Parsing page failed: {e}"),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let bytes = if i == 0x0B { &bytes[1..] } else { &bytes };
|
let bytes = if i == 0x0B { &bytes[1..] } else { &bytes };
|
||||||
data.extend(bytes);
|
data.extend(bytes);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user