build: move the int/stream type mapping into a simdata module shared by the build script

This commit is contained in:
2025-12-24 09:59:12 +01:00
parent 2630c97609
commit 83e4614d10
5 changed files with 54 additions and 22 deletions

View File

@@ -5,6 +5,11 @@ use std::fs::File;
use image::GenericImageView; use image::GenericImageView;
use csv::Reader; use csv::Reader;
#[path ="src/simdata.rs"]
mod simdata;
use crate::simdata::StreamType;
fn main() { fn main() {
linker_be_nice(); linker_be_nice();
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link) // 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 motion_output = File::open(motion_output).unwrap();
let mut gps_output = File::open(gps_output).unwrap(); let mut gps_output = File::open(gps_output).unwrap();
let mut annotation_output = File::open(annotation_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(); let mut buf = Vec::new();
motion_output.read_to_end(&mut buf).unwrap(); motion_output.read_to_end(&mut buf).unwrap();
unified_fd.write_all(buf.as_slice()).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(); let mut buf = Vec::new();
gps_output.read_to_end(&mut buf).unwrap(); gps_output.read_to_end(&mut buf).unwrap();
unified_fd.write_all(buf.as_slice()).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(); let mut buf = Vec::new();
annotation_output.read_to_end(&mut buf).unwrap(); annotation_output.read_to_end(&mut buf).unwrap();
unified_fd.write_all(buf.as_slice()).unwrap(); unified_fd.write_all(buf.as_slice()).unwrap();

View File

@@ -5,7 +5,7 @@ use enum_map::Enum;
use enumset::EnumSetType; use enumset::EnumSetType;
use nalgebra::{Vector2, Vector3}; use nalgebra::{Vector2, Vector3};
use crate::ego::engine::MotionState; use crate::{ego::engine::MotionState, simdata::StreamType};
#[derive(Clone, Copy, Default, Debug)] #[derive(Clone, Copy, Default, Debug)]
pub enum Scene { pub enum Scene {
@@ -91,29 +91,26 @@ pub enum Notification {
#[derive(Debug, EnumSetType, Enum)] #[derive(Debug, EnumSetType, Enum)]
pub enum SensorSource { pub enum SensorSource {
// Real hardware // Real hardware
IMU = 1, IMU,
GPS = 2, GPS,
// Fusion outputs // Fusion outputs
GravityReference = 100, GravityReference,
ForwardsReference, ForwardsReference,
Location, Location,
// Simulated sensors // Simulated sensors
Demo, Demo,
Simulation, Simulation,
Annotations = 3 Annotations
} }
impl TryFrom<i8> for SensorSource { impl From<StreamType> for SensorSource {
type Error = (); fn from(value: StreamType) -> Self {
fn try_from(value: i8) -> Result<Self, Self::Error> {
match value { match value {
1 => Ok(SensorSource::IMU), StreamType::Annotations => Self::Annotations,
2 => Ok(SensorSource::GPS), StreamType::GPS => Self::GPS,
3 => Ok(SensorSource::Annotations), StreamType::IMU => Self::IMU
_ => Err(())
} }
} }
} }

View File

@@ -8,6 +8,7 @@ pub mod animation;
pub mod idle; pub mod idle;
pub mod logging; pub mod logging;
pub mod graphics; pub mod graphics;
pub mod simdata;
extern crate alloc; extern crate alloc;

29
src/simdata.rs Normal file
View File

@@ -0,0 +1,29 @@
#[derive(Debug)]
pub enum StreamType {
IMU,
GPS,
Annotations
}
impl TryFrom<i8> for StreamType {
type Error = ();
fn try_from(value: i8) -> Result<Self, Self::Error> {
match value {
1 => Ok(StreamType::IMU),
2 => Ok(StreamType::GPS),
3 => Ok(StreamType::Annotations),
_ => Err(())
}
}
}
impl From<StreamType> for i8 {
fn from(value: StreamType) -> Self {
match value {
StreamType::IMU => 1,
StreamType::GPS => 2,
StreamType::Annotations => 3
}
}
}

View File

@@ -11,7 +11,7 @@ use nalgebra::{Vector2, Vector3};
use log::*; use log::*;
use rmp::decode::{DecodeStringError, RmpRead, RmpReadErr, ValueReadError}; use rmp::decode::{DecodeStringError, RmpRead, RmpReadErr, ValueReadError};
use crate::events::{Measurement, SensorSource, SensorState}; use crate::{events::{Measurement, SensorSource, SensorState}, simdata::StreamType};
#[derive(Debug)] #[derive(Debug)]
pub struct SharedFlash<S> { pub struct SharedFlash<S> {
@@ -102,8 +102,8 @@ impl<S: ReadStorage + Clone + core::fmt::Debug> Iterator for SimDataTable<S> whe
self.index += 1; self.index += 1;
debug!("Found type={this_type:?}"); debug!("Found type={this_type:?}");
match this_type.typeid.try_into() { match this_type.typeid.try_into() {
Err(()) => error!("Found unknown simulation data chunk {this_type:?}"), Err(_) => error!("Found unknown simulation data chunk {this_type:?}"),
Ok(srcid) => return Some(SimDataReader::open(sensor_reader, srcid)) Ok(stream_type) => return Some(SimDataReader::open(sensor_reader, stream_type))
} }
}, },
Err(err) => { Err(err) => {
@@ -205,13 +205,13 @@ impl<S: ReadStorage> From<DecodeStringError<'_, RangeReadError<S::Error>>> for S
} }
impl<S: ReadStorage> SimDataReader<S> where S::Error: core::fmt::Debug + 'static { impl<S: ReadStorage> SimDataReader<S> where S::Error: core::fmt::Debug + 'static {
pub fn open(mut reader: RangeReader<S>, srcid: SensorSource) -> Self { pub fn open(mut reader: RangeReader<S>, stream_type: StreamType) -> Self {
debug!("Opening {srcid:?} sim data chunk"); debug!("Opening {stream_type:?} sim data chunk");
let event_count = rmp::decode::read_array_len(&mut reader).unwrap() as usize; let event_count = rmp::decode::read_array_len(&mut reader).unwrap() as usize;
debug!("Found {event_count} events!"); debug!("Found {event_count} events!");
Self { Self {
reader, reader,
srcid, srcid: stream_type.into(),
runtime: Default::default(), runtime: Default::default(),
event_count, event_count,
index: 0 index: 0