tasks: simulation: move sim data into a separate partition for flashing

This commit is contained in:
2025-11-05 11:56:10 +01:00
parent 4776227793
commit 41f69833e5
3 changed files with 138 additions and 9 deletions

View File

@@ -1,17 +1,68 @@
use core::cell::RefCell;
use core::fmt::Formatter;
use alloc::rc::Rc;
use alloc::sync::Arc;
use embassy_embedded_hal::flash;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::channel::DynamicSender;
use embassy_sync::mutex::Mutex;
use embassy_time::{Duration, Timer};
use embedded_storage::ReadStorage;
use esp_storage::FlashStorage;
use nalgebra::{Vector2, Vector3};
use log::*;
use rmp::decode::{RmpRead, RmpReadErr};
use crate::events::{Measurement, SensorSource};
use crate::Breaker;
struct OffsetReader<S> {
storage: Rc<RefCell<S>>,
offset: usize
}
#[derive(Debug)]
struct RmpErr{}
impl RmpReadErr for RmpErr {
}
impl core::fmt::Display for RmpErr {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.write_str("RmpErr")
}
}
impl<S: ReadStorage> RmpRead for OffsetReader<S> {
type Error = RmpErr;
fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
match self.storage.borrow_mut().read(self.offset as u32, buf) {
Ok(_) => {
self.offset += buf.len();
Ok(())
},
err => Err(RmpErr{})
}
}
}
#[embassy_executor::task]
pub async fn motion_simulation_task(events: DynamicSender<'static, Measurement>) {
let mut rd = rmp::decode::Bytes::new(include_bytes!("../../target/motion_test_data.msgpack"));
pub async fn motion_simulation_task(storage: Rc<RefCell<FlashStorage>>, offset: usize, events: DynamicSender<'static, Measurement>) {
let mut rd = OffsetReader { storage, offset };
let mut runtime_secs = Breaker::default();
let mut runtime = Duration::default();
events.send(Measurement::SensorOnline(SensorSource::IMU)).await;
loop {
let this_type = rmp::decode::read_ext_meta(&mut rd).unwrap();
if this_type.typeid != 1 {
rd.offset += this_type.size as usize;
} else {
break;
}
}
info!("Found motion data at {:#02x}", rd.offset);
while let Ok(size) = rmp::decode::read_array_len(&mut rd) {
assert_eq!(size, 7, "Expected 7 fields, but only found {size}");
let delay = embassy_time::Duration::from_millis((rmp::decode::read_f64(&mut rd).unwrap() * 1000.0) as u64);
@@ -38,11 +89,21 @@ pub async fn motion_simulation_task(events: DynamicSender<'static, Measurement>)
}
#[embassy_executor::task]
pub async fn location_simulation_task(events: DynamicSender<'static, Measurement>) {
let mut rd = rmp::decode::Bytes::new(include_bytes!("../../target/gps_test_data.msgpack"));
pub async fn location_simulation_task(storage: Rc<RefCell<FlashStorage>>, offset: usize, events: DynamicSender<'static, Measurement>) {
let mut rd = OffsetReader { storage, offset };
let mut runtime_secs = Breaker::default();
let mut runtime = Duration::default();
events.send(Measurement::SensorOnline(SensorSource::GPS)).await;
loop {
let this_type = rmp::decode::read_ext_meta(&mut rd).unwrap();
info!("Found type={this_type:?}");
if this_type.typeid != 2 {
rd.offset += this_type.size as usize;
} else {
break;
}
}
info!("Found GPS data at {:#02x}", rd.offset);
while let Ok(size) = rmp::decode::read_array_len(&mut rd) {
assert_eq!(size, 3, "Expected 3 fields, but only found {size}");
let delay = embassy_time::Duration::from_millis((rmp::decode::read_f64(&mut rd).unwrap() * 1000.0) as u64);