diff --git a/oxy/RustSynth.py b/oxy/RustSynth.py index 3cc18d2..76feecb 100644 --- a/oxy/RustSynth.py +++ b/oxy/RustSynth.py @@ -11,8 +11,8 @@ class RustSynth: decode_data_import: str = "use super::data::{Data,FormatData as fd, ProcessData as pd}; \n" decode_return_type: str = "Vec::" - decode_return_value: str = f" let mut result = {decode_return_type}::new();" - decode_closing: str = " result\n}" + decode_return_value: str = f" let result = vec![" + decode_close: str = " ]; \n result\n}\n" decode_mock: str = """ pub fn decode_mock(_data: &[u8]) -> Vec:: { @@ -66,7 +66,7 @@ def synthesize(self, msg: CANMsg) -> str: generated_lines: list[str] = [] for field in msg.fields: generated_lines.append(self.finalize_line(field.name, field.unit, f"{self.format_data(field, self.parse_decoders(field))}")) - total_list: list[str] = [signature, self.decode_return_value] + generated_lines + [self.decode_closing] + total_list: list[str] = [signature, self.decode_return_value] + generated_lines + [self.decode_close] return "\n".join(total_list) def function_name(self, desc: str) -> str: @@ -76,7 +76,7 @@ def signature(self, desc: str) -> str: return f"pub fn {self.function_name(desc)}(data: &[u8]) -> {self.decode_return_type} {{" def finalize_line(self, topic: str, unit: str, val: str) -> str: - return f" result.push(Data::new({val}, \"{topic}\", \"{unit}\"));" + return f" Data::new({val}, \"{topic}\", \"{unit}\")," def parse_decoders(self, field: CANField) -> str: if isinstance(field.decodings, type(None)): diff --git a/src/data.rs b/src/data.rs index 9c14737..f13f818 100644 --- a/src/data.rs +++ b/src/data.rs @@ -52,11 +52,11 @@ impl ProcessData { /** * Computes the twos complement of the given value. */ - pub fn twos_comp(val: u32, bits: usize) -> f32 { + pub fn twos_comp(val: u32, bits: usize) -> i64 { if (val & (1 << (bits - 1))) != 0 { - (val as f32) - ((1 << bits) as f32) + (val as i64) - ((1 << bits) as i64) } else { - val as f32 + val as i64 } } diff --git a/src/decode_data.rs b/src/decode_data.rs index 0bc8f1b..d38aa85 100644 --- a/src/decode_data.rs +++ b/src/decode_data.rs @@ -6,76 +6,94 @@ pub fn decode_mock(_data: &[u8]) -> Vec:: { result } pub fn decode_accumulator_status(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(fd::high_voltage(pd::big_endian(&data[0..2] as &[u8], 8) as f32), "BMS/Pack/Voltage", "V")); - result.push(Data::new(fd::current(pd::twos_comp(pd::big_endian(&data[2..4] as &[u8], 8) as u32, 16) as f32), "BMS/Pack/Current", "A")); - result.push(Data::new(pd::big_endian(&data[4..6] as &[u8], 8) as f32, "BMS/Pack/Amp-hours", "Ah")); - result.push(Data::new(data[6] as f32, "BMS/Pack/SOC", "%")); - result.push(Data::new(data[7] as f32, "BMS/Pack/Health", "%")); + let result = vec![ + Data::new(fd::high_voltage(pd::big_endian(&data[0..2] as &[u8], 8) as f32), "BMS/Pack/Voltage", "V"), + Data::new(fd::current(pd::twos_comp(pd::big_endian(&data[2..4] as &[u8], 8) as u32, 16) as f32), "BMS/Pack/Current", "A"), + Data::new(pd::big_endian(&data[4..6] as &[u8], 8) as f32, "BMS/Pack/Amp-hours", "Ah"), + Data::new(data[6] as f32, "BMS/Pack/SOC", "%"), + Data::new(data[7] as f32, "BMS/Pack/Health", "%"), + ]; result } + pub fn decode_bms_status(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(data[0] as f32, "BMS/State", "")); - result.push(Data::new(pd::little_endian(&data[1..5] as &[u8], 8) as f32, "BMS/Faults", "")); - result.push(Data::new(pd::twos_comp(data[5] as u32, 8) as f32, "BMS/Temps/Average", "C")); - result.push(Data::new(pd::twos_comp(data[6] as u32, 8) as f32, "BMS/Temps/Internal", "C")); - result.push(Data::new(data[7] as f32, "BMS/Cells/BurningStatus", "")); + let result = vec![ + Data::new(data[0] as f32, "BMS/State", ""), + Data::new(pd::little_endian(&data[1..5] as &[u8], 8) as f32, "BMS/Faults", ""), + Data::new(pd::twos_comp(data[5] as u32, 8) as f32, "BMS/Temps/Average", "C"), + Data::new(pd::twos_comp(data[6] as u32, 8) as f32, "BMS/Temps/Internal", "C"), + Data::new(data[7] as f32, "BMS/Cells/BurningStatus", ""), + ]; result } + pub fn decode_shutdown_control(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(data[0] as f32, "BMS/Shutdown/MPE", "")); + let result = vec![ + Data::new(data[0] as f32, "BMS/Shutdown/MPE", ""), + ]; result } + pub fn decode_cell_data(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(fd::cell_voltage(pd::little_endian(&data[0..2] as &[u8], 8) as f32), "BMS/Cells/Volts/High/Value", "V")); - result.push(Data::new(pd::half(data[2] as u8, 4) as f32, "BMS/Cells/Volts/High/Chip", "")); - result.push(Data::new(pd::half(data[3] as u8, 0) as f32, "BMS/Cells/Volts/High/Cell", "")); - result.push(Data::new(fd::cell_voltage(pd::little_endian(&data[4..6] as &[u8], 8) as f32), "BMS/Cells/Volts/Low/Value", "V")); - result.push(Data::new(pd::half(data[6] as u8, 4) as f32, "BMS/Cells/Volts/Low/Chip", "")); - result.push(Data::new(pd::half(data[7] as u8, 0) as f32, "BMS/Cells/Volts/Low/Cell", "")); - result.push(Data::new(fd::cell_voltage(pd::little_endian(&data[8..10] as &[u8], 8) as f32), "BMS/Cells/Volts/Ave/Value", "V")); + let result = vec![ + Data::new(fd::cell_voltage(pd::little_endian(&data[0..2] as &[u8], 8) as f32), "BMS/Cells/Volts/High/Value", "V"), + Data::new(pd::half(data[2] as u8, 4) as f32, "BMS/Cells/Volts/High/Chip", ""), + Data::new(pd::half(data[3] as u8, 0) as f32, "BMS/Cells/Volts/High/Cell", ""), + Data::new(fd::cell_voltage(pd::little_endian(&data[4..6] as &[u8], 8) as f32), "BMS/Cells/Volts/Low/Value", "V"), + Data::new(pd::half(data[6] as u8, 4) as f32, "BMS/Cells/Volts/Low/Chip", ""), + Data::new(pd::half(data[7] as u8, 0) as f32, "BMS/Cells/Volts/Low/Cell", ""), + Data::new(fd::cell_voltage(pd::little_endian(&data[8..10] as &[u8], 8) as f32), "BMS/Cells/Volts/Ave/Value", "V"), + ]; result } + pub fn decode_cell_temperatures(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(pd::twos_comp(pd::little_endian(&data[0..2] as &[u8], 8) as u32, 16) as f32, "BMS/Cells/Temp/High/Value", "C")); - result.push(Data::new(pd::half(data[2] as u8, 4) as f32, "BMS/Cells/Temp/High/Cell", "")); - result.push(Data::new(pd::half(data[3] as u8, 0) as f32, "BMS/Cells/Temp/High/Chip", "")); - result.push(Data::new(pd::twos_comp(pd::little_endian(&data[4..6] as &[u8], 8) as u32, 16) as f32, "BMS/Cells/Temp/Low/Value", "C")); - result.push(Data::new(pd::half(data[6] as u8, 4) as f32, "BMS/Cells/Temp/Low/Cell", "")); - result.push(Data::new(pd::half(data[7] as u8, 0) as f32, "BMS/Cells/Temp/Low/Chip", "")); - result.push(Data::new(pd::twos_comp(pd::little_endian(&data[8..10] as &[u8], 8) as u32, 16) as f32, "BMS/Cells/Temp/Ave/Value", "C")); + let result = vec![ + Data::new(pd::twos_comp(pd::little_endian(&data[0..2] as &[u8], 8) as u32, 16) as f32, "BMS/Cells/Temp/High/Value", "C"), + Data::new(pd::half(data[2] as u8, 4) as f32, "BMS/Cells/Temp/High/Cell", ""), + Data::new(pd::half(data[3] as u8, 0) as f32, "BMS/Cells/Temp/High/Chip", ""), + Data::new(pd::twos_comp(pd::little_endian(&data[4..6] as &[u8], 8) as u32, 16) as f32, "BMS/Cells/Temp/Low/Value", "C"), + Data::new(pd::half(data[6] as u8, 4) as f32, "BMS/Cells/Temp/Low/Cell", ""), + Data::new(pd::half(data[7] as u8, 0) as f32, "BMS/Cells/Temp/Low/Chip", ""), + Data::new(pd::twos_comp(pd::little_endian(&data[8..10] as &[u8], 8) as u32, 16) as f32, "BMS/Cells/Temp/Ave/Value", "C"), + ]; result } + pub fn decode_segment_temperatures(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(pd::twos_comp(data[0] as u32, 8) as f32, "BMS/Segment/Temp/1", "C")); - result.push(Data::new(pd::twos_comp(data[1] as u32, 8) as f32, "BMS/Segment/Temp/2", "C")); - result.push(Data::new(pd::twos_comp(data[2] as u32, 8) as f32, "BMS/Segment/Temp/3", "C")); - result.push(Data::new(pd::twos_comp(data[3] as u32, 8) as f32, "BMS/Segment/Temp/4", "C")); + let result = vec![ + Data::new(pd::twos_comp(data[0] as u32, 8) as f32, "BMS/Segment/Temp/1", "C"), + Data::new(pd::twos_comp(data[1] as u32, 8) as f32, "BMS/Segment/Temp/2", "C"), + Data::new(pd::twos_comp(data[2] as u32, 8) as f32, "BMS/Segment/Temp/3", "C"), + Data::new(pd::twos_comp(data[3] as u32, 8) as f32, "BMS/Segment/Temp/4", "C"), + ]; result } + pub fn decode_nerduino_acceleromter(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(fd::acceleration(pd::big_endian(&data[0..2] as &[u8], 8) as f32), "MPU/Accel/X", "g")); - result.push(Data::new(fd::acceleration(pd::big_endian(&data[2..4] as &[u8], 8) as f32), "MPU/Accel/Y", "g")); - result.push(Data::new(fd::acceleration(pd::big_endian(&data[4..6] as &[u8], 8) as f32), "MPU/Accel/Z", "g")); + let result = vec![ + Data::new(fd::acceleration(pd::big_endian(&data[0..2] as &[u8], 8) as f32), "MPU/Accel/X", "g"), + Data::new(fd::acceleration(pd::big_endian(&data[2..4] as &[u8], 8) as f32), "MPU/Accel/Y", "g"), + Data::new(fd::acceleration(pd::big_endian(&data[4..6] as &[u8], 8) as f32), "MPU/Accel/Z", "g"), + ]; result } + pub fn decode_mpu_status(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(data[0] as f32, "MPU/State/Mode", "")); - result.push(Data::new(data[1] as f32, "MPU/State/Torque_Limit_Percentage", "")); - result.push(Data::new(data[2] as f32, "MPU/State/Regen_Strength", "")); - result.push(Data::new(data[3] as f32, "MPU/State/Traction_Control", "")); + let result = vec![ + Data::new(data[0] as f32, "MPU/State/Mode", ""), + Data::new(data[1] as f32, "MPU/State/Torque_Limit_Percentage", ""), + Data::new(data[2] as f32, "MPU/State/Regen_Strength", ""), + Data::new(data[3] as f32, "MPU/State/Traction_Control", ""), + ]; result } + pub fn decode_wheel_state(data: &[u8]) -> Vec:: { - let mut result = Vec::::new(); - result.push(Data::new(data[0] as f32, "WHEEL/Buttons/1", "")); - result.push(Data::new(data[1] as f32, "WHEEL/Buttons/2", "")); + let result = vec![ + Data::new(data[0] as f32, "WHEEL/Buttons/1", ""), + Data::new(data[1] as f32, "WHEEL/Buttons/2", ""), + ]; result } +