From 75130e2d2008796b12af2f446d8466392089d6a9 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 10 Oct 2025 01:58:25 +0200 Subject: [PATCH] implemented missing load & save mapping functions --- src/init/sd_card.rs | 46 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/init/sd_card.rs b/src/init/sd_card.rs index 3d99ba2..6cfcb37 100644 --- a/src/init/sd_card.rs +++ b/src/init/sd_card.rs @@ -1,12 +1,10 @@ -use alloc::{format, vec::Vec}; +use alloc::vec::Vec; use embassy_time::Delay; use embedded_hal_bus::spi::ExclusiveDevice; -use embedded_sdmmc::{ - Directory, SdCard, ShortFileName, TimeSource, Timestamp, VolumeIdx, VolumeManager, -}; +use embedded_sdmmc::{SdCard, ShortFileName, TimeSource, Timestamp, VolumeIdx, VolumeManager}; use esp_hal::{Blocking, gpio::Output, spi::master::Spi}; -use crate::store::{AttendanceDay, day::Day, persistence::Persistence}; +use crate::store::{AttendanceDay, IDMapping, day::Day, persistence::Persistence}; pub struct DummyTimesource; @@ -41,6 +39,8 @@ pub struct SDCardPersistence { } impl SDCardPersistence { + const MAPPING_FILENAME: &'static str = "MAPPING.JS"; + fn generate_filename(day: Day) -> ShortFileName { let basename = day.to_string(); let mut filename: heapless::String<11> = heapless::String::new(); @@ -83,6 +83,7 @@ impl Persistence for SDCardPersistence { let mut file = root_dir .open_file_in_dir(filename, embedded_sdmmc::Mode::ReadWriteCreateOrTruncate) .unwrap(); + file.write(&serde_json::to_vec(data).unwrap()).unwrap(); file.flush().unwrap(); @@ -90,11 +91,42 @@ impl Persistence for SDCardPersistence { } async fn load_mapping(&mut self) -> Option { - todo!() + let mut vol_0 = self.vol_mgr.open_volume(VolumeIdx(0)).unwrap(); + let mut root_dir = vol_0.open_root_dir().unwrap(); + + let file = + root_dir.open_file_in_dir(Self::MAPPING_FILENAME, embedded_sdmmc::Mode::ReadOnly); + + if file.is_err() { + return None; + } + + let mut open_file = file.unwrap(); + + let mut read_buffer: [u8; 1024] = [0; 1024]; + let read = open_file.read(&mut read_buffer).unwrap(); + open_file.close().unwrap(); + + let mapping: IDMapping = serde_json::from_slice(&read_buffer[..read]).unwrap(); + + Some(mapping) } async fn save_mapping(&mut self, data: &crate::store::IDMapping) { - todo!() + let mut vol_0 = self.vol_mgr.open_volume(VolumeIdx(0)).unwrap(); + let mut root_dir = vol_0.open_root_dir().unwrap(); + + let mut file = root_dir + .open_file_in_dir( + Self::MAPPING_FILENAME, + embedded_sdmmc::Mode::ReadWriteCreateOrTruncate, + ) + .unwrap(); + + file.write(&serde_json::to_vec(data).unwrap()).unwrap(); + + file.flush().unwrap(); + file.close().unwrap(); } async fn list_days(&mut self) -> Vec {