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