create_simple/
create_simple.rs

1//  _  _       _             _  _
2// | || |  ___| |_ _ __ ___ | || |
3// | || |_/ __| __| '_ ` _ \| || |_
4// |__   _\__ | |_| | | | | |__   _|
5//   |_| |___/\__|_|_|_| |_|  |_|
6//! # ehatrom — EEPROM HAT library for Raspberry Pi HATs
7//! - [Documentation (docs.rs)](https://docs.rs/ehatrom)
8//! - [GitHub](https://github.com/4stm4/ehatrom)
9//!
10use ehatrom::*;
11
12fn main() {
13    println!("📝 Creating minimal EEPROM with basic vendor info...");
14
15    // Create a minimal vendor info atom
16    let vendor_atom = VendorInfoAtom::new(
17        0x5349, // vendor_id (example: "SI" for Simple)
18        0x4D50, // product_id (example: "MP" for MiniProduct)
19        1,      // product_ver
20        "Simple",
21        "MinimalHAT",
22        [
23            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD,
24            0xEE, 0xFF,
25        ], // Simple UUID
26    );
27
28    // Create minimal GPIO map
29    let gpio_atom = GpioMapAtom {
30        flags: 0x0000,
31        pins: [0u8; 28], // All pins unused
32    };
33
34    // Create EEPROM structure
35    #[cfg(feature = "alloc")]
36    let mut eeprom = Eeprom {
37        header: EepromHeader::new(),
38        vendor_info: vendor_atom,
39        gpio_map_bank0: gpio_atom,
40        dt_blob: None,
41        gpio_map_bank1: None,
42        custom_atoms: Vec::new(),
43    };
44
45    #[cfg(not(feature = "alloc"))]
46    let mut eeprom = Eeprom {
47        header: EepromHeader::new(),
48        vendor_info: vendor_atom,
49        gpio_map_bank0: gpio_atom,
50        dt_blob: None,
51        gpio_map_bank1: None,
52        custom_atoms: &[],
53    };
54
55    // Update header with correct counts and length
56    eeprom.update_header();
57
58    // Serialize with CRC
59    #[cfg(feature = "alloc")]
60    let serialized = eeprom.serialize_with_crc();
61
62    #[cfg(not(feature = "alloc"))]
63    let serialized = {
64        let mut buffer = [0u8; 1024]; // Буфер достаточного размера
65        let size = eeprom
66            .serialize_with_crc_to_slice(&mut buffer)
67            .expect("Failed to serialize EEPROM");
68        &buffer[..size]
69    };
70
71    // Create output directory if it doesn't exist
72    if std::fs::metadata("tests/data").is_err() {
73        std::fs::create_dir_all("tests/data").expect("Failed to create tests/data directory");
74    }
75
76    std::fs::write("tests/data/simple.bin", &serialized).expect("Failed to write simple file");
77
78    println!("Created tests/data/simple.bin ({} bytes)", serialized.len());
79
80    // Verify the created file
81    if Eeprom::verify_crc(&serialized) {
82        println!("✅ CRC verification passed");
83    } else {
84        println!("❌ CRC verification failed");
85    }
86}