Skip to content

Commit 5732e5b

Browse files
committed
add shared modulation types for gfsk, lora
1 parent 1172a8a commit 5732e5b

File tree

5 files changed

+99
-18
lines changed

5 files changed

+99
-18
lines changed

src/config.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,22 @@ pub enum ConfigOption {
3434
Promiscuous(bool),
3535
}
3636

37-
/// Radio configuration errors
38-
/// This should be extended with errors generally relevant to configuration,
39-
/// with radio-specific errors passed through the Other(E) field.
40-
#[non_exhaustive]
41-
#[derive(Clone, Debug, PartialEq)]
42-
pub enum ConfigError<E> {
43-
/// Configuration option not supported
44-
NotSupported,
45-
46-
/// Other (device, non-configuration errors)
47-
Other(E),
37+
pub trait ConfigError {
38+
/// Indicates a configuration option is not supported
39+
fn not_supported() -> bool;
4840
}
4941

5042
/// Configure trait implemented by configurable radios
5143
pub trait Configure {
5244
/// Radio error
53-
type Error;
45+
type Error: ConfigError;
5446

5547
/// Set a configuration option
5648
/// Returns Ok(true) on set, Ok(false) for unsupported options, Err(Self::Error) for errors
57-
fn set_option(&mut self, o: &ConfigOption) -> Result<(), ConfigError<Self::Error>>;
49+
fn set_option(&mut self, o: &ConfigOption) -> Result<(), Self::Error>;
5850

5951
/// Fetch a configuration option
6052
/// This will overwrite the value of the provided option enum
6153
/// Returns Ok(true) on successful get, Ok(false) for unsupported options, Err(Self::Error) for errors
62-
fn get_option(&mut self, o: &mut ConfigOption) -> Result<(), ConfigError<Self::Error>>;
54+
fn get_option(&mut self, o: &mut ConfigOption) -> Result<(), Self::Error>;
6355
}

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::fmt::Debug;
1414

1515
pub mod blocking;
1616
pub mod config;
17+
pub mod modulation;
1718

1819
#[cfg(feature = "helpers")]
1920
pub mod helpers;
@@ -125,15 +126,15 @@ impl From<u16> for BasicChannel {
125126
}
126127
}
127128

128-
impl Into<u16> for BasicChannel {
129-
fn into(self) -> u16 {
130-
self.0
129+
impl From<BasicChannel> for u16 {
130+
fn from(ch: BasicChannel) -> u16 {
131+
ch.0
131132
}
132133
}
133134

134135
/// Channel trait for configuring radio channelization
135136
pub trait Channel {
136-
/// Channel information
137+
/// Radio channel type
137138
type Channel: Debug;
138139
/// Radio error type
139140
type Error: Debug;

src/modulation/gfsk.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Common GFSK modulation options
2+
3+
/// Basic GFSK channel configuration
4+
#[derive(Clone, PartialEq, Debug)]
5+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6+
pub struct GfskChannel {
7+
/// Channel frequency in kHz
8+
pub freq_khz: u32,
9+
10+
/// Channel bandwidth in kHz
11+
pub bw_khz: u16,
12+
13+
/// Bitrate in bps
14+
pub bitrate_bps: u32,
15+
}

src/modulation/lora.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Common LoRa modulation options
2+
3+
/// LoRa mode channel configuration
4+
#[derive(Clone, PartialEq, Debug)]
5+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6+
7+
pub struct LoRaChannel {
8+
/// LoRa frequency in kHz
9+
pub freq_khz: u32,
10+
/// LoRa channel bandwidth
11+
pub bw_khz: u16,
12+
/// LoRa Spreading Factor
13+
pub sf: SpreadingFactor,
14+
/// LoRa Coding rate
15+
pub cr: CodingRate,
16+
}
17+
18+
/// Spreading factor for LoRa mode
19+
#[derive(Copy, Clone, PartialEq, Debug)]
20+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
21+
#[non_exhaustive]
22+
pub enum SpreadingFactor {
23+
/// LoRa Spreading Factor 5, 32 chips / symbol
24+
Sf5,
25+
/// LoRa Spreading Factor 6, 64 chips / symbol
26+
Sf6,
27+
/// LoRa Spreading Factor 7, 128 chips / symbol
28+
Sf7,
29+
/// LoRa Spreading Factor 8, 256 chips / symbol
30+
Sf8,
31+
/// LoRa Spreading Factor 9, 512 chips / symbol
32+
Sf9,
33+
/// LoRa Spreading Factor 10 1024 chips / symbol
34+
Sf10,
35+
/// LoRa Spreading Factor 11 2048 chips / symbol
36+
Sf11,
37+
/// LoRa Spreading Factor 12 4096 chips / symbol
38+
Sf12,
39+
}
40+
41+
#[derive(Copy, Clone, PartialEq, Debug)]
42+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
43+
#[non_exhaustive]
44+
pub enum CodingRate {
45+
/// LoRa Coding rate 4/5
46+
Cr4_5,
47+
/// LoRa Coding rate 4/6
48+
Cr4_6,
49+
/// LoRa Coding rate 4/7
50+
Cr4_7,
51+
/// LoRa Coding rate 4/8
52+
Cr4_8,
53+
}

src/modulation/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Shared types for radio channel / modulation configuration
2+
3+
use core::fmt::Debug;
4+
5+
pub mod gfsk;
6+
7+
pub mod lora;
8+
9+
/// Common modulation configuration errors
10+
///
11+
/// These are provided as a helper for `TryFrom` implementations,
12+
/// and not intended to be prescriptive.
13+
#[derive(Copy, Clone, Debug, PartialEq)]
14+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
15+
#[non_exhaustive]
16+
pub enum ModError {
17+
UnsupportedBitrate,
18+
UnsupportedFrequency,
19+
UnsupportedBandwidth,
20+
}

0 commit comments

Comments
 (0)