1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum PacketType {
GFSK = 0x00,
LoRa = 0x01,
}
pub struct PacketParams {
inner: [u8; 9],
}
impl From<PacketParams> for [u8; 9] {
fn from(val: PacketParams) -> Self {
val.inner
}
}
pub use lora::*;
mod lora {
use super::PacketParams;
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum LoRaHeaderType {
/// Variable length packet (explicit header)
VarLen = 0x00,
/// Fixed length packet (implicit header)
FixedLen = 0x01,
}
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum LoRaCrcType {
/// CRC off
CrcOff = 0x00,
/// CRC on
CrcOn = 0x01,
}
#[repr(u8)]
#[derive(Copy, Clone)]
pub enum LoRaInvertIq {
/// Standard IQ setup
Standard = 0x00,
/// Inverted IQ setup
Inverted = 0x01,
}
pub struct LoRaPacketParams {
/// preamble length: number of symbols sent as preamble
/// The preamble length is a 16-bit value which represents
/// the number of LoRa® symbols which will be sent by the radio.
pub preamble_len: u16, // 1, 2
/// Header type. When the byte headerType is at 0x00,
/// the payload length, coding rate and the header
/// CRC will be added to the LoRa® header and transported
/// to the receiver.
pub header_type: LoRaHeaderType, // 3
/// Size of the payload (in bytes) to transmit or maximum size of the
/// payload that the receiver can accept.
pub payload_len: u8, // 4
/// CRC type
pub crc_type: LoRaCrcType, // 5
/// Invert IW
pub invert_iq: LoRaInvertIq,
}
impl From<LoRaPacketParams> for PacketParams {
fn from(val: LoRaPacketParams) -> Self {
let preamble_len = val.preamble_len.to_be_bytes();
PacketParams {
inner: [
preamble_len[0],
preamble_len[1],
val.header_type as u8,
val.payload_len,
val.crc_type as u8,
val.invert_iq as u8,
0x00,
0x00,
0x00,
],
}
}
}
impl Default for LoRaPacketParams {
fn default() -> Self {
Self {
preamble_len: 0x0008,
header_type: LoRaHeaderType::VarLen,
payload_len: 0x00,
crc_type: LoRaCrcType::CrcOff,
invert_iq: LoRaInvertIq::Standard,
}
}
}
impl LoRaPacketParams {
pub fn set_preamble_len(mut self, preamble_len: u16) -> Self {
self.preamble_len = preamble_len;
self
}
pub fn set_header_type(mut self, header_type: LoRaHeaderType) -> Self {
self.header_type = header_type;
self
}
pub fn set_payload_len(mut self, payload_len: u8) -> Self {
self.payload_len = payload_len;
self
}
pub fn set_crc_type(mut self, crc_type: LoRaCrcType) -> Self {
self.crc_type = crc_type;
self
}
pub fn set_invert_iq(mut self, invert_iq: LoRaInvertIq) -> Self {
self.invert_iq = invert_iq;
self
}
}
}