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,5 +1,5 @@
use std::fs;
use std::io::Write;
use std::io::{Read, Seek, Write};
use std::path::Path;
use std::fs::File;
use image::GenericImageView;
@@ -75,6 +75,7 @@ fn write_sim_data() {
let gps_output = output_path.join("gps_test_data.msgpack");
let motion_output = output_path.join("motion_test_data.msgpack");
let unified_output = output_path.join("unified.msgpack");
println!("cargo::rerun-if-changed={}", gps_input.to_str().unwrap());
if !gps_output.exists() || gps_output.metadata().unwrap().modified().unwrap() < gps_input.metadata().unwrap().modified().unwrap() {
@@ -85,7 +86,7 @@ fn write_sim_data() {
headers.iter().position(|x| { x == "longitude" }).unwrap(),
headers.iter().position(|x| { x == "latitude" }).unwrap(),
);
let mut gps_output = File::create(gps_output).unwrap();
let mut gps_output = File::create(gps_output.clone()).unwrap();
let mut last_stamp = 0.0;
for record in gps_data.records().flatten() {
let (timestamp, lat, lon) = (
@@ -130,7 +131,7 @@ fn write_sim_data() {
headers.iter().position(|x| { x == "z" }).unwrap(),
);
let mut motion_output = File::create(motion_output).unwrap();
let mut motion_output = File::create(motion_output.clone()).unwrap();
let mut last_stamp = 0.0;
for (accel_record, gyro_record) in accel_data.records().flatten().zip(gyro_data.records().flatten()) {
let (timestamp, accel_x, accel_y, accel_z) = (
@@ -158,6 +159,60 @@ fn write_sim_data() {
}
}
}
// GPS data = 2, motion data = 1
let mut unified_fd = File::create(unified_output.clone()).unwrap();
let mut motion_output = File::open(motion_output).unwrap();
let mut gps_output = File::open(gps_output).unwrap();
rmp::encode::write_ext_meta(&mut unified_fd, motion_output.metadata().unwrap().len() as u32, 1).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();
let mut buf = Vec::new();
gps_output.read_to_end(&mut buf).unwrap();
unified_fd.write_all(buf.as_slice()).unwrap();
let mut partitions = Reader::from_reader(File::open("partitions.csv").unwrap());
let mut data_offset = 0x9000; // Assumes default bootloader size (0x7000) plus partition table (0x2000)
let mut data_size = 0;
let mut found_sim_partition = false;
for p_desc in partitions.records().flatten() {
let offset = parse_partition_number(p_desc.get(3).unwrap().trim());
data_size = parse_partition_number(p_desc.get(4).unwrap().trim()).unwrap();
data_offset = offset.unwrap_or(data_offset);
if let Some("sim") = p_desc.get(0) {
found_sim_partition = true;
break;
} else {
data_offset += data_size;
}
}
if !found_sim_partition {
panic!("Could not find a 'sim' partition in partitions.csv!");
}
if buf.len() >= data_size {
panic!("Simulation data is too big! Cannot fit {:#x} bytes into a partition with a size of {data_size:#x} bytes.", buf.len());
}
let mut data_flash_script = File::create(output_path.join("flash-sim-data.sh")).unwrap();
data_flash_script.write_all(format!("espflash write-bin {data_offset:#x?} {}", unified_output.to_str().unwrap()).as_bytes()).unwrap();
}
fn parse_partition_number(n: &str) -> Option<usize> {
if n.is_empty() {
None
} else if let Some(hex_offset) = n.strip_prefix("0x") {
Some(usize::from_str_radix(hex_offset, 16).unwrap())
} else if let Some(mb_offset) = n.strip_suffix("M") {
Some(mb_offset.parse::<usize>().unwrap() * 1024 * 1024)
} else {
Some(n.parse().unwrap())
}
}
fn linker_be_nice() {