LD2410Async
Asynchronous Arduino ESP32 library for the LD2410 mmWave radar sensor
Loading...
Searching...
No Matches
unitTest.ino
Go to the documentation of this file.
1/**
2* @brief Unit Test for most methods of the lib
3*
4* @details
5* This sketch tests most methods of the LD2410Async lib and prints the test rusults to the serial monitor.
6*
7* Important:
8* Dont forget to adjust RADAR_RX_PIN and RADAR_TX_PIN according to your wiring.
9*/
10
11
12
13
14#include <Arduino.h>
15#include <Ticker.h>
16
17#include "LD2410Async.h"
18
19
20
21/********************************************************************************************************************************
22** Hardware configuration
23********************************************************************************************************************************/
24// UART pins for the LD2410 sensor
25//#define RADAR_RX_PIN 16 // ESP32 pin that receives data from the radar (radar TX)
26//#define RADAR_TX_PIN 17 // ESP32 pin that transmits data to the radar (radar RX)
27
28#define RADAR_RX_PIN 32
29#define RADAR_TX_PIN 33
30
31
32// UART baudrate for the radar sensor (default is 256000)
33#define RADAR_BAUDRATE 256000
34
35/********************************************************************************************************************************
36** Variables
37********************************************************************************************************************************/
38// Create a HardwareSerial instance (ESP32 has multiple UARTs)
39HardwareSerial RadarSerial(1);
40
41// Create LD2410Async object bound to Serial1
43
44
45
46
47
48// Ticker used for delays in the test methods
50
51// Config data as found on the sensor, so we can restore it when done.
53
54//Used to determine the duration of the tests
55unsigned long testStartMs = 0;
56//Name of the currently running tests
58
59//Counts test outcomes
62
63//Number of test that is currently executed. Is used to iterate through the test sequence
65//Indicates whether tests are currently being executed
67
68//Line length for the printed outputs
69const int LINE_LEN = 80;
70
71
72/********************************************************************************************************************************
73** Definitions and declarations for the test sequence handling
74********************************************************************************************************************************/
75typedef void (*TestAction)();
76
77/// @cond Hide_this_from_the_docu
78struct TestEntry {
79 TestAction action;
80 bool configModeAtTestEnd;
81};
82// @endcond
83//
84// Forward declarations so functions above can use them
85extern TestEntry actions[];
86extern const int NUM_ACTIONS;
87
88
89/********************************************************************************************************************************
90** Print functions
91** They just help to get nice output from the tests
92********************************************************************************************************************************/
93void printLine(char c, int len) {
94 for (int i = 0; i < len; i++) Serial.print(c);
95 Serial.println();
96}
97
98void printBigMessage(const String msg, char lineChar = '*') {
99 Serial.println();
100 printLine(lineChar, LINE_LEN);
101 printLine(lineChar, LINE_LEN);
102 Serial.print(lineChar);
103 Serial.println(lineChar);
104 Serial.print(lineChar);
105 Serial.print(lineChar);
106 Serial.print(" ");
107 Serial.println(msg);
108 Serial.print(lineChar);
109 Serial.println(lineChar);
110 printLine(lineChar, LINE_LEN);
111 printLine(lineChar, LINE_LEN);
112}
113
114/********************************************************************************************************************************
115** Generic print methods so we can pass parameters with differenrt types
116********************************************************************************************************************************/
117template <typename T>
118void printOne(const T& value) {
119 Serial.print(value);
120}
121
122template <typename T, typename... Args>
123void printOne(const T& first, const Args&... rest) {
124 Serial.print(first);
125 printOne(rest...); // recurse
126}
127
128/********************************************************************************************************************************
129** Test start
130** Must be called at the start of each test.
131** Outputs the test name, plus comment and records the starting time of the test.
132********************************************************************************************************************************/
133void testStart(String name, String comment = "") {
134 currentTestName = name;
135
136 printLine('*', LINE_LEN);
137 Serial.print("** ");
138 Serial.println(name);
139 if (comment.length() > 0) {
140 Serial.print("** ");
141 printLine('-', LINE_LEN - 3);
142 Serial.print("** ");
143 Serial.println(comment);
144 }
145 printLine('*', LINE_LEN);
146 testStartMs = millis();
147}
148
149
150/********************************************************************************************************************************
151** Test End
152** Must be called at the end of each test.
153** Outputs the test result, duration and comment.
154** Also starts the next test on success or aborts the test sequence on failure.
155********************************************************************************************************************************/
157
158template <typename... Args>
159void testEnd(bool success, const Args&... commentParts) {
160
161
162 char lineChar = success ? '-' : '=';
163
164 printLine(lineChar, LINE_LEN);
165
166 Serial.print(lineChar);
167 Serial.print(lineChar);
168 Serial.print(" ");
169 Serial.print(currentTestName);
170 Serial.print(": ");
171 Serial.println(success ? "Success" : "Failed");
172
173 printLine(lineChar, LINE_LEN);
174
175 Serial.print(lineChar);
176 Serial.print(lineChar);
177 Serial.print(" Test duration (ms): ");
178 Serial.println(millis() - testStartMs);
179
180 if constexpr (sizeof...(commentParts) > 0) {
181 Serial.print(lineChar);
182 Serial.print(lineChar);
183 Serial.print(" ");
184 printOne(commentParts...);
185 Serial.println();
186 }
187 printLine(lineChar, LINE_LEN);
188 Serial.println();
189
190 if (actions[currentTestIndex].configModeAtTestEnd != ld2410.isConfigModeEnabled()) {
191 printLine(lineChar, LINE_LEN);
192 Serial.print(lineChar);
193 Serial.print(lineChar);
194 Serial.println("Failed,due to config mode failure!!!");
195 Serial.print(lineChar);
196 Serial.print(lineChar);
197 Serial.print("Config mode must be ");
198 Serial.print(actions[currentTestIndex].configModeAtTestEnd ? "enabled" : "disabled");
199 Serial.print(" after the previous test, but is ");
200 Serial.println(ld2410.isConfigModeEnabled() ? "enabled" : "disabled");
201 printLine(lineChar, LINE_LEN);
202 Serial.println();
203 success = false;
204
205 }
206
207
208 // update counters
209 if (success) {
211 // Schedule the next test after 2 second (2000 ms)
212 testDelayTicker.once_ms(2000, startNextTest);
213 }
214 else {
216 testSequenceRunning = false;
217 printBigMessage("TESTS FAILED", '-');
218 }
219
220}
221
222
223/********************************************************************************************************************************
224** Test print
225** Outputs intermadate states and results of the tests
226********************************************************************************************************************************/
227// Versatile testPrint
228template <typename... Args>
229void testPrint(const Args&... args) {
230 unsigned long elapsed = millis() - testStartMs;
231 Serial.print(elapsed);
232 Serial.print("ms ");
233 printOne(args...);
234 Serial.println();
235}
236
237/********************************************************************************************************************************
238** Helper function to convert async test results into a printable string
239********************************************************************************************************************************/
241 switch (result) {
242 case LD2410Async::AsyncCommandResult::SUCCESS: return "SUCCESS";
243 case LD2410Async::AsyncCommandResult::FAILED: return "FAILED (Command has failed)";
244 case LD2410Async::AsyncCommandResult::TIMEOUT: return "TIMEOUT (Async command has timed out)";
245 case LD2410Async::AsyncCommandResult::CANCELED: return "CANCELED (Async command has been canceled by the user)";
246 default: return "UNKNOWN (Unsupported command result, verify code of lib)";
247 }
248}
249
250/********************************************************************************************************************************
251** Helper function to initalize config data strzucts with valid values
252********************************************************************************************************************************/
254{
255 configData.numberOfGates = 9; // This is a read-only value, but we set it here to avoid validation errors
259
260 for (int i = 0; i < 9; i++) {
261 configData.distanceGateMotionSensitivity[i] = 5 + 5 * i;
262 configData.distanceGateStationarySensitivity[i] = 100 - i;
263 }
264 configData.maxMotionDistanceGate = 5;
265 configData.maxStationaryDistanceGate = 4;
266 configData.lightThreshold = 128;
267 configData.noOneTimeout = 120;
268}
269
270
271/********************************************************************************************************************************
272** Data update counting
273** The following methods are used, to monitor whether the LD2410 sends data
274********************************************************************************************************************************/
275
276//Counter variables
279
280//Resets the counter variables
285
286//Callback method for data received event
287void dataUpdateCounterCallback(LD2410Async* sender, bool presenceDetected) {
288 //Output . or * depending on presence detected
289 Serial.print(presenceDetected ? "x" : ".");
290
291 //Record whether normal or engineering mode data has been received
293 if (data.engineeringMode) {
295 }
296 else {
298 }
299}
300
301//Start the data update counting
306
307//Stop the data update counting
310 Serial.println();
311}
312
313/********************************************************************************************************************************
314** Begin test
315********************************************************************************************************************************/
316void beginTest() {
317
318 testStart("begin() Test", "Calling begin() is always the first thing we have to do.");
319 if (ld2410.begin()) {
320 testEnd(true, "LD2410Async task started successfully.");
321 }
322 else {
323 testEnd(false, "LD2410Async task already running.");
324 }
325}
326
327/********************************************************************************************************************************
328** Reboot test
329********************************************************************************************************************************/
330
332 testPrint("Callback for rebootAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
333
334 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
335 testEnd(true, "LD2410 has rebooted");
336 }
337 else {
338 testEnd(false, "Test has failed due to ", asyncCommandResultToString(asyncResult));
339 }
340}
341
342
344 testStart("rebootAsync() Test", "First real test is a reboot of the LD2410 to ensure it is in normal operation mode.");
345
347
348 if (ret) {
349 testPrint("rebootAsync() conmpleted. Expecting callback.");
350 }
351 else {
352 testEnd(false, "rebootAsync() has returned false. This should only happen if another async command is pending pending");
353 }
354}
355
356
357
358
359/********************************************************************************************************************************
360** Normal mode data receive test
361********************************************************************************************************************************/
362
365 Serial.println();
366
368 testEnd(true, dataUpdateCounter_normalModeCount, " normal mode data updates received");
369 }
371 testPrint("Test failed. No normal mode data updates received, ");
372 testPrint(" but got ", dataUpdateCounter_normalModeCount, " engineering mode data updates.");
373 testPrint(" Since only engineering mode data was excpected, the test has failed");
374
375 testEnd(false, "Got only engineering mode data instead of normal mode data as expected.");
376 }
378 testPrint("Test failed. No data updates received ");
379 testEnd(false, "No data updates received");
380 }
381 else {
382 testPrint("Test failed. Received normal mode and engineering mode data,");
383 testPrint(" but expected only normal mode data.");
384 testEnd(false, "Received a mix of normal mode and engineering mode data, but expected only normal mode data.");
385
386 }
387}
388
390 testStart("Normal Mode Data Receive Test", "Tests whether the sensor sends data in normal mode");
391
393
394 testPrint("Callback to get data updates registered with onDetectionDataReceived().");
395 testPrint("Counting and checking received data for 10 secs");
396
398
399}
400
401
402
403/********************************************************************************************************************************
404** Enable engineering mode test
405********************************************************************************************************************************/
406
408 testPrint("Callback for enableEngineeringModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
409
410 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
411 testEnd(true);
412 }
413 else {
414 testEnd(false, "Test has failed due to ", asyncCommandResultToString(asyncResult));
415 }
416}
417
418
420 testStart("enableEngineeringModeAsync() Test", "Enables engineering mode, so we get more detailed detection data");
421
423
424 if (ret) {
425 testPrint("enableEngineeringModeAsync() conmpleted. Expecting callback.");
426 }
427 else {
428 testEnd(false, "enableEngineeringModeAsync() has returned false. This should only happen if another async command is pending pending");
429 }
430}
431
432/********************************************************************************************************************************
433** Engineering mode data receive test
434********************************************************************************************************************************/
435
438
440 testEnd(true, dataUpdateCounter_engineeringModeCount, " engineering mode data updates received");
441 }
443 testPrint("Test failed. No engineering mode data updates received, ");
444 testPrint(" but got ", dataUpdateCounter_engineeringModeCount, " normal mode data updates.");
445 testPrint(" Since only normal mode data was excpected, the test has failed");
446
447 testEnd(false, "Got only normal mode data instead of engineering mode data as expected.");
448 }
450 testPrint("Test failed. No data updates received ");
451 testEnd(false, "No data updates received");
452 }
453 else {
454 testPrint("Test failed. Received engineering mode and normal mode data,");
455 testPrint(" but expected only engineering mode data.");
456 testEnd(false, "Received a mix of engineering mode and normal mode data, but expected only engineering mode data.");
457
458 }
459}
460
462 testStart("Engineering Mode Data Receive Test", "Tests whether the sensor sends data in engineering mode");
463
465
466 testPrint("Callback to get data updates registered with onDetectionDataReceived().");
467 testPrint("Counting and checking received data for 10 secs");
468
470
471}
472
473
474
475/********************************************************************************************************************************
476** Disable engineering mode test
477********************************************************************************************************************************/
478
480 testPrint("Callback for disableEngineeringModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
481
482 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
483 testEnd(true);
484 }
485 else {
486 testEnd(false, "Test has failed due to ", asyncCommandResultToString(asyncResult));
487 }
488}
489
490
492 testStart("disableEngineeringModeAsync() Test", "Disables engineering mode.");
493
495
496 if (ret) {
497 testPrint("disableEngineeringModeAsync() conmpleted. Expecting callback.");
498 }
499 else {
500 testEnd(false, "disableEngineeringModeAsync() has returned false. This should only happen if another async command is pending pending");
501 }
502}
503
504/********************************************************************************************************************************
505** Enable config mode test
506********************************************************************************************************************************/
507
511 testEnd(true, "Config mode has been enabled successfully");
512 }
513 else {
514 Serial.println();
515 testEnd(false, "enableConfigModeAsync() reports success, but the LD2410 still sends detection data.");
516 }
517}
518
520 testPrint("Waiting a 5 seconds, so we can be sure that we are really in config mode (checking if no data updates are sent).");
521
523
524 onceTicker.once_ms(5000, enableConfigModeTestEnd);
525}
526
527
529 testPrint("Callback for enableConfigModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
530
531 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
533 }
534 else {
535 testEnd(false, "Test has failed due to ", asyncCommandResultToString(asyncResult));
536 }
537}
538
539
541 testStart("enableConfigModeAsync() Test", "Enables config mode, which is required if other commands have to be sent to the LD2410.");
542
544
545 if (ret) {
546 testPrint("enableConfigModeAsync() conmpleted. Expecting callback.");
547 }
548 else {
549 testEnd(false, "enableConfigModeAsync() has returned false. This should only happen if another async command is pending pending");
550 }
551}
552
553
554/********************************************************************************************************************************
555** Config mode persistence test
556********************************************************************************************************************************/
557
561 testEnd(true, "Config mode is still active as expected");
562 }
563 else {
564 testEnd(false, "Config mode has been disabled unexpectedly (we got data updates)");
565 }
566}
567
570 testPrint("Config mode is still active according to isConfigModeEnabled().");
571 testPrint("Wait 5 secs to check if we receive any detection data (which would indicate that config mode has been disabled)");
573
575 }
576 else {
577 testEnd(false, "Config mode has been disabled according to isConfigModeEnabled().");
578 }
579}
580
581
583 testPrint("Callback for requestFirmwareAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
584
585 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
587 }
588 else {
589 testEnd(false, "Test has failed due to requestFirmwareAsync() reporting ", asyncCommandResultToString(asyncResult));
590 }
591}
592
594 testStart("Config Mode Persistenc Test", "Checks whther config mode remains active when other commands are sent");
595
597 if (ret) {
598 testPrint("requestFirmwareAsync() started. Waiting for callback (which should not change the config mode state).");
599 }
600 else {
601 testEnd(false, "requestFirmwareAsync() has returned false. This should only happen if another async command is pending pending");
602 }
603
604}
605
606/********************************************************************************************************************************
607** Disable config mode test
608********************************************************************************************************************************/
612 Serial.println();
613 testEnd(true, "Config mode has been disabled successfully");
614 }
615 else {
616 testEnd(false, "disableConfigModeAsync() reports success, but the LD2410 does not send detection data (that typically means that config mode is active).");
617 }
618}
619
621 testPrint("Waiting a 5 seconds, so we can be sure that config mode has really been disabled (checking if data updates are sent).");
622
625}
626
627
628
630 testPrint("Callback for disableConfigModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
631
632 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
634 }
635 else {
636 testEnd(false, "Test has failed due to ", asyncCommandResultToString(asyncResult));
637 }
638}
639
640
642 testStart("disableConfigModeAsync() Test", "Disables config mode. LD2410 will return to normal data detection.");
643
645
646 if (ret) {
647 testPrint("disableConfigModeAsync() conmpleted. Expecting callback.");
648 }
649 else {
650 testEnd(false, "disableConfigModeAsync() has returned false. This should only happen if another async command is pending pending");
651 }
652}
653
654
655/********************************************************************************************************************************
656** Disable disabled config mode test
657********************************************************************************************************************************/
658
660 testPrint("Callback for disableConfigModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
661
662 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
663 testEnd(true);
664 }
665 else {
666 testEnd(false, "Test has failed due to ", asyncCommandResultToString(asyncResult));
667 }
668}
669
671 testStart("disableConfigModeAsync() Test when config mode is already disabled", "Disables config mode, but config mode is not active. The command detects that config mode is inactive and fires the callback avter a minor (1ms) delay.");
673 testEnd(false, "Config mode is enabled. This does not work, when config mode is enabled");
674 return;
675 }
677
678 if (ret) {
679 testPrint("disableConfigModeAsync() conmpleted. Expecting callback.");
680 }
681 else {
682 testEnd(false, "disableConfigModeAsync() has returned false. This should only happen if another async command is pending pending");
683 }
684}
685
686//It seems disableconfig mode still sends an ack, even when config mode is disabled. No need to test this
687//
688///********************************************************************************************************************************
689//** Force Disable disabled config mode test
690//********************************************************************************************************************************/
691//
692//void forceDisableDisabledConfigModeTestDisableDisabledConfigModeCallback(LD2410Async* sender, LD2410Async::AsyncCommandResult asyncResult) {
693// testPrint("Callback for disableConfigModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
694//
695// if (asyncResult == LD2410Async::AsyncCommandResult::TIMEOUT) {
696// testEnd(true, "Command has timmed out as expected");
697// }
698// else {
699// testEnd(false, "Test has failed due to command returning ", asyncCommandResultToString(asyncResult));
700// }
701//}
702//void forceDisableDisabledConfigModeTest() {
703// testStart("Forcing disableConfigModeAsync() Test when config mode is already disabled", "Since config mode is already inactive, the sensor will not send an ACK on the command and therefore the command will timeout");
704// if (ld2410.isConfigModeEnabled()) {
705// testEnd(false,"Config mode is enabled. This does not work, when config mode is enabled");
706// return;
707// }
708//
709//
710// bool ret = ld2410.disableConfigModeAsync(true, forceDisableDisabledConfigModeTestDisableDisabledConfigModeCallback);
711//
712// if (ret) {
713// testPrint("disableConfigModeAsync() with force para executed. Expecting callback.");
714// }
715// else {
716// testEnd(false, "disableConfigModeAsync() has returned false. This should only happen if another async command is pending pending");
717// }
718//}
719
720/********************************************************************************************************************************
721** asyncIsBusy() Test
722********************************************************************************************************************************/
723
725
727 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
728 testEnd(true, "asyncIsBusy() reported true / busy ", asyncIsBusyTest_Count, " times.");
729 }
730 else {
731 testEnd(false, "Test has failed due to diableConfigModeAsync() returning ", asyncCommandResultToString(asyncResult), ". Cant complete asyncIsBusy() test");
732 }
733}
734
736 onceTicker.detach();
737
738 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
739 testPrint("asyncIsBusy() reported true/busy ", asyncIsBusyTest_Count, " times while waiting for the enableConfigModeAsync() command to complete.");
740
742
743 if (ret) {
744 testPrint("Waiting for disableConfigModeAsync() to complete test.");
745 }
746 else {
747 testEnd(false, "disableConfigModeAsync() has returned false. Cant complete asyncIsBusy() test");
748 }
749 }
750 else {
751 testEnd(false, "Test has failed due to enableConfigModeAsync() returning ", asyncCommandResultToString(asyncResult), ". Cant complete asyncIsBusy() test");
752 }
753}
754
756 if (ld2410.asyncIsBusy()) {
758 }
759
761}
762
763
765 testStart("asyncIsBusyTest() Test", "Tries to enable config mode and checks for busy while waiting for the ack");
766
768
769 if (ret) {
770 testPrint("enableConfigModeAsync() started. Checking for asyncIsBusy() while waiting for callback.");
773 }
774 else {
775 testEnd(false, "enableConfigModeAsync() has returned false. Cant execute asyncIsBusy() test");
776 }
777
778}
779
780
781/********************************************************************************************************************************
782** asyncCancel() Test
783********************************************************************************************************************************/
784
786
788 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
789 testEnd(true, "asyncCancel() has successfully canceled the disableConfigModeAsync() command");
790 }
791 else {
792 testEnd(false, "Test has failed due to disableConfigModeAsync() returning ", asyncCommandResultToString(asyncResult), ". Cant complete asyncCancel() test");
793 }
794}
795
798
799 if (ret) {
800 testPrint("Waiting for disableConfigModeAsync() callback to complete test.");
801 }
802 else {
803 testEnd(false, "disableConfigModeAsync() has returned false. Cant complete asyncCancel() test");
804 }
805}
806
808 onceTicker.detach();
809
811 testPrint("Calledback reportet CANCELED as expected.");
812 testPrint("Will wait 5 secs so the disableConfigModeAsync() used for the test can complete, before we disable config mode again.");
813
815
816
817 }
818 else {
819 testEnd(false, "Test has failed due to enableConfigModeAsync() returning ", asyncCommandResultToString(asyncResult), ". Cant complete asyncCancel() test");
820 }
821}
822
824 testPrint("Executing asyncCancel()");
826}
827
828
830 testStart("asyncCancelTest() Test", "Tries to enable config mode and cancels command.");
831
833
834 if (ret) {
835 testPrint("enableConfigModeAsync() started. Checking for asyncCancel() while waiting for callback.");
837 }
838 else {
839 testEnd(false, "enableConfigModeAsync() has returned false. Cant execute asyncCancel() test");
840 }
841
842}
843
844
845
846/********************************************************************************************************************************
847** requestFirmwareAsync() Test
848********************************************************************************************************************************/
849
851 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
852 testEnd(true, "requestFirmwareAsync() callback reports success");
853 }
854 else {
855 testEnd(false, "requestFirmwareAsyncTest has failed. Command result: ", asyncCommandResultToString(asyncResult));
856 }
857}
858
860 testStart("requestFirmwareAsync() Test");
861
863
864 if (ret) {
865 testPrint("requestFirmwareAsync() started. Waiting for callback.");
866
867 }
868 else {
869 testEnd(false, "requestFirmwareAsync() has returned false. Cant execute test");
870 }
871}
872
873/********************************************************************************************************************************
874** requestBluetoothMacAddressAsync() Test
875********************************************************************************************************************************/
876
878 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
879 testEnd(true, "requestBluetoothMacAddressAsync() callback reports success");
880 }
881 else {
882 testEnd(false, "requestBluetoothMacAddressAsyncTest has failed. Command result: ", asyncCommandResultToString(asyncResult));
883 }
884}
885
887 testStart("requestBluetoothMacAddressAsync() Test");
888
890
891 if (ret) {
892 testPrint("requestBluetoothMacAddressAsync() started. Waiting for callback.");
893
894 }
895 else {
896 testEnd(false, "requestBluetoothMacAddressAsync() has returned false. Cant execute test");
897 }
898}
899
900/********************************************************************************************************************************
901** requestDistanceResolutionAsync() Test
902********************************************************************************************************************************/
903
905 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
906 const LD2410Types::ConfigData& cfg = sender->getConfigDataRef();
908 {
909 testEnd(false, "requestDistanceResolutionAsync() callback reports success, but the received data is invalid or no data has been received");
910 }
911 else {
912 testEnd(true, "requestDistanceResolutionAsync() callback reports success and valid data has been received");
913 }
914 }
915 else {
916 testEnd(false, "requestDistanceResolutionAsyncTest has failed. Command result: ", asyncCommandResultToString(asyncResult));
917 }
918}
919
921 testStart("requestDistanceResolutionAsync() Test");
924
925 if (ret) {
926 testPrint("requestDistanceResolutionAsync() started. Waiting for callback.");
927
928 }
929 else {
930 testEnd(false, "requestDistanceResolutionAsync() has returned false. Cant execute test");
931 }
932}
933
934
935
936
937/********************************************************************************************************************************
938** Inactivity handling Test
939********************************************************************************************************************************/
940
943
944 ld2410.setInactivityTimeoutMs(60000); //Set inactivity timeout back to 60 secs
945
947 testEnd(false, "Inactivity test has failed. LD2410 did not go back to normal mode inactivity handling.");
948 }
949 else {
950
951 testEnd(true, "Inactivity test has passed. LD2410 has started to send data after inactivity handling.");
952 }
953}
954
956 testPrint("Callback for enableConfigModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
957 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
958 testPrint("LD2410 is now in config mode. Waiting 25 secs to see whether it goes back to detection mode after inactivity timeout (will try cancel without effect first and then try exit config mode).");
959
961
962 onceTicker.once_ms(25000, inactivityHandlingTestEnd); //Wait 25 secs, so we are sure that the inactivity timeout of 10 secs has passed and 2 attempts to recover (cancel async and exit config mode) have been made
963 }
964 else {
965 testEnd(false, "Test has failed due enableConfigModeAsync() failure. Return value: ", asyncCommandResultToString(asyncResult));
966 }
967}
968
970 testStart("Inactivity Handling Test");
971
973
974 ld2410.setInactivityTimeoutMs(10000); //10 secs
975
976 if (ld2410.getInactivityTimeoutMs() != 10000) {
977 testEnd(false, "Inactivity timeout could not be set correctly");
978 return;
979 }
980
981 testPrint("Inacctivity handling enabled and timeout set to 10 secs");
982 testPrint("Will activate config mode, remain sillent and wait to see if sensor goes back to datection mode automatically.");
983
985 if (ret) {
986 testPrint("enableConfigModeAsync() executed. Expecting callback.");
987 }
988 else {
989 testEnd(false, "enableConfigModeAsync() has returned false. This should only happen if another async command is pending pending");
990 }
991}
992
993/********************************************************************************************************************************
994** Inactivity Handling Disable Test
995********************************************************************************************************************************/
996
997
999 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1000 testEnd(true, "As expected inactivity handling did not kick in an revert sensor to detection mode");
1001 }
1002 else {
1003 testEnd(false, "As expected inactivity handling did not kick in an revert sensor to detection modee, but could not execute disableConfigModeAsync() did not succedd.");
1004 }
1005
1006 testEnd(true, "Disable inactivity handling test has passed. LD2410 has not gone back into detection mode.");
1007
1008}
1009
1012 ld2410.setInactivityTimeoutMs(60000); //Set inactivity timeout back to 60 secs
1013 ld2410.enableInactivityHandling(); //reenable inactivity handling
1014
1017 if (ret) {
1018 testPrint("As expected inactivity handling did not kick in an revert sensor to detection mode.");
1019 }
1020 else {
1021 testEnd(false, "As expected inactivity handling did not kick in an revert sensor to detection mode, but could not execute disableConfigModeAsync() at end of test.");
1022 }
1023 }
1024 else {
1025 testEnd(false, "Disable inactivity handling test has failed. LD2410 did go back into detection mode.");
1026 }
1027}
1028
1029
1031 testPrint("Callback for enableConfigModeAsync() executed. Result: ", asyncCommandResultToString(asyncResult));
1032 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1033 testPrint("Will wait for 25 secs to see whether the LD2410 goes back to detection mode (it should not do this since inactivity handling is disabled).");
1034
1036
1037 onceTicker.once_ms(25000, disableInactivityHandlingTestTimeElapsed); //Wait 25 secs, so we are sure that the inactivity timeout of 10 secs has passed
1038
1039
1040 }
1041 else {
1042 testEnd(false, "Test has failed due enableConfigModeAsync() failure. Return value: ", asyncCommandResultToString(asyncResult));
1043 }
1044}
1045
1046
1048 testStart("Disable Inactivity Handling Test");
1049
1050 testPrint("DIsabling inactivity handling and setting a 10 secs timeout");
1051 ld2410.setInactivityTimeoutMs(10000); //10 secs
1054 testEnd(false, "Inactivity handling could not be disabled");
1055 return;
1056 }
1058 if (ret) {
1059 testPrint("enableConfigModeAsync() executed. Expecting callback.");
1060 }
1061 else {
1062 testEnd(false, "enableConfigModeAsync() has returned false. This should only happen if another async command is pending pending");
1063 }
1064}
1065
1066
1067/********************************************************************************************************************************
1068** Config data struct validation test
1069********************************************************************************************************************************/
1070
1072 testStart("ConfigData Struct Validation Test");
1073
1074 testPrint("Initialize new, empty config data struct.");
1075 LD2410Types::ConfigData testConfigData;
1076
1077 if (testConfigData.isValid()) {
1078 testEnd(false, "Newly initialized config data struct is valid. This should not be the case.");
1079 return;
1080 }
1081 else {
1082 testPrint("Newly initialized config data struct is not valid as expected.");
1083 }
1084
1085 testPrint("Initialize config data struct with valid values.");
1086 initializeConfigDataStruct(testConfigData);
1087
1088 if (!testConfigData.isValid()) {
1089 testEnd(false, "Config data struct is not valid (but has been initialized with values.");
1090 return;
1091 }
1092 else {
1093 testPrint("Config data struct is valid");
1094 }
1095
1096
1098 if (testConfigData.isValid()) {
1099 testEnd(false, "Config data struct is valid, but lightControl value is NOT_SET (which is invalid)");
1100 return;
1101 }
1102 else {
1103 testPrint("Config data struct with lightControl value NOT_SET is not valid as expected.");
1104 }
1106
1108 if (testConfigData.isValid()) {
1109 testEnd(false, "Config data struct is valid, but outputControl value is NOT_SET (which is invalid)");
1110 return;
1111 }
1112 else {
1113 testPrint("Config data struct with outputControl value NOT_SET is not valid as expected.");
1114 }
1116
1118 if (testConfigData.isValid()) {
1119 testEnd(false, "Config data struct is valid, but distanceResolution value is NOT_SET (which is invalid)");
1120 return;
1121 }
1122 else {
1123 testPrint("Config data struct with distanceResolution value NOT_SET is not valid as expected.");
1124 };
1126
1127 for (int i = 0; i < 9; i++) {
1128 testConfigData.distanceGateMotionSensitivity[i] = 210;
1129 if (testConfigData.isValid()) {
1130 testEnd(false, "distanceGateMotionSensitivity is out of range, but config data struct is reported as valid");
1131 return;
1132 }
1133 testConfigData.distanceGateMotionSensitivity[i] = 5 + 5 * i;
1134
1135 testConfigData.distanceGateStationarySensitivity[i] = 200;
1136 if (testConfigData.isValid()) {
1137 testEnd(false, "distanceGateStationarySensitivity is out of range, but config data struct is reported as valid");
1138 return;
1139 }
1140
1141 testConfigData.distanceGateStationarySensitivity[i] = 100 - i;
1142 }
1143 testPrint("Checking distanceGateMotionSensitivity and distanceGateStationarySensitivity with out of range values reported invalid as excpected.");
1144
1145
1146 testConfigData.maxMotionDistanceGate = 0;
1147 if (testConfigData.isValid()) {
1148 testEnd(false, "maxMotionDistanceGate is out of range, but config data struct is reported as valid");
1149 return;
1150 };
1151 testConfigData.maxMotionDistanceGate = 5;
1152 testConfigData.maxStationaryDistanceGate = 10;
1153 if (testConfigData.isValid()) {
1154 testEnd(false, "maxStationaryDistanceGate is out of range, but config data struct is reported as valid");
1155 return;
1156 };
1157 testConfigData.maxStationaryDistanceGate = 4;
1158
1159 testEnd(true, "Config data struct validation test has passed.");
1160}
1161
1162
1163/********************************************************************************************************************************
1164** requestAllConfigSettingsAsync() Test
1165********************************************************************************************************************************/
1166
1168 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1169 const LD2410Types::ConfigData& cfg = sender->getConfigDataRef();
1170 if (!cfg.isValid())
1171 {
1172 testEnd(false, "requestAllConfigSettingsAsync() callback reports success, but no data has been received or data is invalid.");
1173 }
1174 else {
1175 testEnd(true, "requestAllConfigSettingsAsync() callback reports success and data has been received");
1176 }
1177 }
1178 else {
1179 testEnd(false, "requestAllConfigSettingsAsyncTest has failed. Command result: ", asyncCommandResultToString(asyncResult));
1180 }
1181}
1182
1184 testStart("requestAllConfigSettingsAsync() Test");
1185
1188
1189 if (ret) {
1190 testPrint("requestAllConfigSettingsAsync() started. Waiting for callback.");
1191
1192 }
1193 else {
1194 testEnd(false, "requestAllConfigSettingsAsync() has returned false. Cant execute test");
1195 }
1196}
1197
1198/********************************************************************************************************************************
1199** getConfigData() & getConfigDataRef() Test
1200********************************************************************************************************************************/
1201
1202
1203
1205 testStart("getConfigData() & getConfigDataRef() Test");
1206
1207 testPrint("Getting a clone of the config data struct with getConfigData()");
1209 testPrint("The cloned struct has been stored in variable orgConfigData so we can use it to restore the org data at the end of the tests");
1210
1211 testPrint("Getting a reference to the config data struct with getConfigDataRef()");
1212 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1213
1214 testPrint("Check if the are both the same (they should be)");
1215 if (!orgConfigData.equals(configDataRef)) {
1216 testEnd(false, "getConfigData() test has failed. The two config data structs are not equal.");
1217 return;
1218 }
1219 testPrint("The cloned config data struct and the referenced struct are the same as expected.");
1220 testEnd(true, "getConfigData() & getConfigDataRef() test has passed.");
1221
1222}
1223
1224
1225/********************************************************************************************************************************
1226** Aux control settings test
1227********************************************************************************************************************************/
1228
1229
1233
1235 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1236
1237 //Get reference to the last read config data
1238 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1239
1240 //Check if the read values are what we have configured
1241 if (configDataRef.lightControl == lightControlToApply
1242 && configDataRef.lightThreshold == lightThresholdToApply
1243 && configDataRef.outputControl == outputControlToApply)
1244 {
1245 testPrint("requestAuxControlSettingsAsync() has returned the expected values");
1246 testEnd(true, "configureAuxControlSettingsAsync() and requestAuxControlSettingsAsync() have worked as expected");
1247 }
1248 else {
1249
1250 testEnd(false, "requestAuxControlSettingsAsync() has retuned unexpected value. Either the requesyt command or the configure command did not work as expected.");
1251 }
1252 }
1253 else {
1254 testEnd(false, "requestAuxControlSettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1255 }
1256}
1257
1259 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1260 testPrint("configureAuxControlSettingsAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1261
1263 if (ret) {
1264 testPrint("requestAuxControlSettingsAsync() started. Waiting for callback.");
1265 }
1266 else {
1267 testEnd(false, "requestAuxControlSettingsAsync() has returned false. Cant execute test");
1268 }
1269 }
1270 else {
1271 testEnd(false, "configureAuxControlSettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1272 }
1273}
1274
1276 testStart("configureAuxControlSettingsAsync() & requestAuxControlSettingsAsync() Test", "Tries to change the lightContol, lightThreshold and outputControl config settings und reads back the values");
1277
1278 //Get reference to the last read config data
1279 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1280
1281 //Get changed values for all the paras of the command
1284 lightThresholdToApply = configDataRef.lightThreshold != 99 ? 99 : 101;
1285
1286 //Call the command to configure the aux control settings
1288 if (ret) {
1289 testPrint("configureAuxControlSettingsAsync() started. Waiting for callback.");
1290 }
1291 else {
1292 testEnd(false, "configureAuxControlSettingsAsync() has returned false. Cant execute test");
1293 }
1294}
1295
1296
1297/********************************************************************************************************************************
1298** Gate parameters test
1299********************************************************************************************************************************/
1300
1303
1304
1306 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1307
1308 //Get reference to the last read config data
1309 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1310
1311 //Check if the read values are what we have configured
1314 {
1315 testPrint("requestGateParametersAsync() has returned the expected values");
1316 testEnd(true, "configureDistanceGateSensitivityAsync() and requestGateParametersAsync() have worked as expected");
1317 }
1318 else {
1319 testEnd(false, "requestGateParametersAsync() has retuned unexpected values. Either the request command or the configure command did not work as expected.");
1320 }
1321 }
1322 else {
1323 testEnd(false, "requestGateParametersAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1324 }
1325}
1326
1328 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1329 testPrint("configureDistanceGateSensitivityAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1330
1332 if (ret) {
1333 testPrint("requestGateParametersAsync() started. Waiting for callback.");
1334 }
1335 else {
1336 testEnd(false, "requestGateParametersAsync() has returned false. Cant execute test");
1337 }
1338 }
1339 else {
1340 testEnd(false, "configureDistanceGateSensitivityAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1341 }
1342}
1343
1345 testStart("configureDistanceGateSensitivityAsync() & requestGateParametersAsync() Test", "Tries to change gate sensitivity parameter config settings und reads back the values");
1346
1347 //Get reference to the last read config data
1348 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1349
1350 //Determine changed values to apply
1351 movingThresholdToApply = configDataRef.distanceGateMotionSensitivity[4] != 55 ? 55 : 44;
1352 stationaryThresholdToApply = configDataRef.distanceGateStationarySensitivity[4] != 66 ? 66 : 77;
1353
1354
1355 //Call the command to configure gate parameters for gate 4 (could be any other gate as well).
1357 if (ret) {
1358 testPrint("configureDistanceGateSensitivityAsync() started. Waiting for callback.");
1359 }
1360 else {
1361 testEnd(false, "configureDistanceGateSensitivityAsync() has returned false. Cant execute test");
1362 }
1363}
1364
1365
1366/********************************************************************************************************************************
1367** Max Gate test
1368********************************************************************************************************************************/
1369
1373
1374
1376 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1377
1378 //Get reference to the last read config data
1379 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1380
1381 //Check if the read values are what we have configured
1382 if (configDataRef.maxMotionDistanceGate == maxMovingGateToApply
1384 && configDataRef.noOneTimeout == nooneTimeoutToApply)
1385 {
1386 testPrint("requestGateParametersAsync() has returned the expected values");
1387 testEnd(true, "configureMaxGateAndNoOneTimeoutAsync() and requestGateParametersAsync() have worked as expected");
1388 }
1389 else {
1390 testEnd(false, "requestGateParametersAsync() has retuned unexpected values. Either the request command or the configure command did not work as expected.");
1391 }
1392 }
1393 else {
1394 testEnd(false, "requestGateParametersAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1395 }
1396}
1397
1399 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1400 testPrint("configureMaxGateAndNoOneTimeoutAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1401
1403 if (ret) {
1404 testPrint("requestGateParametersAsync() started. Waiting for callback.");
1405 }
1406 else {
1407 testEnd(false, "requestGateParametersAsync() has returned false. Cant execute test");
1408 }
1409 }
1410 else {
1411 testEnd(false, "configureMaxGateAndNoOneTimeoutAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1412 }
1413}
1414
1416 testStart("configureMaxGateAndNoOneTimeoutAsync() & requestGateParametersAsync() Test", "Tries to change max gate parameter and noone timeout config settings und reads back the values");
1417
1418 //Get reference to the last read config data
1419 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1420
1421 //Determine changed values to apply
1422 maxMovingGateToApply = configDataRef.maxMotionDistanceGate != 6 ? 6 : 7;
1423 maxStationaryGateToApply = configDataRef.maxStationaryDistanceGate != 4 ? 4 : 5;
1424 nooneTimeoutToApply = configDataRef.noOneTimeout != 33 ? 33 : 22;
1425
1426 //Call the command to configure gate parameters for gate 4 (could be any other gate as well).
1428 if (ret) {
1429 testPrint("configureMaxGateAndNoOneTimeoutAsync() started. Waiting for callback.");
1430 }
1431 else {
1432 testEnd(false, "configureMaxGateAndNoOneTimeoutAsync() has returned false. Cant execute test");
1433 }
1434}
1435
1436
1437/********************************************************************************************************************************
1438** Distance resolution test
1439********************************************************************************************************************************/
1440
1442
1444 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1445
1446 //Get reference to the last read config data
1447 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1448
1449 //Check if the read values are what we have configured
1450 if (configDataRef.distanceResolution == distanceResolutionToApply)
1451 {
1452 testPrint("requestDistanceResolutionAsync() has returned the expected values");
1453 testEnd(true, "configureDistanceResolutionAsync() and requestDistanceResolutionAsync() have worked as expected");
1454 }
1455 else {
1456 testEnd(false, "requestDistanceResolutionAsync() has retuned unexpected values. Either the request command or the configure command did not work as expected.");
1457 }
1458 }
1459 else {
1460 testEnd(false, "requestDistanceResolutionAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1461 }
1462}
1463
1465 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1466 testPrint("configureDistanceResolutionAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1467
1469 if (ret) {
1470 testPrint("requestDistanceResolutionAsync() started. Waiting for callback.");
1471 }
1472 else {
1473 testEnd(false, "requestDistanceResolutionAsync() has returned false. Cant execute test");
1474 }
1475 }
1476 else {
1477 testEnd(false, "configureDistanceResolutionAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1478 }
1479}
1480
1482 testStart("configureDistanceResolutionAsync() & requestDistanceResolutionAsync() Test", "Tries distance resolution settings and reads back the values");
1483
1484 //Get reference to the last read config data
1485 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1486
1487 //Determine changed values to apply
1489
1490 //Call the command to configure distance resolution
1492 if (ret) {
1493 testPrint("configureDistanceResolutionAsync() started. Waiting for callback.");
1494 }
1495 else {
1496 testEnd(false, "configureDistanceResolutionAsync() has returned false. Cant execute test");
1497 }
1498}
1499
1500
1501/********************************************************************************************************************************
1502** Configure all settings test
1503********************************************************************************************************************************/
1504//Also used in restore factory settings test
1506
1508 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1509
1510 //Get reference to the last read config data
1511 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1512
1513 //Check if the read values are what we have configured
1514 if (configDataToApply.equals(configDataRef))
1515 {
1516 testPrint("requestAllConfigSettingsAsync() has returned the expected values");
1517 testEnd(true, "configureAllConfigSettingsAsync() and requestAllConfigSettingsAsync() have worked as expected");
1518 }
1519 else {
1520 testEnd(false, "requestAllConfigSettingsAsync() has retuned unexpected values. Either the request command or the configure command did not work as expected.");
1521 }
1522 }
1523 else {
1524 testEnd(false, "requestAllConfigSettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1525 }
1526}
1527
1529 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1530 testPrint("configureAllConfigSettingsAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1531
1533 if (ret) {
1534 testPrint("requestAllConfigSettingsAsync() started. Waiting for callback.");
1535 }
1536 else {
1537 testEnd(false, "requestAllConfigSettingsAsync() has returned false. Cant execute test");
1538 }
1539 }
1540 else {
1541 testEnd(false, "configureAllConfigSettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1542 }
1543}
1544
1546 testStart("configureAllConfigSettingsAsync() & requestAllConfigSettingsAsync() Test", "Tries to change all config parameters and reads back the values");
1547
1548 //Get clone of the last read config data
1550
1551 //Modify the config data
1559
1560 for (size_t i = 0; i < 9; i++)
1561 {
1564 }
1565
1566 //Call the command to apply all config settings
1568 if (ret) {
1569 testPrint("configureAllConfigSettingsAsync() started. Waiting for callback.");
1570 }
1571 else {
1572 testEnd(false, "configureAllConfigSettingsAsync() has returned false. Cant execute test");
1573 }
1574}
1575
1576/********************************************************************************************************************************
1577** Restore factory settings test
1578********************************************************************************************************************************/
1579
1580
1581
1583 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1584
1585 //Get reference to the last read config data
1586 const LD2410Types::ConfigData& configDataRef = ld2410.getConfigDataRef();
1587
1588 //Check if the read values are different from what we configured earlier
1589 if (!configDataToApply.equals(configDataRef))
1590 {
1591 testPrint("requestAllConfigSettingsAsync() has returned the expected values");
1592 testEnd(true, "restoreFactorySettingsAsync() and requestAllConfigSettingsAsync() have worked as expected");
1593 }
1594 else {
1595 testEnd(false, "requestAllConfigSettingsAsync() has returned unexpected values (previous config). Either the request command or more likely the configure command did not work as expected.");
1596 }
1597 }
1598 else {
1599 testEnd(false, "requestAllConfigSettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1600 }
1601}
1602
1604 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1605 testPrint("configureAllConfigSettingsAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1606
1608 if (ret) {
1609 testPrint("requestAllConfigSettingsAsync() started. Waiting for callback.");
1610 }
1611 else {
1612 testEnd(false, "requestAllConfigSettingsAsync() has returned false. Cant execute test");
1613 }
1614 }
1615 else {
1616 testEnd(false, "restoreFactorySettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1617 }
1618}
1619
1621 testStart("restoreFactorySettingsAsync() Test", "Restores factory settings and checks if the have changed, compared to the previous settings");
1622
1623
1624 //Call the command to restore factory settings
1626 if (ret) {
1627 testPrint("restoreFactorySettingsAsync() started. Waiting for callback.");
1628 }
1629 else {
1630 testEnd(false, "restoreFactorySettingsAsync() has returned false. Cant execute test");
1631 }
1632}
1633
1634
1635
1636/********************************************************************************************************************************
1637** Write back org config data
1638**
1639** This is not really a test. It just writes back the org config data that was found on the sensor
1640** at the start of the configuration tests.
1641********************************************************************************************************************************/
1643 if (asyncResult == LD2410Async::AsyncCommandResult::SUCCESS) {
1644 testEnd(true, "configureAllConfigSettingsAsync() has reported success. Fetch the relevant settings so we can check if they have been configured as expected.");
1645 }
1646 else {
1647 testEnd(false, "configureAllConfigSettingsAsync() has failed. Command result: ", asyncCommandResultToString(asyncResult));
1648 }
1649}
1650
1652 testStart("Write Back Org Config Data", "Tries to write back the org config data that was read at the begin of the tests");
1653
1654 //Call the command to apply all config settings
1656 if (ret) {
1657 testPrint("configureAllConfigSettingsAsync() started. Waiting for callback.");
1658 }
1659 else {
1660 testEnd(false, "configureAllConfigSettingsAsync() has returned false. Cant execute test");
1661 }
1662}
1663
1664/********************************************************************************************************************************
1665** Test sequence
1666** Defines the sequence of the tests
1667********************************************************************************************************************************/
1668
1669
1670TestEntry actions[] = {
1671 { beginTest, false },
1672 { rebootTest, false },
1673 { normalModeDataReceiveTest, false },
1674 { enableEngineeringModeTest, false },
1676 { disableEngineeringModeTest, false },
1677 { enableConfigModeTest, true },
1678 { configModePersistenceTest, true },
1679 { disableConfigModeTest, false },
1681 //{ forceDisableDisabledConfigModeTest, false },
1682 { asyncIsBusyTest, false },
1683 { asyncCancelTest, false },
1684 { inactivityHandlingTest, false },
1686
1687 // Config data Tests
1690
1691 { requestAllConfigSettingsAsyncTest, false }, //This steps fetches all config data
1692 { getConfigDataTest, false }, //This test does also save the orgConfigData
1693
1694 { auxControlSettingsTest, false },
1695 { gateParametersTest, false },
1697 { distanceResolutionTest, false },
1698 { configureAllSettingsTest, false },
1699 { restoreFactorySettingsTest, false },
1700 { writeBackOrgConfigData, false }, //Writes back the earlier saved org config data
1701
1702 // Static data tests
1704 { requestFirmwareAsyncTest, false }
1705};
1706
1707const int NUM_ACTIONS = sizeof(actions) / sizeof(actions[0]);
1708
1709
1710/********************************************************************************************************************************
1711** Start next test
1712** Executes the next test in the test sequence
1713********************************************************************************************************************************/
1714
1718 actions[currentTestIndex].action(); // call the function
1719 }
1720 else {
1721 printBigMessage("ALL TESTS PASSED", '#');
1722 testSequenceRunning = false;
1723 }
1724}
1725/********************************************************************************************************************************
1726** Starts the first test
1727********************************************************************************************************************************/
1729 currentTestIndex = -1;
1730 testSequenceRunning = true;
1731 startNextTest();
1732};
1733
1734/********************************************************************************************************************************
1735** Setup
1736********************************************************************************************************************************/
1737void setup() {
1738 //Using a big output buffer, since this test sketch prints a lot of data.
1739 Serial.setTxBufferSize(2048);
1740 // Initialize USB serial for debug output
1741 Serial.begin(115200);
1742 while (!Serial) {
1743 ; // wait for Serial Monitor
1744 }
1745
1746 // Initialize Serial1 with user-defined pins and baudrate
1748
1749 //Delay a few ms to ensure that Serial is available
1750 delay(10);
1751
1752 //Start the tests
1753 startTests();
1754
1755}
1756
1757/********************************************************************************************************************************
1758** Loop
1759** Loop is empty since all test are executed in a async fashion
1760********************************************************************************************************************************/
1761void loop() {
1762 //Nothing to do
1763 delay(1000);
1764}
Asynchronous driver class for the LD2410 human presence radar sensor.
Definition LD2410Async.h:38
bool configureAuxControlSettingsAsync(LD2410Types::LightControl light_control, byte light_threshold, LD2410Types::OutputControl output_control, AsyncCommandCallback callback)
Configures the auxiliary control parameters (light and output pin).
bool requestGateParametersAsync(AsyncCommandCallback callback)
Requests the current gate parameters from the sensor.
void asyncCancel()
Cancels any pending asynchronous command or sequence.
bool configureDistanceResolutionAsync(LD2410Types::DistanceResolution distanceResolution, AsyncCommandCallback callback)
Configures the distance resolution of the radar.
bool rebootAsync(AsyncCommandCallback callback)
Reboots the sensor.
bool begin()
Starts the background task that continuously reads data from the sensor.
AsyncCommandResult
Result of an asynchronous command execution.
Definition LD2410Async.h:47
@ TIMEOUT
No ACK received within the expected time window.
@ FAILED
Command failed (sensor responded with negative ACK).
@ SUCCESS
Command completed successfully and ACK was received.
@ CANCELED
Command was canceled by the user before completion.
bool configureMaxGateAndNoOneTimeoutAsync(byte maxMovingGate, byte maxStationaryGate, unsigned short noOneTimeout, AsyncCommandCallback callback)
Configures the maximum detection gates and "no-one" timeout on the sensor.
bool requestAllStaticDataAsync(AsyncCommandCallback callback)
Requests all static information from the sensor.
bool restoreFactorySettingsAsync(AsyncCommandCallback callback)
Restores factory settings of the sensor.
void resetConfigData()
bool disableConfigModeAsync(AsyncCommandCallback callback)
Disables config mode on the radar.
bool isConfigModeEnabled() const
Detects if config mode is enabled.
bool configureAllConfigSettingsAsync(const LD2410Types::ConfigData &configToWrite, bool writeAllConfigData, AsyncCommandCallback callback)
Applies a full ConfigData struct to the LD2410.
void enableInactivityHandling()
Convenience method: enables inactivity handling.
bool enableConfigModeAsync(AsyncCommandCallback callback)
Enables config mode on the radar.
void setInactivityTimeoutMs(unsigned long timeoutMs=60000)
Sets the timeout period for inactivity handling.
LD2410Types::ConfigData getConfigData() const
Returns a clone of the current configuration data of the radar.
void disableInactivityHandling()
Convenience method: disables inactivity handling.
bool requestDistanceResolutionAsync(AsyncCommandCallback callback)
Requests the current distance resolution setting from the sensor.
bool asyncIsBusy()
Checks if an asynchronous command is currently pending.
unsigned long getInactivityTimeoutMs() const
Returns the current inactivity handling timeout period.
bool requestFirmwareAsync(AsyncCommandCallback callback)
Requests the firmware version of the sensor.
bool requestBluetoothMacAddressAsync(AsyncCommandCallback callback)
Requests the Bluetooth MAC address.
bool requestAllConfigSettingsAsync(AsyncCommandCallback callback)
Requests all configuration settings from the sensor.
bool configureDistanceGateSensitivityAsync(const byte movingThresholds[9], const byte stationaryThresholds[9], AsyncCommandCallback callback)
Configures sensitivity thresholds for all gates at once.
bool requestAuxControlSettingsAsync(AsyncCommandCallback callback)
Requests the current auxiliary control settings.
void onDetectionDataReceived(DetectionDataCallback callback)
Registers a callback for new detection data.
bool enableEngineeringModeAsync(AsyncCommandCallback callback)
Enables engineering mode.
bool isInactivityHandlingEnabled() const
Returns whether inactivity handling is currently enabled.
const LD2410Types::DetectionData & getDetectionDataRef() const
Access the current detection data without making a copy.
bool disableEngineeringModeAsync(AsyncCommandCallback callback)
Disables engineering mode.
const LD2410Types::ConfigData & getConfigDataRef() const
Access the current config data without making a copy.
OutputControl
Logic level behavior of the auxiliary output pin.
@ NOT_SET
Placeholder for the initial value. Do not use as a config value (it will cause the configuration comm...
@ DEFAULT_HIGH_DETECTED_LOW
Default high, goes LOW when detection occurs.
@ DEFAULT_LOW_DETECTED_HIGH
Default low, goes HIGH when detection occurs.
DistanceResolution
Distance resolution per gate for detection.
@ NOT_SET
Placeholder for the initial value. Do not use as a config value (it will cause the configuration comm...
@ RESOLUTION_20CM
Each gate is about 0.20 m, max range about 1.8 m.
@ RESOLUTION_75CM
Each gate is about 0.75 m, max range about 6 m.
LightControl
Light-dependent control status of the auxiliary output.
Definition LD2410Types.h:93
@ NOT_SET
Placeholder for the initial value. Do not use as a config value (it will cause the configuration comm...
@ LIGHT_BELOW_THRESHOLD
Condition: light < threshold.
@ LIGHT_ABOVE_THRESHOLD
Condition: light >= threshold.
byte distanceGateStationarySensitivity[9]
Stationary sensitivity values per gate (0-100).
void print() const
Debug helper: print configuration contents to Serial.
byte distanceGateMotionSensitivity[9]
Motion sensitivity values per gate (0-100).
byte maxMotionDistanceGate
Furthest gate used for motion detection.
OutputControl outputControl
Logic configuration of the OUT pin.
bool equals(const ConfigData &other) const
Compares this ConfigData with another for equality.
byte lightThreshold
Threshold for auxiliary light control (0-255).
bool isValid() const
Validates the configuration data for correctness.
LightControl lightControl
Light-dependent auxiliary control mode.
byte maxStationaryDistanceGate
Furthest gate used for stationary detection.
byte numberOfGates
Number of distance gates (2-8). This member is read-only; changing its value will not influence the r...
DistanceResolution distanceResolution
Current distance resolution. A reboot is required to activate changes after configureAllConfigSetting...
unsigned short noOneTimeout
Timeout (seconds) until "no presence" is declared.
Holds the most recent detection data reported by the radar.
bool engineeringMode
True if engineering mode data was received.
void disableEngineeringModeTest()
Definition unitTest.ino:491
void enableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:528
void disableInactivityHandlingTestTimeElapsed()
void asyncCancelTest()
Definition unitTest.ino:829
void requestFirmwareAsyncTest()
Definition unitTest.ino:859
void requestDistanceResolutionAsyncTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:904
void enableConfigModeTestEnd()
Definition unitTest.ino:508
void startNextTest()
void requestAllConfigSettingsAsyncTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void configureAllSettingsTestConfigureCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void engineeringModeDataReceiveTestEnd()
Definition unitTest.ino:436
void enableEngineeringModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:407
void inactivityHandlingTest()
Definition unitTest.ino:969
void rebootTest()
Definition unitTest.ino:343
LD2410Types::DistanceResolution distanceResolutionToApply
void restoreFactorySettingsTesttRequestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
int dataUpdateCounter_engineeringModeCount
Definition unitTest.ino:278
Ticker onceTicker
Definition unitTest.ino:49
void normalModeDataReceiveTestEnd()
Definition unitTest.ino:363
byte maxMovingGateToApply
void asyncCancelTestDiableConfigMode()
Definition unitTest.ino:796
void startDataUpdateCounter()
Definition unitTest.ino:302
void configureAllSettingsTest()
void writeBackOrgConfigDataCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void enableEngineeringModeTest()
Definition unitTest.ino:419
void restoreFactorySettingsTest()
void engineeringModeDataReceiveTest()
Definition unitTest.ino:461
void testEnd(bool success, const Args &... commentParts)
Definition unitTest.ino:159
void requestFirmwareAsyncTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:850
void restoreFactorySettingsTestResetCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void requestAllConfigSettingsAsyncTest()
HardwareSerial RadarSerial(1)
void writeBackOrgConfigData()
void beginTest()
Definition unitTest.ino:316
void inactivityHandlingTestEnd()
Definition unitTest.ino:941
void disableInactivityHandlingTest()
void configureMaxGateAndNoOneTimeoutAsyncTestRequestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void setup()
void enableConfigModeTestCheckForDataUpdates()
Definition unitTest.ino:519
void gateParametersTest()
void asyncCancelCancelCommand()
Definition unitTest.ino:823
void gateParametersTestConfigureSingleGateCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void configDataStructValidationTest()
int asyncIsBusyTest_Count
Definition unitTest.ino:724
void disableInactivityHandlingTestConfigModeDisabled(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:998
unsigned long testStartMs
Definition unitTest.ino:55
void printOne(const T &value)
Definition unitTest.ino:118
void configModePersistenceTestEnd()
Definition unitTest.ino:558
#define RADAR_BAUDRATE
Definition unitTest.ino:33
void disableConfigModeTestEnd()
Definition unitTest.ino:609
void stopDataUpdateCounter()
Definition unitTest.ino:308
void testPrint(const Args &... args)
Definition unitTest.ino:229
#define RADAR_RX_PIN
Unit Test for most methods of the lib.
Definition unitTest.ino:28
int asyncCancelTest_Count
Definition unitTest.ino:785
const int LINE_LEN
Definition unitTest.ino:69
void asyncIsBusyTestEnableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:735
void distanceResolutionTest()
void asyncIsBusyTestCheckBusy()
Definition unitTest.ino:755
int testSuccessCount
Definition unitTest.ino:60
byte stationaryThresholdToApply
LD2410Types::ConfigData orgConfigData
Definition unitTest.ino:52
void disableInactivityHandlingTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void disableDisabledConfigModeTest()
Definition unitTest.ino:670
void rebootTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:331
byte maxStationaryGateToApply
int currentTestIndex
Definition unitTest.ino:64
Ticker testDelayTicker
Definition unitTest.ino:156
LD2410Async ld2410(RadarSerial)
void asyncCancelTestDisableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:787
void gateParametersTestRequestGateParametersCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void distanceResolutionTestRequestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void requestDistanceResolutionAsyncTest()
Definition unitTest.ino:920
void disableEngineeringModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:479
LD2410Types::LightControl lightControlToApply
void auxControlSettingsTestConfigureCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void asyncIsBusyTest()
Definition unitTest.ino:764
void configModePersistenceTest()
Definition unitTest.ino:593
void disableConfigModeTestCheckForDataUpdates()
Definition unitTest.ino:620
TestEntry actions[]
void printLine(char c, int len)
Definition unitTest.ino:93
byte movingThresholdToApply
void configureMaxGateAndNoOneTimeoutAsyncTestConfigureCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
short nooneTimeoutToApply
void configureMaxGateAndNoOneTimeoutAsyncTest()
void enableConfigModeTest()
Definition unitTest.ino:540
void(* TestAction)()
Definition unitTest.ino:75
void normalModeDataReceiveTest()
Definition unitTest.ino:389
void asyncIsBusyTestDisableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:726
bool testSequenceRunning
Definition unitTest.ino:66
void configModePersistenceTestCheckConfigMode()
Definition unitTest.ino:568
void initializeConfigDataStruct(LD2410Types::ConfigData &configData)
Definition unitTest.ino:253
#define RADAR_TX_PIN
Definition unitTest.ino:29
const int NUM_ACTIONS
void asyncCancelTestEnableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:807
void printBigMessage(const String msg, char lineChar=' *')
Definition unitTest.ino:98
int dataUpdateCounter_normalModeCount
Definition unitTest.ino:277
void distanceResolutionTestConfigureCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
int testFailCount
Definition unitTest.ino:61
void disableConfigModeTest()
Definition unitTest.ino:641
void auxControlSettingsTest()
void configModePersistenceTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:582
void getConfigDataTest()
void dataUpdateCounterCallback(LD2410Async *sender, bool presenceDetected)
Definition unitTest.ino:287
void disableDisabledConfigModeTestDisableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:659
LD2410Types::OutputControl outputControlToApply
void startTests()
void dataUpdateCounterReset()
Definition unitTest.ino:281
void auxControlSettingsTestRequestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void requestBluetoothMacAddressAsyncTestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:877
void testStart(String name, String comment="")
Definition unitTest.ino:133
LD2410Types::ConfigData configDataToApply
void disableConfigModeTestDisableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:629
void configureAllSettingsTestRequestCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
void inactivityHandlingTestEnableConfigModeCallback(LD2410Async *sender, LD2410Async::AsyncCommandResult asyncResult)
Definition unitTest.ino:955
void requestBluetoothMacAddressAsyncTest()
Definition unitTest.ino:886
String currentTestName
Definition unitTest.ino:57
void loop()
byte lightThresholdToApply
String asyncCommandResultToString(LD2410Async::AsyncCommandResult result)
Definition unitTest.ino:240