Skip to content

Commit

Permalink
#20 Cleanup Some Linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyton-McKee committed Jan 11, 2024
1 parent 13decd2 commit 80ac688
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 54 deletions.
8 changes: 4 additions & 4 deletions oxy/RustSynth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Data>"
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::<Data> {
Expand Down Expand Up @@ -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:
Expand All @@ -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)):
Expand Down
6 changes: 3 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
112 changes: 65 additions & 47 deletions src/decode_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,94 @@ pub fn decode_mock(_data: &[u8]) -> Vec::<Data> {
result
}
pub fn decode_accumulator_status(data: &[u8]) -> Vec::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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::<Data> {
let mut result = Vec::<Data>::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
}

0 comments on commit 80ac688

Please sign in to comment.