lorawan_device/
radio.rs

1pub use lora_modulation::BaseBandModulationParams;
2
3#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct RfConfig {
6    pub frequency: u32,
7    pub bb: BaseBandModulationParams,
8}
9
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub enum RxMode {
13    Continuous,
14    /// Single shot receive. Argument `ms` indicates how many milliseconds of extra buffer time should
15    /// be added to the preamble detection timeout.
16    Single {
17        ms: u32,
18    },
19}
20
21#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22#[derive(Debug, Clone, Copy, PartialEq)]
23pub struct RxConfig {
24    pub rf: RfConfig,
25    pub mode: RxMode,
26}
27
28#[cfg_attr(feature = "defmt", derive(defmt::Format))]
29#[derive(Debug, Clone, Copy, PartialEq)]
30pub struct TxConfig {
31    pub pw: i8,
32    pub rf: RfConfig,
33}
34
35impl TxConfig {
36    pub fn adjust_power(&mut self, max_power: u8, antenna_gain: i8) {
37        self.pw -= antenna_gain;
38        self.pw = core::cmp::min(self.pw, max_power as i8);
39    }
40}
41
42#[cfg_attr(feature = "defmt", derive(defmt::Format))]
43#[derive(Debug, Clone, Copy, PartialEq)]
44pub struct RxQuality {
45    rssi: i16,
46    snr: i8,
47}
48
49impl RxQuality {
50    pub fn new(rssi: i16, snr: i8) -> RxQuality {
51        RxQuality { rssi, snr }
52    }
53
54    pub fn rssi(self) -> i16 {
55        self.rssi
56    }
57    pub fn snr(self) -> i8 {
58        self.snr
59    }
60}
61
62pub(crate) struct RadioBuffer<const N: usize> {
63    packet: [u8; N],
64    pos: usize,
65}
66
67impl<const N: usize> RadioBuffer<N> {
68    pub(crate) fn new() -> Self {
69        Self { packet: [0; N], pos: 0 }
70    }
71
72    pub(crate) fn clear(&mut self) {
73        self.pos = 0;
74    }
75
76    pub(crate) fn set_pos(&mut self, pos: usize) {
77        self.pos = pos;
78    }
79
80    pub(crate) fn extend_from_slice(&mut self, buf: &[u8]) -> Result<(), ()> {
81        if self.pos + buf.len() < self.packet.len() {
82            self.packet[self.pos..self.pos + buf.len()].copy_from_slice(buf);
83            self.pos += buf.len();
84            Ok(())
85        } else {
86            Err(())
87        }
88    }
89
90    /// Provides a mutable slice of the buffer up to the current position.
91    pub(crate) fn as_mut_for_read(&mut self) -> &mut [u8] {
92        &mut self.packet[..self.pos]
93    }
94
95    /// Provides a reference of the buffer up to the current position.
96
97    pub(crate) fn as_ref_for_read(&self) -> &[u8] {
98        &self.packet[..self.pos]
99    }
100}
101
102impl<const N: usize> AsMut<[u8]> for RadioBuffer<N> {
103    fn as_mut(&mut self) -> &mut [u8] {
104        &mut self.packet
105    }
106}
107
108impl<const N: usize> AsRef<[u8]> for RadioBuffer<N> {
109    fn as_ref(&self) -> &[u8] {
110        &self.packet
111    }
112}