build: add some pixmaps for the oled UI, and generated rust code at build time
This commit is contained in:
52
build.rs
52
build.rs
@@ -1,7 +1,59 @@
|
||||
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<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 linker_be_nice() {
|
||||
|
||||
Reference in New Issue
Block a user