use core::convert::Infallible;
use crate::commands::status::Status;
use crate::{Command, FromByteArray, NoParameters, ToByteArray};
bitflags::bitflags! {
#[derive(Debug, Clone, Copy)]
pub struct IrqMask: u16 {
const TX_DONE = 1 << 0;
const RX_DONE = 1 << 1;
const PREAMBLE_DETECTED = 1 << 2;
const SYNC_WORD_VALID = 1 << 3;
const HEADER_ERROR = 1 << 4;
const CRC_ERROR = 1 << 5;
const CAD_DONE = 1 << 6;
const CAD_DETECTED = 1 << 7;
const TIMEOUT = 1 << 8;
}
}
impl ToByteArray for IrqMask {
type Error = Infallible;
type Array = [u8; 2];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok(self.bits().to_be_bytes())
}
}
impl FromByteArray for IrqMask {
type Error = Infallible;
type Array = [u8; 2];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(IrqMask::from_bits_truncate(u16::from_be_bytes(bytes)))
}
}
#[derive(Debug, Clone, Copy)]
pub struct DioIrqConfig {
pub irq_mask: IrqMask,
pub dio1_mask: IrqMask,
pub dio2_mask: IrqMask,
pub dio3_mask: IrqMask,
}
impl ToByteArray for DioIrqConfig {
type Error = Infallible;
type Array = [u8; 8];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
let mut bytes = [0u8; 8];
bytes[0..2].copy_from_slice(&self.irq_mask.bits().to_be_bytes());
bytes[2..4].copy_from_slice(&self.dio1_mask.bits().to_be_bytes());
bytes[4..6].copy_from_slice(&self.dio2_mask.bits().to_be_bytes());
bytes[6..8].copy_from_slice(&self.dio3_mask.bits().to_be_bytes());
Ok(bytes)
}
}
#[derive(Debug, Clone)]
pub struct SetDioIrqParams {
pub config: DioIrqConfig,
}
impl Command for SetDioIrqParams {
type IdType = u8;
type CommandParameters = DioIrqConfig;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x08
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.config
}
}
#[derive(Debug, Clone, Copy)]
pub struct GetIrqStatusResponse {
pub status: Status,
pub irq_mask: IrqMask,
}
impl FromByteArray for GetIrqStatusResponse {
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(),
irq_mask: IrqMask::from_bytes([bytes[1], bytes[2]]).unwrap(),
})
}
}
#[derive(Debug, Clone)]
pub struct GetIrqStatus;
impl Command for GetIrqStatus {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = GetIrqStatusResponse;
fn id() -> Self::IdType {
0x12
}
fn invoking_parameters(self) -> Self::CommandParameters {
Self::CommandParameters::default()
}
}
#[derive(Debug, Clone)]
pub struct ClearIrqStatus {
pub irq_mask: IrqMask,
}
impl Command for ClearIrqStatus {
type IdType = u8;
type CommandParameters = IrqMask;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x02
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.irq_mask
}
}
#[derive(Debug, Clone, Copy)]
pub struct RfSwitchConfig {
pub enable: bool,
}
impl ToByteArray for RfSwitchConfig {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self.enable as u8])
}
}
#[derive(Debug, Clone)]
pub struct SetDio2AsRfSwitchCtrl {
pub config: RfSwitchConfig,
}
impl Command for SetDio2AsRfSwitchCtrl {
type IdType = u8;
type CommandParameters = RfSwitchConfig;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x9D
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.config
}
}
#[derive(Debug, Clone, Copy)]
pub enum TcxoVoltage {
V1_6 = 0x00,
V1_7 = 0x01,
V1_8 = 0x02,
V2_2 = 0x03,
V2_4 = 0x04,
V2_7 = 0x05,
V3_0 = 0x06,
V3_3 = 0x07,
}
#[derive(Debug, Clone, Copy)]
pub struct TcxoConfig {
pub voltage: TcxoVoltage,
pub delay: u32,
}
impl ToByteArray for TcxoConfig {
type Error = Infallible;
type Array = [u8; 5];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
let mut bytes = [0u8; 5];
bytes[0] = self.voltage as u8;
bytes[1..5].copy_from_slice(&self.delay.to_be_bytes());
Ok(bytes)
}
}
#[derive(Debug, Clone)]
pub struct SetDio3AsTcxoCtrl {
pub config: TcxoConfig,
}
impl Command for SetDio3AsTcxoCtrl {
type IdType = u8;
type CommandParameters = TcxoConfig;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x97
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.config
}
}