use ehatrom::*;
fn main() {
println!("📝 Creating EEPROM with custom atoms...");
let mut custom_atoms = Vec::new();
let vendor_atom = VendorInfoAtom::new(
0x4143, 0x0001, 2, "ACME Custom HATs",
"SensorBoard Plus",
[
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF,
], );
let mut pins = [0u8; 28];
pins[4] = 0x01; pins[17] = 0x02; pins[18] = 0x02; pins[22] = 0x01; pins[23] = 0x01; pins[24] = 0x02; pins[25] = 0x02; let gpio_atom = GpioMapAtom {
flags: 0x0000,
pins,
};
let config_str = "MODE=SENSORS,INTERVAL=250,UNITS=METRIC".to_string();
custom_atoms.push((0x81, config_str.into_bytes()));
let mut sensor_cal = Vec::new();
sensor_cal.extend_from_slice(&((-2.5f32).to_be_bytes()));
sensor_cal.extend_from_slice(&(1.03f32.to_be_bytes()));
sensor_cal.extend_from_slice(&(1.2f32.to_be_bytes()));
sensor_cal.extend_from_slice(&(0.98f32.to_be_bytes()));
sensor_cal.extend_from_slice(&(15.0f32.to_be_bytes()));
sensor_cal.extend_from_slice(&(1.0f32.to_be_bytes()));
custom_atoms.push((0x82, sensor_cal));
let hw_info = format!(
"HW_VERSION={}.{}.{},PCB_REV=C,ASSEMBLY_DATE=2024-12-20",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH")
);
custom_atoms.push((0x83, hw_info.into_bytes()));
let mut lookup_table = Vec::new();
for i in 0..32 {
lookup_table.push((i * i) as u8); }
custom_atoms.push((0x84, lookup_table));
#[cfg(feature = "alloc")]
let mut eeprom = Eeprom {
header: EepromHeader::new(),
vendor_info: vendor_atom,
gpio_map_bank0: gpio_atom,
dt_blob: None,
gpio_map_bank1: None,
custom_atoms,
};
#[cfg(not(feature = "alloc"))]
let mut eeprom = {
static CUSTOM_ATOMS: [(u8, &[u8]); 0] = [];
Eeprom {
header: EepromHeader::new(),
vendor_info: vendor_atom,
gpio_map_bank0: gpio_atom,
dt_blob: None,
gpio_map_bank1: None,
custom_atoms: &CUSTOM_ATOMS,
}
};
eeprom.update_header();
#[cfg(feature = "alloc")]
let serialized = eeprom.serialize_with_crc();
#[cfg(not(feature = "alloc"))]
let serialized = {
let mut buffer = [0u8; 1024]; let size = eeprom
.serialize_with_crc_to_slice(&mut buffer)
.expect("Failed to serialize EEPROM");
buffer[..size].to_vec()
};
let filename = "tests/data/custom_atoms.bin";
if std::fs::metadata("tests/data").is_err() {
std::fs::create_dir_all("tests/data").expect("Failed to create tests/data directory");
}
std::fs::write(filename, &serialized).expect("Failed to write custom atoms EEPROM file");
println!("✅ Created {} ({} bytes)", filename, serialized.len());
println!("📊 EEPROM contains:");
println!(" • Standard HAT header");
println!(" • Vendor info atom");
println!(" • GPIO map atom");
println!(" • 4 custom atoms:");
println!(" - 0x81: Configuration string");
println!(" - 0x82: Sensor calibration data");
println!(" - 0x83: Hardware version info");
println!(" - 0x84: Lookup table (32 bytes)");
if Eeprom::verify_crc(&serialized) {
println!("✅ CRC32 verification passed");
} else {
println!("❌ CRC32 verification failed");
}
println!("💡 This demonstrates how to embed custom application-specific data in HAT EEPROM");
}