-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathradio.cpp
268 lines (240 loc) · 7.84 KB
/
radio.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "radio.h"
/*
* Package structure:
* 0 – 7: Preamble (see constant above)
* 8 – 10: Remote ID
* 11 – 11: Separator (0xFF)
* 12 – 12: Sequence counter
* 13 – 14: Command ID + options
* 15 – 16: CRC16 checksum
*/
Radio::Radio(uint8_t ce, uint8_t csn)
{
this->radio = RF24(ce, csn);
}
Radio::~Radio()
{
this->radio.stopListening();
this->radio.powerDown();
}
bool Radio::addRemote(Remote *remote)
{
if (this->num_remotes >= constants::MAX_REMOTES)
{
Serial.println("[Radio] Could not add remote, because too many remotes are saved!");
Serial.println("[Radio] Please check if you actually want to save more than " + String(constants::MAX_REMOTES, DEC) + " remotes.");
Serial.println("[Radio] If you do, increase MAX_REMOTES in constants.h and recompile.");
return false;
}
if (this->num_package_ids >= constants::MAX_SERIALS)
{
Serial.println("[Radio] Could not add remote, because too many serials are saved!");
Serial.println("[Radio] Please check if you actually want to save more than " + String(constants::MAX_SERIALS, DEC) + " serials.");
Serial.println("[Radio] If you do, increase MAX_SERIALS in constants.h and recompile.");
return false;
}
this->remotes[this->num_remotes] = remote;
this->num_remotes++;
this->package_ids[this->num_package_ids].serial = remote->getSerial();
this->package_ids[this->num_package_ids].package_id = 0;
this->num_package_ids++;
Serial.print("[Radio] Remote ");
Serial.print(remote->getSerialString());
Serial.println(" added!");
return true;
}
bool Radio::removeRemote(Remote *remote)
{
for (int i = 0; i < this->num_remotes; i++)
{
if (this->remotes[i] == remote)
{
for (int j = 0; j < this->num_package_ids; j++)
{
if (this->package_ids[j].serial == remote->getSerial())
{
for (int k = j; k < this->num_package_ids - 1; k++)
{
this->package_ids[k] = this->package_ids[k + 1];
}
this->num_package_ids--;
break;
}
}
for (int j = i; j < this->num_remotes - 1; j++)
{
this->remotes[j] = this->remotes[j + 1];
}
this->num_remotes--;
return true;
}
}
return false;
}
void Radio::sendCommand(uint32_t serial, byte command, byte options)
{
PackageIdForSerial *package_id = nullptr;
for (int i = 0; i < this->num_package_ids; i++)
{
if (this->package_ids[i].serial == serial)
{
package_id = &this->package_ids[i];
break;
}
}
if (package_id == nullptr)
{
if (this->num_package_ids >= constants::MAX_SERIALS)
{
Serial.println("[Radio] Could not send command, because too many serials are saved!");
Serial.println("[Radio] Please check if you actually want to save more than " + String(constants::MAX_SERIALS, DEC) + " serials.");
Serial.println("[Radio] If you do, increase MAX_SERIALS in constants.h and recompile.");
return;
}
package_id = &this->package_ids[this->num_package_ids];
package_id->serial = serial;
package_id->package_id = 0;
this->num_package_ids++;
}
byte data[17] = {0};
memcpy(data, Radio::preamble, sizeof(Radio::preamble));
data[8] = (serial & 0xFF0000) >> 16;
data[9] = (serial & 0x00FF00) >> 8;
data[10] = serial & 0x0000FF;
data[11] = 0xFF;
data[12] = ++package_id->package_id;
data[13] = command;
data[14] = options;
this->crc.restart();
this->crc.add(data, sizeof(data) - 2);
uint16_t checksum = this->crc.calc();
data[15] = (checksum & 0xFF00) >> 8;
data[16] = checksum & 0x00FF;
Serial.print("[Radio] Sending command: 0x");
for (int i = 0; i < 17; i++)
{
Serial.print(data[i], HEX);
}
Serial.println();
this->radio.stopListening();
for (int i = 0; i < 20; i++)
{
this->radio.write(&data, sizeof(data), true);
delay(10);
}
this->radio.startListening();
}
void Radio::sendCommand(uint32_t serial, byte command)
{
return this->sendCommand(serial, command, 0x0);
}
void Radio::setup()
{
uint retries = 0;
while (!this->radio.begin())
{
Serial.println("[Radio] nRF24 not responding! Is it wired correctly?");
delay(1000);
retries++;
if (retries > 60)
ESP.restart();
}
Serial.println("[Radio] Setting up radio...");
this->radio.failureDetected = false;
this->radio.openReadingPipe(0, Radio::address);
this->radio.setChannel(68);
this->radio.setDataRate(RF24_2MBPS);
this->radio.disableCRC();
this->radio.disableDynamicPayloads();
this->radio.setPayloadSize(17);
this->radio.setAutoAck(false);
this->radio.setRetries(15, 15);
this->radio.openWritingPipe(Radio::address);
this->radio.startListening();
Serial.println("[Radio] done!");
}
void Radio::loop()
{
if (this->radio.failureDetected)
{
Serial.println("[Radio] Failure detected!");
delay(1000);
this->setup();
delay(1000);
}
if (this->radio.available())
this->handlePackage();
}
void Radio::handlePackage()
{
// Read raw data, append a 5 and shift it. See
// https://github.com/lamperez/xiaomi-lightbar-nrf24?tab=readme-ov-file#baseband-packet-format
// on why that is necessary.
byte raw_data[18] = {0};
this->radio.read(&raw_data, sizeof(raw_data));
byte data[17] = {0x5};
for (int i = 0; i < 17; i++)
{
if (i == 0)
data[i] = 0x50 | raw_data[i] >> 5;
else
data[i] = ((raw_data[i - 1] >> 1) & 0x0F) << 4 | ((raw_data[i - 1] & 0x01) << 3) | raw_data[i] >> 5;
}
// Check if preamble matches. Ignore package otherwise.
if (memcmp(data, Radio::preamble, sizeof(Radio::preamble)))
return;
// Make sure the checksum of the package is correct.
this->crc.restart();
this->crc.add(data, sizeof(data) - 2);
uint16_t calculated_checksum = this->crc.calc();
uint16_t package_checksum = data[15] << 8 | data[16];
if (calculated_checksum != package_checksum)
{
Serial.println("[Radio] Ignoring pacakge with wrong checksum!");
return;
}
// Check if package is coming from a observed remote.
Remote *remote = nullptr;
uint32_t serial = data[8] << 16 | data[9] << 8 | data[10];
for (int i = 0; i < this->num_remotes; i++)
{
if (serial == this->remotes[i]->getSerial())
{
remote = this->remotes[i];
break;
}
}
if (remote == nullptr)
{
Serial.print("[Radio] Ignoring package with unknown serial: 0x");
Serial.print(serial, HEX);
Serial.println("");
return;
}
// Make sure the same package was not handled before.
uint8_t package_id = data[12];
PackageIdForSerial *package_id_for_serial = nullptr;
for (int i = 0; i < this->num_package_ids; i++)
{
if (this->package_ids[i].serial == serial)
{
package_id_for_serial = &this->package_ids[i];
break;
}
}
if (package_id_for_serial == nullptr)
{
Serial.print("[Radio] Could not find latest package id for serial 0x");
Serial.print(serial, HEX);
Serial.println("!");
return;
}
if (package_id <= package_id_for_serial->serial && package_id > package_id_for_serial->serial - 64)
{
Serial.println("[Radio] Ignoring package with too low package number!");
return;
}
package_id_for_serial->package_id = package_id;
Serial.println("[Radio] Package received!");
remote->callback(data[13], data[14]);
}