diff --git a/src/drivers/nfc_reader.rs b/src/drivers/nfc_reader.rs index 4c24d8a..1b6c375 100644 --- a/src/drivers/nfc_reader.rs +++ b/src/drivers/nfc_reader.rs @@ -2,7 +2,7 @@ use embassy_time::{Duration, Timer}; use esp_hal::{Async, uart::Uart}; use log::{debug, info}; -use crate::{TallyPublisher, store::TallyID}; +use crate::TallyPublisher; #[embassy_executor::task] pub async fn rfid_reader_task(mut uart_device: Uart<'static, Async>, chan: TallyPublisher) { diff --git a/src/drivers/rtc.rs b/src/drivers/rtc.rs index ea96a40..bd3da9b 100644 --- a/src/drivers/rtc.rs +++ b/src/drivers/rtc.rs @@ -1,3 +1,4 @@ +use chrono::{TimeZone, Utc}; use ds3231::{ Config, DS3231, DS3231Error, InterruptControl, Oscillator, SquareWaveFrequency, TimeRepresentation, @@ -9,7 +10,6 @@ use esp_hal::{ use log::{debug, error, info}; use crate::{FEEDBACK_STATE, drivers, feedback, store::Date}; -use chrono::{TimeZone, Utc}; include!(concat!(env!("OUT_DIR"), "/build_time.rs")); @@ -49,7 +49,7 @@ impl RTCClock { pub async fn get_date(&mut self) -> Date { let (year, month, day) = unix_to_ymd_string(self.get_time().await); - let mut buffer: Date = [0; 10] ; + let mut buffer: Date = [0; 10]; // Write YYYY buffer[0] = b'0' + ((year / 1000) % 10) as u8; @@ -70,35 +70,34 @@ impl RTCClock { buffer } - } fn unix_to_ymd_string(timestamp: u64) -> (u16, u8, u8) { - // Apply UTC+1 offset - let ts = timestamp + UTC_PLUS_ONE; + // Apply UTC+1 offset + let ts = timestamp + UTC_PLUS_ONE; - // Convert to total days since UNIX epoch - let days_since_epoch = ts / SECS_PER_DAY; + // Convert to total days since UNIX epoch + let days_since_epoch = ts / SECS_PER_DAY; - // Convert to proleptic Gregorian date - civil_from_days(days_since_epoch as i64 + UNIX_OFFSET_DAYS as i64) - } + // Convert to proleptic Gregorian date + civil_from_days(days_since_epoch as i64 + UNIX_OFFSET_DAYS as i64) +} - // This function returns (year, month, day). - // Based on the algorithm by Howard Hinnant. - fn civil_from_days(z: i64) -> (u16, u8, u8) { - let mut z = z; - z -= 60; // shift epoch for algorithm - let era = (z >= 0).then_some(z).unwrap_or(z - 146096) / 146097; - let doe = z - era * 146097; // [0, 146096] - let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399] - let y = yoe + era * 400; - let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365] - let mp = (5 * doy + 2) / 153; // [0, 11] - let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31] - let m = mp + (if mp < 10 { 3 } else { -9 }); // [1, 12] - ((y + (m <= 2) as i64) as u16, m as u8, d as u8) - } +// This function returns (year, month, day). +// Based on the algorithm by Howard Hinnant. +fn civil_from_days(z: i64) -> (u16, u8, u8) { + let mut z = z; + z -= 60; // shift epoch for algorithm + let era = (z >= 0).then_some(z).unwrap_or(z - 146096) / 146097; + let doe = z - era * 146097; // [0, 146096] + let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399] + let y = yoe + era * 400; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365] + let mp = (5 * doy + 2) / 153; // [0, 11] + let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31] + let m = mp + (if mp < 10 { 3 } else { -9 }); // [1, 12] + ((y + (m <= 2) as i64) as u16, m as u8, d as u8) +} pub async fn rtc_config(i2c: I2c<'static, Async>) -> DS3231> { let mut rtc: DS3231> = DS3231::new(i2c, RTC_ADDRESS); diff --git a/src/feedback.rs b/src/feedback.rs index 7302d79..28dc2b9 100644 --- a/src/feedback.rs +++ b/src/feedback.rs @@ -1,8 +1,7 @@ -use embassy_time::{Delay, Duration, Timer}; -use esp_hal::{delay, gpio::Output, peripherals, rmt::ConstChannelAccess}; +use embassy_time::{Duration, Timer}; +use esp_hal::{peripherals, rmt::ConstChannelAccess}; use esp_hal_smartled::SmartLedsAdapterAsync; -use init::hardware; -use log::{debug, error, info}; +use log::debug; use smart_leds::SmartLedsWriteAsync; use smart_leds::colors::{BLACK, GREEN, RED, YELLOW}; use smart_leds::{brightness, colors::BLUE}; diff --git a/src/init/hardware.rs b/src/init/hardware.rs index b0b9b11..3ffdbc5 100644 --- a/src/init/hardware.rs +++ b/src/init/hardware.rs @@ -1,13 +1,10 @@ use core::cell::RefCell; - -use bleps::att::Att; use critical_section::Mutex; -use ds3231::InterruptControl; use embassy_executor::Spawner; use embassy_net::Stack; - use embassy_time::{Duration, Timer}; -use esp_hal::gpio::{Input, InputConfig}; +use esp_hal::Blocking; +use esp_hal::gpio::Input; use esp_hal::i2c::master::Config; use esp_hal::peripherals::{ GPIO0, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2, @@ -15,8 +12,6 @@ use esp_hal::peripherals::{ }; use esp_hal::rmt::{ConstChannelAccess, Rmt}; use esp_hal::spi::master::{Config as Spi_config, Spi}; - -use esp_hal::Blocking; use esp_hal::time::Rate; use esp_hal::timer::timg::TimerGroup; use esp_hal::{ @@ -28,16 +23,12 @@ use esp_hal::{ uart::Uart, }; use esp_hal_smartled::{SmartLedsAdapterAsync, buffer_size_async}; -use esp_println::dbg; use esp_println::logger::init_logger; -use log::{debug, error, info}; +use log::{debug, error}; -use crate::FEEDBACK_STATE; use crate::init::network; -use crate::init::sd_card::{setup_sdcard, SDCardPersistence}; +use crate::init::sd_card::{SDCardPersistence, setup_sdcard}; use crate::init::wifi; -use crate::store::AttendanceDay; -use crate::store::persistence::Persistence; /************************************************* * GPIO Pinout Xiao Esp32c6 diff --git a/src/init/network.rs b/src/init/network.rs index 7ccff70..378cda4 100644 --- a/src/init/network.rs +++ b/src/init/network.rs @@ -1,5 +1,4 @@ use core::{net::Ipv4Addr, str::FromStr}; - use embassy_executor::Spawner; use embassy_net::{Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4}; use embassy_time::{Duration, Timer}; diff --git a/src/main.rs b/src/main.rs index 375f305..cc033a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,14 +21,14 @@ use esp_hal::{gpio::InputConfig, peripherals}; use log::{debug, info}; use static_cell::make_static; +extern crate alloc; + use crate::{ init::sd_card::SDCardPersistence, store::{Date, IDStore, TallyID}, webserver::start_webserver, }; -extern crate alloc; - mod drivers; mod feedback; mod init; diff --git a/src/store/id_mapping.rs b/src/store/id_mapping.rs index 825df64..d531af2 100644 --- a/src/store/id_mapping.rs +++ b/src/store/id_mapping.rs @@ -1,8 +1,9 @@ -use super::TallyID; use alloc::collections::BTreeMap; use alloc::string::String; use serde::{Deserialize, Serialize}; +use super::TallyID; + #[derive(Clone, Serialize, Deserialize)] pub struct Name { pub first: String, diff --git a/src/store/id_store.rs b/src/store/id_store.rs index 7887254..61ebc4c 100644 --- a/src/store/id_store.rs +++ b/src/store/id_store.rs @@ -1,13 +1,11 @@ -use crate::drivers::rtc; -use crate::drivers::rtc::RTCClock; -use crate::store::persistence::Persistence; +use alloc::vec::Vec; +use serde::Deserialize; +use serde::Serialize; use super::Date; use super::IDMapping; use super::TallyID; -use alloc::vec::Vec; -use serde::Deserialize; -use serde::Serialize; +use crate::store::persistence::Persistence; #[derive(Clone, Serialize, Deserialize, Debug)] pub struct AttendanceDay { @@ -16,7 +14,7 @@ pub struct AttendanceDay { } impl AttendanceDay { - pub fn new(date: Date) -> Self { + pub fn new(date: Date) -> Self { Self { date, ids: Vec::new(), @@ -47,7 +45,7 @@ impl IDStore { // Some(map) => map, // None => IDMapping::new(), // }; - + let current_date: Date = [0; 10]; let day = persistence_layer @@ -75,7 +73,6 @@ impl IDStore { /// Add a new id for the current day /// Returns false if ID is already present at the current day. pub async fn add_id(&mut self, id: TallyID, current_date: Date) -> bool { - if self.current_day.date == current_date { let changed = self.current_day.add_id(id); if changed { diff --git a/src/store/mod.rs b/src/store/mod.rs index 9498aef..66848f0 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -1,11 +1,12 @@ +use heapless::String; + +pub use id_mapping::{IDMapping, Name}; +pub use id_store::{IDStore,AttendanceDay}; + mod id_mapping; pub mod persistence; mod id_store; -use heapless::String; -pub use id_mapping::{IDMapping, Name}; -pub use id_store::{IDStore,AttendanceDay}; - pub type TallyID = [u8; 6]; pub type Date = [u8; 10]; diff --git a/src/webserver/api.rs b/src/webserver/api.rs index 46d12dd..8a49616 100644 --- a/src/webserver/api.rs +++ b/src/webserver/api.rs @@ -4,6 +4,7 @@ use picoserve::{ response::{self, IntoResponse}, }; use serde::Deserialize; + use crate::{ store::{Name, hex_string_to_tally_id}, webserver::{app::AppState, sse::IDEvents}, @@ -40,6 +41,6 @@ pub async fn add_mapping( store.mapping.add_mapping(tally_id, data.name); } -pub async fn get_idevent(State(state): State) -> impl IntoResponse{ +pub async fn get_idevent(State(state): State) -> impl IntoResponse { response::EventStream(IDEvents(state.chan.subscriber().unwrap())) }