mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-04-30 18:49:09 +00:00
Compare commits
5 Commits
7eb18376e1
...
252e63c607
| Author | SHA1 | Date | |
|---|---|---|---|
| 252e63c607 | |||
| 99848f0e6d | |||
| f46cdc4d29 | |||
| a8d64f6af5 | |||
| 8fb6bac651 |
@@ -1,6 +1,6 @@
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_hal::{Async, uart::Uart};
|
||||
use log::{debug, info};
|
||||
use log::{debug, info, warn};
|
||||
|
||||
use crate::TallyPublisher;
|
||||
|
||||
@@ -18,10 +18,14 @@ pub async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: Tally
|
||||
}
|
||||
info!("Read {n} bytes from UART: {hex_str}");
|
||||
|
||||
// The first byte is always 0x02 (Start of text)
|
||||
// Followed by 12 Bytes of chars
|
||||
// Ended by 0x03 (End of text)
|
||||
chan.publish(uart_buffer[1..13].try_into().unwrap()).await;
|
||||
match extract_id(&uart_buffer) {
|
||||
Some(read) => {
|
||||
chan.publish(read.try_into().unwrap()).await;
|
||||
}
|
||||
None => {
|
||||
warn!("Invalid read from the RFID reader");
|
||||
}
|
||||
};
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Error reading from UART: {e}");
|
||||
@@ -30,3 +34,35 @@ pub async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: Tally
|
||||
Timer::after(Duration::from_millis(200)).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Scans the UART output and retuns the first propper read ID
|
||||
/// This ensures that only valid ID are parsed
|
||||
///
|
||||
/// A valid read looks like this:
|
||||
/// The first byte is always 0x02 (Start of text)
|
||||
/// Followed by 12 Bytes of chars
|
||||
/// Ended by 0x03 (End of text)
|
||||
pub fn extract_id(buffer: &[u8]) -> Option<[u8; 12]> {
|
||||
const STX: u8 = 0x02; // Start of Text ASCII char
|
||||
const ETX: u8 = 0x03; // End of Text ASCII char
|
||||
const ID_LENGTH: usize = 12;
|
||||
const MINIMUM_SEQUENCE: usize = ID_LENGTH + 2; // STX + 12 bytes + ETX
|
||||
|
||||
if buffer.len() < MINIMUM_SEQUENCE {
|
||||
return None;
|
||||
}
|
||||
|
||||
for window_start in 0..=buffer.len() - MINIMUM_SEQUENCE {
|
||||
if buffer[window_start] == STX {
|
||||
let id_end = window_start + ID_LENGTH + 1;
|
||||
|
||||
if buffer[id_end] == ETX {
|
||||
let mut id = [0u8; ID_LENGTH];
|
||||
id.copy_from_slice(&buffer[window_start + 1..id_end]);
|
||||
return Some(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
@@ -132,14 +132,17 @@ impl Persistence for SDCardPersistence {
|
||||
async fn list_days(&mut self) -> Vec<Day> {
|
||||
let mut vol_0 = self.vol_mgr.open_volume(VolumeIdx(0)).unwrap();
|
||||
let mut root_dir = vol_0.open_root_dir().unwrap();
|
||||
let mut days_dir = root_dir.open_dir("days").unwrap();
|
||||
|
||||
let mut days_dir = root_dir.open_dir(".").unwrap();
|
||||
|
||||
let mut days: Vec<Day> = Vec::new();
|
||||
days_dir
|
||||
.iterate_dir(|e| {
|
||||
let filename = e.name.clone();
|
||||
let day: Day = filename.try_into().unwrap();
|
||||
days.push(day);
|
||||
|
||||
if let Ok(day) = filename.try_into() {
|
||||
days.push(day);
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use core::fmt::Write;
|
||||
use embedded_sdmmc::ShortFileName;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Day(u32);
|
||||
|
||||
impl Day {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use core::str::FromStr;
|
||||
use core::{fmt::Display, str::FromStr};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
@@ -72,6 +72,21 @@ impl TryFrom<&[u8]> for TallyID {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<[u8; 12]> for TallyID {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: [u8; 12]) -> Result<Self, Self::Error> {
|
||||
Self::try_from(&value as &[u8])
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TallyID {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let s: heapless::String<12> = (*self).into();
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for TallyID {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user