ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2420.cpp
Go to the documentation of this file.
1#include "ld2420.h"
5
6/*
7Configure commands - little endian
8
9No command can exceed 64 bytes, otherwise they would need be to be split up into multiple sends.
10
11All send command frames will have:
12 Header = FD FC FB FA, Bytes 0 - 3, uint32_t 0xFAFBFCFD
13 Length, bytes 4 - 5, uint16_t 0x0002, must be at least 2 for the command byte if no addon data.
14 Command bytes 6 - 7, uint16_t
15 Footer = 04 03 02 01 - uint32_t 0x01020304, Always last 4 Bytes.
16Receive
17 Error bytes 8-9 uint16_t, 0 = success, all other positive values = error
18
19Enable config mode:
20Send:
21 UART Tx: FD FC FB FA 04 00 FF 00 02 00 04 03 02 01
22 Command = FF 00 - uint16_t 0x00FF
23 Protocol version = 02 00, can be 1 or 2 - uint16_t 0x0002
24Reply:
25 UART Rx: FD FC FB FA 06 00 FF 01 00 00 02 00 04 03 02 01
26
27Disable config mode:
28Send:
29 UART Tx: FD FC FB FA 02 00 FE 00 04 03 02 01
30 Command = FE 00 - uint16_t 0x00FE
31Receive:
32 UART Rx: FD FC FB FA 04 00 FE 01 00 00 04 03 02 01
33
34Configure system parameters:
35
36UART Tx: FD FC FB FA 08 00 12 00 00 00 64 00 00 00 04 03 02 01 Set system parms
37Command = 12 00 - uint16_t 0x0012, Param
38There are three documented parameters for modes:
39 00 64 = Basic status mode
40 This mode outputs text as presence "ON" or "OFF" and "Range XXXX"
41 where XXXX is a decimal value for distance in cm
42 00 04 = Energy output mode
43 This mode outputs detailed signal energy values for each gate and the target distance.
44 The data format consist of the following.
45 Header HH, Length LL, Presence PP, Distance DD, 16 Gate Energies EE, Footer FF
46 HH HH HH HH LL LL PP DD DD EE EE .. 16x .. FF FF FF FF
47 F4 F3 F2 F1 23 00 00 00 00 00 00 .. .. .. .. F8 F7 F6 F5
48 00 00 = debug output mode
49 This mode outputs detailed values consisting of 20 Dopplers, 16 Ranges for a total 20 * 16 * 4 bytes
50 The data format consist of the following.
51 Header HH, Doppler DD, Range RR, Footer FF
52 HH HH HH HH DD DD DD DD .. 20x .. RR RR RR RR .. 16x .. FF FF FF FF
53 AA BF 10 14 00 00 00 00 .. .. .. .. 00 00 00 00 .. .. .. .. FD FC FB FA
54
55Configure gate sensitivity parameters:
56UART Tx: FD FC FB FA 0E 00 07 00 10 00 60 EA 00 00 20 00 60 EA 00 00 04 03 02 01
57Command = 12 00 - uint16_t 0x0007
58Gate 0 high thresh = 10 00 uint16_t 0x0010, Threshold value = 60 EA 00 00 uint32_t 0x0000EA60
59Gate 0 low thresh = 20 00 uint16_t 0x0020, Threshold value = 60 EA 00 00 uint32_t 0x0000EA60
60*/
61
62namespace esphome::ld2420 {
63
64ESPHOME_LOG_TAG(TAG, "ld2420");
65
66// Local const's
67static constexpr uint16_t REFRESH_RATE_MS = 1000;
68
69// Command sets
70static constexpr uint16_t CMD_DISABLE_CONF = 0x00FE;
71static constexpr uint16_t CMD_ENABLE_CONF = 0x00FF;
72static constexpr uint16_t CMD_PARM_HIGH_TRESH = 0x0012;
73static constexpr uint16_t CMD_PARM_LOW_TRESH = 0x0021;
74static constexpr uint16_t CMD_PROTOCOL_VER = 0x0002;
75static constexpr uint16_t CMD_READ_ABD_PARAM = 0x0008;
76static constexpr uint16_t CMD_READ_REG_ADDR = 0x0020;
77static constexpr uint16_t CMD_READ_REGISTER = 0x0002;
78static constexpr uint16_t CMD_READ_SERIAL_NUM = 0x0011;
79static constexpr uint16_t CMD_READ_SYS_PARAM = 0x0013;
80static constexpr uint16_t CMD_READ_VERSION = 0x0000;
81static constexpr uint16_t CMD_RESTART = 0x0068;
82static constexpr uint16_t CMD_SYSTEM_MODE = 0x0000;
83static constexpr uint16_t CMD_SYSTEM_MODE_GR = 0x0003;
84static constexpr uint16_t CMD_SYSTEM_MODE_MTT = 0x0001;
85static constexpr uint16_t CMD_SYSTEM_MODE_SIMPLE = 0x0064;
86static constexpr uint16_t CMD_SYSTEM_MODE_DEBUG = 0x0000;
87static constexpr uint16_t CMD_SYSTEM_MODE_ENERGY = 0x0004;
88static constexpr uint16_t CMD_SYSTEM_MODE_VS = 0x0002;
89static constexpr uint16_t CMD_WRITE_ABD_PARAM = 0x0007;
90static constexpr uint16_t CMD_WRITE_REGISTER = 0x0001;
91static constexpr uint16_t CMD_WRITE_SYS_PARAM = 0x0012;
92
93static constexpr uint8_t CMD_ABD_DATA_REPLY_SIZE = 0x04;
94static constexpr uint8_t CMD_ABD_DATA_REPLY_START = 0x0A;
95static constexpr uint8_t CMD_MAX_BYTES = 0x64;
96static constexpr uint8_t CMD_REG_DATA_REPLY_SIZE = 0x02;
97
98static constexpr uint8_t LD2420_ERROR_NONE = 0x00;
99static constexpr uint8_t LD2420_ERROR_TIMEOUT = 0x02;
100static constexpr uint8_t LD2420_ERROR_UNKNOWN = 0x01;
101
102// Register address values
103static constexpr uint16_t CMD_MIN_GATE_REG = 0x0000;
104static constexpr uint16_t CMD_MAX_GATE_REG = 0x0001;
105static constexpr uint16_t CMD_TIMEOUT_REG = 0x0004;
106// Gate n uses the base register plus n
107static constexpr uint16_t CMD_GATE_MOVE_THRESH_REG = 0x0010;
108static constexpr uint16_t CMD_GATE_STILL_THRESH_REG = 0x0020;
109// In flash on ESP8266; indexed directly since each entry is an aligned 32 bit read
110static constexpr uint32_t FACTORY_MOVE_THRESH[TOTAL_GATES] PROGMEM = {60000, 30000, 400, 250, 250, 250, 250, 250,
111 250, 250, 250, 250, 250, 250, 250, 250};
112static constexpr uint32_t FACTORY_STILL_THRESH[TOTAL_GATES] PROGMEM = {40000, 20000, 200, 200, 200, 200, 200, 150,
113 150, 100, 100, 100, 100, 100, 100, 100};
114static constexpr uint16_t FACTORY_TIMEOUT = 120;
115static constexpr uint16_t FACTORY_MIN_GATE = 1;
116static constexpr uint16_t FACTORY_MAX_GATE = 12;
117
118// COMMAND_BYTE Header & Footer
119static constexpr uint32_t CMD_FRAME_FOOTER = 0x01020304;
120static constexpr uint32_t CMD_FRAME_HEADER = 0xFAFBFCFD;
121static constexpr uint32_t DEBUG_FRAME_FOOTER = 0xFAFBFCFD;
122static constexpr uint32_t DEBUG_FRAME_HEADER = 0x1410BFAA;
123static constexpr uint32_t ENERGY_FRAME_FOOTER = 0xF5F6F7F8;
124static constexpr uint32_t ENERGY_FRAME_HEADER = 0xF1F2F3F4;
125static constexpr int CALIBRATE_VERSION_MIN = 154;
126static constexpr uint8_t CMD_FRAME_COMMAND = 6;
127static constexpr uint8_t CMD_FRAME_DATA_LENGTH = 4;
128static constexpr uint8_t CMD_FRAME_STATUS = 7;
129static constexpr uint8_t CMD_ERROR_WORD = 8;
130static constexpr uint8_t ENERGY_SENSOR_START = 9;
131static constexpr uint8_t CALIBRATE_REPORT_INTERVAL = 4;
132// Index + 1 is the OpMode value; the select options use the same order
133PROGMEM_STRING_TABLE(OpModeStrings, "Normal", "Calibrate", "Simple");
134static_assert(OP_NORMAL_MODE == 1 && OP_CALIBRATE_MODE == 2 && OP_SIMPLE_MODE == 3, "OpMode is index + 1");
135PROGMEM_STRING_TABLE(ErrMessages, "None", "Unknown", "Timeout");
136
137static uint8_t calc_checksum(const void *data, size_t size) {
138 return xor_bytes(static_cast<const uint8_t *>(data), size);
139}
140
141// Simple mode needs simple reporting from the module; Normal and Calibrate read energy frames
142static uint16_t system_mode_for(uint8_t mode) {
143 return mode == OP_SIMPLE_MODE ? CMD_SYSTEM_MODE_SIMPLE : CMD_SYSTEM_MODE_ENERGY;
144}
145
146static int32_t get_firmware_int(const char *version_string) {
147 // Convert "v1.5.4" -> 154 by skipping 'v' and '.', accumulating digits
148 const char *p = (*version_string == 'v') ? version_string + 1 : version_string;
149 int32_t result = 0;
150 for (; *p != '\0'; p++) {
151 if (*p == '.')
152 continue;
153 if (*p < '0' || *p > '9')
154 return 0;
155 result = result * 10 + (*p - '0');
156 }
157 return result;
158}
159
161 ESP_LOGCONFIG(TAG,
162 "LD2420:\n"
163 " Firmware version: %7s",
164 this->firmware_ver_);
165#ifdef USE_NUMBER
166 ESP_LOGCONFIG(TAG, "Number:");
167 LOG_NUMBER(" ", "Gate Timeout:", this->gate_timeout_number_);
168 LOG_NUMBER(" ", "Gate Max Distance:", this->max_gate_distance_number_);
169 LOG_NUMBER(" ", "Gate Min Distance:", this->min_gate_distance_number_);
170 LOG_NUMBER(" ", "Gate Select:", this->gate_select_number_);
171 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
172 LOG_NUMBER(" ", "Gate Move Threshold:", this->gate_move_threshold_numbers_[gate]);
173 LOG_NUMBER(" ", "Gate Still Threshold::", this->gate_still_threshold_numbers_[gate]);
174 }
175#endif
176#ifdef USE_BUTTON
177 LOG_BUTTON(" ", "Apply Config:", this->apply_config_button_);
178 LOG_BUTTON(" ", "Revert Edits:", this->revert_config_button_);
179 LOG_BUTTON(" ", "Factory Reset:", this->factory_reset_button_);
180 LOG_BUTTON(" ", "Restart Module:", this->restart_module_button_);
181#endif
182#ifdef USE_SELECT
183 ESP_LOGCONFIG(TAG, "Select:");
184 LOG_SELECT(" ", "Operating Mode", this->operating_mode_select_);
185#endif
186 if (ld2420::get_firmware_int(this->firmware_ver_) < CALIBRATE_VERSION_MIN) {
187 ESP_LOGW(TAG, "Firmware version %s and older supports Simple Mode only", this->firmware_ver_);
188 }
189}
190
192 if (this->set_config_mode(true) == LD2420_ERROR_TIMEOUT) {
193 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
194 this->mark_failed();
195 return;
196 }
198#ifdef USE_NUMBER
200#endif
201 this->get_firmware_version_();
202#ifdef LD2420_LISTENER_COUNT
203 for (auto *listener : this->listeners_) {
204 listener->on_fw_version(this->firmware_ver_);
205 }
206#endif
207
208 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
210 this->get_gate_threshold_(gate);
211 }
212
213 memcpy(&this->new_config, &this->current_config, sizeof(this->current_config));
214 if (ld2420::get_firmware_int(this->firmware_ver_) < CALIBRATE_VERSION_MIN) {
215 ESP_LOGW(TAG, "Firmware version %s and older supports Simple Mode only", this->firmware_ver_);
216 }
217 this->set_operating_mode(OP_NORMAL_MODE); // older firmware is held in Simple
218 this->set_mode_(system_mode_for(this->current_operating_mode));
219#ifdef USE_NUMBER
221#endif
222 this->set_system_mode(this->system_mode_);
223 this->set_config_mode(false);
224}
225
227 const uint8_t checksum = calc_checksum(&this->new_config, sizeof(this->new_config));
228 if (checksum == calc_checksum(&this->current_config, sizeof(this->current_config))) {
229 ESP_LOGD(TAG, "No configuration change detected");
230 return;
231 }
232 ESP_LOGD(TAG, "Reconfiguring");
233 if (this->set_config_mode(true) == LD2420_ERROR_TIMEOUT) {
234 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
235 this->mark_failed();
236 return;
237 }
238 this->set_min_max_distances_timeout(this->new_config.max_gate, this->new_config.min_gate, this->new_config.timeout);
239 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
241 this->set_gate_threshold(gate);
242 }
243 memcpy(&current_config, &new_config, sizeof(new_config));
244#ifdef USE_NUMBER
246#endif
247 this->set_system_mode(this->system_mode_);
248 this->set_config_mode(false); // Disable config mode to save new values in LD2420 nvm
249 // Applying the calibrated thresholds ends calibration; Normal and Simple stay as they are
252}
253
255 ESP_LOGD(TAG, "Setting factory defaults");
256 if (this->set_config_mode(true) == LD2420_ERROR_TIMEOUT) {
257 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
258 this->mark_failed();
259 return;
260 }
261 this->set_min_max_distances_timeout(FACTORY_MAX_GATE, FACTORY_MIN_GATE, FACTORY_TIMEOUT);
262#ifdef USE_NUMBER
263 this->gate_timeout_number_->state = FACTORY_TIMEOUT;
264 this->min_gate_distance_number_->state = FACTORY_MIN_GATE;
265 this->max_gate_distance_number_->state = FACTORY_MAX_GATE;
266#endif
267 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
268 this->new_config.move_thresh[gate] = FACTORY_MOVE_THRESH[gate];
269 this->new_config.still_thresh[gate] = FACTORY_STILL_THRESH[gate];
271 this->set_gate_threshold(gate);
272 }
273 memcpy(&this->current_config, &this->new_config, sizeof(this->new_config));
274 this->set_system_mode(this->system_mode_);
275 this->set_config_mode(false);
276#ifdef USE_NUMBER
279#endif
280}
281
283 ESP_LOGD(TAG, "Restarting");
284 this->send_module_restart();
285 this->set_timeout(250, [this]() {
286 this->set_config_mode(true);
287 this->set_system_mode(this->system_mode_);
288 this->set_config_mode(false);
289 });
290}
291
293 memcpy(&this->new_config, &this->current_config, sizeof(this->current_config));
294#ifdef USE_NUMBER
296#endif
297 ESP_LOGD(TAG, "Reverted config number edits");
298}
299
301 // If there is a active send command do not process it here, the send command call will handle it.
302 if (this->cmd_active_) {
303 return;
304 }
305 this->read_batch_(this->buffer_data_);
306}
307
308void LD2420Component::update_radar_data(uint16_t const *gate_energy, uint8_t sample_number) {
309 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
310 this->radar_data[gate][sample_number] = gate_energy[gate];
311 }
313}
314
316 // Calculate average and peak values for each gate
317 const float move_factor = gate_move_sensitivity_factor + 1;
318 const float still_factor = (gate_still_sensitivity_factor / 2) + 1;
319 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
320 uint32_t sum = 0;
321 uint16_t peak = 0;
322
323 for (uint8_t sample_number = 0; sample_number < CALIBRATE_SAMPLES; ++sample_number) {
324 // Calculate average
325 sum += this->radar_data[gate][sample_number];
326
327 // Calculate max value
328 if (this->radar_data[gate][sample_number] > peak) {
329 peak = this->radar_data[gate][sample_number];
330 }
331 }
332
333 // Store average and peak values
334 this->gate_avg[gate] = sum / CALIBRATE_SAMPLES;
335 if (this->gate_peak[gate] < peak) {
336 this->gate_peak[gate] = peak;
337 }
338
339 uint32_t calculated_value =
340 (static_cast<uint32_t>(this->gate_peak[gate]) + (move_factor * static_cast<uint32_t>(this->gate_peak[gate])));
341 this->new_config.move_thresh[gate] = static_cast<uint16_t>(calculated_value <= 65535 ? calculated_value : 65535);
342 calculated_value =
343 (static_cast<uint32_t>(this->gate_peak[gate]) + (still_factor * static_cast<uint32_t>(this->gate_peak[gate])));
344 this->new_config.still_thresh[gate] = static_cast<uint16_t>(calculated_value <= 65535 ? calculated_value : 65535);
345 }
346}
347
349 for (uint8_t gate = 0; gate < TOTAL_GATES; ++gate) {
350 // Output results
351 ESP_LOGI(TAG, "Gate: %2d Avg: %5d Peak: %5d", gate, this->gate_avg[gate], this->gate_peak[gate]);
352 }
353 ESP_LOGI(TAG, "Total samples: %d", this->total_sample_number_counter);
354}
355
357 const int16_t index = progmem_string_table_find<OpModeStrings>(state, strlen(state));
358 if (index >= 0) {
359 this->set_operating_mode(static_cast<OpMode>(index + 1));
360 }
361}
362
364 this->set_operating_mode(mode);
365 const uint16_t system_mode = system_mode_for(this->current_operating_mode);
366 if (system_mode == this->get_mode_())
367 return;
368 if (this->set_config_mode(true) != LD2420_ERROR_TIMEOUT) {
369 this->set_system_mode(system_mode);
370 this->set_config_mode(false);
371 }
372 if (this->get_mode_() != system_mode) {
373 // The module kept its reporting mode; show the mode that matches it
374 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
375 this->set_operating_mode(this->get_mode_() == CMD_SYSTEM_MODE_SIMPLE ? OP_SIMPLE_MODE : OP_NORMAL_MODE);
376 }
377}
378
380 // Older firmware only supports simple mode
381 if (ld2420::get_firmware_int(this->firmware_ver_) < CALIBRATE_VERSION_MIN) {
383 }
385#ifdef USE_SELECT
386 PUBLISH_STATE(this->operating_mode_select_, static_cast<size_t>(mode - 1));
387#endif
388 if (mode == OP_CALIBRATE_MODE) {
389 // Entering Auto Calibrate we need to clear the previous data collection
390 this->set_calibration_(true);
391 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
392 this->gate_avg[gate] = 0;
393 this->gate_peak[gate] = 0;
394 for (uint8_t i = 0; i < CALIBRATE_SAMPLES; i++) {
395 this->radar_data[gate][i] = 0;
396 }
398 }
399 } else {
400 // Set the current data back so we don't have new data that can be applied in error.
401 if (this->get_calibration_()) {
402 memcpy(&this->new_config, &this->current_config, sizeof(this->current_config));
403 }
404 this->set_calibration_(false);
405 }
406}
407
408void LD2420Component::readline_(int rx_data, uint8_t *buffer, int len) {
409 if (rx_data < 0) {
410 return; // No data available
411 }
412 if (this->buffer_pos_ < len - 1) {
413 buffer[this->buffer_pos_++] = rx_data;
414 buffer[this->buffer_pos_] = 0;
415 } else {
416 // We should never get here, but just in case...
417 ESP_LOGW(TAG, "Max command length exceeded; ignoring");
418 this->buffer_pos_ = 0;
419 }
420 if (this->buffer_pos_ < 4) {
421 return; // Not enough data to process yet
422 }
423 if (memcmp(&buffer[this->buffer_pos_ - 4], &CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER)) == 0) {
424 this->cmd_active_ = false; // Set command state to inactive after response
425 this->handle_ack_data_(buffer, this->buffer_pos_);
426 this->buffer_pos_ = 0;
427 } else if ((buffer[this->buffer_pos_ - 2] == 0x0D && buffer[this->buffer_pos_ - 1] == 0x0A) &&
428 (this->get_mode_() == CMD_SYSTEM_MODE_SIMPLE)) {
429 this->handle_simple_mode_(buffer, this->buffer_pos_);
430 this->buffer_pos_ = 0;
431 } else if ((memcmp(&buffer[this->buffer_pos_ - 4], &ENERGY_FRAME_FOOTER, sizeof(ENERGY_FRAME_FOOTER)) == 0) &&
432 (this->get_mode_() == CMD_SYSTEM_MODE_ENERGY)) {
433 this->handle_energy_mode_(buffer, this->buffer_pos_);
434 this->buffer_pos_ = 0;
435 }
436}
437
439 uint8_t index = 6; // Start at presence byte position
440 uint16_t range;
441 const uint8_t elements = sizeof(this->gate_energy_) / sizeof(this->gate_energy_[0]);
442 if (len < static_cast<int>(index + 1 + sizeof(range) + elements * sizeof(this->gate_energy_[0]))) {
443 ESP_LOGW(TAG, "Energy frame too short: %d bytes", len);
444 return;
445 }
446 this->set_presence_(buffer[index]);
447 index++;
448 memcpy(&range, &buffer[index], sizeof(range));
449 index += sizeof(range);
450 this->set_distance_(range);
451 for (uint8_t i = 0; i < elements; i++) { // NOLINT
452 memcpy(&this->gate_energy_[i], &buffer[index], sizeof(this->gate_energy_[0]));
453 index += sizeof(this->gate_energy_[0]);
454 }
455
458 this->sample_number_counter++;
459 if (this->sample_number_counter >= CALIBRATE_SAMPLES) {
460 this->sample_number_counter = 0;
461 }
462 }
463
464 // Resonable refresh rate for home assistant database size health
465 const int32_t current_millis = App.get_loop_component_start_time();
466 if (current_millis - this->last_periodic_millis < REFRESH_RATE_MS) {
467 return;
468 }
469 this->last_periodic_millis = current_millis;
470#ifdef LD2420_LISTENER_COUNT
471 for (auto *listener : this->listeners_) {
472 listener->on_distance(this->get_distance_());
473 listener->on_presence(this->get_presence_());
474 }
475#endif
476
479 if (current_millis - this->report_periodic_millis > REFRESH_RATE_MS * CALIBRATE_REPORT_INTERVAL) {
480 this->report_periodic_millis = current_millis;
481 this->report_gate_data();
482 }
483 }
484}
485
486void LD2420Component::handle_simple_mode_(const uint8_t *inbuf, int len) {
487 const uint8_t bufsize = 16;
488 uint8_t index{0};
489 uint8_t pos{0};
490 char *endptr{nullptr};
491 char outbuf[bufsize]{0};
492 while (true) {
493 if (pos >= 2 && inbuf[pos - 2] == 'O' && inbuf[pos - 1] == 'F' && inbuf[pos] == 'F') {
494 this->set_presence_(false);
495 } else if (pos >= 1 && inbuf[pos - 1] == 'O' && inbuf[pos] == 'N') {
496 this->set_presence_(true);
497 }
498 if (inbuf[pos] >= '0' && inbuf[pos] <= '9') {
499 if (index < bufsize - 1) {
500 outbuf[index++] = inbuf[pos];
501 }
502 }
503 if (pos < len - 1) {
504 pos++;
505 } else {
506 break;
507 }
508 }
509 outbuf[index] = '\0';
510 if (index > 1) {
511 this->set_distance_(strtol(outbuf, &endptr, 10));
512 }
513
514 if (this->get_mode_() == CMD_SYSTEM_MODE_SIMPLE) {
515 // Resonable refresh rate for home assistant database size health
516 const int32_t current_millis = App.get_loop_component_start_time();
517 if (current_millis - this->last_normal_periodic_millis < REFRESH_RATE_MS) {
518 return;
519 }
520 this->last_normal_periodic_millis = current_millis;
521#ifdef LD2420_LISTENER_COUNT
522 // Every listener gets the distance before any presence callback can run an automation
523 for (auto *listener : this->listeners_) {
524 listener->on_distance(this->get_distance_());
525 }
526 for (auto *listener : this->listeners_) {
527 listener->on_presence(this->get_presence_());
528 }
529#endif
530 }
531}
532
533void LD2420Component::read_batch_(std::span<uint8_t, MAX_LINE_LENGTH> buffer) {
535 [this, &buffer](uint8_t b) { this->readline_(b, buffer.data(), buffer.size()); });
536}
537
539 this->cmd_reply_.command = buffer[CMD_FRAME_COMMAND];
540 this->cmd_reply_.length = buffer[CMD_FRAME_DATA_LENGTH];
541 uint16_t data_pos = 0;
542 if (this->cmd_reply_.length > CMD_MAX_BYTES) {
543 ESP_LOGW(TAG, "Reply frame too long");
544 return;
545 } else if (this->cmd_reply_.length < 2) {
546 ESP_LOGW(TAG, "Command frame too short");
547 return;
548 }
549 memcpy(&this->cmd_reply_.error, &buffer[CMD_ERROR_WORD], sizeof(this->cmd_reply_.error));
550 const char *result = this->cmd_reply_.error ? "failure" : "success";
551 if (this->cmd_reply_.error > 0) {
552 return;
553 };
554 this->cmd_reply_.ack = true;
555 switch ((uint16_t) this->cmd_reply_.command) {
556 case (CMD_ENABLE_CONF):
557 ESP_LOGV(TAG, "Set config enable: CMD = %2X %s", CMD_ENABLE_CONF, result);
558 break;
559 case (CMD_DISABLE_CONF):
560 ESP_LOGV(TAG, "Set config disable: CMD = %2X %s", CMD_DISABLE_CONF, result);
561 break;
562 case (CMD_READ_REGISTER): {
563 ESP_LOGV(TAG, "Read register: CMD = %2X %s", CMD_READ_REGISTER, result);
564 // TODO Read/Write register is not implemented yet, this will get flushed out to a proper header file
565 data_pos = 0x0A;
566 uint16_t reg_count = std::min<uint16_t>((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_REG_DATA_REPLY_SIZE,
567 sizeof(this->cmd_reply_.data) / sizeof(this->cmd_reply_.data[0]));
568 for (uint16_t i = 0; i < reg_count; i++) {
569 memcpy(&this->cmd_reply_.data[i], &buffer[data_pos + i * CMD_REG_DATA_REPLY_SIZE], CMD_REG_DATA_REPLY_SIZE);
570 }
571 break;
572 }
573 case (CMD_WRITE_REGISTER):
574 ESP_LOGV(TAG, "Write register: CMD = %2X %s", CMD_WRITE_REGISTER, result);
575 break;
576 case (CMD_WRITE_ABD_PARAM):
577 ESP_LOGV(TAG, "Write gate parameter(s): %2X %s", CMD_WRITE_ABD_PARAM, result);
578 break;
579 case (CMD_READ_ABD_PARAM): {
580 ESP_LOGV(TAG, "Read gate parameter(s): %2X %s", CMD_READ_ABD_PARAM, result);
581 data_pos = CMD_ABD_DATA_REPLY_START;
582 uint16_t abd_count = std::min<uint16_t>((buffer[CMD_FRAME_DATA_LENGTH] - 4) / CMD_ABD_DATA_REPLY_SIZE,
583 sizeof(this->cmd_reply_.data) / sizeof(this->cmd_reply_.data[0]));
584 for (uint16_t i = 0; i < abd_count; i++) {
585 memcpy(&this->cmd_reply_.data[i], &buffer[data_pos + i * CMD_ABD_DATA_REPLY_SIZE],
586 sizeof(this->cmd_reply_.data[i]));
587 }
588 break;
589 }
590 case (CMD_WRITE_SYS_PARAM):
591 ESP_LOGV(TAG, "Set system parameter(s): %2X %s", CMD_WRITE_SYS_PARAM, result);
592 break;
593 case (CMD_READ_VERSION): {
594 uint8_t ver_len = std::min<uint8_t>(buffer[10], sizeof(this->firmware_ver_) - 1);
595 memcpy(this->firmware_ver_, &buffer[12], ver_len);
596 this->firmware_ver_[ver_len] = '\0';
597 ESP_LOGV(TAG, "Firmware version: %s %s", this->firmware_ver_, result);
598 break;
599 }
600 default:
601 break;
602 }
603}
604
606 uint32_t start_millis = millis();
607 uint8_t error = 0;
608 uint8_t ack_buffer[MAX_LINE_LENGTH];
609 uint8_t cmd_buffer[MAX_LINE_LENGTH];
610 this->cmd_reply_.ack = false;
611 if (frame.command != CMD_RESTART) {
612 this->cmd_active_ = true;
613 } // Restart does not reply, thus no ack state required
614 uint8_t retry = 3;
615 while (retry) {
616 frame.length = 0;
617 uint16_t frame_data_bytes = frame.data_length + 2; // Always add two bytes for the cmd size
618
619 memcpy(&cmd_buffer[frame.length], &frame.header, sizeof(frame.header));
620 frame.length += sizeof(frame.header);
621
622 memcpy(&cmd_buffer[frame.length], &frame_data_bytes, sizeof(frame.data_length));
623 frame.length += sizeof(frame.data_length);
624
625 memcpy(&cmd_buffer[frame.length], &frame.command, sizeof(frame.command));
626 frame.length += sizeof(frame.command);
627
628 for (uint16_t index = 0; index < frame.data_length; index++) {
629 memcpy(&cmd_buffer[frame.length], &frame.data[index], sizeof(frame.data[index]));
630 frame.length += sizeof(frame.data[index]);
631 }
632
633 memcpy(cmd_buffer + frame.length, &frame.footer, sizeof(frame.footer));
634 frame.length += sizeof(frame.footer);
635 this->write_array(cmd_buffer, frame.length);
636
637 error = 0;
638 if (frame.command == CMD_RESTART) {
639 return 0; // restart does not reply exit now
640 }
641
642 while (!this->cmd_reply_.ack) {
643 while (this->available()) {
644 this->readline_(this->read(), ack_buffer, sizeof(ack_buffer));
645 }
647 // Wait on an Rx from the LD2420 for up to 3 1 second loops, otherwise it could trigger a WDT.
648 if ((millis() - start_millis) > 1000) {
649 start_millis = millis();
650 error = LD2420_ERROR_TIMEOUT;
651 retry--;
652 break;
653 }
654 }
655 if (this->cmd_reply_.ack) {
656 retry = 0;
657 }
658 if (this->cmd_reply_.error > 0) {
659 this->handle_cmd_error(this->cmd_reply_.error);
660 }
661 }
662 this->cmd_active_ = false; // Resend and give-up are done; let loop() read the UART again
663 return error;
664}
665
667 CmdFrameT cmd_frame;
668 cmd_frame.data_length = 0;
669 cmd_frame.header = CMD_FRAME_HEADER;
670 cmd_frame.command = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
671 if (enable) {
672 memcpy(&cmd_frame.data[0], &CMD_PROTOCOL_VER, sizeof(CMD_PROTOCOL_VER));
673 cmd_frame.data_length += sizeof(CMD_PROTOCOL_VER);
674 }
675 cmd_frame.footer = CMD_FRAME_FOOTER;
676 ESP_LOGV(TAG, "Sending set config %s command: %2X", enable ? LOG_STR_LITERAL("enable") : LOG_STR_LITERAL("disable"),
677 cmd_frame.command);
678 return this->send_cmd_from_array(cmd_frame);
679}
680
681// Sends a restart and set system running mode to normal
683
685 CmdFrameT cmd_frame;
686 cmd_frame.data_length = 0;
687 cmd_frame.header = CMD_FRAME_HEADER;
688 cmd_frame.command = CMD_RESTART;
689 cmd_frame.footer = CMD_FRAME_FOOTER;
690 ESP_LOGV(TAG, "Sending restart command: %2X", cmd_frame.command);
691 this->send_cmd_from_array(cmd_frame);
692}
693
695 CmdFrameT cmd_frame;
696 cmd_frame.data_length = 0;
697 cmd_frame.header = CMD_FRAME_HEADER;
698 cmd_frame.command = CMD_READ_REGISTER;
699 cmd_frame.data[1] = reg;
700 cmd_frame.data_length += 2;
701 cmd_frame.footer = CMD_FRAME_FOOTER;
702 ESP_LOGV(TAG, "Sending read register %4X command: %2X", reg, cmd_frame.command);
703 this->send_cmd_from_array(cmd_frame);
704}
705
706void LD2420Component::set_reg_value(uint16_t reg, uint16_t value) {
707 CmdFrameT cmd_frame;
708 cmd_frame.data_length = 0;
709 cmd_frame.header = CMD_FRAME_HEADER;
710 cmd_frame.command = CMD_WRITE_REGISTER;
711 memcpy(&cmd_frame.data[cmd_frame.data_length], &reg, CMD_REG_DATA_REPLY_SIZE);
712 cmd_frame.data_length += 2;
713 memcpy(&cmd_frame.data[cmd_frame.data_length], &value, CMD_REG_DATA_REPLY_SIZE);
714 cmd_frame.data_length += 2;
715 cmd_frame.footer = CMD_FRAME_FOOTER;
716 ESP_LOGV(TAG, "Sending write register %4X command: %2X data = %4X", reg, cmd_frame.command, value);
717 this->send_cmd_from_array(cmd_frame);
718}
719
721 if (error < ErrMessages::COUNT) {
722 ESP_LOGE(TAG, "Command failed: %s", LOG_STR_ARG(ErrMessages::get_log_str(error, ErrMessages::LAST_INDEX)));
723 } else {
724 // The error word comes from the device reply frame; unknown codes must not index the table
725 ESP_LOGE(TAG, "Command failed: error 0x%04X", error);
726 }
727}
728
730 uint8_t error;
731 CmdFrameT cmd_frame;
732 cmd_frame.data_length = 0;
733 cmd_frame.header = CMD_FRAME_HEADER;
734 cmd_frame.command = CMD_READ_ABD_PARAM;
735 const uint16_t move_reg = CMD_GATE_MOVE_THRESH_REG + gate;
736 const uint16_t still_reg = CMD_GATE_STILL_THRESH_REG + gate;
737 memcpy(&cmd_frame.data[cmd_frame.data_length], &move_reg, sizeof(move_reg));
738 cmd_frame.data_length += 2;
739 memcpy(&cmd_frame.data[cmd_frame.data_length], &still_reg, sizeof(still_reg));
740 cmd_frame.data_length += 2;
741 cmd_frame.footer = CMD_FRAME_FOOTER;
742 ESP_LOGV(TAG, "Sending read gate %d high/low threshold command: %2X", gate, cmd_frame.command);
743 error = this->send_cmd_from_array(cmd_frame);
744 if (error == 0) {
745 this->current_config.move_thresh[gate] = cmd_reply_.data[0];
747 }
748 return error;
749}
750
752 uint8_t error;
753 CmdFrameT cmd_frame;
754 cmd_frame.data_length = 0;
755 cmd_frame.header = CMD_FRAME_HEADER;
756 cmd_frame.command = CMD_READ_ABD_PARAM;
757 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_MIN_GATE_REG,
758 sizeof(CMD_MIN_GATE_REG)); // Register: global min detect gate number
759 cmd_frame.data_length += sizeof(CMD_MIN_GATE_REG);
760 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_MAX_GATE_REG,
761 sizeof(CMD_MAX_GATE_REG)); // Register: global max detect gate number
762 cmd_frame.data_length += sizeof(CMD_MAX_GATE_REG);
763 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_TIMEOUT_REG,
764 sizeof(CMD_TIMEOUT_REG)); // Register: global delay time
765 cmd_frame.data_length += sizeof(CMD_TIMEOUT_REG);
766 cmd_frame.footer = CMD_FRAME_FOOTER;
767 ESP_LOGV(TAG, "Sending read gate min max and timeout command: %2X", cmd_frame.command);
768 error = this->send_cmd_from_array(cmd_frame);
769 if (error == 0) {
770 this->current_config.min_gate = (uint16_t) cmd_reply_.data[0];
771 this->current_config.max_gate = (uint16_t) cmd_reply_.data[1];
772 this->current_config.timeout = (uint16_t) cmd_reply_.data[2];
773 }
774 return error;
775}
776
778 CmdFrameT cmd_frame;
779 uint16_t unknown_parm = 0x0000;
780 cmd_frame.data_length = 0;
781 cmd_frame.header = CMD_FRAME_HEADER;
782 cmd_frame.command = CMD_WRITE_SYS_PARAM;
783 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_SYSTEM_MODE, sizeof(CMD_SYSTEM_MODE));
784 cmd_frame.data_length += sizeof(CMD_SYSTEM_MODE);
785 memcpy(&cmd_frame.data[cmd_frame.data_length], &mode, sizeof(mode));
786 cmd_frame.data_length += sizeof(mode);
787 memcpy(&cmd_frame.data[cmd_frame.data_length], &unknown_parm, sizeof(unknown_parm));
788 cmd_frame.data_length += sizeof(unknown_parm);
789 cmd_frame.footer = CMD_FRAME_FOOTER;
790 ESP_LOGV(TAG, "Sending write system mode command: %2X", cmd_frame.command);
791 if (this->send_cmd_from_array(cmd_frame) == 0) {
792 this->set_mode_(mode);
793 }
794}
795
797 CmdFrameT cmd_frame;
798 cmd_frame.data_length = 0;
799 cmd_frame.header = CMD_FRAME_HEADER;
800 cmd_frame.command = CMD_READ_VERSION;
801 cmd_frame.footer = CMD_FRAME_FOOTER;
802
803 ESP_LOGV(TAG, "Sending read firmware version command: %2X", cmd_frame.command);
804 this->send_cmd_from_array(cmd_frame);
805}
806
807void LD2420Component::set_min_max_distances_timeout(uint32_t max_gate_distance, uint32_t min_gate_distance, // NOLINT
808 uint32_t timeout) {
809 // Header H, Length L, Register R, Value V, Footer F
810 // |Min Gate |Max Gate |Timeout |
811 // HH HH HH HH LL LL CC CC RR RR VV VV VV VV RR RR VV VV VV VV RR RR VV VV VV VV FF FF FF FF
812 // FD FC FB FA 14 00 07 00 00 00 01 00 00 00 01 00 09 00 00 00 04 00 0A 00 00 00 04 03 02 01 e.g.
813
814 CmdFrameT cmd_frame;
815 cmd_frame.data_length = 0;
816 cmd_frame.header = CMD_FRAME_HEADER;
817 cmd_frame.command = CMD_WRITE_ABD_PARAM;
818 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_MIN_GATE_REG,
819 sizeof(CMD_MIN_GATE_REG)); // Register: global min detect gate number
820 cmd_frame.data_length += sizeof(CMD_MIN_GATE_REG);
821 memcpy(&cmd_frame.data[cmd_frame.data_length], &min_gate_distance, sizeof(min_gate_distance));
822 cmd_frame.data_length += sizeof(min_gate_distance);
823 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_MAX_GATE_REG,
824 sizeof(CMD_MAX_GATE_REG)); // Register: global max detect gate number
825 cmd_frame.data_length += sizeof(CMD_MAX_GATE_REG);
826 memcpy(&cmd_frame.data[cmd_frame.data_length], &max_gate_distance, sizeof(max_gate_distance));
827 cmd_frame.data_length += sizeof(max_gate_distance);
828 memcpy(&cmd_frame.data[cmd_frame.data_length], &CMD_TIMEOUT_REG,
829 sizeof(CMD_TIMEOUT_REG)); // Register: global delay time
830 cmd_frame.data_length += sizeof(CMD_TIMEOUT_REG);
831 memcpy(&cmd_frame.data[cmd_frame.data_length], &timeout, sizeof(timeout));
832 ;
833 cmd_frame.data_length += sizeof(timeout);
834 cmd_frame.footer = CMD_FRAME_FOOTER;
835
836 ESP_LOGV(TAG, "Sending write gate min max and timeout command: %2X", cmd_frame.command);
837 this->send_cmd_from_array(cmd_frame);
838}
839
841 // Header H, Length L, Command C, Register R, Value V, Footer F
842 // HH HH HH HH LL LL CC CC RR RR VV VV VV VV RR RR VV VV VV VV FF FF FF FF
843 // FD FC FB FA 14 00 07 00 10 00 00 FF 00 00 00 01 00 0F 00 00 04 03 02 01
844
845 const uint16_t move_threshold_gate = CMD_GATE_MOVE_THRESH_REG + gate;
846 const uint16_t still_threshold_gate = CMD_GATE_STILL_THRESH_REG + gate;
847 CmdFrameT cmd_frame;
848 cmd_frame.data_length = 0;
849 cmd_frame.header = CMD_FRAME_HEADER;
850 cmd_frame.command = CMD_WRITE_ABD_PARAM;
851 memcpy(&cmd_frame.data[cmd_frame.data_length], &move_threshold_gate, sizeof(move_threshold_gate));
852 cmd_frame.data_length += sizeof(move_threshold_gate);
853 memcpy(&cmd_frame.data[cmd_frame.data_length], &this->new_config.move_thresh[gate],
854 sizeof(this->new_config.move_thresh[gate]));
855 cmd_frame.data_length += sizeof(this->new_config.move_thresh[gate]);
856 memcpy(&cmd_frame.data[cmd_frame.data_length], &still_threshold_gate, sizeof(still_threshold_gate));
857 cmd_frame.data_length += sizeof(still_threshold_gate);
858 memcpy(&cmd_frame.data[cmd_frame.data_length], &this->new_config.still_thresh[gate],
859 sizeof(this->new_config.still_thresh[gate]));
860 cmd_frame.data_length += sizeof(this->new_config.still_thresh[gate]);
861 cmd_frame.footer = CMD_FRAME_FOOTER;
862 ESP_LOGV(TAG, "Sending set gate %4X sensitivity command: %2X", gate, cmd_frame.command);
863 this->send_cmd_from_array(cmd_frame);
864}
865
866#ifdef USE_NUMBER
868 PUBLISH_STATE(this->gate_timeout_number_, static_cast<uint16_t>(this->current_config.timeout));
869 PUBLISH_STATE(this->gate_select_number_, 0);
870 PUBLISH_STATE(this->min_gate_distance_number_, static_cast<uint16_t>(this->current_config.min_gate));
871 PUBLISH_STATE(this->max_gate_distance_number_, static_cast<uint16_t>(this->current_config.max_gate));
872 PUBLISH_STATE(this->gate_move_sensitivity_factor_number_, this->gate_move_sensitivity_factor);
873 PUBLISH_STATE(this->gate_still_sensitivity_factor_number_, this->gate_still_sensitivity_factor);
874 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
875 PUBLISH_STATE(this->gate_still_threshold_numbers_[gate],
876 static_cast<uint16_t>(this->current_config.still_thresh[gate]));
877 PUBLISH_STATE(this->gate_move_threshold_numbers_[gate],
878 static_cast<uint16_t>(this->current_config.move_thresh[gate]));
879 }
880}
881
883 for (uint8_t gate = 0; gate < TOTAL_GATES; gate++) {
884 const bool move = this->gate_move_threshold_numbers_[gate] == n;
885 if (!move && this->gate_still_threshold_numbers_[gate] != n)
886 continue;
887 const uint8_t target = this->is_gate_select() ? this->get_gate_select_value() : gate;
888 (move ? this->new_config.move_thresh : this->new_config.still_thresh)[target] = value;
889 return;
890 }
891}
892
894 this->gate_timeout_number_->publish_state(this->new_config.timeout);
895 this->min_gate_distance_number_->publish_state(this->new_config.min_gate);
896 this->max_gate_distance_number_->publish_state(this->new_config.max_gate);
897}
898
899#endif
900
901} // namespace esphome::ld2420
BedjetMode mode
BedJet operating mode.
uint8_t checksum
Definition bl0906.h:3
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.
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void readline_(int rx_data, uint8_t *buffer, int len)
Definition ld2420.cpp:408
void set_system_mode(uint16_t mode)
Definition ld2420.cpp:777
void handle_ack_data_(uint8_t *buffer, int len)
Definition ld2420.cpp:538
uint8_t set_config_mode(bool enable)
Definition ld2420.cpp:666
void update_radar_data(uint16_t const *gate_energy, uint8_t sample_number)
Definition ld2420.cpp:308
void set_distance_(uint16_t distance)
Definition ld2420.h:162
void set_min_max_distances_timeout(uint32_t max_gate_distance, uint32_t min_gate_distance, uint32_t timeout)
Definition ld2420.cpp:807
void set_gate_threshold(uint8_t gate)
Definition ld2420.cpp:840
void change_operating_mode(OpMode mode)
Definition ld2420.cpp:363
void handle_energy_mode_(uint8_t *buffer, int len)
Definition ld2420.cpp:438
void set_calibration_(bool state)
Definition ld2420.h:168
void set_gate_threshold_value(number::Number *n, float value)
Definition ld2420.cpp:882
uint16_t gate_peak[TOTAL_GATES]
Definition ld2420.h:135
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
Definition ld2420.h:173
void set_mode_(uint16_t mode)
Definition ld2420.h:158
uint16_t gate_avg[TOTAL_GATES]
Definition ld2420.h:134
void read_batch_(std::span< uint8_t, MAX_LINE_LENGTH > buffer)
Definition ld2420.cpp:533
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2420.h:180
int get_gate_threshold_(uint8_t gate)
Definition ld2420.cpp:729
int send_cmd_from_array(CmdFrameT cmd_frame)
Definition ld2420.cpp:605
void get_reg_value_(uint16_t reg)
Definition ld2420.cpp:694
uint16_t gate_energy_[TOTAL_GATES]
Definition ld2420.h:178
void handle_simple_mode_(const uint8_t *inbuf, int len)
Definition ld2420.cpp:486
StaticVector< LD2420Listener *, LD2420_LISTENER_COUNT > listeners_
Definition ld2420.h:187
void set_operating_mode(const char *state)
Definition ld2420.cpp:356
uint16_t radar_data[TOTAL_GATES][CALIBRATE_SAMPLES]
Definition ld2420.h:133
void handle_cmd_error(uint16_t error)
Definition ld2420.cpp:720
void set_presence_(bool presence)
Definition ld2420.h:160
void set_reg_value(uint16_t reg, uint16_t value)
Definition ld2420.cpp:706
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
Definition ld2420.h:172
Base-class for all numbers.
Definition number.h:29
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
Range range
Definition msa3xx.h:0
ESPHOME_LOG_TAG(TAG, "ld2420.binary_sensor")
@ OP_CALIBRATE_MODE
Definition ld2420.h:36
PROGMEM_STRING_TABLE(OpModeStrings, "Normal", "Calibrate", "Simple")
constexpr uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init=0)
XOR the bytes of data with size len, starting from init.
Definition helpers.h:949
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
int16_t progmem_string_table_find(const char *value, size_t len)
The index of the string value (len bytes, in RAM) in a PROGMEM_STRING_TABLE, or -1 when it is not one...
Definition progmem.h:160
size_t size_t pos
Definition helpers.h:1333
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
Definition helpers.cpp:889
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t