#[cfg(test)]
mod tests;
use std::cmp::Ordering;
pub const MIN_QTR_PWM: u16 = 3968u16;
pub const MAX_QTR_PWM: u16 = 8000u16;
const CHANNEL_COUNT: u8 = 6u8;
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[repr(u8)]
pub enum Channel {
#[allow(missing_docs)]
Channel0 = 0x0u8,
#[allow(missing_docs)]
Channel1 = 0x1u8,
#[allow(missing_docs)]
Channel2 = 0x2u8,
#[allow(missing_docs)]
Channel3 = 0x3u8,
#[allow(missing_docs)]
Channel4 = 0x4u8,
#[allow(missing_docs)]
Channel5 = 0x5u8,
}
impl Default for Channel {
fn default() -> Self {
Self::Channel0
}
}
impl Iterator for Channel {
type Item = Channel;
fn next(&mut self) -> Option<Self::Item> {
let channel = *self as u8;
match channel {
_ if channel == CHANNEL_COUNT - 1 => None,
_ => {
let channel = channel + 1;
let channel = unsafe { std::mem::transmute(channel) };
Some(channel)
},
}
}
}
#[derive(Copy, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[repr(u32)]
pub enum Baudrate {
#[allow(missing_docs)]
Baudrate50 = 50u32,
#[allow(missing_docs)]
Baudrate11520 = 115200u32,
}
#[derive(PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[repr(u16)]
pub enum ErrorValues {
SerSignalError = 0u16,
SerOverrunError = 1u16,
SerBufferFull = 2u16,
SerCrcError = 3u16,
SerProtocolError = 4u16,
SerTimeout = 5u16,
ScriptStackError = 6u16,
ScriptCallStackError = 7u16,
ScriptPcError = 8u16,
}
impl ErrorValues {
pub fn from_data(data: u16) -> Vec<ErrorValues> {
const MASK: u16 = 0x0001u16;
let (_, errors) = (0u16..=8u16).into_iter().fold(
(data, vec![]),
|(mut data, mut errors), index| {
let masked_data = data & MASK;
let comparison = masked_data.cmp(&MASK);
if let Ordering::Equal = comparison {
let error = index.into();
errors.push(error);
};
data >>= 1;
(data, errors)
},
);
errors
}
}
impl From<u16> for ErrorValues {
fn from(data: u16) -> Self {
let contained = ((ErrorValues::SerSignalError as u16)
..=(ErrorValues::ScriptPcError as u16))
.contains(&data);
match contained {
true => unsafe { std::mem::transmute(data) },
false => unreachable!(
"The data should always be contained within the above"
),
}
}
}