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#ifndef LD2410_BAUD_RATE
18#define LD2410_BAUD_RATE 256000
19#endif
20#define LD2410_BUFFER_SIZE 0x40
21#define LD2410_LATEST_FIRMWARE "2.44"
22
26enum class LightControl
27{
28 NOT_SET = -1,
29 NO_LIGHT_CONTROL,
30 LIGHT_BELOW_THRESHOLD,
31 LIGHT_ABOVE_THRESHOLD
32};
33
37enum class OutputControl
38{
39 NOT_SET = -1,
40 DEFAULT_LOW,
41 DEFAULT_HIGH,
42};
43
47enum class AutoStatus
48{
49 NOT_SET = -1,
50 NOT_IN_PROGRESS,
51 IN_PROGRESS,
52 COMPLETED
53};
54
56{
57public:
58 enum Response
59 {
60 FAIL = 0,
61 ACK,
62 DATA
63 };
65 {
66 byte values[9];
67 byte N = 0;
68
69 void setN(byte n)
70 {
71 N = (n <= 8) ? n : 8;
72 }
73 ValuesArray &operator=(const ValuesArray &other)
74 {
75 if (this != &other)
76 {
77 N = other.N;
78 for (byte i = 0; i <= N; i++)
79 values[i] = other.values[i];
80 }
81 return *this;
82 }
83 template <typename ByteHandler>
84 void forEach(ByteHandler func) const
85 {
86 for (byte i = 0; i <= N; i++)
87 func(values[i]);
88 }
89 };
91 {
92 byte status;
93 unsigned long timestamp;
94 unsigned long mTargetDistance;
95 byte mTargetSignal;
96 unsigned long sTargetDistance;
97 byte sTargetSignal;
98 unsigned long distance;
99 // Enhanced data
100 ValuesArray mTargetSignals;
101 ValuesArray sTargetSignals;
102 };
103
104private:
105 SensorData sData;
106 ValuesArray stationaryThresholds;
107 ValuesArray movingThresholds;
108 byte maxRange = 0;
109 byte noOne_window = 0;
110 byte lightLevel = 0;
111 byte outLevel = 0;
112 byte lightThreshold = 0;
113 LightControl lightControl = LightControl::NOT_SET;
114 OutputControl outputControl = OutputControl::NOT_SET;
115 AutoStatus autoStatus = AutoStatus::NOT_SET;
116 unsigned long version = 0;
117 unsigned long bufferSize = 0;
118 unsigned long dataFrames = 0;
119 byte MAC[6];
120 String MACstr = "";
121 String firmware = "";
122 byte firmwareMajor = 0;
123 byte firmwareMinor = 0;
124 int fineRes = -1;
125 bool isEnhanced = false;
126 bool isConfig = false;
127 byte inBuf[LD2410_BUFFER_SIZE];
128 byte inBufI = 0;
129 byte headBuf[4];
130 byte headBufI = 0;
131 Stream *sensor;
132 bool _debug = false;
133 bool isDataValid();
134 bool readFrame();
135 bool sendCommand(const byte *command);
136 bool processAck();
137 bool processData();
138
139public:
146 MyLD2410(Stream &serial, bool debug = false);
147
148 // CONTROLS
149
153 bool begin();
154
158 void end();
159
164 void debugOn();
165
170 void debugOff();
171
178 Response check();
179
180 // GETTERS
181
186 bool inConfigMode();
187
192 bool inBasicMode();
193
198 bool inEnhancedMode();
199
213 byte getStatus();
214
227 const char *statusString();
228
232 unsigned long getTimestamp();
233
239 unsigned long getFrameCount();
240
244 bool presenceDetected();
245
250
256 unsigned long stationaryTargetDistance();
257
264
271
276
282 unsigned long movingTargetDistance();
283
289 byte movingTargetSignal();
290
297
303 unsigned long detectedDistance();
304
310 const byte *getMAC();
311
317 String getMACstr();
318
324 String getFirmware();
325
331 byte getFirmwareMajor();
332
338 byte getFirmwareMinor();
339
345 unsigned long getVersion();
346
352 const SensorData &getSensorData();
353
359 byte getResolution();
360
361 // parameters
362
369
376
382 byte getRange();
383
389 unsigned long getRange_cm();
390
398 byte getNoOneWindow();
399 // end parameters
400
401 // REQUESTS
402
409 bool configMode(bool enable = true);
410
417 bool enhancedMode(bool enable = true);
418
424 bool requestAuxConfig();
425
433 bool autoThresholds(byte _timeout = 10);
434
442
448 bool requestMAC();
449
455 bool requestFirmware();
456
462 bool requestResolution();
463
470 bool setResolution(bool fine = false);
471
478 bool requestParameters();
479
489 bool setGateParameters(byte gate, byte movingThreshold, byte stationaryThreshold);
490
498 bool setMovingThreshold(byte gate, byte movingThreshold);
499
507 bool setStationaryThreshold(byte gate, byte stationaryThreshold);
508
517 bool
518 setGateParameters(const ValuesArray &moving_thresholds, const ValuesArray &stationary_thresholds, byte noOneWindow = 5);
519
528 bool setMaxGate(byte movingGate, byte stationaryGate, byte noOneWindow = 5);
529
536 bool setNoOneWindow(byte noOneWindow);
537
544 bool setMaxMovingGate(byte movingGate);
545
552 bool setMaxStationaryGate(byte stationaryGate);
553
559 byte getMaxMovingGate();
560
567
573 bool requestReset();
574
580 bool requestReboot();
581
587 bool requestBTon();
588
594 bool requestBToff();
595
604 bool setBTpassword(const char *passwd);
605
614 bool setBTpassword(const String &passwd);
615
621 bool resetBTpassword();
622
628 bool setBaud(byte baud);
629
635 byte getLightLevel();
636
643
653 bool setAuxControl(LightControl light_control, byte light_threshold, OutputControl output_control);
654
660 bool resetAuxControl();
661
667 byte getLightThreshold();
668
675
681 byte getOutLevel();
682};
683
684#endif // MY_LD2410_H
OutputControl
The auxiliary output control status.
Definition MyLD2410.h:38
AutoStatus
The status of the auto-thresholds routine.
Definition MyLD2410.h:48
LightControl
The auxiliary light control status.
Definition MyLD2410.h:27
byte getLightLevel()
Get the Light Level.
Definition MyLD2410.cpp:793
bool inEnhancedMode()
Check whether the device is in enhanced mode (continuously sends enhanced presence data)
Definition MyLD2410.cpp:332
bool setMovingThreshold(byte gate, byte movingThreshold)
Set the moving target threshold for a particular gate.
Definition MyLD2410.cpp:601
bool requestParameters()
Request the sensor parameters: range, motion thresholds, stationary thresholds, no-one window.
Definition MyLD2410.cpp:570
byte getLightThreshold()
Get the Light Threshold.
Definition MyLD2410.cpp:805
unsigned long getFrameCount()
Get the number of data frames received so far.
Definition MyLD2410.cpp:352
byte getMaxMovingGate()
Get the maximum moving gate.
Definition MyLD2410.cpp:692
bool requestReboot()
Request reboot.
Definition MyLD2410.cpp:713
byte getFirmwareMajor()
Get the Firmware Major.
Definition MyLD2410.cpp:433
bool requestFirmware()
Request the Firmware.
Definition MyLD2410.cpp:549
bool enhancedMode(bool enable=true)
Request enhanced mode.
Definition MyLD2410.cpp:504
bool setMaxMovingGate(byte movingGate)
Set the maximum moving gate.
Definition MyLD2410.cpp:666
bool setMaxStationaryGate(byte stationaryGate)
Set the maximum stationary gate.
Definition MyLD2410.cpp:679
bool autoThresholds(byte _timeout=10)
Begin the automatic threshold detection routine (firmware >= 2.44)
Definition MyLD2410.cpp:519
bool configMode(bool enable=true)
Request config mode.
Definition MyLD2410.cpp:495
void debugOn()
Set the debug flag.
Definition MyLD2410.cpp:312
Response check()
Call this function in the main loop.
Definition MyLD2410.cpp:73
bool requestResolution()
Request the resolution (gate-width)
Definition MyLD2410.cpp:556
bool inBasicMode()
Check whether the device is in basic mode (continuously sends basic presence data)
Definition MyLD2410.cpp:327
bool requestAuxConfig()
Request the current auxiliary configuration.
Definition MyLD2410.cpp:512
byte getStatus()
Get the status of the sensor: 0 - No presence; 1 - Moving only; 2 - Stationary only; 3 - Both moving ...
Definition MyLD2410.cpp:337
bool setAuxControl(LightControl light_control, byte light_threshold, OutputControl output_control)
Set the Auxiliary Control parameters.
Definition MyLD2410.cpp:819
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:577
MyLD2410(Stream &serial, bool debug=false)
Construct a new MyLD2410 object.
Definition MyLD2410.cpp:281
String getMACstr()
Get the Bluetooth MAC address as a String.
Definition MyLD2410.cpp:419
bool setBaud(byte baud)
Reset the serial baud rate. The sensor reboots at the new rate on success.
Definition MyLD2410.cpp:764
bool requestReset()
Request reset to factory default parameters.
Definition MyLD2410.cpp:706
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:619
unsigned long getRange_cm()
Get the maximum detection range in [cm].
Definition MyLD2410.cpp:483
AutoStatus getAutoStatus()
Get the status of the automatic threshold detection routine (firmware >= 2.44)
Definition MyLD2410.cpp:530
byte stationaryTargetSignal()
Get the signal from the stationary target.
Definition MyLD2410.cpp:377
void debugOff()
Reset the debug flag.
Definition MyLD2410.cpp:317
const ValuesArray & getStationarySignals()
Get the Stationary Signals object, if in enhanced mode.
Definition MyLD2410.cpp:382
byte getMaxStationaryGate()
Get the maximum stationary gate.
Definition MyLD2410.cpp:699
byte getRange()
Get the maximum detection gate.
Definition MyLD2410.cpp:476
bool begin()
Call this function in setup() to ascertain whether the device is responding.
Definition MyLD2410.cpp:287
bool setBTpassword(const char *passwd)
Set a new BT password.
Definition MyLD2410.cpp:734
bool setResolution(bool fine=false)
Set the resolution of the sensor.
Definition MyLD2410.cpp:563
bool setStationaryThreshold(byte gate, byte stationaryThreshold)
Set the stationary target threshold for a particular gate.
Definition MyLD2410.cpp:610
bool presenceDetected()
Check whether presence was detected in the latest frame.
Definition MyLD2410.cpp:362
bool requestBTon()
Turn Bluetooth ON.
Definition MyLD2410.cpp:720
unsigned long detectedDistance()
Get the detected distance.
Definition MyLD2410.cpp:407
byte getResolution()
Get the sensor resolution (gate-width) in [cm].
Definition MyLD2410.cpp:776
byte getOutLevel()
Get the Light Level.
Definition MyLD2410.cpp:841
LightControl getLightControl()
Get the Light Control parameter.
Definition MyLD2410.cpp:798
bool resetBTpassword()
Reset the BT password.
Definition MyLD2410.cpp:757
unsigned long getTimestamp()
Get the time (in milliseconds) of the last successfully received data frame.
Definition MyLD2410.cpp:347
const ValuesArray & getMovingSignals()
Get the Moving Signals object, if in enhanced mode.
Definition MyLD2410.cpp:402
bool resetAuxControl()
Reset the Auxiliary Control parameters to their default values.
Definition MyLD2410.cpp:834
byte movingTargetSignal()
Get the signal from the moving target.
Definition MyLD2410.cpp:397
const SensorData & getSensorData()
Get the SensorData object.
Definition MyLD2410.cpp:457
unsigned long stationaryTargetDistance()
Get the distance to the stationary target in [cm].
Definition MyLD2410.cpp:372
bool requestBToff()
Turn Bluetooth OFF.
Definition MyLD2410.cpp:727
bool movingTargetDetected()
Check whether a moving target was detected in the latest frame.
Definition MyLD2410.cpp:387
byte getFirmwareMinor()
Get the Firmware Minor.
Definition MyLD2410.cpp:440
OutputControl getOutputControl()
Get the Output Control parameter.
Definition MyLD2410.cpp:812
void end()
Call this function to gracefully close the sensor. Useful for entering sleep mode.
Definition MyLD2410.cpp:306
const byte * getMAC()
Get the Bluetooth MAC address as an array byte[6].
Definition MyLD2410.cpp:412
bool requestMAC()
Request the Bluetooth MAC address.
Definition MyLD2410.cpp:542
bool inConfigMode()
Check whether the device is in config mode (accepts commands)
Definition MyLD2410.cpp:322
String getFirmware()
Get the Firmware as a String.
Definition MyLD2410.cpp:426
bool stationaryTargetDetected()
Check whether a stationary target was detected in the latest frame.
Definition MyLD2410.cpp:367
unsigned long getVersion()
Get the protocol version.
Definition MyLD2410.cpp:447
byte getNoOneWindow()
Get the time-lag of "no presence" in [s]. The sensor begins reporting "no presence" only after no mot...
Definition MyLD2410.cpp:488
const ValuesArray & getStationaryThresholds()
Get the detection thresholds for stationary targets.
Definition MyLD2410.cpp:469
bool setNoOneWindow(byte noOneWindow)
Set the no-one window parameter.
Definition MyLD2410.cpp:657
const char * statusString()
Get the presence status as a c-string.
Definition MyLD2410.cpp:342
const ValuesArray & getMovingThresholds()
Get the detection thresholds for moving targets.
Definition MyLD2410.cpp:462
unsigned long movingTargetDistance()
Get the distance to the moving target in [cm].
Definition MyLD2410.cpp:392
Definition MyLD2410.h:91
Definition MyLD2410.h:65