209 lines
8.9 KiB
Rust
209 lines
8.9 KiB
Rust
use std::fs;
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
use std::fs::File;
|
|
use image::GenericImageView;
|
|
use csv::Reader;
|
|
|
|
fn main() {
|
|
linker_be_nice();
|
|
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
|
|
println!("cargo:rustc-link-arg=-Tlinkall.x");
|
|
|
|
#[cfg(feature="simulation")]
|
|
write_sim_data();
|
|
|
|
#[cfg(feature="oled")]
|
|
compile_assets();
|
|
|
|
}
|
|
|
|
fn compile_assets() {
|
|
let asset_path = Path::new("assets");
|
|
|
|
let mut image_output = File::create(Path::new("target/images.rs")).unwrap();
|
|
for image in fs::read_dir(asset_path).unwrap() {
|
|
let fname = image.unwrap().file_name();
|
|
let fname_str = fname.to_str().unwrap();
|
|
if fname_str.ends_with(".pbm") {
|
|
let img = image::open(asset_path.join(fname_str)).unwrap();
|
|
let img_name = fname_str.rsplit_once('.').unwrap().0.to_uppercase().replace("-", "_");
|
|
|
|
let mut converted_row = Vec::new();
|
|
let mut byte_buf = String::new();
|
|
image_output.write_all(format!("pub const {img_name}: ImageRaw<BinaryColor> = ImageRaw::new(&[\n").as_bytes()).unwrap();
|
|
for (x, _, pixel) in img.pixels() {
|
|
if pixel.0 == [0, 0, 0, 255] {
|
|
byte_buf.push('1');
|
|
} else {
|
|
byte_buf.push('0');
|
|
}
|
|
if byte_buf.len() == 8 {
|
|
converted_row.push(byte_buf);
|
|
byte_buf = String::new();
|
|
}
|
|
if x == img.width() - 1 {
|
|
if !byte_buf.is_empty() {
|
|
byte_buf.push('_');
|
|
for _ in 0..(9 - byte_buf.len()) {
|
|
byte_buf.push('0');
|
|
}
|
|
converted_row.push(byte_buf);
|
|
byte_buf = String::new();
|
|
}
|
|
image_output.write_all(b" ").unwrap();
|
|
for pix in converted_row.iter() {
|
|
image_output.write_all(format!("0b{pix}, ").as_bytes()).unwrap();
|
|
}
|
|
image_output.write_all(b"\n").unwrap();
|
|
converted_row = Vec::new();
|
|
}
|
|
}
|
|
|
|
image_output.write_all(format!("], {});\n", img.width()).as_bytes()).unwrap();
|
|
println!("cargo::rerun-if-changed={fname_str}");
|
|
}
|
|
}
|
|
}
|
|
|
|
fn write_sim_data() {
|
|
let test_data_path = Path::new("test-data");
|
|
let output_path = Path::new("target");
|
|
let gps_input = test_data_path.join("LocationGps.csv");
|
|
let gyro_input = test_data_path.join("GyroscopeUncalibrated.csv");
|
|
let accel_input = test_data_path.join("AccelerometerUncalibrated.csv");
|
|
|
|
let gps_output = output_path.join("gps_test_data.msgpack");
|
|
let motion_output = output_path.join("motion_test_data.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() {
|
|
let mut gps_data = Reader::from_reader(File::open(gps_input).unwrap());
|
|
let headers = gps_data.headers().unwrap();
|
|
let (timestamp_idx, lat_idx, lon_idx) = (
|
|
headers.iter().position(|x| { x == "seconds_elapsed" }).unwrap(),
|
|
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 last_stamp = 0.0;
|
|
for record in gps_data.records().flatten() {
|
|
let (timestamp, lat, lon) = (
|
|
record.get(timestamp_idx).unwrap().parse().unwrap(),
|
|
record.get(lat_idx).unwrap().parse().unwrap(),
|
|
record.get(lon_idx).unwrap().parse().unwrap()
|
|
);
|
|
let next_delay = timestamp - last_stamp;
|
|
last_stamp = timestamp;
|
|
rmp::encode::write_array_len(&mut gps_output, 3).unwrap();
|
|
rmp::encode::write_f64(&mut gps_output, next_delay).unwrap();
|
|
rmp::encode::write_f64(&mut gps_output, lat).unwrap();
|
|
rmp::encode::write_f64(&mut gps_output, lon).unwrap();
|
|
}
|
|
}
|
|
|
|
println!("cargo::rerun-if-changed={}", accel_input.to_str().unwrap());
|
|
println!("cargo::rerun-if-changed={}", gyro_input.to_str().unwrap());
|
|
let rebuild_motion = {
|
|
if motion_output.exists() {
|
|
let motion_stamp = motion_output.metadata().unwrap().modified().unwrap();
|
|
motion_stamp < accel_input.metadata().unwrap().modified().unwrap() || motion_stamp < gyro_input.metadata().unwrap().modified().unwrap()
|
|
} else {
|
|
true
|
|
}
|
|
};
|
|
if rebuild_motion {
|
|
let mut accel_data = Reader::from_reader(File::open(accel_input).unwrap());
|
|
let mut gyro_data = Reader::from_reader(File::open(gyro_input).unwrap());
|
|
let headers = accel_data.headers().unwrap();
|
|
let (timestamp_idx, accel_x_idx, accel_y_idx, accel_z_idx) = (
|
|
headers.iter().position(|x| { x == "seconds_elapsed" }).unwrap(),
|
|
headers.iter().position(|x| { x == "x" }).unwrap(),
|
|
headers.iter().position(|x| { x == "y" }).unwrap(),
|
|
headers.iter().position(|x| { x == "z" }).unwrap(),
|
|
);
|
|
|
|
let headers = gyro_data.headers().unwrap();
|
|
let (gyro_x_idx, gyro_y_idx, gyro_z_idx) = (
|
|
headers.iter().position(|x| { x == "x" }).unwrap(),
|
|
headers.iter().position(|x| { x == "y" }).unwrap(),
|
|
headers.iter().position(|x| { x == "z" }).unwrap(),
|
|
);
|
|
|
|
let mut motion_output = File::create(motion_output).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) = (
|
|
accel_record.get(timestamp_idx).unwrap().parse().unwrap(),
|
|
accel_record.get(accel_x_idx).unwrap().parse().unwrap(),
|
|
accel_record.get(accel_y_idx).unwrap().parse().unwrap(),
|
|
accel_record.get(accel_z_idx).unwrap().parse().unwrap()
|
|
);
|
|
let (gyro_x, gyro_y, gyro_z) = (
|
|
gyro_record.get(gyro_x_idx).unwrap().parse().unwrap(),
|
|
gyro_record.get(gyro_y_idx).unwrap().parse().unwrap(),
|
|
gyro_record.get(gyro_z_idx).unwrap().parse().unwrap()
|
|
);
|
|
let next_delay = timestamp - last_stamp;
|
|
if next_delay >= 0.02 {
|
|
last_stamp = timestamp;
|
|
rmp::encode::write_array_len(&mut motion_output, 7).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, next_delay).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, accel_x).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, accel_y).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, accel_z).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, gyro_x).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, gyro_y).unwrap();
|
|
rmp::encode::write_f64(&mut motion_output, gyro_z).unwrap();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn linker_be_nice() {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
if args.len() > 1 {
|
|
let kind = &args[1];
|
|
let what = &args[2];
|
|
|
|
match kind.as_str() {
|
|
"undefined-symbol" => match what.as_str() {
|
|
"_defmt_timestamp" => {
|
|
eprintln!();
|
|
eprintln!("💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`");
|
|
eprintln!();
|
|
}
|
|
"_stack_start" => {
|
|
eprintln!();
|
|
eprintln!("💡 Is the linker script `linkall.x` missing?");
|
|
eprintln!();
|
|
}
|
|
"esp_wifi_preempt_enable"
|
|
| "esp_wifi_preempt_yield_task"
|
|
| "esp_wifi_preempt_task_create" => {
|
|
eprintln!();
|
|
eprintln!("💡 `esp-wifi` has no scheduler enabled. Make sure you have the `builtin-scheduler` feature enabled, or that you provide an external scheduler.");
|
|
eprintln!();
|
|
}
|
|
"embedded_test_linker_file_not_added_to_rustflags" => {
|
|
eprintln!();
|
|
eprintln!("💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests");
|
|
eprintln!();
|
|
}
|
|
_ => (),
|
|
},
|
|
// we don't have anything helpful for "missing-lib" yet
|
|
_ => {
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
std::process::exit(0);
|
|
}
|
|
|
|
println!(
|
|
"cargo:rustc-link-arg=-Wl,--error-handling-script={}",
|
|
std::env::current_exe().unwrap().display()
|
|
);
|
|
}
|