Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from plaes/refactor-modem-setup
Browse files Browse the repository at this point in the history
lib: Refactor modem setup code into single function
  • Loading branch information
Ulf Lilleengen authored Oct 22, 2023
2 parents 5654b53 + f2eb88b commit 2f16293
Showing 1 changed file with 30 additions and 48 deletions.
78 changes: 30 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,9 @@ where
tx_boosted_if_possible: bool,
) -> Result<(), RadioError> {
self.rx_continuous = false;
self.radio_kind.ensure_ready(self.radio_mode).await?;
if self.radio_mode != RadioMode::Standby {
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
}
if self.cold_start {
self.do_cold_start().await?;
}
if self.calibrate_image {
self.radio_kind.calibrate_image(mdltn_params.frequency_in_hz).await?;
self.calibrate_image = false;
}

self.prepare_modem(mdltn_params).await?;

self.radio_kind.set_modulation_params(mdltn_params).await?;
self.radio_kind
.set_tx_power_and_ramp_time(output_power, Some(mdltn_params), tx_boosted_if_possible, true)
Expand Down Expand Up @@ -264,18 +255,8 @@ where
}
}

self.radio_kind.ensure_ready(self.radio_mode).await?;
if self.radio_mode != RadioMode::Standby {
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
}
if self.cold_start {
self.do_cold_start().await?;
}
if self.calibrate_image {
self.radio_kind.calibrate_image(mdltn_params.frequency_in_hz).await?;
self.calibrate_image = false;
}
self.prepare_modem(mdltn_params).await?;

self.radio_kind.set_modulation_params(mdltn_params).await?;
self.radio_kind.set_packet_params(rx_pkt_params).await?;
self.radio_kind.set_channel(mdltn_params.frequency_in_hz).await?;
Expand Down Expand Up @@ -353,18 +334,9 @@ where
rx_boosted_if_supported: bool,
) -> Result<(), RadioError> {
self.rx_continuous = false;
self.radio_kind.ensure_ready(self.radio_mode).await?;
if self.radio_mode != RadioMode::Standby {
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
}
if self.cold_start {
self.do_cold_start().await?;
}
if self.calibrate_image {
self.radio_kind.calibrate_image(mdltn_params.frequency_in_hz).await?;
self.calibrate_image = false;
}

self.prepare_modem(mdltn_params).await?;

self.radio_kind.set_modulation_params(mdltn_params).await?;
self.radio_kind.set_channel(mdltn_params.frequency_in_hz).await?;
self.radio_mode = RadioMode::ChannelActivityDetection;
Expand Down Expand Up @@ -410,18 +382,9 @@ where
tx_boosted_if_possible: bool,
) -> Result<(), RadioError> {
self.rx_continuous = false;
self.radio_kind.ensure_ready(self.radio_mode).await?;
if self.radio_mode != RadioMode::Standby {
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
}
if self.cold_start {
self.do_cold_start().await?;
}
if self.calibrate_image {
self.radio_kind.calibrate_image(mdltn_params.frequency_in_hz).await?;
self.calibrate_image = false;
}

self.prepare_modem(mdltn_params).await?;

let tx_pkt_params = self
.radio_kind
.create_packet_params(0, false, 16, false, false, mdltn_params)?;
Expand All @@ -442,6 +405,25 @@ where
self.radio_kind.set_irq_params(Some(self.radio_mode)).await?;
self.radio_kind.set_tx_continuous_wave_mode().await
}

async fn prepare_modem(&mut self, mdltn_params: &ModulationParams) -> Result<(), RadioError> {
self.radio_kind.ensure_ready(self.radio_mode).await?;
if self.radio_mode != RadioMode::Standby {
self.radio_kind.set_standby().await?;
self.radio_mode = RadioMode::Standby;
}

if self.cold_start {
self.do_cold_start().await?;
}

if self.calibrate_image {
self.radio_kind.calibrate_image(mdltn_params.frequency_in_hz).await?;
self.calibrate_image = false;
}

Ok(())
}
}

impl<RK, DLY> AsyncRng for LoRa<RK, DLY>
Expand Down

0 comments on commit 2f16293

Please sign in to comment.