diff options
Diffstat (limited to 'src/formats/mod.rs')
-rw-r--r-- | src/formats/mod.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/formats/mod.rs b/src/formats/mod.rs index 42d198c..82f19f1 100644 --- a/src/formats/mod.rs +++ b/src/formats/mod.rs @@ -1,2 +1,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") + } +} |