use embassy_time::Duration;
use embedded_hal::digital::OutputPin;
use embedded_hal_async::spi::SpiBus;
pub use super::cmd::cmd_radio::*;
use super::{BusyPin, Lr1120, Lr1120Error};
impl<O,SPI, M> Lr1120<O,SPI, M> where
O: OutputPin, SPI: SpiBus<u8>, M: BusyPin
{
pub async fn set_rf(&mut self, freq: u32) -> Result<(), Lr1120Error> {
let req = set_rf_frequency_cmd(freq);
self.cmd_wr(&req).await
}
pub async fn set_packet_type(&mut self, packet_type: PacketType) -> Result<(), Lr1120Error> {
let req = set_packet_type_cmd(packet_type);
self.cmd_wr(&req).await
}
pub async fn set_tx_params(&mut self, tx_power: i8, ramp_time: RampTime) -> Result<(), Lr1120Error> {
let req = set_tx_params_cmd(tx_power, ramp_time);
self.cmd_wr(&req).await
}
pub async fn set_pa(&mut self, pa_sel: PaSel, duty_cycle: u8) -> Result<(), Lr1120Error> {
let pa_supply = if pa_sel==PaSel::HpPa {RegPaSupply::Vbat} else {RegPaSupply::Vreg};
let req = set_pa_config_cmd(pa_sel, pa_supply, duty_cycle, 7);
self.cmd_wr(&req).await
}
pub async fn set_fallback(&mut self, fallback_mode: FallbackMode) -> Result<(), Lr1120Error> {
let req = set_rx_tx_fallback_mode_cmd(fallback_mode);
self.cmd_wr(&req).await
}
pub async fn set_tx(&mut self, tx_timeout: u32) -> Result<(), Lr1120Error> {
let req = set_tx_cmd(tx_timeout);
self.cmd_wr(&req).await
}
pub async fn set_tx_cw(&mut self) -> Result<(), Lr1120Error> {
let req = set_tx_cw_cmd();
self.cmd_wr(&req).await
}
pub async fn set_rx(&mut self, rx_timeout: u32, wait_ready: bool) -> Result<(), Lr1120Error> {
let req = set_rx_cmd(rx_timeout);
self.cmd_wr(&req).await?;
if wait_ready {
self.wait_ready(Duration::from_millis(100)).await?;
}
Ok(())
}
pub async fn set_rx_continous(&mut self) -> Result<(), Lr1120Error> {
self.set_rx(0xFFFFFF,true).await
}
pub async fn set_rx_duty_cycle(&mut self, listen_time: u32, cycle_time: u32, use_lora_cad: bool) -> Result<(), Lr1120Error> {
let req = set_rx_duty_cycle_cmd(listen_time, cycle_time, use_lora_cad);
self.cmd_wr(&req).await
}
pub async fn get_rx_stats(&mut self) -> Result<StatsRsp, Lr1120Error> {
let req = get_stats_req();
let mut rsp = StatsRsp::new();
self.cmd_rd(&req, rsp.as_mut()).await?;
Ok(rsp)
}
pub async fn clear_rx_stats(&mut self) -> Result<(), Lr1120Error> {
self.cmd_wr(&reset_stats_cmd()).await
}
pub async fn get_rx_buffer_status(&mut self) -> Result<RxBufferStatusRsp, Lr1120Error> {
let req = get_rx_buffer_status_req();
let mut rsp = RxBufferStatusRsp::new();
self.cmd_rd(&req, rsp.as_mut()).await?;
Ok(rsp)
}
pub async fn get_rssi_inst(&mut self) -> Result<u8, Lr1120Error> {
let req = get_rssi_inst_req();
let mut rsp = RssiInstRsp::new();
self.cmd_rd(&req, rsp.as_mut()).await?;
Ok(rsp.rssi())
}
pub async fn get_rssi_avg(&mut self, nb_meas: u16) -> Result<u8, Lr1120Error> {
let mut rssi = 0;
for _ in 0..nb_meas {
rssi += self.get_rssi_inst().await? as u16;
}
let avg = (rssi + (nb_meas>>1)) / nb_meas;
Ok(avg as u8)
}
pub async fn set_stop_timeout(&mut self, on_preamble: bool) -> Result<(), Lr1120Error> {
let req = stop_timeout_on_preamble_cmd(on_preamble);
self.cmd_wr(&req).await
}
}