-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadio.cpp
More file actions
64 lines (54 loc) · 1.46 KB
/
Radio.cpp
File metadata and controls
64 lines (54 loc) · 1.46 KB
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
#include "Settings.h"
#include "Radio.h"
#include "RaceLogic.h"
#include "PowerControl.h"
static Radio s_Radio;
Radio::Radio() : rf(RF_CE_PIN, RF_CSN_PIN) {
}
void Radio::begin() {
rf.begin();
if(rf.isPVariant()) {
rf.setChannel(RF_CHANNEL);
rf.setDataRate(RF_DATARATE);
rf.setPALevel(RF_PALEVEL);
}
}
void Radio::prepare_listen() {
if (DEVICE_TYPE == DEVICE_TYPE_START) {
rf.openReadingPipe(1, RF_ADDRESS_FINISH);
} else {
rf.openReadingPipe(1, RF_ADDRESS_START);
}
rf.startListening();
}
void Radio::prepare_send() {
rf.stopListening();
if (DEVICE_TYPE == DEVICE_TYPE_START) {
rf.openWritingPipe(RF_ADDRESS_START);
} else {
rf.openWritingPipe(RF_ADDRESS_FINISH);
}
}
void Radio::loop() {
if (rf.available()) {
rf.read(&update, sizeof(DataPackage));
s_pRaceLogic->process_radio_data(update);
s_PowerControl.timer_reset(); // reset auto power off timer if device is receiving data
}
if (send_update) {
prepare_send();
update.timestamp = millis();
rf.write(&update, sizeof(DataPackage));
send_update = false;
prepare_listen();
s_PowerControl.timer_reset(); // reset auto power off timer if device is sending data
}
}
void Radio::send(DataPackage data) {
update = data;
update.timestamp = millis();
send_update = true;
}
DataPackage Radio::get_update() {
return update;
}