use embedded_hal::digital::OutputPin;
use embedded_hal_async::spi::SpiBus;
use crate::constants::*;
pub use super::cmd::cmd_ble::*;
use super::{BusyPin, Lr2021, Lr2021Error};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AoaSampling {
Cte1us = 0, Cte2us = 1
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CteKind {
AoA = 0, AoD1us = 1, AoD2us = 2
}
impl From<u8> for CteKind {
fn from(value: u8) -> Self {
match value & 3 {
2 => CteKind::AoD2us,
1 => CteKind::AoD1us,
_ => CteKind::AoA,
}
}
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct CteInfo {
pub nb_sample: u8,
pub kind: CteKind,
}
impl<O,SPI, M> Lr2021<O,SPI, M> where
O: OutputPin, SPI: SpiBus<u8>, M: BusyPin
{
pub async fn set_ble_modulation(&mut self, mode: BleMode) -> Result<(), Lr2021Error> {
let req = set_ble_modulation_params_cmd(mode);
self.cmd_wr(&req).await?;
match mode {
BleMode::Le1mb => {}
BleMode::Le2mb => {
self.cmd_wr(&[0x02,0x30,0x01,0x21,0x00,0x07,0x00]).await?;
},
BleMode::LeCoded500k | BleMode::LeCoded125k => {
self.patch_ble_coded(None).await?;
}
}
Ok(())
}
pub async fn set_ble_params(&mut self, crc_in_fifo: bool, channel_type: ChannelType, whit_init: u8, crc_init: u32, syncword: u32) -> Result<(), Lr2021Error> {
let req = set_ble_channel_params_cmd(crc_in_fifo, channel_type, whit_init, crc_init, syncword);
self.cmd_wr(&req).await
}
pub async fn set_ble_tx(&mut self, len: u8) -> Result<(), Lr2021Error> {
let req = set_ble_tx_cmd(len);
self.cmd_wr(&req).await
}
pub async fn set_ble_tx_pdu_len(&mut self, len: u8) -> Result<(), Lr2021Error> {
let req = set_ble_tx_pdu_len_cmd(len);
self.cmd_wr(&req).await
}
pub async fn get_ble_packet_status(&mut self) -> Result<BlePacketStatusRsp, Lr2021Error> {
let req = get_ble_packet_status_req();
let mut rsp = BlePacketStatusRsp::new();
self.cmd_rd(&req, rsp.as_mut()).await?;
Ok(rsp)
}
pub async fn get_ble_rx_stats(&mut self) -> Result<BleRxStatsRsp, Lr2021Error> {
let req = get_ble_rx_stats_req();
let mut rsp = BleRxStatsRsp::new();
self.cmd_rd(&req, rsp.as_mut()).await?;
Ok(rsp)
}
pub async fn patch_ble_coded(&mut self, ret_en: Option<u8>) -> Result<(), Lr2021Error> {
if let Some(slot) = ret_en {
self.add_register_to_retention(slot,ADDR_CPFSK_DEMOD).await?;
self.add_register_to_retention(slot+1,ADDR_CPFSK_DETECT).await?;
}
self.cmd_wr(&[0x02,0x30,0x01,0x20,0x00,0x09,0x00]).await?;
self.wr_reg_mask(ADDR_CPFSK_DEMOD, 0x0020, 0x00).await?;
self.wr_reg_mask(ADDR_CPFSK_DETECT, 0x007F, 0x7C).await
}
}