-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTest.ino
More file actions
668 lines (541 loc) · 17.3 KB
/
Test.ino
File metadata and controls
668 lines (541 loc) · 17.3 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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
RadiaCode API Test
This example tests all APIs of a RadiaCode device over Bluetooth.
*/
#include <RadiaCode.h>
// Bluetooth MAC address
const char* bluetoothMac = "52:43:06:70:24:67"; // Replace with your device's MAC address
// Create RadiaCode instance
RadiaCode* radiacode = nullptr;
void test_BLEconnection(void)
{
Serial.println("------------------------");
Serial.println("Testing BLE connection...");
Serial.println("Connecting via Bluetooth...");
Serial.println("Setting internally the local time and resetting the timestamp ...");
radiacode = new RadiaCode(bluetoothMac);
}
void test_serialNumber(void)
{
String serialNum;
serialNum = radiacode->serialNumber();
Serial.println("------------------------");
Serial.println("Testing serialNumber() method...");
Serial.print("Connected to RadiaCode device: ");
Serial.println(serialNum);
}
void test_fwVersion(void)
{
int boot_major, boot_minor, target_major, target_minor;
String boot_date, target_date;
Serial.println("------------------------");
Serial.println("Testing fwVersion() method...");
auto version = radiacode->fwVersion();
boot_major = std::get<0>(version);
boot_minor = std::get<1>(version);
boot_date = std::get<2>(version);
target_major = std::get<3>(version);
target_minor = std::get<4>(version);
target_date = std::get<5>(version);
Serial.print("Bootloader version: ");
Serial.print(boot_major);
Serial.print(".");
Serial.printf("%02d", boot_minor);
Serial.print(" (");
Serial.print(boot_date);
Serial.println(")");
Serial.print("Firmware version: ");
Serial.print(target_major);
Serial.print(".");
Serial.printf("%02d", target_minor);
Serial.print(" (");
Serial.print(target_date);
Serial.println(")");
}
void test_deviceStatus(void)
{
uint32_t deviceStatus;
Serial.println("------------------------");
Serial.println("Testing deviceStatus() method...");
deviceStatus = radiacode->deviceStatus();
Serial.print("Device status flags: ");
Serial.println(deviceStatus);
}
void test_energyCalib(void)
{
#if 0
std::vector<float> calib;
calib = radiacode->energyCalib();
#else
auto calib = radiacode->energyCalib();
#endif
Serial.println("------------------------");
Serial.println("Testing energyCalib() method...");
if (calib.size() >= 3)
{
Serial.println("Energy calibration coefficients:");
Serial.printf("a0: %.6f a1: %.6f a2: %.6f\n", calib[0], calib[1], calib[2]);
}
}
void test_setEnergyCalib(void)
{
Serial.println("------------------------");
Serial.println("Testing setEnergyCalib() method...");
radiacode->setEnergyCalib(1.352235f, 2.381880f, 0.000348f);
}
void test_dataBuf(void)
{
static float countRate = 0.0f;
static float doseRate = 0.0f;
static float temperature = 0.0f;
static float batteryLevel = 0.0f;
int realTimeCount = 0, rareDataCount = 0, rawDataCount = 0, otherCount = 0;
#if 0
std::vector<DataItem*> data;
data = radiacode->dataBuf();
#else
auto data = radiacode->dataBuf();
#endif
Serial.println("------------------------");
Serial.println("Testing dataBuf() method...");
Serial.print("Data buffer size: ");
Serial.println(data.size());
// Process data
for (size_t i = 0; i < data.size(); i++)
{
DataItem* item = data[i];
if (item != nullptr)
{
switch (item->type)
{
case TYPE_REAL_TIME_DATA:
{
RealTimeData* rtData = (RealTimeData*)item;
if (rtData != nullptr)
{
countRate = rtData->count_rate;
doseRate = rtData->dose_rate;
realTimeCount++;
}
break;
}
case TYPE_RAW_DATA:
{
RawData* rawData = (RawData*)item;
if (rawData != nullptr)
{
// Use dose rate from raw data if real-time data doesn't have it
if (doseRate == 0.0f && rawData->dose_rate > 0.0f)
{
doseRate = rawData->dose_rate;
}
rawDataCount++;
}
break;
}
case TYPE_RARE_DATA:
{
RareData* rareData = (RareData*)item;
if (rareData != nullptr)
{
temperature = rareData->temperature;
batteryLevel = rareData->charge_level;
rareDataCount++;
}
break;
}
case TYPE_DOSE_RATE_DB:
default:
otherCount++;
break;
}
}
}
// Clean up data objects
for (size_t i = 0; i < data.size(); i++)
{
if (data[i] != nullptr)
{
delete data[i];
data[i] = nullptr;
}
}
data.clear();
Serial.print("Count rate: ");
Serial.print(countRate);
Serial.println(" CPS");
Serial.print("Dose rate: ");
Serial.print(doseRate * 10000.0f);
Serial.println(" µSv/h");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Battery level: ");
Serial.print(batteryLevel);
Serial.println("%");
}
void test_getTemperature(void)
{
float temperature;
Serial.println("------------------------");
Serial.println("Testing getTemperature() method...");
temperature = radiacode->getTemperature();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
void test_fwSignature(void)
{
String signature;
Serial.println("------------------------");
Serial.println("Testing fwSignature() method...");
signature = radiacode->fwSignature();
Serial.println(signature);
}
void test_hwSerialNumber(void)
{
String SerialNumber;
Serial.println("------------------------");
Serial.println("Testing hwSerialNumber() method...");
Serial.print("Serial Number: ");
SerialNumber = radiacode->hwSerialNumber();
Serial.println(SerialNumber);
}
void test_textMessage(void)
{
String textMessage;
Serial.println("------------------------");
Serial.println("Testing textMessage() method...");
Serial.print("Text Message: ");
textMessage = radiacode->textMessage();
Serial.println(textMessage);
}
void test_commands(void)
{
String commands;
Serial.println("------------------------");
Serial.println("Testing commands() method...");
Serial.print("SFR file content: ");
commands = radiacode->commands();
Serial.println(commands);
}
void test_getSpectrumFormatVersion(void)
{
uint8_t version = radiacode->getSpectrumFormatVersion();
Serial.println("------------------------");
Serial.println("Testing getSpectrumFormatVersion() method...");
Serial.print("Detected spectrum format version: ");
Serial.println(version);
}
void test_spectrumReset(void)
{
Serial.println("------------------------");
Serial.println("Testing spectrumReset() method...");
radiacode->spectrumReset();
Serial.println("Spectrum data reset.");
}
void test_spectrum(void)
{
Serial.println("------------------------");
Serial.println("Testing spectrum() method...");
Spectrum spectrum = radiacode->spectrum();
int channelCount = spectrum.size();
if (channelCount == 0)
{
Serial.println("Error: No spectrum data available");
return;
}
Serial.print("Duration: ");
Serial.print(spectrum.duration_sec);
Serial.println(" seconds");
Serial.print("Channels: ");
Serial.println(channelCount);
// Find maximum count for scaling
uint32_t maxCount = 0;
for (int i = 0; i < channelCount; i++)
{
uint32_t count = spectrum.at(i);
if (count > maxCount)
{
maxCount = count;
}
}
Serial.print("Maximum count: ");
Serial.println(maxCount);
}
void test_spectrumAccum(void)
{
Serial.println("------------------------");
Serial.println("Testing spectrumAccum() method...");
Spectrum spectrum = radiacode->spectrumAccum();
int channelCount = spectrum.size();
if (channelCount == 0)
{
Serial.println("Error: No spectrum data available");
return;
}
Serial.print("Duration: ");
Serial.print(spectrum.duration_sec);
Serial.println(" seconds");
Serial.print("Channels: ");
Serial.println(channelCount);
// Find maximum count for scaling
uint32_t maxCount = 0;
for (int i = 0; i < channelCount; i++)
{
uint32_t count = spectrum.at(i);
if (count > maxCount)
{
maxCount = count;
}
}
Serial.print("Maximum count: ");
Serial.println(maxCount);
}
void test_doseReset(void)
{
Serial.println("------------------------");
Serial.println("Testing doseReset() method...");
radiacode->doseReset();
Serial.println("Dose reset.");
}
void test_setLanguage(void)
{
Serial.println("------------------------");
Serial.println("Testing setLanguage() method...");
radiacode->setLanguage("en");
Serial.println("Language set to English.");
}
void test_setDeviceOn(void)
{
Serial.println("------------------------");
Serial.println("Testing setDeviceOn() method...");
radiacode->setDeviceOn(false);
Serial.println("Device turned off.");
}
void test_setSoundOn(void)
{
Serial.println("------------------------");
Serial.println("Testing setSoundOn() method...");
// Sound On/Off (main switch)
radiacode->setSoundOn(true);
Serial.println("Sound enabled.");
}
void test_setVibroOn(void)
{
Serial.println("------------------------");
Serial.println("Testing setVibroOn() method...");
// Vibro On/Off (main switch)
radiacode->setVibroOn(true);
Serial.println("Vibration enabled.");
}
void test_setSoundCtrl(void)
{
Serial.println("------------------------");
Serial.println("Testing setSoundCtrl() method...");
radiacode->setSoundCtrl((CTRL)(CTRL::BUTTONS));
//radiacode->setSoundCtrl((CTRL)(CTRL::BUTTONS | CTRL::CLICKS));
Serial.println("Sound control flags set.");
}
void test_setVibroCtrl(void)
{
Serial.println("------------------------");
Serial.println("Testing setVibroCtrl() method...");
radiacode->setVibroCtrl((CTRL)(CTRL::BUTTONS));
Serial.println("Vibration control flags set.");
}
void test_setDisplayOffTime(void)
{
Serial.println("------------------------");
Serial.println("Testing setDisplayOffTime() method...");
radiacode->setDisplayOffTime(10);
Serial.println("Display off time set to 10 seconds.");
}
void test_setDisplayBrightness(void)
{
Serial.println("------------------------");
Serial.println("Testing setDisplayBrightness() method...");
radiacode->setDisplayBrightness(5);
Serial.println("Display brightness set to 5.");
}
void test_setDisplayDirection(void)
{
Serial.println("------------------------");
Serial.println("Testing setDisplayDirection() method...");
radiacode->setDisplayDirection(DisplayDirection::AUTO);
Serial.println("Display direction set to AUTO.");
}
void test_getAlarmLimits(void)
{
Serial.println("------------------------");
Serial.println("Testing getAlarmLimits() method...");
#if 0
AlarmLimits limits;
limits = radiacode->getAlarmLimits();
#else
auto limits = radiacode->getAlarmLimits();
#endif
Serial.print("l1_count_rate: ");
Serial.println(limits.l1_count_rate);
Serial.print("l2_count_rate: ");
Serial.println(limits.l2_count_rate);
Serial.print("count_unit: ");
Serial.println(limits.count_unit);
Serial.print("l1_dose_rate: ");
Serial.println(limits.l1_dose_rate);
Serial.print("l2_dose_rate: ");
Serial.println(limits.l2_dose_rate);
Serial.print("l1_dose: ");
Serial.println(limits.l1_dose);
Serial.print("l2_dose: ");
Serial.println(limits.l2_dose);
Serial.print("dose_unit: ");
Serial.println(limits.dose_unit);
}
void test_setAlarmLimits(void)
{
bool result;
Serial.println("------------------------");
Serial.println("Testing setAlarmLimits() method...");
result = radiacode->setAlarmLimits(20.0f, 60.0f, 0.4f, 1.2f, 9.99f, 9.99f, true, false);
if (!result)
{
Serial.println("Failed to set alarm limits!");
return;
}
Serial.println("Alarm limits set.");
}
void test_setLightOn(void)
{
Serial.println("------------------------");
Serial.println("Testing setLightOn() method...");
radiacode->setLightOn(true);
Serial.println("Light enabled.");
}
void test_setDeviceCtrl(void)
{
Serial.println("------------------------");
Serial.println("Testing setDeviceCtrl() method...");
radiacode->setDeviceCtrl((DEV_CTRL)(DEV_CTRL::PWR | DEV_CTRL::LIGHT | DEV_CTRL::SOUND));
Serial.println("Power, Light, Sound enabled, Vibration disabled.");
}
void test_setAlarmSignalMode(void)
{
Serial.println("------------------------");
Serial.println("Testing setAlarmSignalMode() method...");
// Set alarm signal mode to once
radiacode->setAlarmSignalMode(AlarmSignalMode::ONCE);
Serial.println("Alarm signal mode set to once.");
}
void test_setMeasurementUnit(void)
{
Serial.println("------------------------");
Serial.println("Testing setMeasurementUnit() method...");
radiacode->setMeasurementUnit(MeasurementUnits::SIEVERT);
Serial.println("Measurement unit set to Sievert.");
}
void test_setCountRateUnit(void)
{
Serial.println("------------------------");
Serial.println("Testing setCountRateUnit() method...");
radiacode->setCountRateUnit(CountRateUnits::CPS);
Serial.println("Count rate unit set to CPS.");
}
void test_setTemperatureUnit(void)
{
Serial.println("------------------------");
Serial.println("Testing setTemperatureUnit() method...");
radiacode->setTemperatureUnit(TemperatureUnits::CELSIUS);
Serial.println("Temperature unit set to Celsius.");
}
void test_displayControl(void)
{
Serial.println("------------------------");
Serial.println("Testing displayControl() method...");
radiacode->setDisplayCtrl(DISPLAY_CTRL::BACKLT_ON_AUTO);
Serial.println("Display control set to BACKLT_ON_AUTO.");
}
void setup(void)
{
// Initialize serial
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.printf("RadiaCode API Test Project, Driver Version: %s\n", getDriverVersion());
// Connect to RadiaCode device
try
{
test_BLEconnection();
if (radiacode != nullptr)
{
test_serialNumber();
test_fwVersion();
//test_setEnergyCalib();
test_energyCalib();
test_deviceStatus();
test_dataBuf();
test_fwSignature();
test_hwSerialNumber();
test_textMessage();
test_commands();
test_getSpectrumFormatVersion();
test_spectrumReset();
test_doseReset();
//test_setLanguage();
//test_setDeviceOn();
//test_setSoundOn();
//test_setVibroOn();
//test_setLightOn();
//test_setSoundCtrl();
//test_setVibroCtrl();
//test_setDeviceCtrl();
//test_setDisplayOffTime();
//test_setDisplayBrightness();
//test_setDisplayDirection();
//test_setAlarmSignalMode();
//test_setAlarmLimits();
//test_setMeasurementUnit();
//test_setCountRateUnit();
//test_setTemperatureUnit();
//test_displayControl();
test_getAlarmLimits();
}
else
{
Serial.println("Failed to create RadiaCode instance!");
}
}
catch (...)
{
Serial.println("Exception during RadiaCode initialization!");
if (radiacode != nullptr)
{
delete radiacode;
radiacode = nullptr;
}
}
}
void loop(void)
{
if (radiacode == nullptr)
{
Serial.println("No device connected.");
}
else
{
try
{
test_dataBuf();
test_getTemperature();
test_spectrum();
//test_spectrumAccum();
}
catch (...)
{
Serial.println("Error reading data from device - restarting...");
delay(1000);
ESP.restart();
}
}
// Wait 2 seconds before next cycle
delay(2000);
}