use core::convert::Infallible;
use regiface::FromByteArray;
use crate::{Command, NoParameters};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OperatingModeError {
InvalidValue(u8),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandStatusError {
InvalidValue(u8),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StatusError {
InvalidMode(OperatingModeError),
InvalidCommandStatus(CommandStatusError),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OperatingMode {
StandbyRc = 0x2,
StandbyXosc = 0x3,
FrequencySynthesizer = 0x4,
Receive = 0x5,
Transmit = 0x6,
}
impl TryFrom<u8> for OperatingMode {
type Error = OperatingModeError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x2 => Ok(Self::StandbyRc),
0x3 => Ok(Self::StandbyXosc),
0x4 => Ok(Self::FrequencySynthesizer),
0x5 => Ok(Self::Receive),
0x6 => Ok(Self::Transmit),
invalid => Err(OperatingModeError::InvalidValue(invalid)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CommandStatus {
DataAvailable = 0x2,
Timeout = 0x3,
ProcessingError = 0x4,
ExecutionFailure = 0x5,
TxDone = 0x6,
}
impl TryFrom<u8> for CommandStatus {
type Error = CommandStatusError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x2 => Ok(Self::DataAvailable),
0x3 => Ok(Self::Timeout),
0x4 => Ok(Self::ProcessingError),
0x5 => Ok(Self::ExecutionFailure),
0x6 => Ok(Self::TxDone),
invalid => Err(CommandStatusError::InvalidValue(invalid)),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Status {
pub mode: OperatingMode,
pub cmd_status: CommandStatus,
}
impl FromByteArray for Status {
type Error = StatusError;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
let mode = (bytes[0] >> 4) & 0x7;
let cmd = (bytes[0] >> 1) & 0x7;
Ok(Self {
mode: OperatingMode::try_from(mode).map_err(StatusError::InvalidMode)?,
cmd_status: CommandStatus::try_from(cmd).map_err(StatusError::InvalidCommandStatus)?,
})
}
}
#[derive(Debug, Clone)]
pub struct GetStatus;
impl Command for GetStatus {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = Status;
fn id() -> Self::IdType {
0xC0
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone, Copy)]
pub struct GetRssiInstResponse {
pub status: Status,
pub rssi: u8,
}
impl FromByteArray for GetRssiInstResponse {
type Error = Infallible;
type Array = [u8; 2];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
status: Status::from_bytes([bytes[0]]).unwrap(),
rssi: bytes[1],
})
}
}
#[derive(Debug, Clone)]
pub struct GetRssiInst;
impl Command for GetRssiInst {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = GetRssiInstResponse;
fn id() -> Self::IdType {
0x15
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone, Copy)]
pub struct RxBufferStatus {
pub payload_length: u8,
pub buffer_pointer: u8,
}
impl FromByteArray for RxBufferStatus {
type Error = Infallible;
type Array = [u8; 2];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
payload_length: bytes[0],
buffer_pointer: bytes[1],
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct GetRxBufferStatusResponse {
pub status: Status,
pub buffer_status: RxBufferStatus,
}
impl FromByteArray for GetRxBufferStatusResponse {
type Error = Infallible;
type Array = [u8; 3];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
status: Status::from_bytes([bytes[0]]).unwrap(),
buffer_status: RxBufferStatus::from_bytes([bytes[1], bytes[2]]).unwrap(),
})
}
}
#[derive(Debug, Clone)]
pub struct GetRxBufferStatus;
impl Command for GetRxBufferStatus {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = GetRxBufferStatusResponse;
fn id() -> Self::IdType {
0x13
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone, Copy)]
pub struct PacketStatus {
pub status: [u8; 3],
}
impl FromByteArray for PacketStatus {
type Error = Infallible;
type Array = [u8; 3];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self { status: bytes })
}
}
#[derive(Debug, Clone, Copy)]
pub struct GetPacketStatusResponse {
pub status: Status,
pub packet_status: PacketStatus,
}
impl FromByteArray for GetPacketStatusResponse {
type Error = Infallible;
type Array = [u8; 4];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
status: Status::from_bytes([bytes[0]]).unwrap(),
packet_status: PacketStatus::from_bytes([bytes[1], bytes[2], bytes[3]]).unwrap(),
})
}
}
#[derive(Debug, Clone)]
pub struct GetPacketStatus;
impl Command for GetPacketStatus {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = GetPacketStatusResponse;
fn id() -> Self::IdType {
0x14
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone, Copy)]
pub struct DeviceErrors {
pub rc64k_calib_err: bool,
pub rc13m_calib_err: bool,
pub pll_calib_err: bool,
pub adc_calib_err: bool,
pub img_calib_err: bool,
pub xosc_start_err: bool,
pub pll_lock_err: bool,
pub pa_ramp_err: bool,
}
impl FromByteArray for DeviceErrors {
type Error = Infallible;
type Array = [u8; 2];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
rc64k_calib_err: bytes[1] & 0b1 != 0,
rc13m_calib_err: bytes[1] & 0b10 != 0,
pll_calib_err: bytes[1] & 0b100 != 0,
adc_calib_err: bytes[1] & 0b1000 != 0,
img_calib_err: bytes[1] & 0b1_0000 != 0,
xosc_start_err: bytes[1] & 0b10_0000 != 0,
pll_lock_err: bytes[1] & 0b100_0000 != 0,
pa_ramp_err: bytes[0] & 0b1 != 0,
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct GetDeviceErrorsResponse {
pub status: Status,
pub errors: DeviceErrors,
}
impl FromByteArray for GetDeviceErrorsResponse {
type Error = Infallible;
type Array = [u8; 3];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
status: Status::from_bytes([bytes[0]]).unwrap(),
errors: DeviceErrors::from_bytes([bytes[1], bytes[2]]).unwrap(),
})
}
}
#[derive(Debug, Clone)]
pub struct GetDeviceErrors;
impl Command for GetDeviceErrors {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = GetDeviceErrorsResponse;
fn id() -> Self::IdType {
0x17
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone)]
pub struct ClearDeviceErrors;
impl Command for ClearDeviceErrors {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x07
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone, Copy)]
pub struct Stats {
pub packets_received: u16,
pub packets_crc_error: u16,
pub packets_header_error: u16,
}
impl FromByteArray for Stats {
type Error = Infallible;
type Array = [u8; 6];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
packets_received: u16::from_be_bytes(bytes[0..2].try_into().unwrap()),
packets_crc_error: u16::from_be_bytes(bytes[2..4].try_into().unwrap()),
packets_header_error: u16::from_be_bytes(bytes[4..6].try_into().unwrap()),
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct GetStatsResponse {
pub status: Status,
pub stats: Stats,
}
impl FromByteArray for GetStatsResponse {
type Error = Infallible;
type Array = [u8; 7];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(Self {
status: Status::from_bytes([bytes[0]]).unwrap(),
stats: Stats::from_bytes([bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6]])
.unwrap(),
})
}
}
#[derive(Debug, Clone)]
pub struct GetStats;
impl Command for GetStats {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = GetStatsResponse;
fn id() -> Self::IdType {
0x10
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone)]
pub struct ResetStats;
impl Command for ResetStats {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x00
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}