#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Errors {
pub safe_start_violation: bool,
pub required_channel_invalid: bool,
pub serial_error: bool,
pub command_timeout: bool,
pub limit_kill_switch: bool,
pub low_vin: bool,
pub high_vin: bool,
pub over_temperature: bool,
pub motor_driver_error: bool,
pub err_line_high: bool,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct SerialErrors {
pub frame: bool,
pub noise: bool,
pub rx_overrun: bool,
pub format: bool,
pub crc: bool,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Limits {
pub motor_not_allowed_to_run: bool,
pub temperature_reducing_speed: bool,
pub max_speed: bool,
pub below_starting_speed: bool,
pub speed_limited_acc_dec_brakeduration: bool,
pub rc1_killswitch_active: bool,
pub rc2_killswitch_active: bool,
pub an1_killswitch_active: bool,
pub an2_killswitch_active: bool,
pub usb_killswitch_active: bool,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ResetSource {
NRstPulledLow,
PowerLow,
SoftwareReset,
WatchdogTimer,
Unknown,
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum BrakeAmount {
Coasting,
Braking,
NotAvailable,
Unknown,
}
fn is_bit_set(byte: u16, bit: u8) -> bool {
byte & (1 << bit) != 0
}
impl From<u16> for Errors {
fn from(response: u16) -> Self {
Self {
safe_start_violation: is_bit_set(response, 0),
required_channel_invalid: is_bit_set(response, 1),
serial_error: is_bit_set(response, 2),
command_timeout: is_bit_set(response, 3),
limit_kill_switch: is_bit_set(response, 4),
low_vin: is_bit_set(response, 5),
high_vin: is_bit_set(response, 6),
over_temperature: is_bit_set(response, 7),
motor_driver_error: is_bit_set(response, 0),
err_line_high: is_bit_set(response, 1),
}
}
}
impl From<u16> for SerialErrors {
fn from(response: u16) -> Self {
Self {
frame: is_bit_set(response, 1),
noise: is_bit_set(response, 2),
rx_overrun: is_bit_set(response, 3),
format: is_bit_set(response, 4),
crc: is_bit_set(response, 5),
}
}
}
impl From<u16> for Limits {
fn from(response: u16) -> Self {
Self {
motor_not_allowed_to_run: is_bit_set(response, 0),
temperature_reducing_speed: is_bit_set(response, 1),
max_speed: is_bit_set(response, 2),
below_starting_speed: is_bit_set(response, 3),
speed_limited_acc_dec_brakeduration: is_bit_set(response, 4),
rc1_killswitch_active: is_bit_set(response, 5),
rc2_killswitch_active: is_bit_set(response, 6),
an1_killswitch_active: is_bit_set(response, 7),
an2_killswitch_active: is_bit_set(response, 8),
usb_killswitch_active: is_bit_set(response, 9),
}
}
}
impl From<u16> for ResetSource {
fn from(response: u16) -> Self {
match response {
0x04 => ResetSource::NRstPulledLow,
0x0c => ResetSource::PowerLow,
0x14 => ResetSource::SoftwareReset,
0x24 => ResetSource::WatchdogTimer,
_ => ResetSource::Unknown,
}
}
}
impl From<u16> for BrakeAmount {
fn from(response: u16) -> Self {
match response {
0 => BrakeAmount::Coasting,
32 => BrakeAmount::Braking,
0xff => BrakeAmount::NotAvailable,
_ => BrakeAmount::Unknown,
}
}
}