summaryrefslogtreecommitdiff
path: root/src/formats/mod.rs
blob: 82f19f179380672cfbcd085452269dfe241581e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
mod inhx;
mod inhx32;

pub use inhx::*;
pub use inhx32::*;


pub struct InhxRecord {
    bytes: Vec<u8>,
}

impl InhxRecord {
    pub fn new() -> Self {
        Self { bytes: Vec::new() }
    }

    pub fn byte(&mut self, byte: u8) {
        self.bytes.push(byte);
    }

    pub fn be_double(&mut self, double: u16) {
        let [high, low] = double.to_be_bytes();
        self.byte(high);
        self.byte(low);
    }

    pub fn le_double(&mut self, double: u16) {
        let [high, low] = double.to_be_bytes();
        self.byte(low);
        self.byte(high);
    }

    pub fn to_string(self) -> String {
        let mut sum: u8 = 0;
        for byte in &self.bytes {
            sum = sum.wrapping_add(*byte);
        }
        let checksum = sum.wrapping_neg();
        let mut output = String::new();
        for byte in &self.bytes {
            output.push_str(&format!("{byte:0>2X}"));
        }
        format!(":{output}{checksum:0>2X}\n")
    }
}