MyLD2410 Library
Loading...
Searching...
No Matches
MyLD2410.h
Go to the documentation of this file.
1#ifndef MY_LD2410_H
2#define MY_LD2410_H
3
4/*
5
6MyLD2410 library
7An Arduino library for the LD2410 presence sensor, including HLK-LD2410B and HLK-LD2410C.
8https://github.com/iavorvel/MyLD2410
9
10*/
11
15
16#include <Arduino.h>
17#define LD2410_BAUD_RATE 256000
18#define LD2410_BUFFER_SIZE 0x40
19#define LD2410_LATEST_FIRMWARE "2.44"
20
24enum class LightControl
25{
26 NOT_SET = -1,
27 NO_LIGHT_CONTROL,
28 LIGHT_BELOW_THRESHOLD,
29 LIGHT_ABOVE_THRESHOLD
30};
31
35enum class OutputControl
36{
37 NOT_SET = -1,
38 DEFAULT_LOW,
39 DEFAULT_HIGH,
40};
41
45enum class AutoStatus
46{
47 NOT_SET = -1,
48 NOT_IN_PROGRESS,
49 IN_PROGRESS,
50 COMPLETED
51};
52
54{
55public:
56 enum Response
57 {
58 FAIL = 0,
59 ACK,
60 DATA
61 };
63 {
64 byte values[9];
65 byte N = 0;
66
67 void setN(byte n)
68 {
69 N = (n <= 8) ? n : 8;
70 }
71 ValuesArray &operator=(const ValuesArray &other)
72 {
73 if (this != &other)
74 {
75 N = other.N;
76 for (byte i = 0; i <= N; i++)
77 values[i] = other.values[i];
78 }
79 return *this;
80 }
81 template <typename ByteHandler>
82 void forEach(ByteHandler func) const
83 {
84 for (byte i = 0; i <= N; i++)
85 func(values[i]);
86 }
87 };
89 {
90 byte status;
91 unsigned long timestamp;
92 unsigned long mTargetDistance;
93 byte mTargetSignal;
94 unsigned long sTargetDistance;
95 byte sTargetSignal;
96 unsigned long distance;
97 // Enhanced data
98 ValuesArray mTargetSignals;
99 ValuesArray sTargetSignals;
100 };
101
102private:
103 SensorData sData;
104 ValuesArray stationaryThresholds;
105 ValuesArray movingThresholds;
106 byte maxRange = 0;
107 byte noOne_window = 0;
108 byte lightLevel = 0;
109 byte outLevel = 0;
110 byte lightThreshold = 0;
111 LightControl lightControl = LightControl::NOT_SET;
112 OutputControl outputControl = OutputControl::NOT_SET;
113 AutoStatus autoStatus = AutoStatus::NOT_SET;
114 unsigned long version = 0;
115 unsigned long bufferSize = 0;
116 byte MAC[6];
117 String MACstr = "";
118 String firmware = "";
119 byte firmwareMajor = 0;
120 byte firmwareMinor = 0;
121 int fineRes = -1;
122 bool isEnhanced = false;
123 bool isConfig = false;
124 unsigned long timeout = 2000;
125 unsigned long dataLifespan = 500;
126 byte inBuf[LD2410_BUFFER_SIZE];
127 byte inBufI = 0;
128 byte headBuf[4];
129 byte headBufI = 0;
130 Stream *sensor;
131 bool _debug = false;
132 bool isDataValid();
133 bool readFrame();
134 bool sendCommand(const byte *command);
135 bool processAck();
136 bool processData();
137
138public:
145 MyLD2410(Stream &serial, bool debug = false);
146
147 // CONTROLS
148
152 bool begin();
153
157 void end();
158
163 void debugOn();
164
169 void debugOff();
170
177 Response check();
178
179 // GETTERS
180
185 bool inConfigMode();
186
191 bool inBasicMode();
192
197 bool inEnhancedMode();
198
212 byte getStatus();
213
226 const char *statusString();
227
231 bool presenceDetected();
232
237
243 unsigned long stationaryTargetDistance();
244
251
258
263
269 unsigned long movingTargetDistance();
270
276 byte movingTargetSignal();
277
284
290 unsigned long detectedDistance();
291
297 const byte *getMAC();
298
304 String getMACstr();
305
311 String getFirmware();
312
318 byte getFirmwareMajor();
319
325 byte getFirmwareMinor();
326
332 unsigned long getVersion();
333
339 const SensorData &getSensorData();
340
346 byte getResolution();
347
348 // parameters
349
356
363
369 byte getRange();
370
376 unsigned long getRange_cm();
377
385 byte getNoOneWindow();
386 // end parameters
387
388 // REQUESTS
389
396 bool configMode(bool enable = true);
397
404 bool enhancedMode(bool enable = true);
405
411 bool requestAuxConfig();
412
420 bool autoThresholds(byte _timeout = 10);
421
429
435 bool requestMAC();
436
442 bool requestFirmware();
443
449 bool requestResolution();
450
457 bool setResolution(bool fine = false);
458
465 bool requestParameters();
466
476 bool setGateParameters(byte gate, byte movingThreshold, byte stationaryThreshold);
477
485 bool setMovingThreshold(byte gate, byte movingThreshold);
486
494 bool setStationaryThreshold(byte gate, byte stationaryThreshold);
495
504 bool
505 setGateParameters(const ValuesArray &moving_thresholds, const ValuesArray &stationary_thresholds, byte noOneWindow = 5);
506
515 bool setMaxGate(byte movingGate, byte stationaryGate, byte noOneWindow = 5);
516
523 bool setNoOneWindow(byte noOneWindow);
524
531 bool setMaxMovingGate(byte movingGate);
532
539 bool setMaxStationaryGate(byte stationaryGate);
540
546 byte getMaxMovingGate();
547
554
560 bool requestReset();
561
567 bool requestReboot();
568
574 bool requestBTon();
575
581 bool requestBToff();
582
591 bool setBTpassword(const char *passwd);
592
601 bool setBTpassword(const String &passwd);
602
608 bool resetBTpassword();
609
615 bool setBaud(byte baud);
616
622 byte getLightLevel();
623
630
640 bool setAuxControl(LightControl light_control, byte light_threshold, OutputControl output_control);
641
647 bool resetAuxControl();
648
654 byte getLightThreshold();
655
662
668 byte getOutLevel();
669};
670
671#endif // MY_LD2410_H
OutputControl
The auxiliary output control status.
Definition MyLD2410.h:36
AutoStatus
The status of the auto-thresholds routine.
Definition MyLD2410.h:46
LightControl
The auxiliary light control status.
Definition MyLD2410.h:25
byte getLightLevel()
Get the Light Level.
Definition MyLD2410.cpp:768
bool inEnhancedMode()
Check whether the device is in enhanced mode (continuously sends enhanced presence data)
Definition MyLD2410.cpp:317
bool setMovingThreshold(byte gate, byte movingThreshold)
Set the moving target threshold for a particular gate.
Definition MyLD2410.cpp:576
bool requestParameters()
Request the sensor parameters: range, motion thresholds, stationary thresholds, no-one window.
Definition MyLD2410.cpp:545
byte getLightThreshold()
Get the Light Threshold.
Definition MyLD2410.cpp:780
byte getMaxMovingGate()
Get the maximum moving gate.
Definition MyLD2410.cpp:667
bool requestReboot()
Request reboot.
Definition MyLD2410.cpp:688
byte getFirmwareMajor()
Get the Firmware Major.
Definition MyLD2410.cpp:408
bool requestFirmware()
Request the Firmware.
Definition MyLD2410.cpp:524
bool enhancedMode(bool enable=true)
Request enhanced mode.
Definition MyLD2410.cpp:479
bool setMaxMovingGate(byte movingGate)
Set the maximum moving gate.
Definition MyLD2410.cpp:641
bool setMaxStationaryGate(byte stationaryGate)
Set the maximum stationary gate.
Definition MyLD2410.cpp:654
bool autoThresholds(byte _timeout=10)
Begin the automatic threshold detection routine (firmware >= 2.44)
Definition MyLD2410.cpp:494
bool configMode(bool enable=true)
Request config mode.
Definition MyLD2410.cpp:470
void debugOn()
Set the debug flag.
Definition MyLD2410.cpp:297
Response check()
Call this function in the main loop.
Definition MyLD2410.cpp:73
bool requestResolution()
Request the resolution (gate-width)
Definition MyLD2410.cpp:531
bool inBasicMode()
Check whether the device is in basic mode (continuously sends basic presence data)
Definition MyLD2410.cpp:312
bool requestAuxConfig()
Request the current auxiliary configuration.
Definition MyLD2410.cpp:487
byte getStatus()
Get the status of the sensor: 0 - No presence; 1 - Moving only; 2 - Stationary only; 3 - Both moving ...
Definition MyLD2410.cpp:322
bool setAuxControl(LightControl light_control, byte light_threshold, OutputControl output_control)
Set the Auxiliary Control parameters.
Definition MyLD2410.cpp:794
bool setGateParameters(byte gate, byte movingThreshold, byte stationaryThreshold)
Set the gate parameters for a particular gate, or for all gates at once.
Definition MyLD2410.cpp:552
MyLD2410(Stream &serial, bool debug=false)
Construct a new MyLD2410 object.
Definition MyLD2410.cpp:266
String getMACstr()
Get the Bluetooth MAC address as a String.
Definition MyLD2410.cpp:394
bool setBaud(byte baud)
Reset the serial baud rate. The sensor reboots at the new rate on success.
Definition MyLD2410.cpp:739
bool requestReset()
Request reset to factory default parameters.
Definition MyLD2410.cpp:681
bool setMaxGate(byte movingGate, byte stationaryGate, byte noOneWindow=5)
Set the detection range for moving targets, stationary targets, as well as the no-one window.
Definition MyLD2410.cpp:594
unsigned long getRange_cm()
Get the maximum detection range in [cm].
Definition MyLD2410.cpp:458
AutoStatus getAutoStatus()
Get the status of the automatic threshold detection routine (firmware >= 2.44)
Definition MyLD2410.cpp:505
byte stationaryTargetSignal()
Get the signal from the stationary target.
Definition MyLD2410.cpp:352
void debugOff()
Reset the debug flag.
Definition MyLD2410.cpp:302
const ValuesArray & getStationarySignals()
Get the Stationary Signals object, if in enhanced mode.
Definition MyLD2410.cpp:357
byte getMaxStationaryGate()
Get the maximum stationary gate.
Definition MyLD2410.cpp:674
byte getRange()
Get the maximum detection gate.
Definition MyLD2410.cpp:451
bool begin()
Call this function in setup() to ascertain whether the device is responding.
Definition MyLD2410.cpp:272
bool setBTpassword(const char *passwd)
Set a new BT password.
Definition MyLD2410.cpp:709
bool setResolution(bool fine=false)
Set the resolution of the sensor.
Definition MyLD2410.cpp:538
bool setStationaryThreshold(byte gate, byte stationaryThreshold)
Set the stationary target threshold for a particular gate.
Definition MyLD2410.cpp:585
bool presenceDetected()
Check whether presence was detected in the latest frame.
Definition MyLD2410.cpp:337
bool requestBTon()
Turn Bluetooth ON.
Definition MyLD2410.cpp:695
unsigned long detectedDistance()
Get the detected distance.
Definition MyLD2410.cpp:382
byte getResolution()
Get the sensor resolution (gate-width) in [cm].
Definition MyLD2410.cpp:751
byte getOutLevel()
Get the Light Level.
Definition MyLD2410.cpp:816
LightControl getLightControl()
Get the Light Control parameter.
Definition MyLD2410.cpp:773
bool resetBTpassword()
Reset the BT password.
Definition MyLD2410.cpp:732
const ValuesArray & getMovingSignals()
Get the Moving Signals object, if in enhanced mode.
Definition MyLD2410.cpp:377
bool resetAuxControl()
Reset the Auxiliary Control parameters to their default values.
Definition MyLD2410.cpp:809
byte movingTargetSignal()
Get the signal from the moving target.
Definition MyLD2410.cpp:372
const SensorData & getSensorData()
Get the SensorData object.
Definition MyLD2410.cpp:432
unsigned long stationaryTargetDistance()
Get the distance to the stationary target in [cm].
Definition MyLD2410.cpp:347
bool requestBToff()
Turn Bluetooth OFF.
Definition MyLD2410.cpp:702
bool movingTargetDetected()
Check whether a moving target was detected in the latest frame.
Definition MyLD2410.cpp:362
byte getFirmwareMinor()
Get the Firmware Minor.
Definition MyLD2410.cpp:415
OutputControl getOutputControl()
Get the Output Control parameter.
Definition MyLD2410.cpp:787
void end()
Call this function to gracefully close the sensor. Useful for entering sleep mode.
Definition MyLD2410.cpp:291
const byte * getMAC()
Get the Bluetooth MAC address as an array byte[6].
Definition MyLD2410.cpp:387
bool requestMAC()
Request the Bluetooth MAC address.
Definition MyLD2410.cpp:517
bool inConfigMode()
Check whether the device is in config mode (accepts commands)
Definition MyLD2410.cpp:307
String getFirmware()
Get the Firmware as a String.
Definition MyLD2410.cpp:401
bool stationaryTargetDetected()
Check whether a stationary target was detected in the latest frame.
Definition MyLD2410.cpp:342
unsigned long getVersion()
Get the protocol version.
Definition MyLD2410.cpp:422
byte getNoOneWindow()
Get the time-lag of "no presence" in [s]. The sensor begins reporting "no presence" only after no mot...
Definition MyLD2410.cpp:463
const ValuesArray & getStationaryThresholds()
Get the detection thresholds for stationary targets.
Definition MyLD2410.cpp:444
bool setNoOneWindow(byte noOneWindow)
Set the no-one window parameter.
Definition MyLD2410.cpp:632
const char * statusString()
Get the presence status as a c-string.
Definition MyLD2410.cpp:327
const ValuesArray & getMovingThresholds()
Get the detection thresholds for moving targets.
Definition MyLD2410.cpp:437
unsigned long movingTargetDistance()
Get the distance to the moving target in [cm].
Definition MyLD2410.cpp:367
Definition MyLD2410.h:89
Definition MyLD2410.h:63