From 2aaa64374b3444b87e7edf849147344a37583062 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sat, 31 Jan 2026 16:29:11 +0100 Subject: [PATCH] tasks: simulation: split out flash storage api bits into separate mod --- src/lib.rs | 3 + src/simdata.rs | 2 +- src/storage.rs | 122 ++++++++++++++++++++++++++++++++++++++ src/tasks/simulation.rs | 126 +--------------------------------------- 4 files changed, 129 insertions(+), 124 deletions(-) create mode 100644 src/storage.rs diff --git a/src/lib.rs b/src/lib.rs index 965bbe9..59a9a4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,9 @@ pub mod idle; pub mod logging; pub mod graphics; +#[cfg(feature="simulation")] +pub mod storage; + #[cfg(feature="simulation")] pub mod simdata; diff --git a/src/simdata.rs b/src/simdata.rs index 0e3e7d1..3d2f094 100644 --- a/src/simdata.rs +++ b/src/simdata.rs @@ -97,7 +97,7 @@ impl RmpData for StreamHeader { } } - fn write_rmp(&self, writer: &mut Writer) -> Result<(), ValueWriteError> { + fn write_rmp(&self, _writer: &mut Writer) -> Result<(), ValueWriteError> { todo!() } } diff --git a/src/storage.rs b/src/storage.rs new file mode 100644 index 0000000..50f4ba1 --- /dev/null +++ b/src/storage.rs @@ -0,0 +1,122 @@ +use core::{cell::RefCell, fmt::Formatter}; + +use alloc::rc::Rc; +use embedded_storage::{ReadStorage, Storage}; +use log::*; +use rmp::decode::{RmpRead, RmpReadErr}; + +#[derive(Debug)] +pub struct SharedFlash { + storage: Rc> +} + +impl Clone for SharedFlash { + fn clone(&self) -> Self { + Self { + storage: Rc::clone(&self.storage) + } + } +} + +impl SharedFlash { + pub fn new(storage: S) -> Self { + Self { + storage: Rc::new(RefCell::new(storage)) + } + } +} + +impl Storage for SharedFlash { + fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { + self.storage.borrow_mut().write(offset, bytes) + } +} + +impl ReadStorage for SharedFlash { + type Error = S::Error; + + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + self.storage.borrow_mut().read(offset, bytes) + } + + fn capacity(&self) -> usize { + self.storage.borrow().capacity() + } +} + +#[derive(Debug)] +pub enum RangeReadError { + OutOfData, + Storage(E) +} +impl RmpReadErr for RangeReadError {} + +impl core::fmt::Display for RangeReadError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + f.write_str("RmpErr") + } +} + +#[derive(Debug)] +pub struct RangeReader { + storage: S, + start: usize, + end: usize, + offset: usize +} + +impl RangeReader { + pub const fn new(storage: S, start: usize, end: usize) -> Self { + assert!(start <= end); + // TODO: Should add bounds checking since we will know the size of the chunk already + Self { + storage, + start, + end, + offset: 0 + } + } + + pub fn seek(&mut self, offset: usize) -> Result<(), RangeReadError> { + self.offset += offset; + if self.offset > self.end { + Err(RangeReadError::OutOfData) + } else { + Ok(()) + } + } + + pub fn subset(&self, size: usize) -> Result> where S: Clone + core::fmt::Debug { + trace!("subset {:#02x}:{:#02x} -> {:#02x}:{:#02x}", self.start, self.end, self.start + self.offset, self.start + self.offset + size); + if self.start + self.offset + size > self.end { + Err(RangeReadError::OutOfData) + } else { + Ok(Self { + storage: self.storage.clone(), + start: self.offset + self.start, + end: self.start + self.offset + size, + offset: 0 + }) + } + } +} + +impl RmpRead for RangeReader where S::Error: core::fmt::Debug + 'static { + type Error = RangeReadError; + + fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), RangeReadError> { + let pos = self.start + self.offset; + if pos > self.end { + Err(RangeReadError::OutOfData) + } else { + assert!(pos + buf.len() <= self.end); + match self.storage.read(pos as u32, buf) { + Ok(_) => { + self.offset += buf.len(); + Ok(()) + }, + Err(err) => Err(RangeReadError::Storage(err)) + } + } + } +} diff --git a/src/tasks/simulation.rs b/src/tasks/simulation.rs index a20c54b..a8e53ec 100644 --- a/src/tasks/simulation.rs +++ b/src/tasks/simulation.rs @@ -1,56 +1,13 @@ -use core::cell::RefCell; -use core::fmt::Formatter; - -use alloc::rc::Rc; use embassy_sync::channel::DynamicSender; use embassy_time::{Duration, Timer}; -use embedded_storage::{ReadStorage, Storage}; +use embedded_storage::ReadStorage; use esp_bootloader_esp_idf::partitions::PartitionTable; use esp_storage::FlashStorage; use nalgebra::{Vector2, Vector3}; use log::*; -use rmp::decode::{RmpRead, RmpReadErr, ValueReadError}; +use rmp::decode::ValueReadError; -use crate::{Breaker, events::{Measurement, SensorSource, SensorState}, simdata::{AnnotationReading, EventRecord, EventStreamHeader, GPSReading, IMUReading, RmpData, SimDataError, StreamEvent, StreamHeader, StreamIndex, StreamType}}; - -#[derive(Debug)] -pub struct SharedFlash { - storage: Rc> -} - -impl Clone for SharedFlash { - fn clone(&self) -> Self { - Self { - storage: Rc::clone(&self.storage) - } - } -} - -impl SharedFlash { - pub fn new(storage: S) -> Self { - Self { - storage: Rc::new(RefCell::new(storage)) - } - } -} - -impl Storage for SharedFlash { - fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { - self.storage.borrow_mut().write(offset, bytes) - } -} - -impl ReadStorage for SharedFlash { - type Error = S::Error; - - fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { - self.storage.borrow_mut().read(offset, bytes) - } - - fn capacity(&self) -> usize { - self.storage.borrow().capacity() - } -} +use crate::{Breaker, events::{Measurement, SensorSource, SensorState}, simdata::{AnnotationReading, EventRecord, EventStreamHeader, GPSReading, IMUReading, RmpData, SimDataError, StreamEvent, StreamHeader, StreamIndex, StreamType}, storage::{RangeReadError, RangeReader, SharedFlash}}; pub struct SimDataTable { reader: RangeReader, @@ -129,70 +86,6 @@ impl Iterator for SimDataTable whe } } -#[derive(Debug)] -pub struct RangeReader { - storage: S, - start: usize, - end: usize, - offset: usize -} - -impl RangeReader { - pub const fn new(storage: S, start: usize, end: usize) -> Self { - assert!(start <= end); - // TODO: Should add bounds checking since we will know the size of the chunk already - Self { - storage, - start, - end, - offset: 0 - } - } - - pub fn seek(&mut self, offset: usize) -> Result<(), RangeReadError> { - self.offset += offset; - if self.offset > self.end { - Err(RangeReadError::OutOfData) - } else { - Ok(()) - } - } - - pub fn subset(&self, size: usize) -> Result> where S: Clone + core::fmt::Debug { - trace!("subset {:#02x}:{:#02x} -> {:#02x}:{:#02x}", self.start, self.end, self.start + self.offset, self.start + self.offset + size); - if self.start + self.offset + size > self.end { - Err(SimDataError::EndOfStream) - } else { - Ok(Self { - storage: self.storage.clone(), - start: self.offset + self.start, - end: self.start + self.offset + size, - offset: 0 - }) - } - } -} - -impl RmpRead for RangeReader where S::Error: core::fmt::Debug + 'static { - type Error = RangeReadError; - - fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { - let pos = self.start + self.offset; - if pos > self.end { - Err(RangeReadError::OutOfData) - } else { - assert!(pos + buf.len() <= self.end); - match self.storage.read(pos as u32, buf) { - Ok(_) => { - self.offset += buf.len(); - Ok(()) - }, - Err(err) => Err(RangeReadError::Storage(err)) - } - } - } -} - pub struct SimDataReader { reader: RangeReader, srcid: SensorSource, @@ -265,19 +158,6 @@ impl SimDataReader where S::Error: core::fmt::Debug + 'static } } -#[derive(Debug)] -pub enum RangeReadError { - OutOfData, - Storage(E) -} -impl RmpReadErr for RangeReadError {} - -impl core::fmt::Display for RangeReadError { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - f.write_str("RmpErr") - } -} - #[embassy_executor::task(pool_size = 3)] pub async fn simulation_task(mut reader: SimDataReader>, events: DynamicSender<'static, Measurement>) { warn!("Starting simulation for {:?}", reader.srcid());