14static const char *
const TAG =
"nextion";
17static constexpr uint8_t COMMAND_DELIMITER[3] = {0xFF, 0xFF, 0xFF};
18static constexpr size_t DELIMITER_SIZE =
sizeof(COMMAND_DELIMITER);
39#ifdef USE_NEXTION_COMMAND_SPACING
41 if (!this->
connection_state_.ignore_is_setup_ && !this->command_pacer_.can_send(now)) {
42 ESP_LOGN(TAG,
"Command spacing: delaying '%s'", command.c_str());
47 ESP_LOGN(TAG,
"cmd: %s", command.c_str());
50 const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
53#ifdef USE_NEXTION_COMMAND_SPACING
70#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
71 ESP_LOGW(TAG,
"Connected (no handshake)");
80#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
97 if (!response.empty() && response[0] == 0x1A) {
99 ESP_LOGV(TAG,
"0x1A error ignored (setup)");
102 if (response.empty() || response.find(
"comok") == std::string::npos) {
103#ifdef NEXTION_PROTOCOL_LOG
104 ESP_LOGN(TAG,
"Bad connect: %s", response.c_str());
105 for (
size_t i = 0; i < response.length(); i++) {
106 ESP_LOGN(TAG,
"resp: %s %d %d %c", response.c_str(), i, response[i], response[i]);
110 ESP_LOGW(TAG,
"Not connected");
116 ESP_LOGI(TAG,
"Connected");
119 ESP_LOGN(TAG,
"connect: %s", response.c_str());
123 size_t field_count = 0;
126 auto copy_field = [&](
char *dst,
size_t cap) {
127 size_t len = (
end == std::string::npos ? response.size() :
end) - start;
128 size_t n =
len < cap ?
len : cap;
129 std::memcpy(dst, response.data() + start, n);
132 while ((start = response.find_first_not_of(
',',
end)) != std::string::npos) {
133 end = response.find(
',', start);
134 switch (field_count) {
145 this->
flash_size_ =
static_cast<uint32_t>(std::strtoul(response.data() + start,
nullptr, 10));
155 ESP_LOGN(TAG,
"Connect info: %zu fields", field_count);
157 ESP_LOGE(TAG,
"Bad connect value: '%s'", response.c_str());
174 delete entry->component;
178 this->nextion_queue_.clear();
179#ifdef USE_NEXTION_WAVEFORM
183 this->waveform_queue_.clear();
188 ESP_LOGCONFIG(TAG,
"Nextion:");
190#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
191 ESP_LOGCONFIG(TAG,
" Skip handshake: YES");
195 " Device Model: %s\n"
197 " Serial Number: %s\n"
198 " Flash Size: %" PRIu32
" bytes",
201 ESP_LOGCONFIG(TAG,
" Device info: not yet detected");
204#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
205 " Exit reparse: YES\n"
207 " Max queue age: %u ms\n"
208 " Startup override: %u ms\n"
209 " Wake On Touch: %s\n"
210 " Touch Timeout: %" PRIu16,
212 this->touch_sleep_timeout_);
215#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
220 ESP_LOGCONFIG(TAG,
" Wake Up Page: %u", this->
wake_up_page_);
223#ifdef USE_NEXTION_CONF_START_UP_PAGE
229#ifdef USE_NEXTION_COMMAND_SPACING
233#ifdef USE_NEXTION_MAX_QUEUE_SIZE
236#ifdef USE_NEXTION_TFT_UPLOAD
239 " TFT upload HTTP timeout: %" PRIu16
"ms\n"
240 " TFT upload HTTP retries: %u",
241 this->
tft_url_.c_str(), this->tft_upload_http_timeout_, this->tft_upload_http_retries_);
264 binarysensortype->update_component();
267 sensortype->update_component();
270 switchtype->update_component();
273 textsensortype->update_component();
292 int ret = vsnprintf(buffer,
sizeof(buffer),
format, arg);
295 ESP_LOGW(TAG,
"Bad cmd format: '%s'",
format);
303#ifdef NEXTION_PROTOCOL_LOG
305 ESP_LOGN(TAG,
"print_queue_members_ (top 10) size %zu", this->nextion_queue_.size());
306 ESP_LOGN(TAG,
"*******************************************");
308 for (
auto *i : this->nextion_queue_) {
313 ESP_LOGN(TAG,
"Queue null");
315 ESP_LOGN(TAG,
"Queue type: %d:%s, name: %s", i->component->get_queue_type(),
316 i->component->get_queue_type_string(), i->component->get_variable_name().c_str());
319 ESP_LOGN(TAG,
"*******************************************");
327 if (this->
connection_state_.nextion_reports_is_setup_ && !this->connection_state_.sent_setup_commands_) {
336#ifdef USE_NEXTION_CONF_START_UP_PAGE
366 ESP_LOGV(TAG,
"Manual ready set");
371#ifdef USE_NEXTION_COMMAND_SPACING
373#ifdef USE_NEXTION_WAVEFORM
374 if (!this->waveform_queue_.empty()) {
381#ifdef USE_NEXTION_COMMAND_SPACING
383#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
384 size_t commands_sent = 0;
387 for (
auto *item : this->nextion_queue_) {
388 if (item ==
nullptr || item->pending_command.empty()) {
392#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
394 ESP_LOGV(TAG,
"Pending cmds: loop limit reached, deferring");
407 item->pending_command.clear();
408 ESP_LOGVV(TAG,
"Pending cmd sent: %s", item->component->get_variable_name().c_str());
414 if (this->nextion_queue_.empty()) {
416 ESP_LOGE(TAG,
"Queue empty");
421 NextionQueue *nb = this->nextion_queue_.front();
422 if (!nb || !nb->component) {
423 ESP_LOGE(TAG,
"Invalid queue");
424 this->nextion_queue_.pop_front();
427 NextionComponentBase *
component = nb->component;
429 ESP_LOGN(TAG,
"Removed: %s",
component->get_variable_name().c_str());
432 if (
component->get_variable_name() ==
"sleep_wake") {
438 this->nextion_queue_.pop_front();
447 size_t to_read = std::min(avail,
sizeof(buf));
453 this->
command_data_.append(
reinterpret_cast<const char *
>(buf), to_read);
462#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
463 size_t commands_processed = 0;
466 size_t to_process_length = 0;
467 std::string to_process;
469 ESP_LOGN(TAG,
"command_data_ %s len %d", this->
command_data_.c_str(), this->command_data_.length());
470#ifdef NEXTION_PROTOCOL_LOG
473 while ((to_process_length = this->
command_data_.find(
reinterpret_cast<const char *
>(COMMAND_DELIMITER), 0,
474 DELIMITER_SIZE)) != std::string::npos) {
475#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
477 ESP_LOGV(TAG,
"Command limit reached, deferring");
481 ESP_LOGN(TAG,
"queue size: %zu", this->nextion_queue_.size());
482 while (to_process_length + DELIMITER_SIZE < this->
command_data_.length() &&
483 static_cast<uint8_t
>(this->command_data_[to_process_length + DELIMITER_SIZE]) == 0xFF) {
485 ESP_LOGN(TAG,
"Add 0xFF");
490 to_process_length -= 1;
491 to_process = this->
command_data_.substr(1, to_process_length);
493 switch (nextion_event) {
495 ESP_LOGW(TAG,
"Invalid instruction");
501 ESP_LOGVV(TAG,
"Cmd OK");
502 ESP_LOGN(TAG,
"this->nextion_queue_.empty() %s", YESNO(this->nextion_queue_.empty()));
506 if (this->nextion_queue_.empty()) {
513 ESP_LOGW(TAG,
"Invalid component ID/name");
517 ESP_LOGW(TAG,
"Invalid page ID");
521 ESP_LOGW(TAG,
"Invalid picture ID");
525 ESP_LOGW(TAG,
"Invalid font ID");
529 ESP_LOGW(TAG,
"File operation failed");
532 ESP_LOGW(TAG,
"CRC validation failed");
535 ESP_LOGW(TAG,
"Invalid baud rate");
538#ifdef USE_NEXTION_WAVEFORM
539 if (this->waveform_queue_.empty()) {
540 ESP_LOGW(TAG,
"Waveform ID/ch used but no sensor queued");
542 auto &nb = this->waveform_queue_.front();
543 NextionComponentBase *
component = nb->component;
544 ESP_LOGW(TAG,
"Invalid waveform ID %d/ch %d",
component->get_component_id(),
546 ESP_LOGN(TAG,
"Remove waveform ID %d/ch %d",
component->get_component_id(),
component->get_wave_channel_id());
548 this->waveform_queue_.pop();
551 ESP_LOGW(TAG,
"Waveform ID/ch error but waveform not enabled");
555 ESP_LOGW(TAG,
"Invalid variable name");
559 ESP_LOGW(TAG,
"Invalid variable operation");
563 ESP_LOGW(TAG,
"Variable assign failed");
567 ESP_LOGW(TAG,
"EEPROM operation failed");
570 ESP_LOGW(TAG,
"Invalid parameter count");
574 ESP_LOGW(TAG,
"Invalid component I/O");
577 ESP_LOGW(TAG,
"Undefined escape chars");
581 ESP_LOGW(TAG,
"Variable name too long");
586 ESP_LOGE(TAG,
"Serial buffer overflow");
590 if (to_process_length != 3) {
591 ESP_LOGW(TAG,
"Incorrect touch len: %zu (need 3)", to_process_length);
595 uint8_t page_id = to_process[0];
596 uint8_t component_id = to_process[1];
597 uint8_t touch_event = to_process[2];
598 ESP_LOGV(TAG,
"Touch %s: page %u comp %u", touch_event ?
"PRESS" :
"RELEASE", page_id, component_id);
599 for (
auto *touch : this->
touch_) {
600 touch->process_touch(page_id, component_id, touch_event != 0);
607 if (to_process_length != 1) {
608 ESP_LOGW(TAG,
"Page event: expect 1, got %zu", to_process_length);
612 uint8_t page_id = to_process[0];
613 ESP_LOGV(TAG,
"New page: %u", page_id);
622 if (to_process_length != 5) {
623 ESP_LOGW(TAG,
"Touch coordinate: expect 5, got %zu", to_process_length);
624 ESP_LOGW(TAG,
"%s", to_process.c_str());
628 const uint16_t
x = (uint16_t(to_process[0]) << 8) | to_process[1];
629 const uint16_t
y = (uint16_t(to_process[2]) << 8) | to_process[3];
630 const uint8_t touch_event = to_process[4];
631 ESP_LOGV(TAG,
"Touch %s at %u,%u", touch_event ?
"PRESS" :
"RELEASE",
x,
y);
641 if (this->nextion_queue_.empty()) {
642 ESP_LOGW(TAG,
"String return but queue is empty");
646 NextionQueue *nb = this->nextion_queue_.front();
647 if (!nb || !nb->component) {
648 ESP_LOGE(TAG,
"Invalid queue entry");
649 this->nextion_queue_.pop_front();
652 NextionComponentBase *
component = nb->component;
655 ESP_LOGE(TAG,
"String return but '%s' not text sensor",
component->get_variable_name().c_str());
657 ESP_LOGN(TAG,
"String resp: '%s' id: %s type: %s", to_process.c_str(),
component->get_variable_name().c_str(),
659 component->set_state_from_string(to_process,
true,
false);
663 this->nextion_queue_.pop_front();
674 if (this->nextion_queue_.empty()) {
675 ESP_LOGE(TAG,
"Numeric return but queue empty");
679 if (to_process_length < 4) {
680 ESP_LOGE(TAG,
"Numeric return but insufficient data (need 4, got %zu)", to_process_length);
684 int value =
static_cast<int>(
encode_uint32(to_process[3], to_process[2], to_process[1], to_process[0]));
686 NextionQueue *nb = this->nextion_queue_.front();
687 if (!nb || !nb->component) {
688 ESP_LOGE(TAG,
"Invalid queue");
689 this->nextion_queue_.pop_front();
692 NextionComponentBase *
component = nb->component;
697 ESP_LOGE(TAG,
"Numeric return but '%s' invalid type %d",
component->get_variable_name().c_str(),
700 ESP_LOGN(TAG,
"Numeric: %s type %d:%s val %d",
component->get_variable_name().c_str(),
702 component->set_state_from_int(value,
true,
false);
706 this->nextion_queue_.pop_front();
712 ESP_LOGVV(TAG,
"Auto sleep");
719 ESP_LOGVV(TAG,
"Auto wake");
727 ESP_LOGV(TAG,
"System start: %zu", to_process_length);
741 std::string variable_name;
744 auto index = to_process.find(
'\0');
745 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
746 ESP_LOGE(TAG,
"Bad switch data (0x90)");
747 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
751 variable_name = to_process.substr(0, index);
754 ESP_LOGN(TAG,
"Switch %s: %s", ONOFF(to_process[index] != 0), variable_name.c_str());
756#ifdef USE_NEXTION_TRIGGER_CUSTOM_SWITCH
760 for (
auto *switchtype : this->switchtype_) {
761 switchtype->process_bool(variable_name, to_process[index] != 0);
772 std::string variable_name;
774 auto index = to_process.find(
'\0');
775 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
776 ESP_LOGE(TAG,
"Bad sensor data (0x91)");
777 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
781 index = to_process.find(
'\0');
782 variable_name = to_process.substr(0, index);
784 int value =
static_cast<int>(
785 encode_uint32(to_process[index + 4], to_process[index + 3], to_process[index + 2], to_process[index + 1]));
787 ESP_LOGN(TAG,
"Sensor: %s=%d", variable_name.c_str(), value);
789#ifdef USE_NEXTION_TRIGGER_CUSTOM_SENSOR
793 for (
auto *sensor : this->sensortype_) {
794 sensor->process_sensor(variable_name, value);
807 std::string variable_name;
808 std::string text_value;
811 auto index = to_process.find(
'\0');
812 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
813 ESP_LOGE(TAG,
"Bad text data (0x92)");
814 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
818 variable_name = to_process.substr(0, index);
822 text_value = to_process.substr(index, to_process_length - index - 1);
824 ESP_LOGN(TAG,
"Text sensor: %s='%s'", variable_name.c_str(), text_value.c_str());
831#ifdef USE_NEXTION_TRIGGER_CUSTOM_TEXT_SENSOR
835 for (
auto *textsensortype : this->textsensortype_) {
836 textsensortype->process_text(variable_name, text_value);
847 std::string variable_name;
850 auto index = to_process.find(
'\0');
851 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
852 ESP_LOGE(TAG,
"Bad binary data (0x93)");
853 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
857 variable_name = to_process.substr(0, index);
860 ESP_LOGN(TAG,
"Binary sensor: %s=%s", variable_name.c_str(), ONOFF(to_process[index] != 0));
862#ifdef USE_NEXTION_TRIGGER_CUSTOM_BINARY_SENSOR
866 for (
auto *binarysensortype : this->binarysensortype_) {
867 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
872 ESP_LOGVV(TAG,
"Data transmit done");
873#ifdef USE_NEXTION_WAVEFORM
879 ESP_LOGVV(TAG,
"Ready for transmit");
880#ifdef USE_NEXTION_WAVEFORM
881 if (this->waveform_queue_.empty()) {
882 ESP_LOGE(TAG,
"No waveforms queued");
885 auto &nb = this->waveform_queue_.front();
887 size_t buffer_to_send =
component->get_wave_buffer_size() < 255 ?
component->get_wave_buffer_size() : 255;
888 this->
write_array(component->get_wave_buffer().data(),
static_cast<int>(buffer_to_send));
889 ESP_LOGN(TAG,
"Send waveform: component id %d, waveform id %d, size %zu",
component->get_component_id(),
890 component->get_wave_channel_id(), buffer_to_send);
891 component->clear_wave_buffer(buffer_to_send);
893 this->waveform_queue_.pop();
895 ESP_LOGW(TAG,
"Waveform transmit ready but waveform not enabled");
900 ESP_LOGW(TAG,
"Unknown event: 0x%02X", nextion_event);
904 this->
command_data_.erase(0, to_process_length + DELIMITER_SIZE + 1);
907 ESP_LOGN(TAG,
"Loop end");
914 if (this->
max_q_age_ms_ > 0 && !this->nextion_queue_.empty() &&
915 ms - this->nextion_queue_.front()->queue_time > this->max_q_age_ms_) {
916 for (
auto it = this->nextion_queue_.begin(); it != this->nextion_queue_.end();) {
917 if (ms - (*it)->queue_time > this->max_q_age_ms_) {
918 NextionComponentBase *
component = (*it)->component;
919 ESP_LOGV(TAG,
"Remove old queue '%s':'%s'",
component->get_queue_type_string(),
923 if (
component->get_variable_name() ==
"sleep_wake") {
930 it = this->nextion_queue_.erase(it);
944 ESP_LOGN(TAG,
"State: %s=%lf (type %d)",
name.c_str(),
state, queue_type);
946 switch (queue_type) {
948 for (
auto *sensor : this->sensortype_) {
949 if (name == sensor->get_variable_name()) {
950 sensor->set_state(
state,
true,
true);
957 for (
auto *sensor : this->binarysensortype_) {
958 if (name == sensor->get_variable_name()) {
959 sensor->set_state(
state != 0,
true,
true);
966 for (
auto *sensor : this->switchtype_) {
967 if (name == sensor->get_variable_name()) {
968 sensor->set_state(
state != 0,
true,
true);
975 ESP_LOGW(TAG,
"set_sensor_state: bad type %d", queue_type);
981 ESP_LOGV(TAG,
"State: %s='%s'",
name.c_str(),
state.c_str());
983 for (
auto *sensor : this->textsensortype_) {
984 if (name == sensor->get_variable_name()) {
985 sensor->set_state(
state,
true,
true);
992 ESP_LOGV(TAG,
"Send states");
993 for (
auto *binarysensortype : this->binarysensortype_) {
994 if (force_update || binarysensortype->get_needs_to_send_update())
995 binarysensortype->send_state_to_nextion();
997 for (
auto *sensortype : this->sensortype_) {
998#ifdef USE_NEXTION_WAVEFORM
999 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_channel_id() == UINT8_MAX) {
1001 if (force_update || sensortype->get_needs_to_send_update()) {
1003 sensortype->send_state_to_nextion();
1006 for (
auto *switchtype : this->switchtype_) {
1007 if (force_update || switchtype->get_needs_to_send_update())
1008 switchtype->send_state_to_nextion();
1010 for (
auto *textsensortype : this->textsensortype_) {
1011 if (force_update || textsensortype->get_needs_to_send_update())
1012 textsensortype->send_state_to_nextion();
1017 for (
auto *binarysensortype : this->binarysensortype_) {
1018 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1019 binarysensortype->update_component_settings(
true);
1021 for (
auto *sensortype : this->sensortype_) {
1022 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1023 sensortype->update_component_settings(
true);
1025 for (
auto *switchtype : this->switchtype_) {
1026 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
1027 switchtype->update_component_settings(
true);
1029 for (
auto *textsensortype : this->textsensortype_) {
1030 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1031 textsensortype->update_component_settings(
true);
1037 uint8_t nr_of_ff_bytes = 0;
1038 bool exit_flag =
false;
1039 bool ff_flag =
false;
1043 while ((timeout == 0 && this->
available()) ||
millis() - start <= timeout) {
1058 if (nr_of_ff_bytes >= 3)
1061 response += (char) c;
1063 if (response.find(0x05) != std::string::npos) {
1070 if (exit_flag || ff_flag) {
1076 response = response.substr(0, response.length() - 3);
1078 return response.length();
1092#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1093 if (this->
max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1094 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
1099 RAMAllocator<nextion::NextionQueue> allocator;
1100 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1101 if (nextion_queue ==
nullptr) {
1102 ESP_LOGW(TAG,
"Queue alloc failed");
1105 new (nextion_queue) nextion::NextionQueue();
1108 nextion_queue->component =
new (std::nothrow) nextion::NextionComponentBase;
1109 if (nextion_queue->component ==
nullptr) {
1110 ESP_LOGW(TAG,
"Component alloc failed");
1111 nextion_queue->~NextionQueue();
1112 allocator.deallocate(nextion_queue, 1);
1115 nextion_queue->component->set_variable_name(variable_name);
1119 this->nextion_queue_.push_back(nextion_queue);
1121 ESP_LOGN(TAG,
"Queue NORESULT: %s", nextion_queue->component->get_variable_name().c_str());
1144#ifdef USE_NEXTION_COMMAND_SPACING
1152#ifdef USE_NEXTION_COMMAND_SPACING
1154 const std::string &command) {
1155#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1156 if (this->
max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1157 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
1162 RAMAllocator<nextion::NextionQueue> allocator;
1163 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1164 if (nextion_queue ==
nullptr) {
1165 ESP_LOGW(TAG,
"Queue alloc failed");
1168 new (nextion_queue) nextion::NextionQueue();
1170 nextion_queue->component =
new (std::nothrow) nextion::NextionComponentBase;
1171 if (nextion_queue->component ==
nullptr) {
1172 ESP_LOGW(TAG,
"Component alloc failed");
1173 nextion_queue->~NextionQueue();
1174 allocator.deallocate(nextion_queue, 1);
1177 nextion_queue->component->set_variable_name(variable_name);
1179 nextion_queue->pending_command = command;
1181 this->nextion_queue_.push_back(nextion_queue);
1182 ESP_LOGVV(TAG,
"Queue with pending command: %s", variable_name.c_str());
1194 int ret = vsnprintf(buffer,
sizeof(buffer),
format, arg);
1197 ESP_LOGW(TAG,
"Bad cmd format: '%s'",
format);
1219 int ret = vsnprintf(buffer,
sizeof(buffer),
format, arg);
1222 ESP_LOGW(TAG,
"Bad cmd format: '%s'",
format);
1245 const std::string &variable_name_to_send, int32_t state_value) {
1250 const std::string &variable_name_to_send, int32_t state_value,
1251 bool is_sleep_safe) {
1272 const std::string &variable_name_to_send,
1273 const std::string &state_value) {
1278 const std::string &variable_name_to_send,
1279 const std::string &state_value,
bool is_sleep_safe) {
1284 state_value.c_str());
1300#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1301 if (this->
max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1302 ESP_LOGW(TAG,
"Queue full (%zu), drop GET: %s", this->nextion_queue_.size(),
1303 component->get_variable_name().c_str());
1308 RAMAllocator<nextion::NextionQueue> allocator;
1309 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1310 if (nextion_queue ==
nullptr) {
1311 ESP_LOGW(TAG,
"Queue alloc failed");
1314 new (nextion_queue) nextion::NextionQueue();
1319 ESP_LOGN(TAG,
"Queue %s: %s",
component->get_queue_type_string(),
component->get_variable_name().c_str());
1321 std::string command =
"get " +
component->get_variable_name_to_send();
1323#ifdef USE_NEXTION_COMMAND_SPACING
1327 nextion_queue->pending_command = command;
1328 this->nextion_queue_.push_back(nextion_queue);
1330 nextion_queue->pending_command.clear();
1334 this->nextion_queue_.push_back(nextion_queue);
1336 delete nextion_queue;
1341#ifdef USE_NEXTION_WAVEFORM
1351 RAMAllocator<nextion::NextionQueue> allocator;
1352 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1353 if (nextion_queue ==
nullptr) {
1354 ESP_LOGW(TAG,
"Queue alloc failed");
1357 new (nextion_queue) nextion::NextionQueue();
1362 if (!this->waveform_queue_.push(nextion_queue)) {
1363 ESP_LOGW(TAG,
"Waveform queue full, drop");
1364 delete nextion_queue;
1367 if (this->waveform_queue_.size() == 1)
1372 if (this->waveform_queue_.empty())
1375 auto *nb = this->waveform_queue_.front();
1377 size_t buffer_to_send =
component->get_wave_buffer_size() < 255 ?
component->get_wave_buffer_size() : 255;
1380 buf_append_printf(command,
sizeof(command), 0,
"addt %u,%u,%zu",
component->get_component_id(),
1381 component->get_wave_channel_id(), buffer_to_send);
void feed_wdt()
Feed the task watchdog.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
uint8_t get_spacing() const
Get current command spacing.
bool can_send(uint32_t now) const
Check if enough time has passed to send the next command.
void mark_sent(uint32_t now)
Record the transmit timestamp for the most recently sent command.
std::vector< NextionComponentBase * > touch_
std::vector< NextionComponentBase * > switchtype_
std::vector< NextionComponentBase * > binarysensortype_
StaticRingBuffer< NextionQueue *, 4 > waveform_queue_
Fixed-size ring buffer for waveform queue.
uint16_t max_q_age_ms_
Maximum age for queue items in ms.
bool send_command_(const std::string &command)
Manually send a raw command to the display and don't wait for an acknowledgement packet.
static constexpr size_t NEXTION_MODEL_MAX
Max observed ~18 chars from product numbering rules.
std::vector< NextionComponentBase * > textsensortype_
char firmware_version_[NEXTION_FW_MAX+1]
void purge_stale_queue_entries_()
Drop queue entries older than max_q_age_ms_.
CallbackManager< void(uint8_t)> page_callback_
void check_pending_waveform_()
CallbackManager< void(StringRef, int32_t)> custom_sensor_callback_
void set_wake_up_page(uint8_t wake_up_page=255)
Sets which page Nextion loads when exiting sleep mode.
void all_components_send_state_(bool force_update=false)
void set_nextion_sensor_state(int queue_type, const std::string &name, float state)
Set the nextion sensor state object.
char serial_number_[NEXTION_SERIAL_MAX+1]
void add_addt_command_to_queue(NextionComponentBase *component) override
uint32_t flash_size_
Flash size in bytes — plain integer, no string needed.
uint16_t max_commands_per_loop_
uint16_t startup_override_ms_
Timeout before forcing setup complete.
void process_nextion_commands_()
void add_to_get_queue(NextionComponentBase *component) override
bool send_command_printf(const char *format,...) __attribute__((format(printf
Manually send a raw formatted command to the display.
std::list< NextionQueue * > nextion_queue_
void set_auto_wake_on_touch(bool auto_wake_on_touch)
Sets if Nextion should auto-wake from sleep when touch press occurs.
bool remove_from_q_(bool report_empty=true)
std::vector< NextionComponentBase * > sensortype_
uint16_t touch_sleep_timeout_
bool add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
void set_touch_sleep_timeout(uint16_t touch_sleep_timeout=0)
Set the touch sleep timeout of the display using the thsp command.
CallbackManager< void()> setup_callback_
std::string command_data_
optional< float > brightness_
CallbackManager< void()> wake_callback_
bool is_updating() override
Check if the TFT update process is currently running.
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
struct esphome::nextion::Nextion::@147 connection_state_
Status flags for Nextion display state management.
void goto_page(const char *page)
Show the page with a given name.
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int32_t state_value, bool is_sleep_safe=false)
bool void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command)
CallbackManager< void(StringRef, bool)> custom_binary_sensor_callback_
bool send_command(const char *command)
Manually send a raw command to the display.
CallbackManager< void()> sleep_callback_
void reset_(bool reset_nextion=true)
void update_all_components()
void process_pending_in_queue_()
Process any commands in the queue that are pending due to command spacing.
static constexpr size_t NEXTION_FW_MAX
'S' prefix + integer (e.g. 'S99' or 123)
void add_no_result_to_queue_(const std::string &variable_name)
CallbackManager< void(uint8_t, uint8_t, bool)> touch_callback_
void print_queue_members_()
void dump_config() override
NextionCommandPacer command_pacer_
void set_nextion_text_state(const std::string &name, const std::string &state)
bool add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,...) __attribute__((format(printf
CallbackManager< void()> buffer_overflow_callback_
void set_writer(const nextion_writer_t &writer)
char device_model_[NEXTION_MODEL_MAX+1]
CallbackManager< void(StringRef, StringRef)> custom_text_sensor_callback_
void add_no_result_to_queue_with_pending_command_(const std::string &variable_name, const std::string &command)
Add a command to the Nextion queue with a pending command for retry.
void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value) override
static constexpr size_t NEXTION_SERIAL_MAX
Consistently 16 hex chars across all documented examples.
CallbackManager< void(StringRef, bool)> custom_switch_callback_
void update_components_by_prefix(const std::string &prefix)
uint32_t tft_upload_watchdog_timeout_
WDT timeout in ms (0 = no adjustment)
void set_backlight_brightness(float brightness)
Set the brightness of the backlight.
optional< std::array< uint8_t, N > > read_array()
void write_str(const char *str)
bool read_byte(uint8_t *data)
void write_array(const uint8_t *data, size_t len)
const Component * component
display::DisplayWriter< Nextion > nextion_writer_t
const char int const __FlashStringHelper * format
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
size_t size_t const char va_start(args, fmt)
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.