-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadio.cpp
372 lines (299 loc) · 7.21 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
CavyCave - A temperature controlled box for guinea pigs and other
small animals kept outside in winter
Copyright (C) 2020-2021 Flössie <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <EEPROM.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Radio.hpp"
#include "Controller.hpp"
#include "Pins.hpp"
#include "Stats.hpp"
class Radio::Implementation final
{
public:
Implementation(Controller& _controller, Stats& _stats) :
rf24(Pin::CE, Pin::CSN),
controller(_controller),
stats(_stats),
configuration{
110,
{'C', 'C', 'a', 'v', 'e'}
}
{
}
void begin()
{
loadConfiguration();
rf24.begin();
rf24.enableDynamicPayloads();
rf24.setAutoAck(true);
rf24.enableAckPayload();
rf24.setRetries(15, 15);
rf24.setChannel(configuration.channel);
rf24.setPALevel(RF24_PA_MAX);
rf24.setDataRate(RF24_250KBPS);
rf24.setAddressWidth(5);
rf24.setCRCLength(RF24_CRC_16);
rf24.openReadingPipe(1, configuration.address);
rf24.startListening();
}
bool isReady()
{
return rf24.isChipConnected();
}
const Configuration& getConfiguration() const
{
return configuration;
}
void setConfiguration(const Configuration& value)
{
configuration = value;
saveConfiguration();
}
bool run()
{
enum class Command : uint8_t {
POLL,
GET_STATE,
GET_CONFIG,
SET_CONFIG,
SET_AUTO,
SET_FAN,
SET_LOUNGE,
SET_VESTIBULE,
SET_LED,
GET_STATS_MIN_MAX,
GET_STATS_DURATIONS,
RESET_STATS
};
bool again = false;
if (rf24.available() && rf24.getDynamicPayloadSize()) {
char buffer[32];
rf24.read(buffer, min(32, rf24.getDynamicPayloadSize()));
switch (Command(buffer[0])) {
case Command::POLL: {
break;
}
case Command::GET_STATE: {
struct Reply {
Command command;
Controller::State state;
};
const Reply reply = {
Command::GET_STATE,
controller.getState()
};
delay(5);
rf24.flush_tx();
rf24.writeAckPayload(1, &reply, sizeof(reply));
again = true;
break;
}
case Command::GET_CONFIG: {
struct Reply {
Command command;
Controller::Configuration configuration;
};
const Reply reply = {
Command::GET_CONFIG,
controller.getConfiguration()
};
delay(5);
rf24.flush_tx();
rf24.writeAckPayload(1, &reply, sizeof(reply));
again = true;
break;
}
case Command::SET_CONFIG: {
if (rf24.getDynamicPayloadSize() - 1 == sizeof(Controller::Configuration)) {
controller.setConfiguration(*reinterpret_cast<Controller::Configuration*>(buffer + 1));
}
break;
}
case Command::SET_AUTO: {
controller.setAutoMode();
break;
}
case Command::SET_FAN: {
if (rf24.getDynamicPayloadSize() > 1) {
controller.setFanSpeed(Fan::Speed(buffer[1]));
}
break;
}
case Command::SET_LOUNGE: {
if (rf24.getDynamicPayloadSize() > 1) {
controller.setHeatingLounge(buffer[1]);
}
break;
}
case Command::SET_VESTIBULE: {
if (rf24.getDynamicPayloadSize() > 1) {
controller.setHeatingVestibule(buffer[1]);
}
break;
}
case Command::SET_LED: {
if (rf24.getDynamicPayloadSize() > 1) {
controller.setLedColor(Led::Color(buffer[1]));
}
break;
}
case Command::GET_STATS_MIN_MAX: {
struct Reply {
Command command;
int16_t min_room_temperature_10th_c;
int16_t max_room_temperature_10th_c;
int16_t min_floor_temperature_10th_c;
int16_t max_floor_temperature_10th_c;
int16_t min_humidity_per_mill;
int16_t max_humidity_per_mill;
};
const Reply reply = {
Command::GET_STATS_MIN_MAX,
stats.getMinRoomTemperature10thC(),
stats.getMaxRoomTemperature10thC(),
stats.getMinFloorTemperature10thC(),
stats.getMaxFloorTemperature10thC(),
stats.getMinHumidityPerMill(),
stats.getMaxHumidityPerMill()
};
delay(5);
rf24.flush_tx();
rf24.writeAckPayload(1, &reply, sizeof(reply));
again = true;
break;
}
case Command::GET_STATS_DURATIONS: {
struct Reply {
Command command;
uint32_t seconds_since_reset;
uint16_t lounge_heating_count;
uint32_t lounge_heating_seconds;
uint16_t vestibule_heating_count;
uint32_t vestibule_heating_seconds;
uint16_t fan_count;
uint32_t fan_low_seconds;
uint32_t fan_high_seconds;
};
const Reply reply = {
Command::GET_STATS_DURATIONS,
stats.getSecondsSinceReset(),
stats.getLoungeHeatingCount(),
stats.getLoungeHeatingSeconds(),
stats.getVestibuleHeatingCount(),
stats.getVestibuleHeatingSeconds(),
stats.getFanCount(),
stats.getFanLowSeconds(),
stats.getFanHighSeconds()
};
delay(5);
rf24.flush_tx();
rf24.writeAckPayload(1, &reply, sizeof(reply));
again = true;
break;
}
case Command::RESET_STATS: {
stats.reset();
break;
}
}
}
return again;
}
void dump()
{
Serial.println(F("Radio:"));
Serial.print(F(" Ready: "));
if (isReady()) {
Serial.println(F("YES"));
} else {
Serial.println(F("NO"));
}
Serial.print(F(" Carrier: "));
if (rf24.testCarrier()) {
Serial.println(F("YES"));
} else {
Serial.println(F("NO"));
}
Serial.print(F(" RPD: "));
if (rf24.testRPD()) {
Serial.println(F("YES"));
} else {
Serial.println(F("NO"));
}
Serial.print(F(" Channel: "));
Serial.println(configuration.channel);
Serial.print(F(" Address: "));
for (uint8_t i = 0; i < 5; ++i) {
if (i) {
Serial.print(' ');
}
if (configuration.address[i] < 16) {
Serial.print('0');
}
Serial.print(configuration.address[i], HEX);
}
Serial.println();
}
private:
void loadConfiguration()
{
if (EEPROM.read(256) != 0xFF) {
EEPROM.get(257, configuration);
}
}
void saveConfiguration()
{
EEPROM.write(256, 0);
EEPROM.put(257, configuration);
}
RF24 rf24;
Controller& controller;
Stats& stats;
Configuration configuration;
};
Radio::Radio(Controller& _controller, Stats& _stats) :
implementation(new Implementation(_controller, _stats))
{
}
Radio::~Radio()
{
delete implementation;
}
void Radio::begin()
{
implementation->begin();
}
bool Radio::isReady() const
{
return implementation->isReady();
}
const Radio::Configuration& Radio::getConfiguration() const
{
return implementation->getConfiguration();
}
void Radio::setConfiguration(const Configuration& value)
{
implementation->setConfiguration(value);
}
bool Radio::run()
{
return implementation->run();
}
void Radio::dump() const
{
implementation->dump();
}