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 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();

View File

@@ -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<i8> for SensorSource {
type Error = ();
fn try_from(value: i8) -> Result<Self, Self::Error> {
impl From<StreamType> 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
}
}
}

View File

@@ -8,6 +8,7 @@ pub mod animation;
pub mod idle;
pub mod logging;
pub mod graphics;
pub mod simdata;
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 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<S> {
@@ -102,8 +102,8 @@ impl<S: ReadStorage + Clone + core::fmt::Debug> Iterator for SimDataTable<S> 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<S: ReadStorage> From<DecodeStringError<'_, RangeReadError<S::Error>>> for S
}
impl<S: ReadStorage> SimDataReader<S> where S::Error: core::fmt::Debug + 'static {
pub fn open(mut reader: RangeReader<S>, srcid: SensorSource) -> Self {
debug!("Opening {srcid:?} sim data chunk");
pub fn open(mut reader: RangeReader<S>, 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