From 83e4614d10f7b189286cdc0429150ed0610ab82f Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Wed, 24 Dec 2025 09:59:12 +0100 Subject: [PATCH] build: move the int/stream type mapping into a simdata module shared by the build script --- build.rs | 11 ++++++++--- src/events.rs | 23 ++++++++++------------- src/lib.rs | 1 + src/simdata.rs | 29 +++++++++++++++++++++++++++++ src/tasks/simulation.rs | 12 ++++++------ 5 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 src/simdata.rs diff --git a/build.rs b/build.rs index dd137d7..f9a741d 100644 --- a/build.rs +++ b/build.rs @@ -5,6 +5,11 @@ use std::fs::File; use image::GenericImageView; use csv::Reader; +#[path ="src/simdata.rs"] +mod simdata; + +use crate::simdata::StreamType; + fn main() { linker_be_nice(); // make sure linkall.x is the last linker script (otherwise might cause problems with flip-link) @@ -205,17 +210,17 @@ fn write_sim_data() { let mut motion_output = File::open(motion_output).unwrap(); let mut gps_output = File::open(gps_output).unwrap(); let mut annotation_output = File::open(annotation_output).unwrap(); - rmp::encode::write_ext_meta(&mut unified_fd, motion_output.metadata().unwrap().len() as u32, 1).unwrap(); + rmp::encode::write_ext_meta(&mut unified_fd, motion_output.metadata().unwrap().len() as u32, StreamType::IMU.into()).unwrap(); let mut buf = Vec::new(); motion_output.read_to_end(&mut buf).unwrap(); unified_fd.write_all(buf.as_slice()).unwrap(); - rmp::encode::write_ext_meta(&mut unified_fd, gps_output.metadata().unwrap().len() as u32, 2).unwrap(); + rmp::encode::write_ext_meta(&mut unified_fd, gps_output.metadata().unwrap().len() as u32, StreamType::GPS.into()).unwrap(); let mut buf = Vec::new(); gps_output.read_to_end(&mut buf).unwrap(); unified_fd.write_all(buf.as_slice()).unwrap(); - rmp::encode::write_ext_meta(&mut unified_fd, annotation_output.metadata().unwrap().len() as u32, 3).unwrap(); + rmp::encode::write_ext_meta(&mut unified_fd, annotation_output.metadata().unwrap().len() as u32, StreamType::Annotations.into()).unwrap(); let mut buf = Vec::new(); annotation_output.read_to_end(&mut buf).unwrap(); unified_fd.write_all(buf.as_slice()).unwrap(); diff --git a/src/events.rs b/src/events.rs index 8370d77..70d04e1 100644 --- a/src/events.rs +++ b/src/events.rs @@ -5,7 +5,7 @@ use enum_map::Enum; use enumset::EnumSetType; use nalgebra::{Vector2, Vector3}; -use crate::ego::engine::MotionState; +use crate::{ego::engine::MotionState, simdata::StreamType}; #[derive(Clone, Copy, Default, Debug)] pub enum Scene { @@ -91,29 +91,26 @@ pub enum Notification { #[derive(Debug, EnumSetType, Enum)] pub enum SensorSource { // Real hardware - IMU = 1, - GPS = 2, + IMU, + GPS, // Fusion outputs - GravityReference = 100, + GravityReference, ForwardsReference, Location, // Simulated sensors Demo, Simulation, - Annotations = 3 + Annotations } -impl TryFrom for SensorSource { - type Error = (); - - fn try_from(value: i8) -> Result { +impl From for SensorSource { + fn from(value: StreamType) -> Self { match value { - 1 => Ok(SensorSource::IMU), - 2 => Ok(SensorSource::GPS), - 3 => Ok(SensorSource::Annotations), - _ => Err(()) + StreamType::Annotations => Self::Annotations, + StreamType::GPS => Self::GPS, + StreamType::IMU => Self::IMU } } } diff --git a/src/lib.rs b/src/lib.rs index fa24809..53a12b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub mod animation; pub mod idle; pub mod logging; pub mod graphics; +pub mod simdata; extern crate alloc; diff --git a/src/simdata.rs b/src/simdata.rs new file mode 100644 index 0000000..8deb8b0 --- /dev/null +++ b/src/simdata.rs @@ -0,0 +1,29 @@ +#[derive(Debug)] +pub enum StreamType { + IMU, + GPS, + Annotations +} + +impl TryFrom for StreamType { + type Error = (); + + fn try_from(value: i8) -> Result { + match value { + 1 => Ok(StreamType::IMU), + 2 => Ok(StreamType::GPS), + 3 => Ok(StreamType::Annotations), + _ => Err(()) + } + } +} + +impl From for i8 { + fn from(value: StreamType) -> Self { + match value { + StreamType::IMU => 1, + StreamType::GPS => 2, + StreamType::Annotations => 3 + } + } +} \ No newline at end of file diff --git a/src/tasks/simulation.rs b/src/tasks/simulation.rs index a84d6f1..9c2841a 100644 --- a/src/tasks/simulation.rs +++ b/src/tasks/simulation.rs @@ -11,7 +11,7 @@ use nalgebra::{Vector2, Vector3}; use log::*; use rmp::decode::{DecodeStringError, RmpRead, RmpReadErr, ValueReadError}; -use crate::events::{Measurement, SensorSource, SensorState}; +use crate::{events::{Measurement, SensorSource, SensorState}, simdata::StreamType}; #[derive(Debug)] pub struct SharedFlash { @@ -102,8 +102,8 @@ impl Iterator for SimDataTable whe self.index += 1; debug!("Found type={this_type:?}"); match this_type.typeid.try_into() { - Err(()) => error!("Found unknown simulation data chunk {this_type:?}"), - Ok(srcid) => return Some(SimDataReader::open(sensor_reader, srcid)) + Err(_) => error!("Found unknown simulation data chunk {this_type:?}"), + Ok(stream_type) => return Some(SimDataReader::open(sensor_reader, stream_type)) } }, Err(err) => { @@ -205,13 +205,13 @@ impl From>> for S } impl SimDataReader where S::Error: core::fmt::Debug + 'static { - pub fn open(mut reader: RangeReader, srcid: SensorSource) -> Self { - debug!("Opening {srcid:?} sim data chunk"); + pub fn open(mut reader: RangeReader, stream_type: StreamType) -> Self { + debug!("Opening {stream_type:?} sim data chunk"); let event_count = rmp::decode::read_array_len(&mut reader).unwrap() as usize; debug!("Found {event_count} events!"); Self { reader, - srcid, + srcid: stream_type.into(), runtime: Default::default(), event_count, index: 0