63static const char *
const TAG =
"ld2420";
66static const uint16_t REFRESH_RATE_MS = 1000;
69static const uint16_t CMD_DISABLE_CONF = 0x00FE;
70static const uint16_t CMD_ENABLE_CONF = 0x00FF;
71static const uint16_t CMD_PARM_HIGH_TRESH = 0x0012;
72static const uint16_t CMD_PARM_LOW_TRESH = 0x0021;
73static const uint16_t CMD_PROTOCOL_VER = 0x0002;
74static const uint16_t CMD_READ_ABD_PARAM = 0x0008;
75static const uint16_t CMD_READ_REG_ADDR = 0x0020;
76static const uint16_t CMD_READ_REGISTER = 0x0002;
77static const uint16_t CMD_READ_SERIAL_NUM = 0x0011;
78static const uint16_t CMD_READ_SYS_PARAM = 0x0013;
79static const uint16_t CMD_READ_VERSION = 0x0000;
80static const uint16_t CMD_RESTART = 0x0068;
81static const uint16_t CMD_SYSTEM_MODE = 0x0000;
82static const uint16_t CMD_SYSTEM_MODE_GR = 0x0003;
83static const uint16_t CMD_SYSTEM_MODE_MTT = 0x0001;
84static const uint16_t CMD_SYSTEM_MODE_SIMPLE = 0x0064;
85static const uint16_t CMD_SYSTEM_MODE_DEBUG = 0x0000;
86static const uint16_t CMD_SYSTEM_MODE_ENERGY = 0x0004;
87static const uint16_t CMD_SYSTEM_MODE_VS = 0x0002;
88static const uint16_t CMD_WRITE_ABD_PARAM = 0x0007;
89static const uint16_t CMD_WRITE_REGISTER = 0x0001;
90static const uint16_t CMD_WRITE_SYS_PARAM = 0x0012;
92static const uint8_t CMD_ABD_DATA_REPLY_SIZE = 0x04;
93static const uint8_t CMD_ABD_DATA_REPLY_START = 0x0A;
94static const uint8_t CMD_MAX_BYTES = 0x64;
95static const uint8_t CMD_REG_DATA_REPLY_SIZE = 0x02;
97static const uint8_t LD2420_ERROR_NONE = 0x00;
98static const uint8_t LD2420_ERROR_TIMEOUT = 0x02;
99static const uint8_t LD2420_ERROR_UNKNOWN = 0x01;
102static const uint16_t CMD_MIN_GATE_REG = 0x0000;
103static const uint16_t CMD_MAX_GATE_REG = 0x0001;
104static const uint16_t CMD_TIMEOUT_REG = 0x0004;
105static const uint16_t CMD_GATE_MOVE_THRESH[TOTAL_GATES] = {0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015,
106 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B,
107 0x001C, 0x001D, 0x001E, 0x001F};
108static const uint16_t CMD_GATE_STILL_THRESH[TOTAL_GATES] = {0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025,
109 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B,
110 0x002C, 0x002D, 0x002E, 0x002F};
111static const uint32_t FACTORY_MOVE_THRESH[TOTAL_GATES] = {60000, 30000, 400, 250, 250, 250, 250, 250,
112 250, 250, 250, 250, 250, 250, 250, 250};
113static const uint32_t FACTORY_STILL_THRESH[TOTAL_GATES] = {40000, 20000, 200, 200, 200, 200, 200, 150,
114 150, 100, 100, 100, 100, 100, 100, 100};
115static const uint16_t FACTORY_TIMEOUT = 120;
116static const uint16_t FACTORY_MIN_GATE = 1;
117static const uint16_t FACTORY_MAX_GATE = 12;
120static const uint32_t CMD_FRAME_FOOTER = 0x01020304;
121static const uint32_t CMD_FRAME_HEADER = 0xFAFBFCFD;
122static const uint32_t DEBUG_FRAME_FOOTER = 0xFAFBFCFD;
123static const uint32_t DEBUG_FRAME_HEADER = 0x1410BFAA;
124static const uint32_t ENERGY_FRAME_FOOTER = 0xF5F6F7F8;
125static const uint32_t ENERGY_FRAME_HEADER = 0xF1F2F3F4;
126static const int CALIBRATE_VERSION_MIN = 154;
127static const uint8_t CMD_FRAME_COMMAND = 6;
128static const uint8_t CMD_FRAME_DATA_LENGTH = 4;
129static const uint8_t CMD_FRAME_STATUS = 7;
130static const uint8_t CMD_ERROR_WORD = 8;
131static const uint8_t ENERGY_SENSOR_START = 9;
132static const uint8_t CALIBRATE_REPORT_INTERVAL = 4;
133static const char *
const OP_NORMAL_MODE_STRING =
"Normal";
134static const char *
const OP_SIMPLE_MODE_STRING =
"Simple";
137struct StringToUint8 {
142static constexpr StringToUint8 OP_MODE_BY_STR[] = {
148static constexpr const char *ERR_MESSAGE[] = {
155template<
size_t N> uint8_t
find_uint8(
const StringToUint8 (&arr)[N],
const std::string &str) {
156 for (
const auto &entry : arr) {
157 if (str == entry.str) {
164static uint8_t calc_checksum(
void *data,
size_t size) {
166 uint8_t *data_bytes = (uint8_t *) data;
167 for (
size_t i = 0; i < size; i++) {
173static int get_firmware_int(
const char *version_string) {
174 std::string version_str = version_string;
175 if (version_str[0] ==
'v') {
176 version_str.erase(0, 1);
178 version_str.erase(remove(version_str.begin(), version_str.end(),
'.'), version_str.end());
179 int version_integer = stoi(version_str);
180 return version_integer;
188 " Firmware version: %7s",
191 ESP_LOGCONFIG(TAG,
"Number:");
196 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
208 ESP_LOGCONFIG(TAG,
"Select:");
211 if (ld2420::get_firmware_int(this->
firmware_ver_) < CALIBRATE_VERSION_MIN) {
212 ESP_LOGW(TAG,
"Firmware version %s and older supports Simple Mode only", this->
firmware_ver_);
218 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
228 std::string fw_str(pfw);
231 listener->on_fw_version(fw_str);
234 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
240 if (ld2420::get_firmware_int(this->
firmware_ver_) < CALIBRATE_VERSION_MIN) {
248 ESP_LOGW(TAG,
"Firmware version %s and older supports Simple Mode only", this->
firmware_ver_);
267 ESP_LOGD(TAG,
"No configuration change detected");
270 ESP_LOGD(TAG,
"Reconfiguring");
272 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
277 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
291 ESP_LOGD(TAG,
"Setting factory defaults");
293 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
303 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
319 ESP_LOGD(TAG,
"Restarting");
333 ESP_LOGD(TAG,
"Reverted config number edits");
344 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
345 this->
radar_data[gate][sample_number] = gate_energy[gate];
354 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
358 for (uint8_t sample_number = 0; sample_number < CALIBRATE_SAMPLES; ++sample_number) {
363 if (this->
radar_data[gate][sample_number] > peak) {
369 this->
gate_avg[gate] = sum / CALIBRATE_SAMPLES;
374 uint32_t calculated_value =
375 (
static_cast<uint32_t
>(this->
gate_peak[gate]) + (move_factor *
static_cast<uint32_t
>(this->
gate_peak[gate])));
376 this->
new_config.
move_thresh[gate] =
static_cast<uint16_t
>(calculated_value <= 65535 ? calculated_value : 65535);
378 (
static_cast<uint32_t
>(this->
gate_peak[gate]) + (still_factor *
static_cast<uint32_t
>(this->
gate_peak[gate])));
379 this->
new_config.
still_thresh[gate] =
static_cast<uint16_t
>(calculated_value <= 65535 ? calculated_value : 65535);
384 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
386 ESP_LOGI(TAG,
"Gate: %2d Avg: %5d Peak: %5d", gate, this->
gate_avg[gate], this->
gate_peak[gate]);
393 if (ld2420::get_firmware_int(
firmware_ver_) >= CALIBRATE_VERSION_MIN) {
403 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
406 for (uint8_t i = 0; i < CALIBRATE_SAMPLES; i++) {
437 ESP_LOGW(TAG,
"Max command length exceeded; ignoring");
443 if (memcmp(&buffer[this->
buffer_pos_ - 4], &CMD_FRAME_FOOTER,
sizeof(CMD_FRAME_FOOTER)) == 0) {
448 (this->
get_mode_() == CMD_SYSTEM_MODE_SIMPLE)) {
451 }
else if ((memcmp(&buffer[this->
buffer_pos_ - 4], &ENERGY_FRAME_FOOTER,
sizeof(ENERGY_FRAME_FOOTER)) == 0) &&
452 (this->
get_mode_() == CMD_SYSTEM_MODE_ENERGY)) {
464 memcpy(&
range, &buffer[index],
sizeof(
range));
465 index +=
sizeof(
range);
467 for (uint8_t i = 0; i < elements; i++) {
499 const uint8_t bufsize = 16;
502 char *endptr{
nullptr};
503 char outbuf[bufsize]{0};
505 if (inbuf[pos - 2] ==
'O' && inbuf[pos - 1] ==
'F' && inbuf[pos] ==
'F') {
507 }
else if (inbuf[pos - 1] ==
'O' && inbuf[pos] ==
'N') {
510 if (inbuf[pos] >=
'0' && inbuf[pos] <=
'9') {
511 if (index < bufsize - 1) {
512 outbuf[index++] = inbuf[pos];
523 outbuf[index] =
'\0';
528 if (this->
get_mode_() == CMD_SYSTEM_MODE_SIMPLE) {
537 for (
auto &listener : this->listeners_)
545 uint8_t reg_element = 0;
546 uint8_t data_element = 0;
547 uint16_t data_pos = 0;
549 ESP_LOGW(TAG,
"Reply frame too long");
552 ESP_LOGW(TAG,
"Command frame too short");
555 memcpy(&this->
cmd_reply_.
error, &buffer[CMD_ERROR_WORD],
sizeof(this->cmd_reply_.error));
562 case (CMD_ENABLE_CONF):
563 ESP_LOGV(TAG,
"Set config enable: CMD = %2X %s", CMD_ENABLE_CONF, result);
565 case (CMD_DISABLE_CONF):
566 ESP_LOGV(TAG,
"Set config disable: CMD = %2X %s", CMD_DISABLE_CONF, result);
568 case (CMD_READ_REGISTER):
569 ESP_LOGV(TAG,
"Read register: CMD = %2X %s", CMD_READ_REGISTER, result);
572 for (uint16_t index = 0; index < (CMD_REG_DATA_REPLY_SIZE *
573 ((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_REG_DATA_REPLY_SIZE));
574 index += CMD_REG_DATA_REPLY_SIZE) {
575 memcpy(&this->
cmd_reply_.
data[reg_element], &buffer[data_pos + index],
sizeof(CMD_REG_DATA_REPLY_SIZE));
580 case (CMD_WRITE_REGISTER):
581 ESP_LOGV(TAG,
"Write register: CMD = %2X %s", CMD_WRITE_REGISTER, result);
583 case (CMD_WRITE_ABD_PARAM):
584 ESP_LOGV(TAG,
"Write gate parameter(s): %2X %s", CMD_WRITE_ABD_PARAM, result);
586 case (CMD_READ_ABD_PARAM):
587 ESP_LOGV(TAG,
"Read gate parameter(s): %2X %s", CMD_READ_ABD_PARAM, result);
588 data_pos = CMD_ABD_DATA_REPLY_START;
589 for (uint16_t index = 0; index < (CMD_ABD_DATA_REPLY_SIZE *
590 ((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_ABD_DATA_REPLY_SIZE));
591 index += CMD_ABD_DATA_REPLY_SIZE) {
592 memcpy(&this->
cmd_reply_.
data[data_element], &buffer[data_pos + index],
593 sizeof(this->cmd_reply_.data[data_element]));
598 case (CMD_WRITE_SYS_PARAM):
599 ESP_LOGV(TAG,
"Set system parameter(s): %2X %s", CMD_WRITE_SYS_PARAM, result);
601 case (CMD_READ_VERSION):
603 ESP_LOGV(TAG,
"Firmware version: %7s %s", this->
firmware_ver_, result);
611 uint32_t start_millis =
millis();
613 uint8_t ack_buffer[MAX_LINE_LENGTH];
614 uint8_t cmd_buffer[MAX_LINE_LENGTH];
616 if (frame.
command != CMD_RESTART) {
633 for (uint16_t index = 0; index < frame.
data_length; index++) {
634 memcpy(&cmd_buffer[frame.
length], &frame.
data[index],
sizeof(frame.
data[index]));
643 if (frame.
command == CMD_RESTART) {
649 this->
readline_(this->
read(), ack_buffer,
sizeof(ack_buffer));
653 if ((
millis() - start_millis) > 1000) {
655 error = LD2420_ERROR_TIMEOUT;
673 cmd_frame.
header = CMD_FRAME_HEADER;
674 cmd_frame.
command = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
676 memcpy(&cmd_frame.
data[0], &CMD_PROTOCOL_VER,
sizeof(CMD_PROTOCOL_VER));
679 cmd_frame.
footer = CMD_FRAME_FOOTER;
680 ESP_LOGV(TAG,
"Sending set config %s command: %2X", enable ?
"enable" :
"disable", cmd_frame.
command);
690 cmd_frame.
header = CMD_FRAME_HEADER;
691 cmd_frame.
command = CMD_RESTART;
692 cmd_frame.
footer = CMD_FRAME_FOOTER;
693 ESP_LOGV(TAG,
"Sending restart command: %2X", cmd_frame.
command);
700 cmd_frame.
header = CMD_FRAME_HEADER;
701 cmd_frame.
command = CMD_READ_REGISTER;
702 cmd_frame.
data[1] = reg;
704 cmd_frame.
footer = CMD_FRAME_FOOTER;
705 ESP_LOGV(TAG,
"Sending read register %4X command: %2X", reg, cmd_frame.
command);
712 cmd_frame.
header = CMD_FRAME_HEADER;
713 cmd_frame.
command = CMD_WRITE_REGISTER;
714 memcpy(&cmd_frame.
data[cmd_frame.
data_length], ®,
sizeof(CMD_REG_DATA_REPLY_SIZE));
716 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &value,
sizeof(CMD_REG_DATA_REPLY_SIZE));
718 cmd_frame.
footer = CMD_FRAME_FOOTER;
719 ESP_LOGV(TAG,
"Sending write register %4X command: %2X data = %4X", reg, cmd_frame.
command, value);
729 cmd_frame.
header = CMD_FRAME_HEADER;
730 cmd_frame.
command = CMD_READ_ABD_PARAM;
731 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &CMD_GATE_MOVE_THRESH[gate],
sizeof(CMD_GATE_MOVE_THRESH[gate]));
733 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &CMD_GATE_STILL_THRESH[gate],
sizeof(CMD_GATE_STILL_THRESH[gate]));
735 cmd_frame.
footer = CMD_FRAME_FOOTER;
736 ESP_LOGV(TAG,
"Sending read gate %d high/low threshold command: %2X", gate, cmd_frame.
command);
749 cmd_frame.
header = CMD_FRAME_HEADER;
750 cmd_frame.
command = CMD_READ_ABD_PARAM;
752 sizeof(CMD_MIN_GATE_REG));
755 sizeof(CMD_MAX_GATE_REG));
758 sizeof(CMD_TIMEOUT_REG));
760 cmd_frame.
footer = CMD_FRAME_FOOTER;
761 ESP_LOGV(TAG,
"Sending read gate min max and timeout command: %2X", cmd_frame.
command);
773 uint16_t unknown_parm = 0x0000;
775 cmd_frame.
header = CMD_FRAME_HEADER;
776 cmd_frame.
command = CMD_WRITE_SYS_PARAM;
777 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &CMD_SYSTEM_MODE,
sizeof(CMD_SYSTEM_MODE));
781 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &unknown_parm,
sizeof(unknown_parm));
783 cmd_frame.
footer = CMD_FRAME_FOOTER;
784 ESP_LOGV(TAG,
"Sending write system mode command: %2X", cmd_frame.
command);
793 cmd_frame.
header = CMD_FRAME_HEADER;
794 cmd_frame.
command = CMD_READ_VERSION;
795 cmd_frame.
footer = CMD_FRAME_FOOTER;
797 ESP_LOGV(TAG,
"Sending read firmware version command: %2X", cmd_frame.
command);
810 cmd_frame.
header = CMD_FRAME_HEADER;
811 cmd_frame.
command = CMD_WRITE_ABD_PARAM;
813 sizeof(CMD_MIN_GATE_REG));
815 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &min_gate_distance,
sizeof(min_gate_distance));
816 cmd_frame.
data_length +=
sizeof(min_gate_distance);
818 sizeof(CMD_MAX_GATE_REG));
820 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &max_gate_distance,
sizeof(max_gate_distance));
821 cmd_frame.
data_length +=
sizeof(max_gate_distance);
823 sizeof(CMD_TIMEOUT_REG));
825 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &timeout,
sizeof(timeout));
828 cmd_frame.
footer = CMD_FRAME_FOOTER;
830 ESP_LOGV(TAG,
"Sending write gate min max and timeout command: %2X", cmd_frame.
command);
839 uint16_t move_threshold_gate = CMD_GATE_MOVE_THRESH[gate];
840 uint16_t still_threshold_gate = CMD_GATE_STILL_THRESH[gate];
843 cmd_frame.
header = CMD_FRAME_HEADER;
844 cmd_frame.
command = CMD_WRITE_ABD_PARAM;
845 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &move_threshold_gate,
sizeof(move_threshold_gate));
846 cmd_frame.
data_length +=
sizeof(move_threshold_gate);
847 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &this->new_config.move_thresh[gate],
848 sizeof(this->new_config.move_thresh[gate]));
850 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &still_threshold_gate,
sizeof(still_threshold_gate));
851 cmd_frame.
data_length +=
sizeof(still_threshold_gate);
852 memcpy(&cmd_frame.
data[cmd_frame.
data_length], &this->new_config.still_thresh[gate],
853 sizeof(this->new_config.still_thresh[gate]));
855 cmd_frame.
footer = CMD_FRAME_FOOTER;
856 ESP_LOGV(TAG,
"Sending set gate %4X sensitivity command: %2X", gate, cmd_frame.
command);
880 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_
int get_min_max_distances_timeout_()
number::Number * gate_timeout_number_
uint16_t total_sample_number_counter
void set_operating_mode(const char *state)
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.
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]