use crate::ants::*;
use crate::ffi;
use nom::{bits, do_parse, named, take_bits, tuple};
use std::mem::transmute;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum KANTSAnt {
Ant1,
Ant2,
Ant3,
Ant4,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum KANTSController {
Primary,
Secondary,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct AntsTelemetry {
pub raw_temp: u16,
pub deploy_status: DeployStatus,
pub uptime: u32,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct DeployStatus {
pub sys_burn_active: bool,
pub sys_ignore_deploy: bool,
pub sys_armed: bool,
pub ant_1_not_deployed: bool,
pub ant_1_stopped_time: bool,
pub ant_1_active: bool,
pub ant_2_not_deployed: bool,
pub ant_2_stopped_time: bool,
pub ant_2_active: bool,
pub ant_3_not_deployed: bool,
pub ant_3_stopped_time: bool,
pub ant_3_active: bool,
pub ant_4_not_deployed: bool,
pub ant_4_stopped_time: bool,
pub ant_4_active: bool,
}
named!(parse_status<&[u8], DeployStatus>,
do_parse!(
bits: bits!(
tuple!(
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1),
take_bits!(u8, 1)
)
) >>
(DeployStatus {
sys_burn_active: bits.3 == 1,
sys_ignore_deploy: bits.15 == 1,
sys_armed: bits.7 == 1,
ant_1_not_deployed: bits.8 == 1,
ant_1_stopped_time: bits.9 == 1,
ant_1_active: bits.10 == 1,
ant_2_not_deployed: bits.12 == 1,
ant_2_stopped_time: bits.13 == 1,
ant_2_active: bits.14 == 1,
ant_3_not_deployed: bits.0 == 1,
ant_3_stopped_time: bits.1 == 1,
ant_3_active: bits.2 == 1,
ant_4_not_deployed: bits.4 == 1,
ant_4_stopped_time: bits.5 == 1,
ant_4_active: bits.6 == 1,
})
)
);
impl AntsTelemetry {
#[doc(hidden)]
pub fn new(c_telem: &ffi::AntsTelemetry) -> Result<AntsTelemetry, AntsError> {
let raw_status: [u8; 2] = unsafe { transmute(c_telem.deploy_status) };
let status = DeployStatus::new(&raw_status)?;
let telem = AntsTelemetry {
raw_temp: c_telem.raw_temp,
deploy_status: status,
uptime: c_telem.uptime,
};
Ok(telem)
}
}
impl DeployStatus {
#[doc(hidden)]
pub fn new(input: &[u8]) -> Result<DeployStatus, AntsError> {
match parse_status(input) {
Ok(v) => {
let (_input, status) = v;
Ok(status)
}
_ => Err(AntsError::GenericError),
}
}
}
pub fn convert_controller(controller: &KANTSController) -> ffi::KANTSController {
match *controller {
self::KANTSController::Primary => ffi::KANTSController::Primary,
self::KANTSController::Secondary => ffi::KANTSController::Secondary,
}
}
pub fn convert_antenna(antenna: &KANTSAnt) -> ffi::KANTSAnt {
match *antenna {
self::KANTSAnt::Ant1 => ffi::KANTSAnt::Ant1,
self::KANTSAnt::Ant2 => ffi::KANTSAnt::Ant2,
self::KANTSAnt::Ant3 => ffi::KANTSAnt::Ant3,
self::KANTSAnt::Ant4 => ffi::KANTSAnt::Ant4,
}
}