Files
renderbug-bike/build.rs

113 lines
4.4 KiB
Rust

use std::fs;
use std::io::Write;
use std::path::Path;
use std::fs::File;
use image::GenericImageView;
use rmp;
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");
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}");
}
}
/*let test_data_path = Path::new("test-data");
let gps_data = File::open(test_data_path.join("LocationGps.csv")).unwrap();
let accel_data = File::open(test_data_path.join("AccelerometerUncalibrated.csv")).unwrap();
let gyro_data = File::open(test_data_path.join("GyroscopeUncalibrated.csv")).unwrap();
let mut test_data_output = File::create(Path::new("target/test_data.rs")).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()
);
}