14static const char *
const TAG =
"nextion";
17static bool is_sleep_wake_command(
const NextionComponentBase *
component) {
22static constexpr uint8_t COMMAND_DELIMITER[3] = {0xFF, 0xFF, 0xFF};
23static constexpr size_t DELIMITER_SIZE =
sizeof(COMMAND_DELIMITER);
44#ifdef USE_NEXTION_COMMAND_SPACING
46 if (!this->
connection_state_.ignore_is_setup_ && !this->command_pacer_.can_send(now)) {
47 ESP_LOGN(TAG,
"Command spacing: delaying '%s'", command.c_str());
52 ESP_LOGN(TAG,
"cmd: %s", command.c_str());
55 const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
58#ifdef USE_NEXTION_COMMAND_SPACING
75#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
76 ESP_LOGW(TAG,
"Connected (no handshake)");
85#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
102 if (!response.empty() && response[0] == 0x1A) {
104 ESP_LOGV(TAG,
"0x1A error ignored (setup)");
107 if (response.empty() || response.find(
"comok") == std::string::npos) {
108#ifdef NEXTION_PROTOCOL_LOG
109 ESP_LOGN(TAG,
"Bad connect: %s", response.c_str());
110 for (
size_t i = 0; i < response.length(); i++) {
111 ESP_LOGN(TAG,
"resp: %s %d %d %c", response.c_str(), i, response[i], response[i]);
115 ESP_LOGW(TAG,
"Not connected");
121 ESP_LOGI(TAG,
"Connected");
124 ESP_LOGN(TAG,
"connect: %s", response.c_str());
128 size_t field_count = 0;
131 auto copy_field = [&](
char *dst,
size_t cap) {
132 size_t len = (
end == std::string::npos ? response.size() :
end) - start;
133 size_t n =
len < cap ?
len : cap;
134 std::memcpy(dst, response.data() + start, n);
137 while ((start = response.find_first_not_of(
',',
end)) != std::string::npos) {
138 end = response.find(
',', start);
139 switch (field_count) {
150 this->
flash_size_ =
static_cast<uint32_t>(std::strtoul(response.data() + start,
nullptr, 10));
160 ESP_LOGN(TAG,
"Connect info: %zu fields", field_count);
162 ESP_LOGE(TAG,
"Bad connect value: '%s'", response.c_str());
175 nb->component->~NextionComponentBase();
176 RAMAllocator<NextionComponentBase>().deallocate(nb->component, 1);
179 RAMAllocator<NextionQueue>().deallocate(nb, 1);
191 this->nextion_queue_.clear();
192#ifdef USE_NEXTION_WAVEFORM
196 this->waveform_queue_.clear();
201 ESP_LOGCONFIG(TAG,
"Nextion:");
203#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
204 ESP_LOGCONFIG(TAG,
" Skip handshake: YES");
208 " Device Model: %s\n"
210 " Serial Number: %s\n"
211 " Flash Size: %" PRIu32
" bytes",
214 ESP_LOGCONFIG(TAG,
" Device info: not yet detected");
217#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
218 " Exit reparse: YES\n"
220 " Max queue age: %u ms\n"
221 " Startup override: %u ms\n"
222 " Wake On Touch: %s\n"
223 " Touch Timeout: %" PRIu16,
225 this->touch_sleep_timeout_);
228#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
233 ESP_LOGCONFIG(TAG,
" Wake Up Page: %u", this->
wake_up_page_);
236#ifdef USE_NEXTION_CONF_START_UP_PAGE
242#ifdef USE_NEXTION_COMMAND_SPACING
246#ifdef USE_NEXTION_MAX_QUEUE_SIZE
249#ifdef USE_NEXTION_TFT_UPLOAD
252 " TFT upload HTTP timeout: %" PRIu16
"ms\n"
253 " TFT upload HTTP retries: %u",
254 this->
tft_url_.c_str(), this->tft_upload_http_timeout_, this->tft_upload_http_retries_);
277 binarysensortype->update_component();
280 sensortype->update_component();
283 switchtype->update_component();
286 textsensortype->update_component();
305 int ret = vsnprintf(buffer,
sizeof(buffer),
format, arg);
308 ESP_LOGW(TAG,
"Bad cmd format: '%s'",
format);
316#ifdef NEXTION_PROTOCOL_LOG
318 ESP_LOGN(TAG,
"print_queue_members_ (top 10) size %zu", this->nextion_queue_.size());
319 ESP_LOGN(TAG,
"*******************************************");
321 for (
auto *i : this->nextion_queue_) {
326 ESP_LOGN(TAG,
"Queue null");
328 ESP_LOGN(TAG,
"Queue type: %d:%s, name: %s", i->component->get_queue_type(),
329 i->component->get_queue_type_string(), i->component->get_variable_name().c_str());
332 ESP_LOGN(TAG,
"*******************************************");
340 if (this->
connection_state_.nextion_reports_is_setup_ && !this->connection_state_.sent_setup_commands_) {
349#ifdef USE_NEXTION_CONF_START_UP_PAGE
379 ESP_LOGV(TAG,
"Manual ready set");
384#ifdef USE_NEXTION_COMMAND_SPACING
386#ifdef USE_NEXTION_WAVEFORM
387 if (!this->waveform_queue_.empty()) {
394#ifdef USE_NEXTION_COMMAND_SPACING
396#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
397 size_t commands_sent = 0;
400 for (
auto *item : this->nextion_queue_) {
401 if (item ==
nullptr || item->pending_command.empty()) {
405#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
407 ESP_LOGV(TAG,
"Pending cmds: loop limit reached, deferring");
420 item->pending_command.clear();
421 ESP_LOGVV(TAG,
"Pending cmd sent: %s", item->component->get_variable_name().c_str());
427 if (this->nextion_queue_.empty()) {
429 ESP_LOGE(TAG,
"Queue empty");
434 NextionQueue *nb = this->nextion_queue_.front();
435 if (!nb || !nb->component) {
436 ESP_LOGE(TAG,
"Invalid queue");
440 this->nextion_queue_.pop_front();
443 NextionComponentBase *
component = nb->component;
445 ESP_LOGN(TAG,
"Removed: %s",
component->get_variable_name().c_str());
451 this->nextion_queue_.pop_front();
460 size_t to_read = std::min(avail,
sizeof(buf));
466 this->
command_data_.append(
reinterpret_cast<const char *
>(buf), to_read);
475#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
476 size_t commands_processed = 0;
479 size_t to_process_length = 0;
480 std::string to_process;
482 ESP_LOGN(TAG,
"command_data_ %s len %d", this->
command_data_.c_str(), this->command_data_.length());
483#ifdef NEXTION_PROTOCOL_LOG
486 while ((to_process_length = this->
command_data_.find(
reinterpret_cast<const char *
>(COMMAND_DELIMITER), 0,
487 DELIMITER_SIZE)) != std::string::npos) {
488#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
490 ESP_LOGV(TAG,
"Command limit reached, deferring");
494 ESP_LOGN(TAG,
"queue size: %zu", this->nextion_queue_.size());
495 while (to_process_length + DELIMITER_SIZE < this->
command_data_.length() &&
496 static_cast<uint8_t
>(this->command_data_[to_process_length + DELIMITER_SIZE]) == 0xFF) {
498 ESP_LOGN(TAG,
"Add 0xFF");
503 to_process_length -= 1;
504 to_process = this->
command_data_.substr(1, to_process_length);
506 switch (nextion_event) {
508 ESP_LOGW(TAG,
"Invalid instruction");
514 ESP_LOGVV(TAG,
"Cmd OK");
515 ESP_LOGN(TAG,
"this->nextion_queue_.empty() %s", YESNO(this->nextion_queue_.empty()));
519 if (this->nextion_queue_.empty()) {
526 ESP_LOGW(TAG,
"Invalid component ID/name");
530 ESP_LOGW(TAG,
"Invalid page ID");
534 ESP_LOGW(TAG,
"Invalid picture ID");
538 ESP_LOGW(TAG,
"Invalid font ID");
542 ESP_LOGW(TAG,
"File operation failed");
545 ESP_LOGW(TAG,
"CRC validation failed");
548 ESP_LOGW(TAG,
"Invalid baud rate");
551#ifdef USE_NEXTION_WAVEFORM
552 if (this->waveform_queue_.empty()) {
553 ESP_LOGW(TAG,
"Waveform ID/ch used but no sensor queued");
555 auto &nb = this->waveform_queue_.front();
556 NextionComponentBase *
component = nb->component;
557 ESP_LOGW(TAG,
"Invalid waveform ID %d/ch %d",
component->get_component_id(),
559 ESP_LOGN(TAG,
"Remove waveform ID %d/ch %d",
component->get_component_id(),
component->get_wave_channel_id());
561 this->waveform_queue_.pop();
564 ESP_LOGW(TAG,
"Waveform ID/ch error but waveform not enabled");
568 ESP_LOGW(TAG,
"Invalid variable name");
572 ESP_LOGW(TAG,
"Invalid variable operation");
576 ESP_LOGW(TAG,
"Variable assign failed");
580 ESP_LOGW(TAG,
"EEPROM operation failed");
583 ESP_LOGW(TAG,
"Invalid parameter count");
587 ESP_LOGW(TAG,
"Invalid component I/O");
590 ESP_LOGW(TAG,
"Undefined escape chars");
594 ESP_LOGW(TAG,
"Variable name too long");
599 ESP_LOGE(TAG,
"Serial buffer overflow");
603 if (to_process_length != 3) {
604 ESP_LOGW(TAG,
"Incorrect touch len: %zu (need 3)", to_process_length);
608 uint8_t page_id = to_process[0];
609 uint8_t component_id = to_process[1];
610 uint8_t touch_event = to_process[2];
611 ESP_LOGV(TAG,
"Touch %s: page %u comp %u", touch_event ? LOG_STR_LITERAL(
"PRESS") : LOG_STR_LITERAL(
"RELEASE"),
612 page_id, component_id);
613 for (
auto *touch : this->
touch_) {
614 touch->process_touch(page_id, component_id, touch_event != 0);
621 if (to_process_length != 1) {
622 ESP_LOGW(TAG,
"Page event: expect 1, got %zu", to_process_length);
626 uint8_t page_id = to_process[0];
627 ESP_LOGV(TAG,
"New page: %u", page_id);
636 if (to_process_length != 5) {
637 ESP_LOGW(TAG,
"Touch coordinate: expect 5, got %zu", to_process_length);
638 ESP_LOGW(TAG,
"%s", to_process.c_str());
642 const uint16_t
x = (uint16_t(to_process[0]) << 8) | to_process[1];
643 const uint16_t
y = (uint16_t(to_process[2]) << 8) | to_process[3];
644 const uint8_t touch_event = to_process[4];
645 ESP_LOGV(TAG,
"Touch %s at %u,%u", touch_event ? LOG_STR_LITERAL(
"PRESS") : LOG_STR_LITERAL(
"RELEASE"),
x,
y);
655 if (this->nextion_queue_.empty()) {
656 ESP_LOGW(TAG,
"String return but queue is empty");
660 NextionQueue *nb = this->nextion_queue_.front();
661 if (!nb || !nb->component) {
662 ESP_LOGE(TAG,
"Invalid queue entry");
666 this->nextion_queue_.pop_front();
669 NextionComponentBase *
component = nb->component;
672 ESP_LOGE(TAG,
"String return but '%s' not text sensor",
component->get_variable_name().c_str());
674 ESP_LOGN(TAG,
"String resp: '%s' id: %s type: %s", to_process.c_str(),
component->get_variable_name().c_str(),
676 component->set_state_from_string(to_process,
true,
false);
680 this->nextion_queue_.pop_front();
691 if (this->nextion_queue_.empty()) {
692 ESP_LOGE(TAG,
"Numeric return but queue empty");
696 if (to_process_length < 4) {
697 ESP_LOGE(TAG,
"Numeric return but insufficient data (need 4, got %zu)", to_process_length);
701 int value =
static_cast<int>(
encode_uint32(to_process[3], to_process[2], to_process[1], to_process[0]));
703 NextionQueue *nb = this->nextion_queue_.front();
704 if (!nb || !nb->component) {
705 ESP_LOGE(TAG,
"Invalid queue");
709 this->nextion_queue_.pop_front();
712 NextionComponentBase *
component = nb->component;
717 ESP_LOGE(TAG,
"Numeric return but '%s' invalid type %d",
component->get_variable_name().c_str(),
720 ESP_LOGN(TAG,
"Numeric: %s type %d:%s val %d",
component->get_variable_name().c_str(),
722 component->set_state_from_int(value,
true,
false);
726 this->nextion_queue_.pop_front();
732 ESP_LOGVV(TAG,
"Auto sleep");
739 ESP_LOGVV(TAG,
"Auto wake");
747 ESP_LOGV(TAG,
"System start: %zu", to_process_length);
761 std::string variable_name;
764 auto index = to_process.find(
'\0');
765 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
766 ESP_LOGE(TAG,
"Bad switch data (0x90)");
767 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
771 variable_name = to_process.substr(0, index);
774 ESP_LOGN(TAG,
"Switch %s: %s", ONOFF(to_process[index] != 0), variable_name.c_str());
776#ifdef USE_NEXTION_TRIGGER_CUSTOM_SWITCH
780 for (
auto *switchtype : this->switchtype_) {
781 switchtype->process_bool(variable_name, to_process[index] != 0);
792 std::string variable_name;
794 auto index = to_process.find(
'\0');
795 if (index == std::string::npos || (to_process_length - index - 1) != 4) {
796 ESP_LOGE(TAG,
"Bad sensor data (0x91)");
797 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
801 index = to_process.find(
'\0');
802 variable_name = to_process.substr(0, index);
804 int value =
static_cast<int>(
805 encode_uint32(to_process[index + 4], to_process[index + 3], to_process[index + 2], to_process[index + 1]));
807 ESP_LOGN(TAG,
"Sensor: %s=%d", variable_name.c_str(), value);
809#ifdef USE_NEXTION_TRIGGER_CUSTOM_SENSOR
813 for (
auto *sensor : this->sensortype_) {
814 sensor->process_sensor(variable_name, value);
827 std::string variable_name;
828 std::string text_value;
831 auto index = to_process.find(
'\0');
832 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
833 ESP_LOGE(TAG,
"Bad text data (0x92)");
834 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
838 variable_name = to_process.substr(0, index);
842 text_value = to_process.substr(index, to_process_length - index - 1);
844 ESP_LOGN(TAG,
"Text sensor: %s='%s'", variable_name.c_str(), text_value.c_str());
851#ifdef USE_NEXTION_TRIGGER_CUSTOM_TEXT_SENSOR
855 for (
auto *textsensortype : this->textsensortype_) {
856 textsensortype->process_text(variable_name, text_value);
867 std::string variable_name;
870 auto index = to_process.find(
'\0');
871 if (index == std::string::npos || (to_process_length - index - 1) < 1) {
872 ESP_LOGE(TAG,
"Bad binary data (0x93)");
873 ESP_LOGN(TAG,
"proc: %s %zu %zu", to_process.c_str(), to_process_length, index);
877 variable_name = to_process.substr(0, index);
880 ESP_LOGN(TAG,
"Binary sensor: %s=%s", variable_name.c_str(), ONOFF(to_process[index] != 0));
882#ifdef USE_NEXTION_TRIGGER_CUSTOM_BINARY_SENSOR
886 for (
auto *binarysensortype : this->binarysensortype_) {
887 binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
892 ESP_LOGVV(TAG,
"Data transmit done");
893#ifdef USE_NEXTION_WAVEFORM
899 ESP_LOGVV(TAG,
"Ready for transmit");
900#ifdef USE_NEXTION_WAVEFORM
901 if (this->waveform_queue_.empty()) {
902 ESP_LOGE(TAG,
"No waveforms queued");
905 auto &nb = this->waveform_queue_.front();
907 size_t buffer_to_send =
component->get_wave_buffer_size() < 255 ?
component->get_wave_buffer_size() : 255;
908 this->
write_array(component->get_wave_buffer().data(),
static_cast<int>(buffer_to_send));
909 ESP_LOGN(TAG,
"Send waveform: component id %d, waveform id %d, size %zu",
component->get_component_id(),
910 component->get_wave_channel_id(), buffer_to_send);
911 component->clear_wave_buffer(buffer_to_send);
913 this->waveform_queue_.pop();
915 ESP_LOGW(TAG,
"Waveform transmit ready but waveform not enabled");
920 ESP_LOGW(TAG,
"Unknown event: 0x%02X", nextion_event);
924 this->
command_data_.erase(0, to_process_length + DELIMITER_SIZE + 1);
927 ESP_LOGN(TAG,
"Loop end");
934 if (this->
max_q_age_ms_ > 0 && !this->nextion_queue_.empty() &&
935 ms - this->nextion_queue_.front()->queue_time > this->max_q_age_ms_) {
936 for (
auto it = this->nextion_queue_.begin(); it != this->nextion_queue_.end();) {
937 if (ms - (*it)->queue_time > this->max_q_age_ms_) {
938 NextionComponentBase *
component = (*it)->component;
939 ESP_LOGV(TAG,
"Remove old queue '%s':'%s'",
component->get_queue_type_string(),
946 it = this->nextion_queue_.erase(it);
960 ESP_LOGN(TAG,
"State: %s=%lf (type %d)",
name.c_str(),
state, queue_type);
962 switch (queue_type) {
964 for (
auto *sensor : this->sensortype_) {
965 if (name == sensor->get_variable_name()) {
966 sensor->set_state(
state,
true,
true);
973 for (
auto *sensor : this->binarysensortype_) {
974 if (name == sensor->get_variable_name()) {
975 sensor->set_state(
state != 0,
true,
true);
982 for (
auto *sensor : this->switchtype_) {
983 if (name == sensor->get_variable_name()) {
984 sensor->set_state(
state != 0,
true,
true);
991 ESP_LOGW(TAG,
"set_sensor_state: bad type %d", queue_type);
997 ESP_LOGV(TAG,
"State: %s='%s'",
name.c_str(),
state.c_str());
999 for (
auto *sensor : this->textsensortype_) {
1000 if (name == sensor->get_variable_name()) {
1001 sensor->set_state(
state,
true,
true);
1008 ESP_LOGV(TAG,
"Send states");
1009 for (
auto *binarysensortype : this->binarysensortype_) {
1010 if (force_update || binarysensortype->get_needs_to_send_update())
1011 binarysensortype->send_state_to_nextion();
1013 for (
auto *sensortype : this->sensortype_) {
1014#ifdef USE_NEXTION_WAVEFORM
1015 if ((force_update || sensortype->get_needs_to_send_update()) && sensortype->get_wave_channel_id() == UINT8_MAX) {
1017 if (force_update || sensortype->get_needs_to_send_update()) {
1019 sensortype->send_state_to_nextion();
1022 for (
auto *switchtype : this->switchtype_) {
1023 if (force_update || switchtype->get_needs_to_send_update())
1024 switchtype->send_state_to_nextion();
1026 for (
auto *textsensortype : this->textsensortype_) {
1027 if (force_update || textsensortype->get_needs_to_send_update())
1028 textsensortype->send_state_to_nextion();
1033 for (
auto *binarysensortype : this->binarysensortype_) {
1034 if (binarysensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1035 binarysensortype->update_component_settings(
true);
1037 for (
auto *sensortype : this->sensortype_) {
1038 if (sensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1039 sensortype->update_component_settings(
true);
1041 for (
auto *switchtype : this->switchtype_) {
1042 if (switchtype->get_variable_name().find(prefix, 0) != std::string::npos)
1043 switchtype->update_component_settings(
true);
1045 for (
auto *textsensortype : this->textsensortype_) {
1046 if (textsensortype->get_variable_name().find(prefix, 0) != std::string::npos)
1047 textsensortype->update_component_settings(
true);
1053 uint8_t nr_of_ff_bytes = 0;
1054 bool exit_flag =
false;
1055 bool ff_flag =
false;
1059 while ((timeout == 0 && this->
available()) ||
millis() - start <= timeout) {
1074 if (nr_of_ff_bytes >= 3)
1077 response += (char) c;
1079 if (response.find(0x05) != std::string::npos) {
1086 if (exit_flag || ff_flag) {
1092 response = response.substr(0, response.length() - 3);
1094 return response.length();
1099#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1100 if (this->
max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1101 ESP_LOGW(TAG,
"Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
1106 auto *nextion_queue = RAMAllocator<nextion::NextionQueue>().allocate(1);
1107 if (nextion_queue ==
nullptr) {
1108 ESP_LOGW(TAG,
"Queue alloc failed");
1111 new (nextion_queue) nextion::NextionQueue;
1113 nextion_queue->
component = RAMAllocator<nextion::NextionComponentBase>().allocate(1);
1114 if (nextion_queue->component ==
nullptr) {
1115 ESP_LOGW(TAG,
"Component alloc failed");
1119 new (nextion_queue->component) nextion::NextionComponentBase;
1122 return nextion_queue;
1137 if (nextion_queue ==
nullptr)
1139 this->nextion_queue_.push_back(nextion_queue);
1140 ESP_LOGN(TAG,
"Queue NORESULT: %s", variable_name.c_str());
1163#ifdef USE_NEXTION_COMMAND_SPACING
1171#ifdef USE_NEXTION_COMMAND_SPACING
1173 const std::string &command) {
1175 if (nextion_queue ==
nullptr)
1177 nextion_queue->pending_command = command;
1178 this->nextion_queue_.push_back(nextion_queue);
1179 ESP_LOGVV(TAG,
"Queue with pending command: %s", variable_name.c_str());
1191 int ret = vsnprintf(buffer,
sizeof(buffer),
format, arg);
1194 ESP_LOGW(TAG,
"Bad cmd format: '%s'",
format);
1216 int ret = vsnprintf(buffer,
sizeof(buffer),
format, arg);
1219 ESP_LOGW(TAG,
"Bad cmd format: '%s'",
format);
1242 const std::string &variable_name_to_send, int32_t state_value) {
1247 const std::string &variable_name_to_send, int32_t state_value,
1248 bool is_sleep_safe) {
1269 const std::string &variable_name_to_send,
1270 const std::string &state_value) {
1275 const std::string &variable_name_to_send,
1276 const std::string &state_value,
bool is_sleep_safe) {
1281 state_value.c_str());
1297#ifdef USE_NEXTION_MAX_QUEUE_SIZE
1298 if (this->
max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
1299 ESP_LOGW(TAG,
"Queue full (%zu), drop GET: %s", this->nextion_queue_.size(),
1300 component->get_variable_name().c_str());
1305 RAMAllocator<nextion::NextionQueue> allocator;
1306 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1307 if (nextion_queue ==
nullptr) {
1308 ESP_LOGW(TAG,
"Queue alloc failed");
1311 new (nextion_queue) nextion::NextionQueue;
1316 ESP_LOGN(TAG,
"Queue %s: %s",
component->get_queue_type_string(),
component->get_variable_name().c_str());
1318 std::string command =
"get " +
component->get_variable_name_to_send();
1320#ifdef USE_NEXTION_COMMAND_SPACING
1324 nextion_queue->pending_command = command;
1325 this->nextion_queue_.push_back(nextion_queue);
1327 nextion_queue->pending_command.clear();
1331 this->nextion_queue_.push_back(nextion_queue);
1338#ifdef USE_NEXTION_WAVEFORM
1348 RAMAllocator<nextion::NextionQueue> allocator;
1349 nextion::NextionQueue *nextion_queue = allocator.allocate(1);
1350 if (nextion_queue ==
nullptr) {
1351 ESP_LOGW(TAG,
"Queue alloc failed");
1354 new (nextion_queue) nextion::NextionQueue;
1359 if (!this->waveform_queue_.push(nextion_queue)) {
1360 ESP_LOGW(TAG,
"Waveform queue full, drop");
1364 if (this->waveform_queue_.size() == 1)
1369 if (this->waveform_queue_.empty())
1372 auto *nb = this->waveform_queue_.front();
1374 size_t buffer_to_send =
component->get_wave_buffer_size() < 255 ?
component->get_wave_buffer_size() : 255;
1377 buf_append_printf(command,
sizeof(command), 0,
"addt %u,%u,%zu",
component->get_component_id(),
1378 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.
void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send="")
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.
NextionQueue * make_no_result_entry_(const std::string &variable_name)
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_
void release_queue_entry_(NextionQueue *nb)
bool is_updating() override
Check if the TFT update process is currently running.
uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag)
void goto_page(const char *page)
Show the page with a given name.
bool void add_no_result_to_queue_with_set_internal_(const std::string &variable_name, const std::string &variable_name_to_send, int32_t state_value, bool is_sleep_safe=false)
bool void add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command)
CallbackManager< void(StringRef, bool)> custom_binary_sensor_callback_
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_
struct esphome::nextion::Nextion::@148 connection_state_
Status flags for Nextion display state management.
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.
NextionComponentBase * component
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
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.