use std::fs; use std::io::Write; use std::path::Path; use std::fs::File; use image::GenericImageView; 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 = 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 linker_be_nice() { let args: Vec = 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() ); }