64static const char *
const TAG =
"ld2420";
67static const uint16_t REFRESH_RATE_MS = 1000;
70static const uint16_t CMD_DISABLE_CONF = 0x00FE;
71static const uint16_t CMD_ENABLE_CONF = 0x00FF;
72static const uint16_t CMD_PARM_HIGH_TRESH = 0x0012;
73static const uint16_t CMD_PARM_LOW_TRESH = 0x0021;
74static const uint16_t CMD_PROTOCOL_VER = 0x0002;
75static const uint16_t CMD_READ_ABD_PARAM = 0x0008;
76static const uint16_t CMD_READ_REG_ADDR = 0x0020;
77static const uint16_t CMD_READ_REGISTER = 0x0002;
78static const uint16_t CMD_READ_SERIAL_NUM = 0x0011;
79static const uint16_t CMD_READ_SYS_PARAM = 0x0013;
80static const uint16_t CMD_READ_VERSION = 0x0000;
81static const uint16_t CMD_RESTART = 0x0068;
82static const uint16_t CMD_SYSTEM_MODE = 0x0000;
83static const uint16_t CMD_SYSTEM_MODE_GR = 0x0003;
84static const uint16_t CMD_SYSTEM_MODE_MTT = 0x0001;
85static const uint16_t CMD_SYSTEM_MODE_SIMPLE = 0x0064;
86static const uint16_t CMD_SYSTEM_MODE_DEBUG = 0x0000;
87static const uint16_t CMD_SYSTEM_MODE_ENERGY = 0x0004;
88static const uint16_t CMD_SYSTEM_MODE_VS = 0x0002;
89static const uint16_t CMD_WRITE_ABD_PARAM = 0x0007;
90static const uint16_t CMD_WRITE_REGISTER = 0x0001;
91static const uint16_t CMD_WRITE_SYS_PARAM = 0x0012;
93static const uint8_t CMD_ABD_DATA_REPLY_SIZE = 0x04;
94static const uint8_t CMD_ABD_DATA_REPLY_START = 0x0A;
95static const uint8_t CMD_MAX_BYTES = 0x64;
96static const uint8_t CMD_REG_DATA_REPLY_SIZE = 0x02;
98static const uint8_t LD2420_ERROR_NONE = 0x00;
99static const uint8_t LD2420_ERROR_TIMEOUT = 0x02;
100static const uint8_t LD2420_ERROR_UNKNOWN = 0x01;
103static const uint16_t CMD_MIN_GATE_REG = 0x0000;
104static const uint16_t CMD_MAX_GATE_REG = 0x0001;
105static const uint16_t CMD_TIMEOUT_REG = 0x0004;
106static const uint16_t CMD_GATE_MOVE_THRESH[TOTAL_GATES] = {0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015,
107 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B,
108 0x001C, 0x001D, 0x001E, 0x001F};
109static const uint16_t CMD_GATE_STILL_THRESH[TOTAL_GATES] = {0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025,
110 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B,
111 0x002C, 0x002D, 0x002E, 0x002F};
112static const uint32_t FACTORY_MOVE_THRESH[TOTAL_GATES] = {60000, 30000, 400, 250, 250, 250, 250, 250,
113 250, 250, 250, 250, 250, 250, 250, 250};
114static const uint32_t FACTORY_STILL_THRESH[TOTAL_GATES] = {40000, 20000, 200, 200, 200, 200, 200, 150,
115 150, 100, 100, 100, 100, 100, 100, 100};
116static const uint16_t FACTORY_TIMEOUT = 120;
117static const uint16_t FACTORY_MIN_GATE = 1;
118static const uint16_t FACTORY_MAX_GATE = 12;
121static const uint32_t CMD_FRAME_FOOTER = 0x01020304;
122static const uint32_t CMD_FRAME_HEADER = 0xFAFBFCFD;
123static const uint32_t DEBUG_FRAME_FOOTER = 0xFAFBFCFD;
124static const uint32_t DEBUG_FRAME_HEADER = 0x1410BFAA;
125static const uint32_t ENERGY_FRAME_FOOTER = 0xF5F6F7F8;
126static const uint32_t ENERGY_FRAME_HEADER = 0xF1F2F3F4;
127static const int CALIBRATE_VERSION_MIN = 154;
128static const uint8_t CMD_FRAME_COMMAND = 6;
129static const uint8_t CMD_FRAME_DATA_LENGTH = 4;
130static const uint8_t CMD_FRAME_STATUS = 7;
131static const uint8_t CMD_ERROR_WORD = 8;
132static const uint8_t ENERGY_SENSOR_START = 9;
133static const uint8_t CALIBRATE_REPORT_INTERVAL = 4;
134static const std::string OP_NORMAL_MODE_STRING =
"Normal";
135static const std::string OP_SIMPLE_MODE_STRING =
"Simple";
138struct StringToUint8 {
143static constexpr StringToUint8 OP_MODE_BY_STR[] = {
149static constexpr const char *ERR_MESSAGE[] = {
156template<
size_t N> uint8_t
find_uint8(
const StringToUint8 (&arr)[N],
const std::string &str) {
157 for (
const auto &entry : arr) {
158 if (str == entry.str) {
165static uint8_t calc_checksum(
void *data,
size_t size) {
167 uint8_t *data_bytes = (uint8_t *) data;
168 for (
size_t i = 0; i < size; i++) {
174static int get_firmware_int(
const char *version_string) {
175 std::string version_str = version_string;
176 if (version_str[0] ==
'v') {
177 version_str = version_str.substr(1);
179 version_str.erase(remove(version_str.begin(), version_str.end(),
'.'), version_str.end());
180 int version_integer = stoi(version_str);
181 return version_integer;
189 " Firmware version: %7s",
192 ESP_LOGCONFIG(TAG,
"Number:");
197 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
208 ESP_LOGCONFIG(TAG,
"Select:");
210 if (ld2420::get_firmware_int(this->
firmware_ver_) < CALIBRATE_VERSION_MIN) {
211 ESP_LOGW(TAG,
"Firmware version %s and older supports Simple Mode only", this->
firmware_ver_);
217 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
227 std::string fw_str(pfw);
230 listener->on_fw_version(fw_str);
233 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
239 if (ld2420::get_firmware_int(this->
firmware_ver_) < CALIBRATE_VERSION_MIN) {
243 ESP_LOGW(TAG,
"Firmware version %s and older supports Simple Mode only", this->
firmware_ver_);
258 ESP_LOGD(TAG,
"No configuration change detected");
261 ESP_LOGD(TAG,
"Reconfiguring");
263 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
268 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
282 ESP_LOGD(TAG,
"Setting factory defaults");
284 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
294 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
310 ESP_LOGD(TAG,
"Restarting");
324 ESP_LOGD(TAG,
"Reverted config number edits");
335 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
336 this->
radar_data[gate][sample_number] = gate_energy[gate];
345 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
349 for (uint8_t sample_number = 0; sample_number < CALIBRATE_SAMPLES; ++sample_number) {
354 if (this->
radar_data[gate][sample_number] > peak) {
360 this->
gate_avg[gate] = sum / CALIBRATE_SAMPLES;
365 uint32_t calculated_value =
366 (
static_cast<uint32_t
>(this->
gate_peak[gate]) + (move_factor *
static_cast<uint32_t
>(this->
gate_peak[gate])));
367 this->
new_config.
move_thresh[gate] =
static_cast<uint16_t
>(calculated_value <= 65535 ? calculated_value : 65535);
369 (
static_cast<uint32_t
>(this->
gate_peak[gate]) + (still_factor *
static_cast<uint32_t
>(this->
gate_peak[gate])));
370 this->
new_config.
still_thresh[gate] =
static_cast<uint16_t
>(calculated_value <= 65535 ? calculated_value : 65535);
375 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
377 ESP_LOGI(TAG,
"Gate: %2d Avg: %5d Peak: %5d", gate, this->
gate_avg[gate], this->
gate_peak[gate]);
384 if (ld2420::get_firmware_int(
firmware_ver_) >= CALIBRATE_VERSION_MIN) {
390 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
393 for (uint8_t i = 0; i < CALIBRATE_SAMPLES; i++) {
420 ESP_LOGW(TAG,
"Max command length exceeded; ignoring");
426 if (memcmp(&buffer[this->
buffer_pos_ - 4], &CMD_FRAME_FOOTER,
sizeof(CMD_FRAME_FOOTER)) == 0) {
431 (this->
get_mode_() == CMD_SYSTEM_MODE_SIMPLE)) {
434 }
else if ((memcmp(&buffer[this->
buffer_pos_ - 4], &ENERGY_FRAME_FOOTER,
sizeof(ENERGY_FRAME_FOOTER)) == 0) &&
435 (this->
get_mode_() == CMD_SYSTEM_MODE_ENERGY)) {
447 memcpy(&
range, &buffer[index],
sizeof(
range));
448 index +=
sizeof(
range);
450 for (uint8_t i = 0; i < elements; i++) {
482 const uint8_t bufsize = 16;
485 char *endptr{
nullptr};
486 char outbuf[bufsize]{0};
488 if (inbuf[pos - 2] ==
'O' && inbuf[pos - 1] ==
'F' && inbuf[pos] ==
'F') {
490 }
else if (inbuf[pos - 1] ==
'O' && inbuf[pos] ==
'N') {
493 if (inbuf[pos] >=
'0' && inbuf[pos] <=
'9') {
494 if (index < bufsize - 1) {
495 outbuf[index++] = inbuf[pos];
506 outbuf[index] =
'\0';
511 if (this->
get_mode_() == CMD_SYSTEM_MODE_SIMPLE) {
520 for (
auto &listener : this->listeners_)
528 uint8_t reg_element = 0;
529 uint8_t data_element = 0;
530 uint16_t data_pos = 0;
532 ESP_LOGW(TAG,
"Reply frame too long");
535 ESP_LOGW(TAG,
"Command frame too short");
538 memcpy(&this->
cmd_reply_.
error, &buffer[CMD_ERROR_WORD],
sizeof(this->cmd_reply_.error));
545 case (CMD_ENABLE_CONF):
546 ESP_LOGV(TAG,
"Set config enable: CMD = %2X %s", CMD_ENABLE_CONF, result);
548 case (CMD_DISABLE_CONF):
549 ESP_LOGV(TAG,
"Set config disable: CMD = %2X %s", CMD_DISABLE_CONF, result);
551 case (CMD_READ_REGISTER):
552 ESP_LOGV(TAG,
"Read register: CMD = %2X %s", CMD_READ_REGISTER, result);
555 for (uint16_t index = 0; index < (CMD_REG_DATA_REPLY_SIZE *
556 ((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_REG_DATA_REPLY_SIZE));
557 index += CMD_REG_DATA_REPLY_SIZE) {
558 memcpy(&this->
cmd_reply_.
data[reg_element], &buffer[data_pos + index],
sizeof(CMD_REG_DATA_REPLY_SIZE));
563 case (CMD_WRITE_REGISTER):
564 ESP_LOGV(TAG,
"Write register: CMD = %2X %s", CMD_WRITE_REGISTER, result);
566 case (CMD_WRITE_ABD_PARAM):
567 ESP_LOGV(TAG,
"Write gate parameter(s): %2X %s", CMD_WRITE_ABD_PARAM, result);
569 case (CMD_READ_ABD_PARAM):
570 ESP_LOGV(TAG,
"Read gate parameter(s): %2X %s", CMD_READ_ABD_PARAM, result);
571 data_pos = CMD_ABD_DATA_REPLY_START;
572 for (uint16_t index = 0; index < (CMD_ABD_DATA_REPLY_SIZE *
573 ((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_ABD_DATA_REPLY_SIZE));
574 index += CMD_ABD_DATA_REPLY_SIZE) {
575 memcpy(&this->
cmd_reply_.
data[data_element], &buffer[data_pos + index],
576 sizeof(this->cmd_reply_.data[data_element]));
581 case (CMD_WRITE_SYS_PARAM):
582 ESP_LOGV(TAG,
"Set system parameter(s): %2X %s", CMD_WRITE_SYS_PARAM, result);
584 case (CMD_READ_VERSION):
586 ESP_LOGV(TAG,
"Firmware version: %7s %s", this->
firmware_ver_, result);
594 uint32_t start_millis =
millis();
596 uint8_t ack_buffer[MAX_LINE_LENGTH];
597 uint8_t cmd_buffer[MAX_LINE_LENGTH];
599 if (frame.
command != CMD_RESTART) {
616 for (uint16_t index = 0; index < frame.
data_length; index++) {
617 memcpy(&cmd_buffer[frame.
length], &frame.
data[index],
sizeof(frame.
data[index]));
626 if (frame.
command == CMD_RESTART) {
632 this->
readline_(this->
read(), ack_buffer,
sizeof(ack_buffer));
636 if ((
millis() - start_millis) > 1000) {
638 error = LD2420_ERROR_TIMEOUT;
656 cmd_frame.
header = CMD_FRAME_HEADER;
657 cmd_frame.
command = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
659 memcpy(&cmd_frame.
data[0], &CMD_PROTOCOL_VER,
sizeof(CMD_PROTOCOL_VER));
662 cmd_frame.
footer = CMD_FRAME_FOOTER;
663 ESP_LOGV(TAG,
"Sending set config %s command: %2X", enable ?
"enable" :
"disable", cmd_frame.
command);
673 cmd_frame.
header = CMD_FRAME_HEADER;
674 cmd_frame.
command = CMD_RESTART;
675 cmd_frame.
footer = CMD_FRAME_FOOTER;
676 ESP_LOGV(TAG,
"Sending restart command: %2X", cmd_frame.
command);
683 cmd_frame.
header = CMD_FRAME_HEADER;
684 cmd_frame.
command = CMD_READ_REGISTER;
685 cmd_frame.
data[1] = reg;
687 cmd_frame.
footer = CMD_FRAME_FOOTER;
688 ESP_LOGV(TAG,
"Sending read register %4X command: %2X", reg, cmd_frame.
command);
695 cmd_frame.
header = CMD_FRAME_HEADER;
696 cmd_frame.
command = CMD_WRITE_REGISTER;
697 memcpy(&cmd_frame.
data[cmd_frame.
data_length], ®,
sizeof(CMD_REG_DATA_REPLY_SIZE));
699 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &value,
sizeof(CMD_REG_DATA_REPLY_SIZE));
701 cmd_frame.
footer = CMD_FRAME_FOOTER;
702 ESP_LOGV(TAG,
"Sending write register %4X command: %2X data = %4X", reg, cmd_frame.
command, value);
712 cmd_frame.
header = CMD_FRAME_HEADER;
713 cmd_frame.
command = CMD_READ_ABD_PARAM;
714 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &CMD_GATE_MOVE_THRESH[gate],
sizeof(CMD_GATE_MOVE_THRESH[gate]));
716 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &CMD_GATE_STILL_THRESH[gate],
sizeof(CMD_GATE_STILL_THRESH[gate]));
718 cmd_frame.
footer = CMD_FRAME_FOOTER;
719 ESP_LOGV(TAG,
"Sending read gate %d high/low threshold command: %2X", gate, cmd_frame.
command);
732 cmd_frame.
header = CMD_FRAME_HEADER;
733 cmd_frame.
command = CMD_READ_ABD_PARAM;
735 sizeof(CMD_MIN_GATE_REG));
738 sizeof(CMD_MAX_GATE_REG));
741 sizeof(CMD_TIMEOUT_REG));
743 cmd_frame.
footer = CMD_FRAME_FOOTER;
744 ESP_LOGV(TAG,
"Sending read gate min max and timeout command: %2X", cmd_frame.
command);
756 uint16_t unknown_parm = 0x0000;
758 cmd_frame.
header = CMD_FRAME_HEADER;
759 cmd_frame.
command = CMD_WRITE_SYS_PARAM;
760 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &CMD_SYSTEM_MODE,
sizeof(CMD_SYSTEM_MODE));
764 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &unknown_parm,
sizeof(unknown_parm));
766 cmd_frame.
footer = CMD_FRAME_FOOTER;
767 ESP_LOGV(TAG,
"Sending write system mode command: %2X", cmd_frame.
command);
776 cmd_frame.
header = CMD_FRAME_HEADER;
777 cmd_frame.
command = CMD_READ_VERSION;
778 cmd_frame.
footer = CMD_FRAME_FOOTER;
780 ESP_LOGV(TAG,
"Sending read firmware version command: %2X", cmd_frame.
command);
793 cmd_frame.
header = CMD_FRAME_HEADER;
794 cmd_frame.
command = CMD_WRITE_ABD_PARAM;
796 sizeof(CMD_MIN_GATE_REG));
798 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &min_gate_distance,
sizeof(min_gate_distance));
799 cmd_frame.
data_length +=
sizeof(min_gate_distance);
801 sizeof(CMD_MAX_GATE_REG));
803 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &max_gate_distance,
sizeof(max_gate_distance));
804 cmd_frame.
data_length +=
sizeof(max_gate_distance);
806 sizeof(CMD_TIMEOUT_REG));
808 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &timeout,
sizeof(timeout));
811 cmd_frame.
footer = CMD_FRAME_FOOTER;
813 ESP_LOGV(TAG,
"Sending write gate min max and timeout command: %2X", cmd_frame.
command);
822 uint16_t move_threshold_gate = CMD_GATE_MOVE_THRESH[gate];
823 uint16_t still_threshold_gate = CMD_GATE_STILL_THRESH[gate];
826 cmd_frame.
header = CMD_FRAME_HEADER;
827 cmd_frame.
command = CMD_WRITE_ABD_PARAM;
828 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &move_threshold_gate,
sizeof(move_threshold_gate));
829 cmd_frame.
data_length +=
sizeof(move_threshold_gate);
830 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &this->new_config.move_thresh[gate],
831 sizeof(this->new_config.move_thresh[gate]));
833 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &still_threshold_gate,
sizeof(still_threshold_gate));
834 cmd_frame.
data_length +=
sizeof(still_threshold_gate);
835 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &this->new_config.still_thresh[gate],
836 sizeof(this->new_config.still_thresh[gate]));
838 cmd_frame.
footer = CMD_FRAME_FOOTER;
839 ESP_LOGV(TAG,
"Sending set gate %4X sensitivity command: %2X", gate, cmd_frame.
command);
863 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void mark_failed()
Mark this component as failed.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void dump_config() override
void init_gate_config_numbers()
void readline_(int rx_data, uint8_t *buffer, int len)
void set_system_mode(uint16_t mode)
void handle_ack_data_(uint8_t *buffer, int len)
void revert_config_action()
float gate_still_sensitivity_factor
int32_t last_periodic_millis
void restart_module_action()
std::vector< number::Number * > gate_still_threshold_numbers_
number::Number * gate_select_number_
uint8_t set_config_mode(bool enable)
button::Button * factory_reset_button_
void update_radar_data(uint16_t const *gate_energy, uint8_t sample_number)
float get_setup_priority() const override
void send_module_restart()
void set_distance_(uint16_t distance)
void set_min_max_distances_timeout(uint32_t max_gate_distance, uint32_t min_gate_distance, uint32_t timeout)
void set_gate_threshold(uint8_t gate)
void handle_energy_mode_(uint8_t *buffer, int len)
button::Button * revert_config_button_
void auto_calibrate_sensitivity()
void set_calibration_(bool state)
number::Number * min_gate_distance_number_
button::Button * apply_config_button_
int32_t report_periodic_millis
uint16_t gate_peak[TOTAL_GATES]
std::vector< LD2420Listener * > listeners_
void get_firmware_version_()
std::vector< number::Number * > gate_move_threshold_numbers_
float gate_move_sensitivity_factor
void set_mode_(uint16_t mode)
void apply_config_action()
uint16_t gate_avg[TOTAL_GATES]
RegConfigT current_config
uint8_t buffer_data_[MAX_LINE_LENGTH]
int get_gate_threshold_(uint8_t gate)
number::Number * gate_still_sensitivity_factor_number_
void handle_cmd_error(uint8_t error)
void factory_reset_action()
int send_cmd_from_array(CmdFrameT cmd_frame)
void get_reg_value_(uint16_t reg)
void refresh_gate_config_numbers()
uint16_t gate_energy_[TOTAL_GATES]
int32_t last_normal_periodic_millis
void handle_simple_mode_(const uint8_t *inbuf, int len)
number::Number * max_gate_distance_number_
select::Select * operating_selector_
button::Button * restart_module_button_
void set_operating_mode(const std::string &state)
int get_min_max_distances_timeout_()
number::Number * gate_timeout_number_
uint16_t total_sample_number_counter
uint8_t current_operating_mode
uint16_t radar_data[TOTAL_GATES][CALIBRATE_SAMPLES]
uint8_t sample_number_counter
void set_presence_(bool presence)
number::Number * gate_move_sensitivity_factor_number_
void set_reg_value(uint16_t reg, uint16_t value)
void publish_state(float state)
void publish_state(const std::string &state)
void write_array(const uint8_t *data, size_t len)
uint8_t find_uint8(const StringToUint8(&arr)[N], const std::string &str)
const float BUS
For communication buses like i2c/spi.
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
uint32_t move_thresh[TOTAL_GATES]
uint32_t still_thresh[TOTAL_GATES]