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