-
Notifications
You must be signed in to change notification settings - Fork 4
/
radio_node.cpp
184 lines (150 loc) · 4.03 KB
/
radio_node.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "radio_node.h"
#include "lora.h"
uint32_t getFrequencyForChannel(uint8_t channel) {
return RADIO_FREQUENCY_MIN + (RADIO_CHANNEL_WIDTH * channel);
}
uint8_t getNextChannel(uint8_t channel) {
return (channel + RADIO_HOP_OFFSET) % RADIO_CHANNEL_COUNT;
}
uint8_t getPrevChannel(uint8_t channel) {
return (RADIO_CHANNEL_COUNT + channel - RADIO_HOP_OFFSET) % RADIO_CHANNEL_COUNT;
}
RadioNode::RadioNode(void) {
}
void RadioNode::reset(void) {
set(
loraTxPower,
loraBandwidth,
loraSpreadingFactor,
loraCodingRate,
getFrequencyForChannel(getChannel())
);
}
void RadioNode::init(uint8_t ss, uint8_t rst, uint8_t di0, void(*callback)(int)) {
/*
* Setup hardware
*/
LoRa.setPins(
ss,
rst,
di0
);
if (!LoRa.begin(getFrequencyForChannel(getChannel()))) {
while (true);
}
reset();
LoRa.enableCrc();
//Setup ISR callback and start receiving
LoRa.onReceive(callback);
LoRa.receive();
radioState = RADIO_STATE_RX;
}
void RadioNode::readRssi(void)
{
rssi = 164 - constrain(LoRa.packetRssi() * -1, 0, 164);
}
void RadioNode::readSnr(void)
{
snr = (uint8_t) constrain(LoRa.packetSnr(), 0, 255);
}
uint8_t RadioNode::getChannel(void) {
return _channel;
}
uint32_t RadioNode::getChannelEntryMillis(void) {
return _channelEntryMillis;
}
void RadioNode::readAndDecode(
QspConfiguration_t *qsp,
uint8_t bindKey[]
) {
uint8_t tmpBuffer[MAX_PACKET_SIZE];
/*
* There is data to be read from radio!
*/
if (bytesToRead != NO_DATA_TO_READ) {
LoRa.read(tmpBuffer, bytesToRead);
for (int i = 0; i < bytesToRead; i++) {
qspDecodeIncomingFrame(qsp, tmpBuffer[i], bindKey);
}
//After reading, flush radio buffer, we have no need for whatever might be over there
LoRa.sleep();
LoRa.receive();
radioState = RADIO_STATE_RX;
bytesToRead = NO_DATA_TO_READ;
}
}
void RadioNode::hopFrequency(bool forward, uint8_t fromChannel, uint32_t timestamp) {
_channelEntryMillis = timestamp;
if (forward) {
_channel = getNextChannel(fromChannel);
} else {
_channel = getPrevChannel(fromChannel);
}
// And set hardware
LoRa.sleep();
LoRa.setFrequency(
getFrequencyForChannel(_channel)
);
LoRa.idle();
}
bool RadioNode::handleTxDoneState(bool hop) {
uint32_t currentMillis = millis();
if (
currentMillis > nextTxCheckMillis &&
radioState == RADIO_STATE_TX &&
!LoRa.isTransmitting()
) {
/*
* In case of TX module, hop right now
*/
if (hop) {
hopFrequency(true, getChannel(), currentMillis);
}
LoRa.receive();
radioState = RADIO_STATE_RX;
nextTxCheckMillis = currentMillis + 1; //We check of TX done every 1ms
return true;
} else {
return false;
}
}
void RadioNode::handleTx(QspConfiguration_t *qsp, uint8_t bindKey[]) {
if (!canTransmit) {
return;
}
uint8_t size;
uint8_t tmpBuffer[MAX_PACKET_SIZE];
LoRa.beginPacket();
//Prepare packet
qspEncodeFrame(qsp, tmpBuffer, &size, getChannel(), bindKey);
//Sent it to radio in one SPI transaction
LoRa.write(tmpBuffer, size);
LoRa.endPacketAsync();
//Set state to be able to detect the moment when TX is done
radioState = RADIO_STATE_TX;
}
void RadioNode::set(
uint8_t power,
long bandwidth,
uint8_t spreadingFactor,
uint8_t codingRate,
long frequency
) {
LoRa.sleep();
LoRa.setTxPower(power, PA_OUTPUT_PA_BOOST_PIN);
LoRa.setSignalBandwidth(bandwidth);
LoRa.setCodingRate4(codingRate);
LoRa.setFrequency(frequency);
LoRa.idle();
}
void RadioNode::configure(
uint8_t _power,
long _bandwidth,
uint8_t _spreadingFactor,
uint8_t _codingRate
) {
loraTxPower = _power;
loraBandwidth = _bandwidth;
loraSpreadingFactor = _spreadingFactor;
loraCodingRate = _codingRate;
}