diff --git a/src/config.rs b/src/config.rs index f89741d..f2dc1a2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,30 +34,22 @@ pub enum ConfigOption { Promiscuous(bool), } -/// Radio configuration errors -/// This should be extended with errors generally relevant to configuration, -/// with radio-specific errors passed through the Other(E) field. -#[non_exhaustive] -#[derive(Clone, Debug, PartialEq)] -pub enum ConfigError { - /// Configuration option not supported - NotSupported, - - /// Other (device, non-configuration errors) - Other(E), +pub trait ConfigError { + /// Indicates a configuration option is not supported + fn not_supported() -> bool; } /// Configure trait implemented by configurable radios pub trait Configure { /// Radio error - type Error; + type Error: ConfigError; /// Set a configuration option /// Returns Ok(true) on set, Ok(false) for unsupported options, Err(Self::Error) for errors - fn set_option(&mut self, o: &ConfigOption) -> Result<(), ConfigError>; + fn set_option(&mut self, o: &ConfigOption) -> Result<(), Self::Error>; /// Fetch a configuration option /// This will overwrite the value of the provided option enum /// Returns Ok(true) on successful get, Ok(false) for unsupported options, Err(Self::Error) for errors - fn get_option(&mut self, o: &mut ConfigOption) -> Result<(), ConfigError>; + fn get_option(&mut self, o: &mut ConfigOption) -> Result<(), Self::Error>; } diff --git a/src/lib.rs b/src/lib.rs index 6854f8f..ffbf63f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ use core::fmt::Debug; pub mod blocking; pub mod config; +pub mod modulation; #[cfg(feature = "helpers")] pub mod helpers; @@ -125,15 +126,15 @@ impl From for BasicChannel { } } -impl Into for BasicChannel { - fn into(self) -> u16 { - self.0 +impl From for u16 { + fn from(ch: BasicChannel) -> u16 { + ch.0 } } /// Channel trait for configuring radio channelization pub trait Channel { - /// Channel information + /// Radio channel type type Channel: Debug; /// Radio error type type Error: Debug; diff --git a/src/modulation/gfsk.rs b/src/modulation/gfsk.rs new file mode 100644 index 0000000..8a29164 --- /dev/null +++ b/src/modulation/gfsk.rs @@ -0,0 +1,15 @@ +//! Common GFSK modulation options + +/// Basic GFSK channel configuration +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +pub struct GfskChannel { + /// Channel frequency in kHz + pub freq_khz: u32, + + /// Channel bandwidth in kHz + pub bw_khz: u16, + + /// Bitrate in bps + pub bitrate_bps: u32, +} diff --git a/src/modulation/lora.rs b/src/modulation/lora.rs new file mode 100644 index 0000000..a0d0a95 --- /dev/null +++ b/src/modulation/lora.rs @@ -0,0 +1,53 @@ +//! Common LoRa modulation options + +/// LoRa mode channel configuration +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] + +pub struct LoRaChannel { + /// LoRa frequency in kHz + pub freq_khz: u32, + /// LoRa channel bandwidth + pub bw_khz: u16, + /// LoRa Spreading Factor + pub sf: SpreadingFactor, + /// LoRa Coding rate + pub cr: CodingRate, +} + +/// Spreading factor for LoRa mode +#[derive(Copy, Clone, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[non_exhaustive] +pub enum SpreadingFactor { + /// LoRa Spreading Factor 5, 32 chips / symbol + Sf5, + /// LoRa Spreading Factor 6, 64 chips / symbol + Sf6, + /// LoRa Spreading Factor 7, 128 chips / symbol + Sf7, + /// LoRa Spreading Factor 8, 256 chips / symbol + Sf8, + /// LoRa Spreading Factor 9, 512 chips / symbol + Sf9, + /// LoRa Spreading Factor 10 1024 chips / symbol + Sf10, + /// LoRa Spreading Factor 11 2048 chips / symbol + Sf11, + /// LoRa Spreading Factor 12 4096 chips / symbol + Sf12, +} + +#[derive(Copy, Clone, PartialEq, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[non_exhaustive] +pub enum CodingRate { + /// LoRa Coding rate 4/5 + Cr4_5, + /// LoRa Coding rate 4/6 + Cr4_6, + /// LoRa Coding rate 4/7 + Cr4_7, + /// LoRa Coding rate 4/8 + Cr4_8, +} diff --git a/src/modulation/mod.rs b/src/modulation/mod.rs new file mode 100644 index 0000000..ecbbdc8 --- /dev/null +++ b/src/modulation/mod.rs @@ -0,0 +1,20 @@ +//! Shared types for radio channel / modulation configuration + +use core::fmt::Debug; + +pub mod gfsk; + +pub mod lora; + +/// Common modulation configuration errors +/// +/// These are provided as a helper for `TryFrom` implementations, +/// and not intended to be prescriptive. +#[derive(Copy, Clone, Debug, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[non_exhaustive] +pub enum ModError { + UnsupportedBitrate, + UnsupportedFrequency, + UnsupportedBandwidth, +}