-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNEET_RF24.cpp
208 lines (176 loc) · 5.66 KB
/
NEET_RF24.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
#include "NEET_RF24.h"
/**
* Constructor for the NEET specific RF24 library with helper functions
*
* @param ce_pin chip enable pin used
* @param csn_pin chip select pin used
* @param channel Channel to transmit on, must match between transmitter and receiver pair
* @param isTransmit true if this is a transmitter
**/
NEET_RF24::NEET_RF24(uint8_t ce_pin, uint8_t csn_pin, uint8_t channel, bool is_transmit){
_radio = RF24(ce_pin, csn_pin);
_is_transmit = is_transmit;
_channel = channel;
_rx_cur_input = ControlInput();
_rx_last_packet_time = 0;
_rx_has_ack_payload = false;
_rx_telem_length = 0;
_rx_telem_idx = 0;
_rx_telem_payload = Telemetry();
_tx_telem_idx = 0;
_tx_has_telem = false;
_tx_telem_length = 0;
_tx_telem_payload = Telemetry();
}
/**
* Initializes radio and configures it to operate in its give role (either transmitter or receiver)
*
* @returns true if radio is successfully initalized
**/
bool NEET_RF24::begin(){
bool success = _radio.begin();
if (!success) {
return false;
}
// Allows dynamic ack payloads for telemetry
_radio.enableDynamicPayloads();
_radio.enableAckPayload();
_radio.setChannel(_channel);
_radio.setDataRate(DATA_RATE);
// Sets up correct reading and writing pipes depending on role
_radio.openWritingPipe(_address[_is_transmit]);
_radio.openReadingPipe(1, _address[!_is_transmit]);
_radio.setPALevel(RADIO_POWER_LVL);
if (!_is_transmit){
_radio.startListening();
} else {
_radio.stopListening();
}
return success;
}
/**
* Checks the radio for incoming messages and updates timeouts
* Used only for receivers
*
* @returns true if the current input is sent within RECEIVER_TIMEOUT_MSEC
**/
bool NEET_RF24::rxUpdate(){
uint8_t pipe;
if (_radio.available(&pipe)) {
_radio.read(&_rx_cur_input, sizeof(_rx_cur_input));
uint8_t bytes = _radio.getDynamicPayloadSize();
if (bytes != sizeof(_rx_cur_input)){
// Something is wrong, maybe packet corrupted?
return false;
}
_rx_last_packet_time = millis();
if (_rx_has_ack_payload){
// Assemble payload
uint8_t end_idx = min(_rx_telem_length, _rx_telem_idx + sizeof(_rx_telem_payload.msg));
uint8_t msg_len = end_idx - _rx_telem_idx;
strncpy(_rx_telem_payload.msg, _rx_telem + _rx_telem_idx, msg_len);
_rx_telem_payload.length = msg_len;
_rx_telem_payload.eol = end_idx == _rx_telem_length;
_rx_telem_idx += msg_len;
_rx_has_ack_payload = !_rx_telem_payload.eol;
// Send to radio
_radio.writeAckPayload(1, &_rx_telem_payload, sizeof(_rx_telem_payload));
}
return true;
}
return (millis() - _rx_last_packet_time) < RECEIVER_TIMEOUT_MSEC;
}
/**
* Gets the most recent values from the controller, need to call rxUpdate() to fetch
*
* @returns most recent ControlInput value
**/
ControlInput NEET_RF24::rxGetInput(){
return _rx_cur_input;
}
/**
* Populates the telemetry string to be sent using ack payloads
*
* @param s String telemetry, should be under MAX_TELEM_STRING_LEN
* @param override bool, if true it will start sending regardless of if rx_has_ack_payload
* @returns true if s is within length limit and radio is not already sending a telemetry msg
**/
bool NEET_RF24::rxSendTelemetry(String s, bool override){
if (_rx_has_ack_payload && !override){
return false;
}
if (s.length() > MAX_TELEM_STRING_LEN){
s = "Telemetry message too long!";
}
// Populate internal telemetry buffer
_rx_telem_length = s.length() + 1; // Add 1 to include null terminator
s.toCharArray(_rx_telem, sizeof(_rx_telem)); // Copy content to _rx_telem
_rx_telem_idx = 0;
_rx_has_ack_payload = true; // Set flag to start transmitting via ack payload
return true;
}
/**
* Sends a transmission with given ControlInput
*
* @param in ControlInput object to send
* @returns true if the transmission was successful
**/
bool NEET_RF24::txSendControlInput(ControlInput in){
bool success = _radio.write(&in, sizeof(in));
if (!success){
return success;
}
uint8_t pipe;
// If there's a ack payload attached
if (_radio.available(&pipe)){
_radio.read(&_tx_telem_payload, sizeof(_tx_telem_payload));
if (_tx_has_telem || _tx_telem_idx + _tx_telem_payload.length > sizeof(_tx_telem)){
// Something is wrong, missed a eol or currently already has telem ready
// Reset by starting over
_tx_telem_idx = 0;
}
// Copy message content to buffer
strncpy(_tx_telem + _tx_telem_idx, _tx_telem_payload.msg, _tx_telem_payload.length);
_tx_telem_idx += _tx_telem_payload.length;
_tx_has_telem = _tx_telem_payload.eol;
// Populate _tx_telem_length for reading
if (_tx_has_telem) {
_tx_telem_length = _tx_telem_idx;
_tx_telem_idx = 0;
}
} else { // If it's a regular ack packet, handles loss of eol packet
if (_tx_telem_idx != 0) {
_tx_has_telem = true;
_tx_telem_length = _tx_telem_idx;
_tx_telem_idx = 0;
}
}
return success;
}
/**
* Puts telemetry into the buffer given if there is a new complete telemetry message
*
* @param buf pointer to a character buffer with a large size
* @returns number of characters in the telemetry message, 0 if no message
**/
uint8_t NEET_RF24::txGetTelemetry(char* buf){
if (_tx_has_telem){
strncpy(buf, _tx_telem, _tx_telem_length);
uint8_t ret_len = _tx_telem_length;
_tx_telem_length = 0;
_tx_has_telem = false;
return ret_len;
}
return 0;
}
/**
* Sets the channel if this is a transmitter
*
* @param channel Channel as a uint8_t
**/
void NEET_RF24::txSetChannel(uint8_t channel){
if (_is_transmit){
_channel = channel;
_radio.setChannel(_channel);
}
}