use core::convert::Infallible;
use regiface::FromByteArray;
use crate::{Command, NoParameters, ToByteArray};
#[derive(Debug, Clone, Copy)]
pub struct RfFrequencyConfig {
pub frequency: u32,
}
impl ToByteArray for RfFrequencyConfig {
type Error = Infallible;
type Array = [u8; 4];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
let f = ((self.frequency as u64 * (1_u64 << 25)) / 32_000_000) as u32;
Ok(f.to_be_bytes())
}
}
#[derive(Debug, Clone)]
pub struct SetRfFrequency {
pub config: RfFrequencyConfig,
}
impl Command for SetRfFrequency {
type IdType = u8;
type CommandParameters = RfFrequencyConfig;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x86
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.config
}
}
#[derive(Debug, Clone, Copy)]
pub enum PacketType {
Gfsk = 0x00,
LoRa = 0x01,
}
impl FromByteArray for PacketType {
type Error = Infallible;
type Array = [u8; 1];
fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> {
Ok(match bytes[0] {
0x00 => Self::Gfsk,
0x01 => Self::LoRa,
_ => Self::LoRa,
})
}
}
impl ToByteArray for PacketType {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self as u8])
}
}
#[derive(Debug, Clone)]
pub struct SetPacketType {
pub packet_type: PacketType,
}
impl Command for SetPacketType {
type IdType = u8;
type CommandParameters = PacketType;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x8A
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.packet_type
}
}
#[derive(Debug, Clone)]
pub struct GetPacketType;
impl Command for GetPacketType {
type IdType = u8;
type CommandParameters = NoParameters;
type ResponseParameters = PacketType;
fn id() -> Self::IdType {
0x11
}
fn invoking_parameters(self) -> Self::CommandParameters {
NoParameters::default()
}
}
#[derive(Debug, Clone, Copy)]
pub enum RampTime {
Micros10 = 0x00,
Micros20 = 0x01,
Micros40 = 0x02,
Micros80 = 0x03,
Micros200 = 0x04,
Micros800 = 0x05,
Micros1700 = 0x06,
Micros3400 = 0x07,
}
#[derive(Debug, Clone, Copy)]
pub struct TxParams {
pub power: i8,
pub ramp_time: RampTime,
}
impl ToByteArray for TxParams {
type Error = Infallible;
type Array = [u8; 2];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self.power as u8, self.ramp_time as u8])
}
}
#[derive(Debug, Clone)]
pub struct SetTxParams {
pub params: TxParams,
}
impl Command for SetTxParams {
type IdType = u8;
type CommandParameters = TxParams;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x8E
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.params
}
}
#[derive(Debug, Clone, Copy)]
pub struct GfskModParams {
pub bit_rate: u32,
pub pulse_shape: GfskPulseShape,
pub bandwidth: GfskBandwidth,
pub freq_deviation: u32,
}
#[derive(Debug, Clone, Copy)]
pub enum GfskPulseShape {
NoFilter = 0x00,
Bt03 = 0x08,
Bt05 = 0x09,
Bt07 = 0x0A,
Bt1 = 0x0B,
}
#[derive(Debug, Clone, Copy)]
pub enum GfskBandwidth {
Bw48 = 0x1F,
Bw58 = 0x17,
Bw73 = 0x0F,
Bw97 = 0x1E,
Bw117 = 0x16,
Bw146 = 0x0E,
Bw293 = 0x0D,
Bw39 = 0x1C,
Bw469 = 0x14,
Bw586 = 0x0C,
Bw782 = 0x1B,
Bw938 = 0x13,
Bw1173 = 0x0B,
Bw1562 = 0x1A,
Bw1872 = 0x12,
Bw2323 = 0x0A,
Bw3120 = 0x19,
Bw3736 = 0x11,
Bw4670 = 0x09,
}
#[derive(Debug, Clone, Copy)]
pub struct LoRaModParams {
pub spreading_factor: SpreadingFactor,
pub bandwidth: LoRaBandwidth,
pub coding_rate: CodingRate,
pub low_data_rate_opt: bool,
}
#[derive(Debug, Clone, Copy)]
pub enum SpreadingFactor {
SF5 = 5,
SF6 = 6,
SF7 = 7,
SF8 = 8,
SF9 = 9,
SF10 = 10,
SF11 = 11,
SF12 = 12,
}
#[derive(Debug, Clone, Copy)]
pub enum LoRaBandwidth {
Bw4 = 0x1F,
Bw5 = 0x17,
Bw7 = 0x0F,
Bw9 = 0x1E,
Bw11 = 0x16,
Bw14 = 0x0E,
Bw19 = 0x1D,
Bw23 = 0x15,
Bw29 = 0x0D,
Bw39 = 0x1C,
Bw46 = 0x14,
Bw58 = 0x0C,
Bw78 = 0x1B,
Bw93 = 0x13,
Bw117 = 0x0B,
Bw156 = 0x1A,
Bw187 = 0x12,
Bw234 = 0x0A,
Bw312 = 0x19,
Bw373 = 0x11,
Bw467 = 0x09,
}
#[derive(Debug, Clone, Copy)]
pub enum CodingRate {
Cr45 = 0x01,
Cr46 = 0x02,
Cr47 = 0x03,
Cr48 = 0x04,
}
#[derive(Debug, Clone)]
pub enum ModulationParams {
Gfsk(GfskModParams),
LoRa(LoRaModParams),
}
impl ToByteArray for ModulationParams {
type Error = Infallible;
type Array = [u8; 8];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
let mut bytes = [0u8; 8];
match self {
ModulationParams::Gfsk(params) => {
let br_val = (32 * 32_000_000) / params.bit_rate;
bytes[0..3].copy_from_slice(&br_val.to_be_bytes()[1..]);
bytes[3] = params.pulse_shape as u8;
bytes[4] = params.bandwidth as u8;
let fdev = ((params.freq_deviation as u64 * (1_u64 << 25)) / 32_000_000) as u32;
bytes[5..8].copy_from_slice(&fdev.to_be_bytes()[1..]);
}
ModulationParams::LoRa(params) => {
bytes[0] = params.spreading_factor as u8;
bytes[1] = params.bandwidth as u8;
bytes[2] = params.coding_rate as u8;
bytes[3] = params.low_data_rate_opt as u8;
}
}
Ok(bytes)
}
}
#[derive(Debug, Clone)]
pub struct SetModulationParams {
pub params: ModulationParams,
}
impl Command for SetModulationParams {
type IdType = u8;
type CommandParameters = ModulationParams;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x8B
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.params
}
}
#[derive(Debug, Clone)]
pub struct PacketParams {
pub params: [u8; 9],
}
impl ToByteArray for PacketParams {
type Error = Infallible;
type Array = [u8; 9];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok(self.params)
}
}
#[derive(Debug, Clone)]
pub struct SetPacketParams {
pub params: PacketParams,
}
impl Command for SetPacketParams {
type IdType = u8;
type CommandParameters = PacketParams;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x8C
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.params
}
}
#[derive(Debug, Clone, Copy)]
pub struct CadParams {
pub cad_symbol_num: u8,
pub cad_detect_peak: u8,
pub cad_detect_min: u8,
pub cad_exit_mode: u8,
pub cad_timeout: u32,
}
impl ToByteArray for CadParams {
type Error = Infallible;
type Array = [u8; 8];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
let mut bytes = [0u8; 8];
bytes[0] = self.cad_symbol_num;
bytes[1] = self.cad_detect_peak;
bytes[2] = self.cad_detect_min;
bytes[3] = self.cad_exit_mode;
bytes[4..8].copy_from_slice(&self.cad_timeout.to_be_bytes());
Ok(bytes)
}
}
#[derive(Debug, Clone)]
pub struct SetCadParams {
pub params: CadParams,
}
impl Command for SetCadParams {
type IdType = u8;
type CommandParameters = CadParams;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x88
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.params
}
}
#[derive(Debug, Clone, Copy)]
pub struct BufferBaseAddressConfig {
pub tx_base_addr: u8,
pub rx_base_addr: u8,
}
impl ToByteArray for BufferBaseAddressConfig {
type Error = Infallible;
type Array = [u8; 2];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self.tx_base_addr, self.rx_base_addr])
}
}
#[derive(Debug, Clone)]
pub struct SetBufferBaseAddress {
pub config: BufferBaseAddressConfig,
}
impl Command for SetBufferBaseAddress {
type IdType = u8;
type CommandParameters = BufferBaseAddressConfig;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0x8F
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.config
}
}
#[derive(Debug, Clone, Copy)]
pub struct LoRaSymbNumTimeout {
pub symb_num: u8,
}
impl ToByteArray for LoRaSymbNumTimeout {
type Error = Infallible;
type Array = [u8; 1];
fn to_bytes(self) -> Result<Self::Array, Self::Error> {
Ok([self.symb_num])
}
}
#[derive(Debug, Clone)]
pub struct SetLoRaSymbNumTimeout {
pub config: LoRaSymbNumTimeout,
}
impl Command for SetLoRaSymbNumTimeout {
type IdType = u8;
type CommandParameters = LoRaSymbNumTimeout;
type ResponseParameters = NoParameters;
fn id() -> Self::IdType {
0xA0
}
fn invoking_parameters(self) -> Self::CommandParameters {
self.config
}
}