ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
nextion.cpp
Go to the documentation of this file.
1#include "nextion.h"
2
3#include <cinttypes>
4
7#include "esphome/core/log.h"
9#include "esphome/core/util.h"
10
11namespace esphome::nextion {
12
13static const char *const TAG = "nextion";
14
15// Nextion command terminator: three consecutive 0xFF bytes (per Nextion Instruction Set v1.1).
16static constexpr uint8_t COMMAND_DELIMITER[3] = {0xFF, 0xFF, 0xFF};
17static constexpr size_t DELIMITER_SIZE = sizeof(COMMAND_DELIMITER);
18
19void Nextion::setup() {
20 this->is_setup_ = false;
21 this->connection_state_.ignore_is_setup_ = true;
22
23 // Wake up the nextion and ensure clean communication state
24 this->send_command_("sleep=0"); // Exit sleep mode if sleeping
25 this->send_command_("bkcmd=0"); // Disable return data during init sequence
26
27 // Reset device for clean state - critical for reliable communication
28 this->send_command_("rest");
29
30 this->connection_state_.ignore_is_setup_ = false;
31}
32
33bool Nextion::send_command_(const std::string &command) {
34 if (!this->connection_state_.ignore_is_setup_ && !this->is_setup()) {
35 return false;
36 }
37
38#ifdef USE_NEXTION_COMMAND_SPACING
40 if (!this->connection_state_.ignore_is_setup_ && !this->command_pacer_.can_send(now)) {
41 ESP_LOGN(TAG, "Command spacing: delaying '%s'", command.c_str());
42 return false;
43 }
44#endif // USE_NEXTION_COMMAND_SPACING
45
46 ESP_LOGN(TAG, "cmd: %s", command.c_str());
47
48 this->write_str(command.c_str());
49 const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
50 this->write_array(to_send, sizeof(to_send));
51
52#ifdef USE_NEXTION_COMMAND_SPACING
53 // Mark sent immediately after writing to UART. The pacer enforces inter-command
54 // spacing from the transmit side. Marking on ACK (0x01) would leave last_command_time_
55 // at zero indefinitely, making can_send() always return true and spacing a no-op.
56 // ignore_is_setup_ commands (setup/init sequence) bypass spacing intentionally.
57 if (!this->connection_state_.ignore_is_setup_) {
58 this->command_pacer_.mark_sent(now);
59 }
60#endif // USE_NEXTION_COMMAND_SPACING
61
62 return true;
63}
64
66 if (this->connection_state_.is_connected_)
67 return true;
68
69#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
70 ESP_LOGW(TAG, "Connected (no handshake)"); // Log the connection status without handshake
71 this->connection_state_.is_connected_ = true; // Set the connection status to true
72 return true; // Return true indicating the connection is set
73#else // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
74 if (this->comok_sent_ == 0) {
75 this->reset_(false);
76
77 this->connection_state_.ignore_is_setup_ = true;
78 this->send_command_("boguscommand=0"); // bogus command. needed sometimes after updating
79#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
80 this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN");
81#endif // USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
82 this->send_command_("connect");
83
85 this->connection_state_.ignore_is_setup_ = false;
86
87 return false;
88 }
89
90 if (App.get_loop_component_start_time() - this->comok_sent_ <= 500) // Wait 500 ms
91 return false;
92
93 std::string response;
94
95 this->recv_ret_string_(response, 0, false);
96 if (!response.empty() && response[0] == 0x1A) {
97 // Swallow invalid variable name responses that may be caused by the above commands
98 ESP_LOGV(TAG, "0x1A error ignored (setup)");
99 return false;
100 }
101 if (response.empty() || response.find("comok") == std::string::npos) {
102#ifdef NEXTION_PROTOCOL_LOG
103 ESP_LOGN(TAG, "Bad connect: %s", response.c_str());
104 for (size_t i = 0; i < response.length(); i++) {
105 ESP_LOGN(TAG, "resp: %s %d %d %c", response.c_str(), i, response[i], response[i]);
106 }
107#endif // NEXTION_PROTOCOL_LOG
108
109 ESP_LOGW(TAG, "Not connected");
110 this->comok_sent_ = 0;
111 return false;
112 }
113
114 this->connection_state_.ignore_is_setup_ = true;
115 ESP_LOGI(TAG, "Connected");
116 this->connection_state_.is_connected_ = true;
117
118 ESP_LOGN(TAG, "connect: %s", response.c_str());
119
120 size_t start;
121 size_t end = 0;
122 std::vector<std::string> connect_info;
123 while ((start = response.find_first_not_of(',', end)) != std::string::npos) {
124 end = response.find(',', start);
125 connect_info.push_back(response.substr(start, end - start));
126 }
127
128 this->is_detected_ = (connect_info.size() == 7);
129 if (this->is_detected_) {
130 ESP_LOGN(TAG, "Connect info: %zu", connect_info.size());
131#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
132 this->device_model_ = connect_info[2];
133 this->firmware_version_ = connect_info[3];
134 this->serial_number_ = connect_info[5];
135 this->flash_size_ = connect_info[6];
136#else // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
137 ESP_LOGI(TAG,
138 " Device Model: %s\n"
139 " FW Version: %s\n"
140 " Serial Number: %s\n"
141 " Flash Size: %s\n",
142 connect_info[2].c_str(), connect_info[3].c_str(), connect_info[5].c_str(), connect_info[6].c_str());
143#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
144 } else {
145 ESP_LOGE(TAG, "Bad connect value: '%s'", response.c_str());
146 }
147
148 this->connection_state_.ignore_is_setup_ = false;
149 this->dump_config();
150 return true;
151#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
152}
153
154void Nextion::reset_(bool reset_nextion) {
155 uint8_t d;
156
157 while (this->available()) { // Clear receive buffer
158 this->read_byte(&d);
159 }
160 for (auto *entry : this->nextion_queue_) {
161 if (entry->component != nullptr && entry->component->get_queue_type() == NextionQueueType::NO_RESULT) {
162 delete entry->component; // NOLINT(cppcoreguidelines-owning-memory)
163 }
164 delete entry; // NOLINT(cppcoreguidelines-owning-memory)
165 }
166 this->nextion_queue_.clear();
167#ifdef USE_NEXTION_WAVEFORM
168 for (auto *entry : this->waveform_queue_) {
169 delete entry; // NOLINT(cppcoreguidelines-owning-memory)
170 }
171 this->waveform_queue_.clear();
172#endif // USE_NEXTION_WAVEFORM
173}
174
176 ESP_LOGCONFIG(TAG, "Nextion:");
177
178#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
179 ESP_LOGCONFIG(TAG, " Skip handshake: YES");
180#else // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
181#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
182 ESP_LOGCONFIG(TAG,
183 " Device Model: %s\n"
184 " FW Version: %s\n"
185 " Serial Number: %s\n"
186 " Flash Size: %s\n"
187 " Max queue age: %u ms\n"
188 " Startup override: %u ms\n",
189 this->device_model_.c_str(), this->firmware_version_.c_str(), this->serial_number_.c_str(),
190 this->flash_size_.c_str(), this->max_q_age_ms_, this->startup_override_ms_);
191#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
192#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
193 ESP_LOGCONFIG(TAG, " Exit reparse: YES\n");
194#endif // USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
195 ESP_LOGCONFIG(TAG,
196 " Wake On Touch: %s\n"
197 " Touch Timeout: %" PRIu16,
198 YESNO(this->connection_state_.auto_wake_on_touch_), this->touch_sleep_timeout_);
199#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
200
201#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
202 ESP_LOGCONFIG(TAG, " Max commands per loop: %u", this->max_commands_per_loop_);
203#endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP
204
205 if (this->wake_up_page_ != 255) {
206 ESP_LOGCONFIG(TAG, " Wake Up Page: %u", this->wake_up_page_);
207 }
208
209#ifdef USE_NEXTION_CONF_START_UP_PAGE
210 if (this->start_up_page_ != 255) {
211 ESP_LOGCONFIG(TAG, " Start Up Page: %u", this->start_up_page_);
212 }
213#endif // USE_NEXTION_CONF_START_UP_PAGE
214
215#ifdef USE_NEXTION_COMMAND_SPACING
216 ESP_LOGCONFIG(TAG, " Cmd spacing: %u ms", this->command_pacer_.get_spacing());
217#endif // USE_NEXTION_COMMAND_SPACING
218
219#ifdef USE_NEXTION_MAX_QUEUE_SIZE
220 ESP_LOGCONFIG(TAG, " Max queue size: %zu", this->max_queue_size_);
221#endif
222#ifdef USE_NEXTION_TFT_UPLOAD
223 ESP_LOGCONFIG(TAG,
224 " TFT URL: %s\n"
225 " TFT upload HTTP timeout: %" PRIu16 "ms\n"
226 " TFT upload HTTP retries: %u",
227 this->tft_url_.c_str(), this->tft_upload_http_timeout_, this->tft_upload_http_retries_);
228#ifdef USE_ESP32
229 if (this->tft_upload_watchdog_timeout_ > 0) {
230 ESP_LOGCONFIG(TAG, " TFT upload WDT timeout: %" PRIu32 "ms", this->tft_upload_watchdog_timeout_);
231 }
232#endif // USE_ESP32
233#endif // USE_NEXTION_TFT_UPLOAD
234}
235
236void Nextion::update() {
237 if (!this->is_setup()) {
238 return;
239 }
240 if (this->writer_.has_value()) {
241 (*this->writer_)(*this);
242 }
243}
244
246 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
247 return;
248
249 for (auto *binarysensortype : this->binarysensortype_) {
250 binarysensortype->update_component();
251 }
252 for (auto *sensortype : this->sensortype_) {
253 sensortype->update_component();
254 }
255 for (auto *switchtype : this->switchtype_) {
256 switchtype->update_component();
257 }
258 for (auto *textsensortype : this->textsensortype_) {
259 textsensortype->update_component();
260 }
261}
262
263bool Nextion::send_command(const char *command) {
264 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
265 return false;
266
267 this->add_no_result_to_queue_with_command_("command", command);
268 return true;
269}
270
271bool Nextion::send_command_printf(const char *format, ...) {
272 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
273 return false;
274
275 char buffer[256];
276 va_list arg;
277 va_start(arg, format);
278 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
279 va_end(arg);
280 if (ret <= 0) {
281 ESP_LOGW(TAG, "Bad cmd format: '%s'", format);
282 return false;
283 }
284
285 this->add_no_result_to_queue_with_command_("command_printf", buffer);
286 return true;
287}
288
289#ifdef NEXTION_PROTOCOL_LOG
291 ESP_LOGN(TAG, "print_queue_members_ (top 10) size %zu", this->nextion_queue_.size());
292 ESP_LOGN(TAG, "*******************************************");
293 int count = 0;
294 for (auto *i : this->nextion_queue_) {
295 if (count++ == 10)
296 break;
297
298 if (i == nullptr) {
299 ESP_LOGN(TAG, "Queue null");
300 } else {
301 ESP_LOGN(TAG, "Queue type: %d:%s, name: %s", i->component->get_queue_type(),
302 i->component->get_queue_type_string(), i->component->get_variable_name().c_str());
303 }
304 }
305 ESP_LOGN(TAG, "*******************************************");
306}
307#endif
308
309void Nextion::loop() {
310 if (!this->check_connect_() || this->connection_state_.is_updating_)
311 return;
312
313 if (this->connection_state_.nextion_reports_is_setup_ && !this->connection_state_.sent_setup_commands_) {
314 this->connection_state_.ignore_is_setup_ = true;
315 this->connection_state_.sent_setup_commands_ = true;
316 this->send_command_("bkcmd=3"); // Always, returns 0x00 to 0x23 result of serial command.
317
318 if (this->brightness_.has_value()) {
319 this->set_backlight_brightness(this->brightness_.value());
320 }
321
322#ifdef USE_NEXTION_CONF_START_UP_PAGE
323 // Check if a startup page has been set and send the command
324 if (this->start_up_page_ != 255) {
325 this->goto_page(this->start_up_page_);
326 }
327#endif // USE_NEXTION_CONF_START_UP_PAGE
328
329 if (this->wake_up_page_ != 255) {
330 this->set_wake_up_page(this->wake_up_page_);
331 }
332
333 if (this->touch_sleep_timeout_ != 0) {
335 }
336
337 this->set_auto_wake_on_touch(this->connection_state_.auto_wake_on_touch_);
338
339 this->connection_state_.ignore_is_setup_ = false;
340 }
341
342 this->process_serial_(); // Receive serial data
343 this->process_nextion_commands_(); // Process nextion return commands
344
345 if (!this->connection_state_.nextion_reports_is_setup_) {
346 if (this->started_ms_ == 0)
348
349 if (this->startup_override_ms_ > 0 &&
350 App.get_loop_component_start_time() - this->started_ms_ > this->startup_override_ms_) {
351 ESP_LOGV(TAG, "Manual ready set");
352 this->connection_state_.nextion_reports_is_setup_ = true;
353 }
354 }
355
356#ifdef USE_NEXTION_COMMAND_SPACING
358#ifdef USE_NEXTION_WAVEFORM
359 if (!this->waveform_queue_.empty()) {
361 }
362#endif // USE_NEXTION_WAVEFORM
363#endif // USE_NEXTION_COMMAND_SPACING
364}
365
366#ifdef USE_NEXTION_COMMAND_SPACING
368#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
369 size_t commands_sent = 0;
370#endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP
371
372 for (auto *item : this->nextion_queue_) {
373 if (item == nullptr || item->pending_command.empty()) {
374 continue; // Already sent, waiting for ACK — skip, don't stop
375 }
376
377#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
378 if (++commands_sent > this->max_commands_per_loop_) {
379 ESP_LOGV(TAG, "Pending cmds: loop limit reached, deferring");
380 break;
381 }
382#endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP
383
385 if (!this->command_pacer_.can_send(now)) {
386 break; // Spacing not elapsed, stop for this loop iteration
387 }
388
389 if (!this->send_command_(item->pending_command)) {
390 break; // Unexpected send failure, stop
391 }
392 item->pending_command.clear();
393 ESP_LOGVV(TAG, "Pending cmd sent: %s", item->component->get_variable_name().c_str());
394 }
395}
396#endif // USE_NEXTION_COMMAND_SPACING
397
398bool Nextion::remove_from_q_(bool report_empty) {
399 if (this->nextion_queue_.empty()) {
400 if (report_empty) {
401 ESP_LOGE(TAG, "Queue empty");
402 }
403 return false;
404 }
405
406 NextionQueue *nb = this->nextion_queue_.front();
407 if (!nb || !nb->component) {
408 ESP_LOGE(TAG, "Invalid queue");
409 this->nextion_queue_.pop_front();
410 return false;
411 }
412 NextionComponentBase *component = nb->component;
413
414 ESP_LOGN(TAG, "Removed: %s", component->get_variable_name().c_str());
415
416 if (component->get_queue_type() == NextionQueueType::NO_RESULT) {
417 if (component->get_variable_name() == "sleep_wake") {
418 this->is_sleeping_ = false;
419 }
420 delete component; // NOLINT(cppcoreguidelines-owning-memory)
421 }
422 delete nb; // NOLINT(cppcoreguidelines-owning-memory)
423 this->nextion_queue_.pop_front();
424 return true;
425}
426
428 // Read all available bytes in batches to reduce UART call overhead.
429 size_t avail = this->available();
430 uint8_t buf[64];
431 while (avail > 0) {
432 size_t to_read = std::min(avail, sizeof(buf));
433 if (!this->read_array(buf, to_read)) {
434 break;
435 }
436 avail -= to_read;
437
438 this->command_data_.append(reinterpret_cast<const char *>(buf), to_read);
439 }
440}
441// nextion.tech/instruction-set/
443 if (this->command_data_.empty()) {
444 return;
445 }
446
447#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
448 size_t commands_processed = 0;
449#endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP
450
451 size_t to_process_length = 0;
452 std::string to_process;
453
454 ESP_LOGN(TAG, "command_data_ %s len %d", this->command_data_.c_str(), this->command_data_.length());
455#ifdef NEXTION_PROTOCOL_LOG
456 this->print_queue_members_();
457#endif
458 while ((to_process_length = this->command_data_.find(reinterpret_cast<const char *>(COMMAND_DELIMITER), 0,
459 DELIMITER_SIZE)) != std::string::npos) {
460#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
461 if (++commands_processed > this->max_commands_per_loop_) {
462 ESP_LOGV(TAG, "Command limit reached, deferring");
463 break;
464 }
465#endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP
466 ESP_LOGN(TAG, "queue size: %zu", this->nextion_queue_.size());
467 while (to_process_length + DELIMITER_SIZE < this->command_data_.length() &&
468 static_cast<uint8_t>(this->command_data_[to_process_length + DELIMITER_SIZE]) == 0xFF) {
469 ++to_process_length;
470 ESP_LOGN(TAG, "Add 0xFF");
471 }
472
473 const uint8_t nextion_event = this->command_data_[0];
474
475 to_process_length -= 1;
476 to_process = this->command_data_.substr(1, to_process_length);
477
478 switch (nextion_event) {
479 case 0x00: // instruction sent by user has failed
480 ESP_LOGW(TAG, "Invalid instruction");
481 this->remove_from_q_();
482
483 break;
484 case 0x01: // instruction sent by user was successful
485
486 ESP_LOGVV(TAG, "Cmd OK");
487 ESP_LOGN(TAG, "this->nextion_queue_.empty() %s", YESNO(this->nextion_queue_.empty()));
488
489 this->remove_from_q_();
490 if (!this->is_setup_) {
491 if (this->nextion_queue_.empty()) {
492 this->is_setup_ = true;
493 this->setup_callback_.call();
494 }
495 }
496 break;
497 case 0x02: // invalid Component ID or name was used
498 ESP_LOGW(TAG, "Invalid component ID/name");
499 this->remove_from_q_();
500 break;
501 case 0x03: // invalid Page ID or name was used
502 ESP_LOGW(TAG, "Invalid page ID");
503 this->remove_from_q_();
504 break;
505 case 0x04: // invalid Picture ID was used
506 ESP_LOGW(TAG, "Invalid picture ID");
507 this->remove_from_q_();
508 break;
509 case 0x05: // invalid Font ID was used
510 ESP_LOGW(TAG, "Invalid font ID");
511 this->remove_from_q_();
512 break;
513 case 0x06: // File operation fails
514 ESP_LOGW(TAG, "File operation failed");
515 break;
516 case 0x09: // Instructions with CRC validation fails their CRC check
517 ESP_LOGW(TAG, "CRC validation failed");
518 break;
519 case 0x11: // invalid Baud rate was used
520 ESP_LOGW(TAG, "Invalid baud rate");
521 break;
522 case 0x12: // invalid Waveform ID or Channel # was used
523#ifdef USE_NEXTION_WAVEFORM
524 if (this->waveform_queue_.empty()) {
525 ESP_LOGW(TAG, "Waveform ID/ch used but no sensor queued");
526 } else {
527 auto &nb = this->waveform_queue_.front();
528 NextionComponentBase *component = nb->component;
529 ESP_LOGW(TAG, "Invalid waveform ID %d/ch %d", component->get_component_id(),
530 component->get_wave_channel_id());
531 ESP_LOGN(TAG, "Remove waveform ID %d/ch %d", component->get_component_id(), component->get_wave_channel_id());
532 delete nb; // NOLINT(cppcoreguidelines-owning-memory)
533 this->waveform_queue_.pop();
534 }
535#else // USE_NEXTION_WAVEFORM
536 ESP_LOGW(TAG, "Waveform ID/ch error but waveform not enabled");
537#endif // USE_NEXTION_WAVEFORM
538 break;
539 case 0x1A: // variable name invalid
540 ESP_LOGW(TAG, "Invalid variable name");
541 this->remove_from_q_();
542 break;
543 case 0x1B: // variable operation invalid
544 ESP_LOGW(TAG, "Invalid variable operation");
545 this->remove_from_q_();
546 break;
547 case 0x1C: // failed to assign
548 ESP_LOGW(TAG, "Variable assign failed");
549 this->remove_from_q_();
550 break;
551 case 0x1D: // operate EEPROM failed
552 ESP_LOGW(TAG, "EEPROM operation failed");
553 break;
554 case 0x1E: // parameter quantity invalid
555 ESP_LOGW(TAG, "Invalid parameter count");
556 this->remove_from_q_();
557 break;
558 case 0x1F: // IO operation failed
559 ESP_LOGW(TAG, "Invalid component I/O");
560 break;
561 case 0x20: // undefined escape characters
562 ESP_LOGW(TAG, "Undefined escape chars");
563 this->remove_from_q_();
564 break;
565 case 0x23: // too long variable name
566 ESP_LOGW(TAG, "Variable name too long");
567 this->remove_from_q_();
568 break;
569 case 0x24: // Serial Buffer overflow occurs
570 // Buffer will continue to receive the current instruction, all previous instructions are lost.
571 ESP_LOGE(TAG, "Serial buffer overflow");
572 this->buffer_overflow_callback_.call();
573 break;
574 case 0x65: { // touch event return data
575 if (to_process_length != 3) {
576 ESP_LOGW(TAG, "Incorrect touch len: %zu (need 3)", to_process_length);
577 break;
578 }
579
580 uint8_t page_id = to_process[0];
581 uint8_t component_id = to_process[1];
582 uint8_t touch_event = to_process[2]; // 0 -> release, 1 -> press
583 ESP_LOGV(TAG, "Touch %s: page %u comp %u", touch_event ? "PRESS" : "RELEASE", page_id, component_id);
584 for (auto *touch : this->touch_) {
585 touch->process_touch(page_id, component_id, touch_event != 0);
586 }
587 this->touch_callback_.call(page_id, component_id, touch_event != 0);
588 break;
589 }
590 case 0x66: { // Nextion initiated new page event return data.
591 // Also is used for sendme command which we never explicitly initiate
592 if (to_process_length != 1) {
593 ESP_LOGW(TAG, "Page event: expect 1, got %zu", to_process_length);
594 break;
595 }
596
597 uint8_t page_id = to_process[0];
598 ESP_LOGV(TAG, "New page: %u", page_id);
599 this->page_callback_.call(page_id);
600 break;
601 }
602 case 0x67: { // Touch Coordinate (awake)
603 break;
604 }
605 case 0x68: { // touch coordinate data (sleep)
606
607 if (to_process_length != 5) {
608 ESP_LOGW(TAG, "Touch coordinate: expect 5, got %zu", to_process_length);
609 ESP_LOGW(TAG, "%s", to_process.c_str());
610 break;
611 }
612
613 const uint16_t x = (uint16_t(to_process[0]) << 8) | to_process[1];
614 const uint16_t y = (uint16_t(to_process[2]) << 8) | to_process[3];
615 const uint8_t touch_event = to_process[4]; // 0 -> release, 1 -> press
616 ESP_LOGV(TAG, "Touch %s at %u,%u", touch_event ? "PRESS" : "RELEASE", x, y);
617 break;
618 }
619
620 // 0x70 0x61 0x62 0x31 0x32 0x33 0xFF 0xFF 0xFF
621 // Returned when using get command for a string.
622 // Each byte is converted to char.
623 // data: ab123
624 case 0x70: // string variable data return
625 {
626 if (this->nextion_queue_.empty()) {
627 ESP_LOGW(TAG, "String return but queue is empty");
628 break;
629 }
630
631 NextionQueue *nb = this->nextion_queue_.front();
632 if (!nb || !nb->component) {
633 ESP_LOGE(TAG, "Invalid queue entry");
634 this->nextion_queue_.pop_front();
635 return;
636 }
637 NextionComponentBase *component = nb->component;
638
639 if (component->get_queue_type() != NextionQueueType::TEXT_SENSOR) {
640 ESP_LOGE(TAG, "String return but '%s' not text sensor", component->get_variable_name().c_str());
641 } else {
642 ESP_LOGN(TAG, "String resp: '%s' id: %s type: %s", to_process.c_str(), component->get_variable_name().c_str(),
643 component->get_queue_type_string());
644 }
645
646 delete nb; // NOLINT(cppcoreguidelines-owning-memory)
647 this->nextion_queue_.pop_front();
648
649 break;
650 }
651 // 0x71 0x01 0x02 0x03 0x04 0xFF 0xFF 0xFF
652 // Returned when get command to return a number
653 // 4 byte 32-bit value in little endian order.
654 // (0x01+0x02*256+0x03*65536+0x04*16777216)
655 // data: 67305985
656 case 0x71: // numeric variable data return
657 {
658 if (this->nextion_queue_.empty()) {
659 ESP_LOGE(TAG, "Numeric return but queue empty");
660 break;
661 }
662
663 if (to_process_length < 4) {
664 ESP_LOGE(TAG, "Numeric return but insufficient data (need 4, got %zu)", to_process_length);
665 break;
666 }
667
668 int value = static_cast<int>(encode_uint32(to_process[3], to_process[2], to_process[1], to_process[0]));
669
670 NextionQueue *nb = this->nextion_queue_.front();
671 if (!nb || !nb->component) {
672 ESP_LOGE(TAG, "Invalid queue");
673 this->nextion_queue_.pop_front();
674 return;
675 }
676 NextionComponentBase *component = nb->component;
677
678 if (component->get_queue_type() != NextionQueueType::SENSOR &&
679 component->get_queue_type() != NextionQueueType::BINARY_SENSOR &&
680 component->get_queue_type() != NextionQueueType::SWITCH) {
681 ESP_LOGE(TAG, "Numeric return but '%s' invalid type %d", component->get_variable_name().c_str(),
682 component->get_queue_type());
683 } else {
684 ESP_LOGN(TAG, "Numeric: %s type %d:%s val %d", component->get_variable_name().c_str(),
685 component->get_queue_type(), component->get_queue_type_string(), value);
686 component->set_state_from_int(value, true, false);
687 }
688
689 delete nb; // NOLINT(cppcoreguidelines-owning-memory)
690 this->nextion_queue_.pop_front();
691
692 break;
693 }
694
695 case 0x86: { // device automatically enters into sleep mode
696 ESP_LOGVV(TAG, "Auto sleep");
697 this->is_sleeping_ = true;
698 this->sleep_callback_.call();
699 break;
700 }
701 case 0x87: // device automatically wakes up
702 {
703 ESP_LOGVV(TAG, "Auto wake");
704 this->is_sleeping_ = false;
705 this->wake_callback_.call();
706 this->all_components_send_state_(false);
707 break;
708 }
709 case 0x88: // system successful start up
710 {
711 ESP_LOGV(TAG, "System start: %zu", to_process_length);
712 this->connection_state_.nextion_reports_is_setup_ = true;
713 break;
714 }
715 case 0x89: { // start SD card upgrade
716 break;
717 }
718 // Data from nextion is
719 // 0x90 - Start
720 // variable length of 0x70 return formatted data (bytes) that contain the variable name: prints "temp1",0
721 // 00 - NULL
722 // 00/01 - Single byte for on/off
723 // FF FF FF - End
724 case 0x90: { // Switched component
725 std::string variable_name;
726
727 // Get variable name
728 auto index = to_process.find('\0');
729 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
730 ESP_LOGE(TAG, "Bad switch data (0x90)");
731 ESP_LOGN(TAG, "proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
732 break;
733 }
734
735 variable_name = to_process.substr(0, index);
736 ++index;
737
738 ESP_LOGN(TAG, "Switch %s: %s", ONOFF(to_process[index] != 0), variable_name.c_str());
739
740#ifdef USE_NEXTION_TRIGGER_CUSTOM_SWITCH
741 this->custom_switch_callback_.call(StringRef(variable_name), to_process[index] != 0);
742#endif // USE_NEXTION_TRIGGER_CUSTOM_SWITCH
743
744 for (auto *switchtype : this->switchtype_) {
745 switchtype->process_bool(variable_name, to_process[index] != 0);
746 }
747 break;
748 }
749 // Data from nextion is
750 // 0x91 - Start
751 // variable length of 0x70 return formatted data (bytes) that contain the variable name: prints "temp1",0
752 // 00 - NULL
753 // variable length of 0x71 return data: prints temp1.val,0
754 // FF FF FF - End
755 case 0x91: { // Sensor component
756 std::string variable_name;
757
758 auto index = to_process.find('\0');
759 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
760 ESP_LOGE(TAG, "Bad sensor data (0x91)");
761 ESP_LOGN(TAG, "proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
762 break;
763 }
764
765 index = to_process.find('\0');
766 variable_name = to_process.substr(0, index);
767 // // Get variable name
768 int value = static_cast<int>(
769 encode_uint32(to_process[index + 4], to_process[index + 3], to_process[index + 2], to_process[index + 1]));
770
771 ESP_LOGN(TAG, "Sensor: %s=%d", variable_name.c_str(), value);
772
773#ifdef USE_NEXTION_TRIGGER_CUSTOM_SENSOR
774 this->custom_sensor_callback_.call(StringRef(variable_name), value);
775#endif // USE_NEXTION_TRIGGER_CUSTOM_SENSOR
776
777 for (auto *sensor : this->sensortype_) {
778 sensor->process_sensor(variable_name, value);
779 }
780 break;
781 }
782
783 // Data from nextion is
784 // 0x92 - Start
785 // variable length of 0x70 return formatted data (bytes) that contain the variable name: prints "temp1",0
786 // 00 - NULL
787 // variable length of 0x70 return formatted data (bytes) that contain the text prints temp1.txt,0
788 // 00 - NULL
789 // FF FF FF - End
790 case 0x92: { // Text Sensor Component
791 std::string variable_name;
792 std::string text_value;
793
794 // Get variable name
795 auto index = to_process.find('\0');
796 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
797 ESP_LOGE(TAG, "Bad text data (0x92)");
798 ESP_LOGN(TAG, "proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
799 break;
800 }
801
802 variable_name = to_process.substr(0, index);
803 ++index;
804
805 // Get variable value without terminating NUL byte. Length check above ensures substr len >= 0.
806 text_value = to_process.substr(index, to_process_length - index - 1);
807
808 ESP_LOGN(TAG, "Text sensor: %s='%s'", variable_name.c_str(), text_value.c_str());
809
810 // NextionTextSensorResponseQueue *nq = new NextionTextSensorResponseQueue;
811 // nq->variable_name = variable_name;
812 // nq->state = text_value;
813 // this->textsensorq_.push_back(nq);
814
815#ifdef USE_NEXTION_TRIGGER_CUSTOM_TEXT_SENSOR
816 this->custom_text_sensor_callback_.call(StringRef(variable_name), StringRef(text_value));
817#endif // USE_NEXTION_TRIGGER_CUSTOM_TEXT_SENSOR
818
819 for (auto *textsensortype : this->textsensortype_) {
820 textsensortype->process_text(variable_name, text_value);
821 }
822 break;
823 }
824 // Data from nextion is
825 // 0x93 - Start
826 // variable length of 0x70 return formatted data (bytes) that contain the variable name: prints "temp1",0
827 // 00 - NULL
828 // 00/01 - Single byte for on/off
829 // FF FF FF - End
830 case 0x93: { // Binary Sensor component
831 std::string variable_name;
832
833 // Get variable name
834 auto index = to_process.find('\0');
835 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
836 ESP_LOGE(TAG, "Bad binary data (0x93)");
837 ESP_LOGN(TAG, "proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
838 break;
839 }
840
841 variable_name = to_process.substr(0, index);
842 ++index;
843
844 ESP_LOGN(TAG, "Binary sensor: %s=%s", variable_name.c_str(), ONOFF(to_process[index] != 0));
845
846#ifdef USE_NEXTION_TRIGGER_CUSTOM_BINARY_SENSOR
847 this->custom_binary_sensor_callback_.call(StringRef(variable_name), to_process[index] != 0);
848#endif // USE_NEXTION_TRIGGER_CUSTOM_BINARY_SENSOR
849
850 for (auto *binarysensortype : this->binarysensortype_) {
851 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
852 }
853 break;
854 }
855 case 0xFD: { // data transparent transmit finished
856 ESP_LOGVV(TAG, "Data transmit done");
857#ifdef USE_NEXTION_WAVEFORM
859#endif // USE_NEXTION_WAVEFORM
860 break;
861 }
862 case 0xFE: { // data transparent transmit ready
863 ESP_LOGVV(TAG, "Ready for transmit");
864#ifdef USE_NEXTION_WAVEFORM
865 if (this->waveform_queue_.empty()) {
866 ESP_LOGE(TAG, "No waveforms queued");
867 break;
868 }
869 auto &nb = this->waveform_queue_.front();
870 auto *component = nb->component;
871 size_t buffer_to_send = component->get_wave_buffer_size() < 255 ? component->get_wave_buffer_size() : 255;
872 this->write_array(component->get_wave_buffer().data(), static_cast<int>(buffer_to_send));
873 ESP_LOGN(TAG, "Send waveform: component id %d, waveform id %d, size %zu", component->get_component_id(),
874 component->get_wave_channel_id(), buffer_to_send);
875 component->clear_wave_buffer(buffer_to_send);
876 delete nb; // NOLINT(cppcoreguidelines-owning-memory)
877 this->waveform_queue_.pop();
878#else // USE_NEXTION_WAVEFORM
879 ESP_LOGW(TAG, "Waveform transmit ready but waveform not enabled");
880#endif // USE_NEXTION_WAVEFORM
881 break;
882 }
883 default:
884 ESP_LOGW(TAG, "Unknown event: 0x%02X", nextion_event);
885 break;
886 }
887
888 this->command_data_.erase(0, to_process_length + DELIMITER_SIZE + 1);
889 }
890
892
893 if (this->max_q_age_ms_ > 0 && !this->nextion_queue_.empty() &&
894 ms - this->nextion_queue_.front()->queue_time > this->max_q_age_ms_) {
895 for (auto it = this->nextion_queue_.begin(); it != this->nextion_queue_.end();) {
896 if (ms - (*it)->queue_time > this->max_q_age_ms_) {
897 NextionComponentBase *component = (*it)->component;
898 ESP_LOGV(TAG, "Remove old queue '%s':'%s'", component->get_queue_type_string(),
899 component->get_variable_name().c_str());
900
901 if (component->get_queue_type() == NextionQueueType::NO_RESULT) {
902 if (component->get_variable_name() == "sleep_wake") {
903 this->is_sleeping_ = false;
904 }
905 delete component; // NOLINT(cppcoreguidelines-owning-memory)
906 }
907
908 delete *it; // NOLINT(cppcoreguidelines-owning-memory)
909 it = this->nextion_queue_.erase(it);
910
911 } else {
912 break;
913 }
914 }
915 }
916 ESP_LOGN(TAG, "Loop end");
917 // App.feed_wdt(); Remove before master merge
918 this->process_serial_();
919} // Nextion::process_nextion_commands_()
920
921void Nextion::set_nextion_sensor_state(int queue_type, const std::string &name, float state) {
922 this->set_nextion_sensor_state(static_cast<NextionQueueType>(queue_type), name, state);
923}
924
925void Nextion::set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state) {
926 ESP_LOGN(TAG, "State: %s=%lf (type %d)", name.c_str(), state, queue_type);
927
928 switch (queue_type) {
930 for (auto *sensor : this->sensortype_) {
931 if (name == sensor->get_variable_name()) {
932 sensor->set_state(state, true, true);
933 break;
934 }
935 }
936 break;
937 }
939 for (auto *sensor : this->binarysensortype_) {
940 if (name == sensor->get_variable_name()) {
941 sensor->set_state(state != 0, true, true);
942 break;
943 }
944 }
945 break;
946 }
948 for (auto *sensor : this->switchtype_) {
949 if (name == sensor->get_variable_name()) {
950 sensor->set_state(state != 0, true, true);
951 break;
952 }
953 }
954 break;
955 }
956 default: {
957 ESP_LOGW(TAG, "set_sensor_state: bad type %d", queue_type);
958 }
959 }
960}
961
962void Nextion::set_nextion_text_state(const std::string &name, const std::string &state) {
963 ESP_LOGV(TAG, "State: %s='%s'", name.c_str(), state.c_str());
964
965 for (auto *sensor : this->textsensortype_) {
966 if (name == sensor->get_variable_name()) {
967 sensor->set_state(state, true, true);
968 break;
969 }
970 }
971}
972
973void Nextion::all_components_send_state_(bool force_update) {
974 ESP_LOGV(TAG, "Send states");
975 for (auto *binarysensortype : this->binarysensortype_) {
976 if (force_update || binarysensortype->get_needs_to_send_update())
977 binarysensortype->send_state_to_nextion();
978 }
979 for (auto *sensortype : this->sensortype_) {
980#ifdef USE_NEXTION_WAVEFORM
981 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_channel_id() == UINT8_MAX) {
982#else // USE_NEXTION_WAVEFORM
983 if (force_update || sensortype->get_needs_to_send_update()) {
984#endif // USE_NEXTION_WAVEFORM
985 sensortype->send_state_to_nextion();
986 }
987 }
988 for (auto *switchtype : this->switchtype_) {
989 if (force_update || switchtype->get_needs_to_send_update())
990 switchtype->send_state_to_nextion();
991 }
992 for (auto *textsensortype : this->textsensortype_) {
993 if (force_update || textsensortype->get_needs_to_send_update())
994 textsensortype->send_state_to_nextion();
995 }
996}
997
998void Nextion::update_components_by_prefix(const std::string &prefix) {
999 for (auto *binarysensortype : this->binarysensortype_) {
1000 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1001 binarysensortype->update_component_settings(true);
1002 }
1003 for (auto *sensortype : this->sensortype_) {
1004 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1005 sensortype->update_component_settings(true);
1006 }
1007 for (auto *switchtype : this->switchtype_) {
1008 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
1009 switchtype->update_component_settings(true);
1010 }
1011 for (auto *textsensortype : this->textsensortype_) {
1012 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1013 textsensortype->update_component_settings(true);
1014 }
1015}
1016
1017uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag) {
1018 uint8_t c = 0;
1019 uint8_t nr_of_ff_bytes = 0;
1020 bool exit_flag = false;
1021 bool ff_flag = false;
1022
1023 const uint32_t start = millis();
1024
1025 while ((timeout == 0 && this->available()) || millis() - start <= timeout) {
1026 if (!this->available()) {
1027 App.feed_wdt();
1028 delay(1);
1029 continue;
1030 }
1031
1032 this->read_byte(&c);
1033 if (c == 0xFF) {
1034 nr_of_ff_bytes++;
1035 } else {
1036 nr_of_ff_bytes = 0;
1037 ff_flag = false;
1038 }
1039
1040 if (nr_of_ff_bytes >= 3)
1041 ff_flag = true;
1042
1043 response += (char) c;
1044 if (recv_flag) {
1045 if (response.find(0x05) != std::string::npos) {
1046 exit_flag = true;
1047 }
1048 }
1049 App.feed_wdt();
1050 delay(2);
1051
1052 if (exit_flag || ff_flag) {
1053 break;
1054 }
1055 }
1056
1057 if (ff_flag)
1058 response = response.substr(0, response.length() - 3); // Remove last 3 0xFF
1059
1060 return response.length();
1061}
1062
1073void Nextion::add_no_result_to_queue_(const std::string &variable_name) {
1074#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1075 if (this->max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1076 ESP_LOGW(TAG, "Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
1077 return;
1078 }
1079#endif
1080
1081 RAMAllocator<nextion::NextionQueue> allocator;
1082 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1083 if (nextion_queue == nullptr) {
1084 ESP_LOGW(TAG, "Queue alloc failed");
1085 return;
1086 }
1087 new (nextion_queue) nextion::NextionQueue();
1088
1089 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
1090 nextion_queue->component = new nextion::NextionComponentBase;
1091 nextion_queue->component->set_variable_name(variable_name);
1092
1093 nextion_queue->queue_time = App.get_loop_component_start_time();
1094
1095 this->nextion_queue_.push_back(nextion_queue);
1096
1097 ESP_LOGN(TAG, "Queue NORESULT: %s", nextion_queue->component->get_variable_name().c_str());
1098}
1099
1114void Nextion::add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command) {
1115 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || command.empty())
1116 return;
1117
1118 if (this->send_command_(command)) {
1119 this->add_no_result_to_queue_(variable_name);
1120#ifdef USE_NEXTION_COMMAND_SPACING
1121 } else {
1122 // Command blocked by spacing, add to queue WITH the command for retry
1123 this->add_no_result_to_queue_with_pending_command_(variable_name, command);
1124#endif // USE_NEXTION_COMMAND_SPACING
1125 }
1126}
1127
1128#ifdef USE_NEXTION_COMMAND_SPACING
1129void Nextion::add_no_result_to_queue_with_pending_command_(const std::string &variable_name,
1130 const std::string &command) {
1131#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1132 if (this->max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1133 ESP_LOGW(TAG, "Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
1134 return;
1135 }
1136#endif
1137
1138 RAMAllocator<nextion::NextionQueue> allocator;
1139 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1140 if (nextion_queue == nullptr) {
1141 ESP_LOGW(TAG, "Queue alloc failed");
1142 return;
1143 }
1144 new (nextion_queue) nextion::NextionQueue();
1145
1146 nextion_queue->component = new nextion::NextionComponentBase;
1147 nextion_queue->component->set_variable_name(variable_name);
1148 nextion_queue->queue_time = App.get_loop_component_start_time();
1149 nextion_queue->pending_command = command; // Store command for retry
1150
1151 this->nextion_queue_.push_back(nextion_queue);
1152 ESP_LOGVV(TAG, "Queue with pending command: %s", variable_name.c_str());
1153}
1154#endif // USE_NEXTION_COMMAND_SPACING
1155
1156bool Nextion::add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,
1157 ...) {
1158 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_))
1159 return false;
1160
1161 char buffer[256];
1162 va_list arg;
1163 va_start(arg, format);
1164 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
1165 va_end(arg);
1166 if (ret <= 0) {
1167 ESP_LOGW(TAG, "Bad cmd format: '%s'", format);
1168 return false;
1169 }
1170
1171 this->add_no_result_to_queue_with_command_(variable_name, buffer);
1172 return true;
1173}
1174
1182bool Nextion::add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...) {
1183 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
1184 return false;
1185
1186 char buffer[256];
1187 va_list arg;
1188 va_start(arg, format);
1189 int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
1190 va_end(arg);
1191 if (ret <= 0) {
1192 ESP_LOGW(TAG, "Bad cmd format: '%s'", format);
1193 return false;
1194 }
1195
1196 this->add_no_result_to_queue_with_command_(variable_name, buffer);
1197 return true;
1198}
1199
1209void Nextion::add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value) {
1210 this->add_no_result_to_queue_with_set(component->get_variable_name(), component->get_variable_name_to_send(),
1211 state_value);
1212}
1213
1214void Nextion::add_no_result_to_queue_with_set(const std::string &variable_name,
1215 const std::string &variable_name_to_send, int32_t state_value) {
1216 this->add_no_result_to_queue_with_set_internal_(variable_name, variable_name_to_send, state_value);
1217}
1218
1219void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
1220 const std::string &variable_name_to_send, int32_t state_value,
1221 bool is_sleep_safe) {
1222 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping()))
1223 return;
1224
1225 this->add_no_result_to_queue_with_ignore_sleep_printf_(variable_name, "%s=%" PRId32, variable_name_to_send.c_str(),
1226 state_value);
1227}
1228
1237void Nextion::add_no_result_to_queue_with_set(NextionComponentBase *component, const std::string &state_value) {
1238 this->add_no_result_to_queue_with_set(component->get_variable_name(), component->get_variable_name_to_send(),
1239 state_value);
1240}
1241void Nextion::add_no_result_to_queue_with_set(const std::string &variable_name,
1242 const std::string &variable_name_to_send,
1243 const std::string &state_value) {
1244 this->add_no_result_to_queue_with_set_internal_(variable_name, variable_name_to_send, state_value);
1245}
1246
1247void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
1248 const std::string &variable_name_to_send,
1249 const std::string &state_value, bool is_sleep_safe) {
1250 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping()))
1251 return;
1252
1253 this->add_no_result_to_queue_with_printf_(variable_name, "%s=\"%s\"", variable_name_to_send.c_str(),
1254 state_value.c_str());
1255}
1256
1266void Nextion::add_to_get_queue(NextionComponentBase *component) {
1267 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_))
1268 return;
1269
1270#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1271 if (this->max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1272 ESP_LOGW(TAG, "Queue full (%zu), drop GET: %s", this->nextion_queue_.size(),
1273 component->get_variable_name().c_str());
1274 return;
1275 }
1276#endif
1277
1278 RAMAllocator<nextion::NextionQueue> allocator;
1279 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1280 if (nextion_queue == nullptr) {
1281 ESP_LOGW(TAG, "Queue alloc failed");
1282 return;
1283 }
1284 new (nextion_queue) nextion::NextionQueue();
1285
1286 nextion_queue->component = component;
1287 nextion_queue->queue_time = App.get_loop_component_start_time();
1288
1289 ESP_LOGN(TAG, "Queue %s: %s", component->get_queue_type_string(), component->get_variable_name().c_str());
1290
1291 std::string command = "get " + component->get_variable_name_to_send();
1292
1293#ifdef USE_NEXTION_COMMAND_SPACING
1294 // Always enqueue first so the response handler is present when the command
1295 // is eventually sent. Store the command for retry if spacing blocked it;
1296 // process_pending_in_queue_() will transmit it when the pacer allows.
1297 nextion_queue->pending_command = command;
1298 this->nextion_queue_.push_back(nextion_queue);
1299 if (this->send_command_(command)) {
1300 nextion_queue->pending_command.clear();
1301 }
1302#else // USE_NEXTION_COMMAND_SPACING
1303 if (this->send_command_(command)) {
1304 this->nextion_queue_.push_back(nextion_queue);
1305 } else {
1306 delete nextion_queue; // NOLINT(cppcoreguidelines-owning-memory)
1307 }
1308#endif // USE_NEXTION_COMMAND_SPACING
1309}
1310
1311#ifdef USE_NEXTION_WAVEFORM
1317void Nextion::add_addt_command_to_queue(NextionComponentBase *component) {
1318 if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
1319 return;
1320
1321 RAMAllocator<nextion::NextionQueue> allocator;
1322 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1323 if (nextion_queue == nullptr) {
1324 ESP_LOGW(TAG, "Queue alloc failed");
1325 return;
1326 }
1327 new (nextion_queue) nextion::NextionQueue();
1328
1329 nextion_queue->component = component;
1330 nextion_queue->queue_time = App.get_loop_component_start_time();
1331
1332 if (!this->waveform_queue_.push(nextion_queue)) {
1333 ESP_LOGW(TAG, "Waveform queue full, drop");
1334 delete nextion_queue; // NOLINT(cppcoreguidelines-owning-memory)
1335 return;
1336 }
1337 if (this->waveform_queue_.size() == 1)
1339}
1340
1342 if (this->waveform_queue_.empty())
1343 return;
1344
1345 auto *nb = this->waveform_queue_.front();
1346 auto *component = nb->component;
1347 size_t buffer_to_send = component->get_wave_buffer_size() < 255 ? component->get_wave_buffer_size() : 255;
1348
1349 char command[24]; // "addt " + uint8 + "," + uint8 + "," + uint8 + null = max 17 chars
1350 buf_append_printf(command, sizeof(command), 0, "addt %u,%u,%zu", component->get_component_id(),
1351 component->get_wave_channel_id(), buffer_to_send);
1352 // If spacing or setup state blocks the send, leave the entry at the front
1353 // of waveform_queue_ for retry on the next loop iteration via
1354 // check_pending_waveform_(). Only pop on a successful send.
1355 this->send_command_(command);
1356}
1357#endif // USE_NEXTION_WAVEFORM
1358
1359void Nextion::set_writer(const nextion_writer_t &writer) { this->writer_ = writer; }
1360
1361bool Nextion::is_updating() { return this->connection_state_.is_updating_; }
1362
1363} // namespace esphome::nextion
void feed_wdt()
Feed the task watchdog.
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.
uint8_t get_spacing() const
Get current command spacing.
Definition nextion.h:55
bool can_send(uint32_t now) const
Check if enough time has passed to send the next command.
Definition nextion.h:63
void mark_sent(uint32_t now)
Record the transmit timestamp for the most recently sent command.
Definition nextion.h:71
std::vector< NextionComponentBase * > touch_
Definition nextion.h:1586
std::vector< NextionComponentBase * > switchtype_
Definition nextion.h:1587
std::vector< NextionComponentBase * > binarysensortype_
Definition nextion.h:1590
StaticRingBuffer< NextionQueue *, 4 > waveform_queue_
Fixed-size ring buffer for waveform queue.
Definition nextion.h:1464
uint16_t max_q_age_ms_
Maximum age for queue items in ms.
Definition nextion.h:1631
bool send_command_(const std::string &command)
Manually send a raw command to the display and don't wait for an acknowledgement packet.
std::vector< NextionComponentBase * > textsensortype_
Definition nextion.h:1589
CallbackManager< void(uint8_t)> page_callback_
Definition nextion.h:1594
struct esphome::nextion::Nextion::@144 connection_state_
Status flags for Nextion display state management.
CallbackManager< void(StringRef, int32_t)> custom_sensor_callback_
Definition nextion.h:1601
void set_wake_up_page(uint8_t wake_up_page=255)
Sets which page Nextion loads when exiting sleep mode.
std::string device_model_
Definition nextion.h:1614
void all_components_send_state_(bool force_update=false)
std::string firmware_version_
Definition nextion.h:1615
void set_nextion_sensor_state(int queue_type, const std::string &name, float state)
Set the nextion sensor state object.
void add_addt_command_to_queue(NextionComponentBase *component) override
uint16_t max_commands_per_loop_
Definition nextion.h:1438
nextion_writer_t writer_
Definition nextion.h:1610
uint16_t startup_override_ms_
Timeout before forcing setup complete.
Definition nextion.h:1630
void add_to_get_queue(NextionComponentBase *component) override
bool send_command_printf(const char *format,...) __attribute__((format(printf
Manually send a raw formatted command to the display.
std::list< NextionQueue * > nextion_queue_
Definition nextion.h:1460
void set_auto_wake_on_touch(bool auto_wake_on_touch)
Sets if Nextion should auto-wake from sleep when touch press occurs.
bool remove_from_q_(bool report_empty=true)
std::vector< NextionComponentBase * > sensortype_
Definition nextion.h:1588
bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
void set_touch_sleep_timeout(uint16_t touch_sleep_timeout=0)
Set the touch sleep timeout of the display using the thsp command.
CallbackManager< void()> setup_callback_
Definition nextion.h:1591
std::string command_data_
Definition nextion.h:1627
optional< float > brightness_
Definition nextion.h:1611
CallbackManager< void()> wake_callback_
Definition nextion.h:1593
bool is_updating() override
Check if the TFT update process is currently running.
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
void goto_page(const char *page)
Show the page with a given name.
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int32_t state_value, bool is_sleep_safe=false)
bool void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command)
CallbackManager< void(StringRef, bool)> custom_binary_sensor_callback_
Definition nextion.h:1598
bool send_command(const char *command)
Manually send a raw command to the display.
std::string serial_number_
Definition nextion.h:1616
CallbackManager< void()> sleep_callback_
Definition nextion.h:1592
void reset_(bool reset_nextion=true)
void process_pending_in_queue_()
Process any commands in the queue that are pending due to command spacing.
void add_no_result_to_queue_(const std::string &variable_name)
CallbackManager< void(uint8_t, uint8_t, bool)> touch_callback_
Definition nextion.h:1595
void dump_config() override
NextionCommandPacer command_pacer_
Definition nextion.h:1445
void set_nextion_text_state(const std::string &name, const std::string &state)
bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
void update() override
CallbackManager< void()> buffer_overflow_callback_
Definition nextion.h:1596
void set_writer(const nextion_writer_t &writer)
CallbackManager< void(StringRef, StringRef)> custom_text_sensor_callback_
Definition nextion.h:1607
void add_no_result_to_queue_with_pending_command_(const std::string &variable_name, const std::string &command)
Add a command to the Nextion queue with a pending command for retry.
void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value) override
CallbackManager< void(StringRef, bool)> custom_switch_callback_
Definition nextion.h:1604
void update_components_by_prefix(const std::string &prefix)
uint32_t tft_upload_watchdog_timeout_
WDT timeout in ms (0 = no adjustment)
Definition nextion.h:1553
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
void write_str(const char *str)
Definition uart.h:32
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
const Component * component
Definition component.cpp:34
bool state
Definition fan.h:2
display::DisplayWriter< Nextion > nextion_writer_t
Definition nextion.h:34
const char *const TAG
Definition spi.cpp:7
const char int const __FlashStringHelper * format
Definition log.h:74
va_end(args)
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:889
size_t size_t const char va_start(args, fmt)
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6