mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 00:51:49 +00:00 
			
		
		
		
	[nextion] memory optimization (#9164)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		@@ -164,7 +164,7 @@ void Nextion::dump_config() {
 | 
			
		||||
#endif  // USE_NEXTION_MAX_COMMANDS_PER_LOOP
 | 
			
		||||
 | 
			
		||||
  if (this->touch_sleep_timeout_ != 0) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Touch Timeout:  %" PRIu32, this->touch_sleep_timeout_);
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Touch Timeout:  %" PRIu16, this->touch_sleep_timeout_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->wake_up_page_ != -1) {
 | 
			
		||||
@@ -302,11 +302,11 @@ void Nextion::loop() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Check if a startup page has been set and send the command
 | 
			
		||||
    if (this->start_up_page_ != -1) {
 | 
			
		||||
    if (this->start_up_page_ >= 0) {
 | 
			
		||||
      this->goto_page(this->start_up_page_);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (this->wake_up_page_ != -1) {
 | 
			
		||||
    if (this->wake_up_page_ >= 0) {
 | 
			
		||||
      this->set_wake_up_page(this->wake_up_page_);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -418,12 +418,12 @@ void Nextion::process_nextion_commands_() {
 | 
			
		||||
      ESP_LOGN(TAG, "Add 0xFF");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this->nextion_event_ = this->command_data_[0];
 | 
			
		||||
    const uint8_t nextion_event = this->command_data_[0];
 | 
			
		||||
 | 
			
		||||
    to_process_length -= 1;
 | 
			
		||||
    to_process = this->command_data_.substr(1, to_process_length);
 | 
			
		||||
 | 
			
		||||
    switch (this->nextion_event_) {
 | 
			
		||||
    switch (nextion_event) {
 | 
			
		||||
      case 0x00:  // instruction sent by user has failed
 | 
			
		||||
        ESP_LOGW(TAG, "Invalid instruction");
 | 
			
		||||
        this->remove_from_q_();
 | 
			
		||||
@@ -562,9 +562,9 @@ void Nextion::process_nextion_commands_() {
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        uint16_t x = (uint16_t(to_process[0]) << 8) | to_process[1];
 | 
			
		||||
        uint16_t y = (uint16_t(to_process[2]) << 8) | to_process[3];
 | 
			
		||||
        uint8_t touch_event = to_process[4];  // 0 -> release, 1 -> press
 | 
			
		||||
        const uint16_t x = (uint16_t(to_process[0]) << 8) | to_process[1];
 | 
			
		||||
        const uint16_t y = (uint16_t(to_process[2]) << 8) | to_process[3];
 | 
			
		||||
        const uint8_t touch_event = to_process[4];  // 0 -> release, 1 -> press
 | 
			
		||||
        ESP_LOGD(TAG, "Touch %s at %u,%u", touch_event ? "PRESS" : "RELEASE", x, y);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -820,15 +820,14 @@ void Nextion::process_nextion_commands_() {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default:
 | 
			
		||||
        ESP_LOGW(TAG, "Unknown event: 0x%02X", this->nextion_event_);
 | 
			
		||||
        ESP_LOGW(TAG, "Unknown event: 0x%02X", nextion_event);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ESP_LOGN(TAG, "nextion_event_ deleting from 0 to %d", to_process_length + COMMAND_DELIMITER.length() + 1);
 | 
			
		||||
    this->command_data_.erase(0, to_process_length + COMMAND_DELIMITER.length() + 1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint32_t ms = App.get_loop_component_start_time();
 | 
			
		||||
  const uint32_t ms = App.get_loop_component_start_time();
 | 
			
		||||
 | 
			
		||||
  if (!this->nextion_queue_.empty() && this->nextion_queue_.front()->queue_time + this->max_q_age_ms_ < ms) {
 | 
			
		||||
    for (size_t i = 0; i < this->nextion_queue_.size(); i++) {
 | 
			
		||||
@@ -960,7 +959,6 @@ void Nextion::update_components_by_prefix(const std::string &prefix) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag) {
 | 
			
		||||
  uint16_t ret = 0;
 | 
			
		||||
  uint8_t c = 0;
 | 
			
		||||
  uint8_t nr_of_ff_bytes = 0;
 | 
			
		||||
  bool exit_flag = false;
 | 
			
		||||
@@ -1003,8 +1001,7 @@ uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool
 | 
			
		||||
  if (ff_flag)
 | 
			
		||||
    response = response.substr(0, response.length() - 3);  // Remove last 3 0xFF
 | 
			
		||||
 | 
			
		||||
  ret = response.length();
 | 
			
		||||
  return ret;
 | 
			
		||||
  return response.length();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -1190,11 +1190,11 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
 | 
			
		||||
   * After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up
 | 
			
		||||
   * `thup`.
 | 
			
		||||
   */
 | 
			
		||||
  void set_touch_sleep_timeout(uint32_t touch_sleep_timeout);
 | 
			
		||||
  void set_touch_sleep_timeout(uint16_t touch_sleep_timeout);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Sets which page Nextion loads when exiting sleep mode. Note this can be set even when Nextion is in sleep mode.
 | 
			
		||||
   * @param wake_up_page The page id, from 0 to the lage page in Nextion. Set 255 (not set to any existing page) to
 | 
			
		||||
   * @param wake_up_page The page id, from 0 to the last page in Nextion. Set -1 (not set to any existing page) to
 | 
			
		||||
   * wakes up to current page.
 | 
			
		||||
   *
 | 
			
		||||
   * Example:
 | 
			
		||||
@@ -1204,11 +1204,11 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
 | 
			
		||||
   *
 | 
			
		||||
   * The display will wake up to page 2.
 | 
			
		||||
   */
 | 
			
		||||
  void set_wake_up_page(uint8_t wake_up_page = 255);
 | 
			
		||||
  void set_wake_up_page(int16_t wake_up_page = -1);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Sets which page Nextion loads when connecting to ESPHome.
 | 
			
		||||
   * @param start_up_page The page id, from 0 to the lage page in Nextion. Set 255 (not set to any existing page) to
 | 
			
		||||
   * @param start_up_page The page id, from 0 to the last page in Nextion. Set -1 (not set to any existing page) to
 | 
			
		||||
   * wakes up to current page.
 | 
			
		||||
   *
 | 
			
		||||
   * Example:
 | 
			
		||||
@@ -1218,7 +1218,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
 | 
			
		||||
   *
 | 
			
		||||
   * The display will go to page 2 when it establishes a connection to ESPHome.
 | 
			
		||||
   */
 | 
			
		||||
  void set_start_up_page(uint8_t start_up_page = 255) { this->start_up_page_ = start_up_page; }
 | 
			
		||||
  void set_start_up_page(int16_t start_up_page = -1) { this->start_up_page_ = start_up_page; }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Sets if Nextion should auto-wake from sleep when touch press occurs.
 | 
			
		||||
@@ -1330,7 +1330,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
 | 
			
		||||
  std::deque<NextionQueue *> waveform_queue_;
 | 
			
		||||
  uint16_t recv_ret_string_(std::string &response, uint32_t timeout, bool recv_flag);
 | 
			
		||||
  void all_components_send_state_(bool force_update = false);
 | 
			
		||||
  uint64_t comok_sent_ = 0;
 | 
			
		||||
  uint32_t comok_sent_ = 0;
 | 
			
		||||
  bool remove_from_q_(bool report_empty = true);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@@ -1340,12 +1340,10 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
 | 
			
		||||
  bool ignore_is_setup_ = false;
 | 
			
		||||
 | 
			
		||||
  bool nextion_reports_is_setup_ = false;
 | 
			
		||||
  uint8_t nextion_event_;
 | 
			
		||||
 | 
			
		||||
  void process_nextion_commands_();
 | 
			
		||||
  void process_serial_();
 | 
			
		||||
  bool is_updating_ = false;
 | 
			
		||||
  uint32_t touch_sleep_timeout_ = 0;
 | 
			
		||||
  uint16_t touch_sleep_timeout_ = 0;
 | 
			
		||||
  int16_t wake_up_page_ = -1;
 | 
			
		||||
  int16_t start_up_page_ = -1;
 | 
			
		||||
  bool auto_wake_on_touch_ = true;
 | 
			
		||||
 
 | 
			
		||||
@@ -10,12 +10,12 @@ static const char *const TAG = "nextion";
 | 
			
		||||
// Sleep safe commands
 | 
			
		||||
void Nextion::soft_reset() { this->send_command_("rest"); }
 | 
			
		||||
 | 
			
		||||
void Nextion::set_wake_up_page(uint8_t wake_up_page) {
 | 
			
		||||
void Nextion::set_wake_up_page(int16_t wake_up_page) {
 | 
			
		||||
  this->wake_up_page_ = wake_up_page;
 | 
			
		||||
  this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", wake_up_page, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Nextion::set_touch_sleep_timeout(uint32_t touch_sleep_timeout) {
 | 
			
		||||
void Nextion::set_touch_sleep_timeout(uint16_t touch_sleep_timeout) {
 | 
			
		||||
  if (touch_sleep_timeout < 3) {
 | 
			
		||||
    ESP_LOGD(TAG, "Sleep timeout out of bounds (3-65535)");
 | 
			
		||||
    return;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user