From da5c8173a56d5be7fa23d2b18eaba1542aa31dd6 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Fri, 28 Feb 2025 14:35:04 +1300 Subject: Implement inhx format inhx is the original Intel hex format. --- src/formats/mod.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/formats/mod.rs') 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, +} + +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") + } +} -- cgit v1.2.3-70-g09d2