create_test/
create_test.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    // Create a vendor info atom
14    let vendor_atom = VendorInfoAtom::new(
15        0x4D4F, // vendor_id (example: "MO")
16        0x1234, // product_id
17        1,      // product_ver
18        "TestVendor",
19        "TestProduct",
20        [
21            0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC,
22            0xDE, 0xF0,
23        ], // UUID
24    );
25
26    // Create GPIO map for bank 0
27    let gpio_atom = GpioMapAtom {
28        flags: 0x0000,
29        pins: [0u8; 28], // All pins as inputs
30    };
31
32    // Create EEPROM structure
33    #[cfg(feature = "alloc")]
34    let mut eeprom = Eeprom {
35        header: EepromHeader::new(),
36        vendor_info: vendor_atom,
37        gpio_map_bank0: gpio_atom,
38        dt_blob: None,
39        gpio_map_bank1: None,
40        custom_atoms: Vec::new(),
41    };
42
43    #[cfg(not(feature = "alloc"))]
44    let mut eeprom = Eeprom {
45        header: EepromHeader::new(),
46        vendor_info: vendor_atom,
47        gpio_map_bank0: gpio_atom,
48        dt_blob: None,
49        gpio_map_bank1: None,
50        custom_atoms: &[],
51    };
52
53    // Update header with correct counts and length
54    eeprom.update_header();
55
56    // Serialize with CRC
57    #[cfg(feature = "alloc")]
58    let serialized = eeprom.serialize_with_crc();
59
60    #[cfg(not(feature = "alloc"))]
61    let serialized = {
62        let mut buffer = [0u8; 1024]; // Буфер достаточного размера
63        let size = eeprom
64            .serialize_with_crc_to_slice(&mut buffer)
65            .expect("Failed to serialize EEPROM");
66        &buffer[..size]
67    };
68
69    // Create output directory if it doesn't exist
70    if std::fs::metadata("tests/data").is_err() {
71        std::fs::create_dir_all("tests/data").expect("Failed to create tests/data directory");
72    }
73
74    std::fs::write("tests/data/test.bin", &serialized).expect("Failed to write test file");
75
76    println!("Created tests/data/test.bin ({} bytes)", serialized.len());
77
78    // Verify the created file
79    if Eeprom::verify_crc(&serialized) {
80        println!("✅ CRC verification passed");
81    } else {
82        println!("❌ CRC verification failed");
83    }
84}