-
Notifications
You must be signed in to change notification settings - Fork 2
/
se8r01.ino
494 lines (441 loc) · 10.3 KB
/
se8r01.ino
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
#include "se8r01.h"
#define RPD_MODE 3
#define RPD_MODE2 4
#define RPD_MODE3 5
#define TEST_MODE 6
LiquidCrystal_I2C lcd(0x27, 20, 4);
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
int counter_for_tx = 0;
byte start_ch_for_RPD = 0, width_for_RPD2 = 45;
int tryes_for_test_link = 100;
byte mode = RX_MODE;
byte txData[] = { "123456" }; //must be min 4 byte (wtf??), max - 32 bytes
const int PAYLOAD_WIDTH = sizeof(txData);
byte rxData[PAYLOAD_WIDTH]; //size of rx must be the same as tx if not DNPL
void init_radio() {
if (!init_rf(10, 7, 8, PAYLOAD_WIDTH)) {
Serial.println("Chip not found!");
while (1);
}
setPower(POWER_5dbm);
selectTxPipe(0);
setRtr(15);
changeMode(mode);
Serial.setTimeout(100);
Serial.println("********************** Radio starting *******************");
Serial.print("************************ ");
printMode();
Serial.println(" ************************");
}
void init_buttons() {
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
debouncer1.attach(2);
debouncer1.interval(5); // interval in ms
debouncer2.attach(3);
debouncer2.interval(5); // interval in ms
debouncer3.attach(4);
debouncer3.interval(5); // interval in ms
}
void setup() {
init_buttons();
Serial.begin(9600);
lcd.init();
lcd.backlight();
init_radio();
}
void scanNoise() {
lcd.clear();
lcd.print("SCANNING...");
Serial.println("SCANNING...");
changeMode(RX_MODE);
char curNoise, minNoise = 100, maxNoise = -100;
byte chnlMinNoise, chnlMaxNoise;
for (byte i = 0; i < 125; i++) {
setChannel(i);
curNoise = getRpd();
minNoise = min(curNoise, minNoise);
if (minNoise == curNoise) {
chnlMinNoise = i;
}
maxNoise = max(curNoise, maxNoise);
if (maxNoise == curNoise) {
chnlMaxNoise = i;
}
}
lcd.clear();
lcd.print("MIN:");
lcd.print(minNoise, DEC);
lcd.print(" CH:");
lcd.print(chnlMinNoise);
lcd.setCursor(0, 1);
lcd.print("MAX:");
lcd.print(maxNoise, DEC);
lcd.print(" CH:");
lcd.print(chnlMaxNoise);
Serial.print("MIN NOISE:");
Serial.print(minNoise, DEC);
Serial.print(" CHNL:");
Serial.println(chnlMinNoise);
Serial.print("MAX NOISE:");
Serial.print(maxNoise, DEC);
Serial.print(" CHNL:");
Serial.println(chnlMaxNoise);
}
bool sendCommandChangeChannel(byte newCh) {
bool result = true;
lcd.clear();
lcd.print("CHANGING CH...");
changeMode(TX_MODE);
selectTxPipe(1);
String(newCh).getBytes(txData, PAYLOAD_WIDTH);
char send = sendWithAck(txData);
delay(1);
selectTxPipe(0);
if (send < 0) {
result = false;
byte i;
for (i = 0; i < 10; i++)
if (sendWithAck(txData) >= 0)
break;
if (i == 10) { //check if we are not still on prev channel
byte prevCh = readReg(REG_RF_CH);
setChannel(newCh);
for (i = 0; i < 10; i++)
if (sendWithAck(txData) >= 0)
break;
if (i < 10) //check if got response on new channel
result = true;
setChannel(prevCh);
}
}
changeMode(mode);
return result;
}
void buttons_processing() {
for (byte i = 0; i < 10; i++) {
debouncer1.update();
debouncer2.update();
debouncer3.update();
delay(10);
}
bool but1 = !debouncer1.read();
bool but2 = !debouncer2.read();
bool but3 = !debouncer3.read();
if (but1) {
if (but2) {
byte prevCh = readReg(REG_RF_CH);
scanNoise();
delay(3000);
setChannel(prevCh);
but2 = false;
} else if (but3) {
but3 = false;
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("RPD:");
lcd.print(getRpd(), DEC);
delay(1000);
} else {
mode++;
if (mode == RPD_MODE || mode == RPD_MODE2 || mode == RPD_MODE3) {
mode = TEST_MODE;
}
if (mode > TEST_MODE)
mode = 0;
if (mode != TEST_MODE)
printMode();
delay(500);
}
}
if (but2) {
byte newCh = readReg(REG_RF_CH) + 10;
if (newCh > 124)
newCh = 0;
if (sendCommandChangeChannel(newCh))
setChannel(newCh);
printMode();
}
if (but3) {
byte newCh = readReg(REG_RF_CH) + 1;
if (newCh > 124)
newCh = 0;
if (sendCommandChangeChannel(newCh))
setChannel(newCh);
printMode();
}
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil(' ');
int param = Serial.parseInt();
Serial.print("********************** ");
if (command.equalsIgnoreCase("ch")) {
setChannel(param);
Serial.print("Set channel:" + String(param));
lcd.setCursor(0, 1);
lcd.print("CHANNEL:");
lcd.print(readReg(REG_RF_CH));
lcd.print(" ");
}
if (command.equalsIgnoreCase("pi")) {
selectTxPipe(param);
Serial.print("Set pipe:" + String(param));
}
if (command.equalsIgnoreCase("rx")) {
mode = RX_MODE;
changeMode(mode);
printMode();
}
if (command.equalsIgnoreCase("tx")) {
mode = TX_MODE;
changeMode(mode);
printMode();
}
if (command.equalsIgnoreCase("st")) {
mode = STANDBY;
changeMode(mode);
printMode();
}
if (command.equalsIgnoreCase("rpd")) {
mode = RPD_MODE;
changeMode(RX_MODE);
printMode();
}
if (command.equalsIgnoreCase("rpd2")) {
mode = RPD_MODE2;
start_ch_for_RPD = param * width_for_RPD2;
changeMode(RX_MODE);
printMode();
}
if (command.equalsIgnoreCase("rpd3")) {
mode = RPD_MODE3;
start_ch_for_RPD = param;
changeMode(RX_MODE);
printMode();
}
if (command.equalsIgnoreCase("pw")) {
switch (param) {
case 5:
setPower(POWER_5dbm);
break;
case 0:
setPower(POWER_0dbm);
break;
case 6:
setPower(POWER_m6dbm);
break;
case 12:
setPower(POWER_m12dbm);
break;
case 18:
setPower(POWER_m18dbm);
break;
}
if (param != 5 && param != 0)
param = -param;
Serial.print("Set power:" + String(param));
Serial.print(" dbm");
}
if (command.equalsIgnoreCase("sc")) {
scanNoise();
}
if (command.equalsIgnoreCase("ts")) {
mode = TEST_MODE;
tryes_for_test_link = 100;
if (param != 0)
tryes_for_test_link = param;
}
Serial.println(" ************************");
if (command.equalsIgnoreCase("rpd2"))
printHeadRPD2();
}
buttons_processing();
switch (mode) {
case RX_MODE: {
byte pipe = 7;
pipe = getRxData(rxData);
if (pipe < 7) {
while ((getStatusReg() & 0xE) != 0xE) //while fifo not empty
getRxPayload(rxData, PAYLOAD_WIDTH); //reading payload from fifo to buffer
String strVal((char*) rxData);
Serial.print("Received on pipe:");
Serial.println(pipe);
Serial.println(strVal);
if (pipe == 1) {
byte newCh = (byte) strVal.toInt();
Serial.print("Change channel to:");
Serial.println(newCh);
setChannel(newCh);
}
}
break;
}
case TX_MODE: {
String(counter_for_tx).getBytes(txData, PAYLOAD_WIDTH);
char rtr = sendWithAck(txData);
if (rtr >= 0)
Serial.println("Success! Data packet delivered!");
else {
Serial.println(
"************************Fail! Data packet may be lost!********************************");
}
Serial.print("Rtr: ");
Serial.print(rtr, DEC);
Serial.print(" - ");
Serial.println((readReg(REG_OBSERVE_TX) & 0xF0) >> 4);
counter_for_tx++;
delay(500);
break;
}
case RPD_MODE:
//Serial.println(int(getRpd()));
//delay(500);
plot(getRpd() * 10);
delay(100);
break;
case RPD_MODE2:
for (byte i = start_ch_for_RPD; i < min(start_ch_for_RPD + width_for_RPD2, 125); i++) {
setChannel(i);
delay(1);
char rpd = getRpd();
Serial.print(" ");
if (rpd >= 0)
Serial.print(" ");
else
Serial.print("-");
if (abs(rpd) < 10)
Serial.print("0");
Serial.print(abs(rpd));
if (abs(rpd) < 100)
Serial.print(" ");
Serial.print(" ");
}
Serial.println();
//delay(500);
break;
case RPD_MODE3:
int rpds[4];
for (byte i = start_ch_for_RPD; i < start_ch_for_RPD + 4; i++) {
setChannel(i);
delay(1);
rpds[i - start_ch_for_RPD] = getRpd() * 10;
}
plot(rpds[0], rpds[1], rpds[2], rpds[3]);
delay(100);
case TEST_MODE:
testLink();
delay(1000);
break;
}
}
void printMode() {
lcd.clear();
String modeName;
if (mode == STANDBY) {
modeName = "STANDBY";
}
if (mode == RX_MODE) {
modeName = "RX";
}
if (mode == TX_MODE) {
modeName = "TX";
}
if (mode == RPD_MODE) {
modeName = "RPD";
}
if (mode == RPD_MODE2) {
modeName = "RPD 2";
}
if (mode == RPD_MODE3) {
modeName = "RPD 3";
}
Serial.print(modeName + " MODE");
lcd.print(modeName + " MODE ");
lcd.setCursor(0, 1);
lcd.print("CHANNEL:");
if (mode == RPD_MODE3) {
lcd.print(start_ch_for_RPD);
lcd.print("-");
lcd.print(start_ch_for_RPD + 3);
} else {
lcd.print(readReg(REG_RF_CH));
lcd.print(" ");
}
}
void printHeadRPD2() {
for (byte i = start_ch_for_RPD; i < min(start_ch_for_RPD + width_for_RPD2, 124); i++) {
Serial.print(" ");
if (i < 10)
Serial.print(0);
Serial.print(i);
Serial.print(" ");
}
Serial.println();
}
char getRpd() {
byte avCount = 100;
int rpd = 0;
for (byte i = 0; i < avCount; i++) {
rpd += (char) readReg(REG_RPD);
delayMicroseconds(500);
}
rpd /= avCount;
return (char) rpd;
}
void testLink() {
int suc = 0;
lcd.clear();
lcd.print("TESTING...");
Serial.println("TESTING...");
changeMode(TX_MODE);
for (int i = 0; i < tryes_for_test_link; i++) {
if (sendWithAck(txData) >= 0)
suc++;
delay(10);
}
lcd.clear();
lcd.print("S:");
lcd.print(tryes_for_test_link);
lcd.print(" ACK:");
lcd.print(suc);
lcd.setCursor(0, 1);
lcd.print("%:");
lcd.print(suc * 100 / tryes_for_test_link);
Serial.print("Send:");
Serial.print(tryes_for_test_link);
Serial.print(" ACK:");
Serial.println(suc);
Serial.print("%:");
Serial.println(suc * 100 / tryes_for_test_link);
}
void printRXdata(byte* address, byte dlength) {
for (byte i = 0; i < dlength; i++) {
Serial.print(address[i], HEX);
Serial.print(" : ");
}
Serial.println();
}
void plot(int data1) {
plot(data1, 0, 0, 0);
}
void plot(int data1, int data2, int data3, int data4) {
int pktSize;
int buffer[20];
buffer[0] = 0xCDAB; //SimPlot packet header. Indicates start of data packet
buffer[1] = 4 * sizeof(int); //Size of data in bytes. Does not include the header and size fields
buffer[2] = data1;
buffer[3] = data2;
buffer[4] = data3;
buffer[5] = data4;
pktSize = 2 + 2 + (4 * sizeof(int)); //Header bytes + size field bytes + data
Serial.write((uint8_t *) buffer, pktSize);
}