My Project
|
Class to communicate and configure the LD2410S radar sensor. More...
#include <HLK-LD2410S.h>
Public Member Functions | |
LD2410S (HardwareSerial &serial, int rxPin, int txPin) | |
Constructor. | |
~LD2410S () | |
Destructor. | |
bool | begin (uint32_t baudrate=115200) |
Initialize serial communication and enter config mode. | |
void | loop () |
Read and process incoming frames (non-blocking) | |
bool | enterConfigMode () |
Enter configuration mode. | |
bool | exitConfigMode () |
Exit configuration mode. | |
bool | switchToStandardMode () |
Switch to standard output mode (detailed) | |
bool | switchToMinimalMode () |
Switch to minimal output mode (shorter packets) | |
bool | readFirmwareVersion (uint16_t &major, uint16_t &minor, uint16_t &patch) |
Read firmware version of the sensor. | |
bool | readCommonParametersCommand (uint32_t &farthest_distance, uint32_t &nearest_distance, uint32_t &delay_time, uint32_t &freq_status, uint32_t &freq_distance, uint32_t &respond_speed) |
Read configuration parameters from sensor. | |
bool | readSerialNumber (char *buffer, size_t len) |
Read 8-byte serial number. | |
bool | isMotionDetected () const |
Check if motion is currently detected. | |
uint16_t | getDistance () const |
Get target distance. |
Class to communicate and configure the LD2410S radar sensor.
LD2410S::LD2410S | ( | HardwareSerial & | serial, |
int | rxPin, | ||
int | txPin ) |
Constructor.
Constructs an LD2410S object with the specified serial interface and pin configuration.
serial | Reference to HardwareSerial object |
rxPin | GPIO used for sensor TX |
txPin | GPIO used for sensor RX |
Initializes the LD2410S sensor driver, associating it with the provided HardwareSerial instance and configuring the RX and TX pins for communication.
serial | Reference to a HardwareSerial object used for UART communication with the sensor. |
rxPin | GPIO pin number used for receiving data from the sensor. |
txPin | GPIO pin number used for transmitting data to the sensor. |
|
default |
bool LD2410S::begin | ( | uint32_t | baudrate = 115200 | ) |
Initialize serial communication and enter config mode.
Initializes the LD2410S sensor and configures its communication parameters.
baudrate | Serial baudrate (default = 115200) |
This function sets up the serial communication with the specified baud rate and pin configuration, attempts to enter configuration mode (with up to two retries), writes generic parameters to the sensor, and switches the sensor to minimal data output mode. Delays are used to ensure proper timing between operations.
baudrate | The baud rate for serial communication with the LD2410S sensor. |
bool LD2410S::enterConfigMode | ( | ) |
Enter configuration mode.
Sends the command to enter configuration mode on the LD2410S device.
This function transmits a specific command frame to the LD2410S device via the serial interface to request entry into configuration mode. It waits for up to 500 milliseconds for an 18-byte acknowledgment frame from the device. The function checks specific bytes in the response to verify successful entry into configuration mode.
bool LD2410S::exitConfigMode | ( | ) |
Exit configuration mode.
Exits the configuration mode of the LD2410S device.
Sends the appropriate command to the device to exit configuration mode and waits for an acknowledgment. The function waits up to 500 milliseconds for a response from the device. If a valid acknowledgment frame is received, it logs a success message and returns true. Otherwise, it returns false.
uint16_t LD2410S::getDistance | ( | ) | const |
Get target distance.
Retrieves the last measured distance value.
This method returns the most recent distance measurement obtained by the LD2410S sensor.
bool LD2410S::isMotionDetected | ( | ) | const |
Check if motion is currently detected.
Checks if motion has been detected by the LD2410S sensor.
void LD2410S::loop | ( | ) |
Read and process incoming frames (non-blocking)
read data serial continously
Processes incoming serial data to detect and parse minimal data frames from the LD2410S sensor.
This function should be called repeatedly (typically in the main loop) to read bytes from the serial interface. It accumulates bytes into a buffer when a frame start byte (0x6E) is detected, and continues until a frame end byte (0x62) is found or the maximum frame size is reached. Upon detecting a complete frame, it logs the frame contents and invokes the minimal frame parser.
The function is non-blocking and processes all available bytes in the serial buffer during each call.
bool LD2410S::readCommonParametersCommand | ( | uint32_t & | farthest_distance, |
uint32_t & | nearest_distance, | ||
uint32_t & | delay_time, | ||
uint32_t & | freq_status, | ||
uint32_t & | freq_distance, | ||
uint32_t & | respond_speed ) |
Read configuration parameters from sensor.
Reads the common parameters from the LD2410S sensor.
farthest_distance | Max detection distance |
nearest_distance | Min detection distance |
delay_time | Delay before entering static state |
freq_status | Report frequency for motion/static state |
freq_distance | Report frequency for distance |
respond_speed | Sensor response speed (normal/fast) |
This function sends a command to the LD2410S sensor to request its common parameters, waits for a response, and parses the returned data. The parameters retrieved include the farthest and nearest detection distances, delay time, frequency status, frequency distance, and response speed. The function returns true if the parameters are successfully read and parsed, otherwise returns false.
[out] | farthest_distance | The farthest detection distance reported by the sensor. |
[out] | nearest_distance | The nearest detection distance reported by the sensor. |
[out] | delay_time | The delay time parameter from the sensor. |
[out] | freq_status | The frequency status parameter from the sensor. |
[out] | freq_distance | The frequency distance parameter from the sensor. |
[out] | respond_speed | The response speed parameter from the sensor. |
bool LD2410S::readFirmwareVersion | ( | uint16_t & | major, |
uint16_t & | minor, | ||
uint16_t & | patch ) |
Read firmware version of the sensor.
Reads the firmware version from the LD2410S device.
major | Major version |
minor | Minor version |
patch | Patch version |
This function sends a command to the LD2410S device to request its firmware version. It waits for a response and parses the major, minor, and patch version numbers from the received data.
[out] | major | Reference to a uint16_t variable where the major version will be stored. |
[out] | minor | Reference to a uint16_t variable where the minor version will be stored. |
[out] | patch | Reference to a uint16_t variable where the patch version will be stored. |
bool LD2410S::readSerialNumber | ( | char * | buffer, |
size_t | len ) |
Read 8-byte serial number.
Reads the serial number from the LD2410S device via the serial interface.
buffer | Buffer to store null-terminated serial number |
len | Buffer size (must be at least 9) |
This function sends a command to the LD2410S device to request its serial number, waits for the response, and extracts the serial number from the received data. The serial number is expected to be 8 bytes long and will be stored as a null-terminated string in the provided buffer.
The function first clears the serial buffer, sends the serial number request command, and waits up to 1 second for a valid response. If a valid response is received, the serial number is extracted and copied into the buffer.
[out] | buffer | Pointer to a character array where the serial number will be stored. The buffer must be at least 9 bytes long to accommodate the 8-byte serial number and the null terminator. |
[in] | len | The length of the provided buffer. |
bool LD2410S::switchToMinimalMode | ( | ) |
Switch to minimal output mode (shorter packets)
Switches the LD2410S device to minimal output mode.
This function sends a command frame to the LD2410S device to change its output mode to minimal. It writes the appropriate command and parameters to the serial interface, then waits for an acknowledgment response from the device. The function checks the response for specific header and command bytes to confirm the mode change was successful.
bool LD2410S::switchToStandardMode | ( | ) |
Switch to standard output mode (detailed)
Switches the LD2410S device to standard output mode.
This function sends the appropriate command sequence to the LD2410S device over the configured serial interface to switch it into standard data output mode. It waits for an acknowledgment frame from the device to confirm the mode change.