1#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
7pub struct Errors {
8 pub safe_start_violation: bool,
10 pub required_channel_invalid: bool,
12 pub serial_error: bool,
14 pub command_timeout: bool,
16 pub limit_kill_switch: bool,
18 pub low_vin: bool,
20 pub high_vin: bool,
22 pub over_temperature: bool,
24 pub motor_driver_error: bool,
26 pub err_line_high: bool,
28}
29
30#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
32pub struct SerialErrors {
33 pub frame: bool,
35 pub noise: bool,
37 pub rx_overrun: bool,
39 pub format: bool,
41 pub crc: bool,
43}
44
45#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
47pub struct Limits {
48 pub motor_not_allowed_to_run: bool,
50 pub temperature_reducing_speed: bool,
52 pub max_speed: bool,
54 pub below_starting_speed: bool,
56 pub speed_limited_acc_dec_brakeduration: bool,
58 pub rc1_killswitch_active: bool,
60 pub rc2_killswitch_active: bool,
62 pub an1_killswitch_active: bool,
64 pub an2_killswitch_active: bool,
66 pub usb_killswitch_active: bool,
68}
69
70#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
72pub enum ResetSource {
73 NRstPulledLow,
75 PowerLow,
77 SoftwareReset,
79 WatchdogTimer,
81 Unknown,
83}
84
85#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
87pub enum BrakeAmount {
88 Coasting,
90 Braking,
92 NotAvailable,
94 Unknown,
96}
97
98
99
100fn is_bit_set(byte: u16, bit: u8) -> bool {
101 byte & (1 << bit) != 0
102}
103
104impl From<u16> for Errors {
105 fn from(response: u16) -> Self {
106 Self {
107 safe_start_violation: is_bit_set(response, 0),
108 required_channel_invalid: is_bit_set(response, 1),
109 serial_error: is_bit_set(response, 2),
110 command_timeout: is_bit_set(response, 3),
111 limit_kill_switch: is_bit_set(response, 4),
112 low_vin: is_bit_set(response, 5),
113 high_vin: is_bit_set(response, 6),
114 over_temperature: is_bit_set(response, 7),
115 motor_driver_error: is_bit_set(response, 0),
116 err_line_high: is_bit_set(response, 1),
117 }
118 }
119}
120
121impl From<u16> for SerialErrors {
122 fn from(response: u16) -> Self {
123 Self {
124 frame: is_bit_set(response, 1),
125 noise: is_bit_set(response, 2),
126 rx_overrun: is_bit_set(response, 3),
127 format: is_bit_set(response, 4),
128 crc: is_bit_set(response, 5),
129 }
130 }
131}
132
133impl From<u16> for Limits {
134 fn from(response: u16) -> Self {
135 Self {
136 motor_not_allowed_to_run: is_bit_set(response, 0),
137 temperature_reducing_speed: is_bit_set(response, 1),
138 max_speed: is_bit_set(response, 2),
139 below_starting_speed: is_bit_set(response, 3),
140 speed_limited_acc_dec_brakeduration: is_bit_set(response, 4),
141 rc1_killswitch_active: is_bit_set(response, 5),
142 rc2_killswitch_active: is_bit_set(response, 6),
143 an1_killswitch_active: is_bit_set(response, 7),
144 an2_killswitch_active: is_bit_set(response, 8),
145 usb_killswitch_active: is_bit_set(response, 9),
146 }
147 }
148}
149
150impl From<u16> for ResetSource {
151 fn from(response: u16) -> Self {
152 match response {
153 0x04 => ResetSource::NRstPulledLow,
154 0x0c => ResetSource::PowerLow,
155 0x14 => ResetSource::SoftwareReset,
156 0x24 => ResetSource::WatchdogTimer,
157 _ => ResetSource::Unknown,
158 }
159 }
160}
161
162impl From<u16> for BrakeAmount {
163 fn from(response: u16) -> Self {
164 match response {
165 0 => BrakeAmount::Coasting,
166 32 => BrakeAmount::Braking,
167 0xff => BrakeAmount::NotAvailable,
168 _ => BrakeAmount::Unknown,
169 }
170 }
171}
172