From 74fd52e05fdc6f63ef64ada77f8a7b8d63de8c45 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:29:57 +0200 Subject: [PATCH] [nextion] Set alternative TFT update baud rate (#6587) --- esphome/components/nextion/nextion.h | 16 +++++++++-- .../nextion/nextion_upload_arduino.cpp | 27 +++++++++++++++++-- .../components/nextion/nextion_upload_idf.cpp | 27 +++++++++++++++++-- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index 833db675fc..b6a6204cd4 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -992,14 +992,26 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe #endif /** - * Upload the tft file and soft reset Nextion + * @brief Uploads the TFT file to the Nextion display. + * + * This function initiates the upload of a TFT file to the Nextion display. Users can specify a target baud rate for + * the transfer. If the provided baud rate is not supported by Nextion, the function defaults to using the current + * baud rate set for the display. If no baud rate is specified (or if 0 is passed), the current baud rate is used. + * + * Supported baud rates are: 2400, 4800, 9600, 19200, 31250, 38400, 57600, 115200, 230400, 250000, 256000, 512000 + * and 921600. Selecting a baud rate supported by both the Nextion display and the host hardware is essential for + * ensuring a successful upload process. + * + * @param baud_rate The desired baud rate for the TFT file transfer, specified as an unsigned 32-bit integer. + * If the specified baud rate is not supported, or if 0 is passed, the function will use the current baud rate. + * The default value is 0, which implies using the current baud rate. * @param exit_reparse If true, the function exits reparse mode before uploading the TFT file. This parameter * defaults to true, ensuring that the display is ready to receive and apply the new TFT file without needing * to manually reset or reconfigure. Exiting reparse mode is recommended for most upload scenarios to ensure * the display properly processes the uploaded file command. * @return bool True: Transfer completed successfuly, False: Transfer failed. */ - bool upload_tft(bool exit_reparse = true); + bool upload_tft(uint32_t baud_rate = 0, bool exit_reparse = true); void dump_config() override; diff --git a/esphome/components/nextion/nextion_upload_arduino.cpp b/esphome/components/nextion/nextion_upload_arduino.cpp index 4f8df67006..6e7c8b9563 100644 --- a/esphome/components/nextion/nextion_upload_arduino.cpp +++ b/esphome/components/nextion/nextion_upload_arduino.cpp @@ -141,7 +141,7 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) { return range_end + 1; } -bool Nextion::upload_tft(bool exit_reparse) { +bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { ESP_LOGD(TAG, "Nextion TFT upload requested"); ESP_LOGD(TAG, "Exit reparse: %s", YESNO(exit_reparse)); ESP_LOGD(TAG, "URL: %s", this->tft_url_.c_str()); @@ -166,6 +166,15 @@ bool Nextion::upload_tft(bool exit_reparse) { } } + // Check if baud rate is supported + this->original_baud_rate_ = this->parent_->get_baud_rate(); + static const std::vector SUPPORTED_BAUD_RATES = {2400, 4800, 9600, 19200, 31250, 38400, 57600, + 115200, 230400, 250000, 256000, 512000, 921600}; + if (std::find(SUPPORTED_BAUD_RATES.begin(), SUPPORTED_BAUD_RATES.end(), baud_rate) == SUPPORTED_BAUD_RATES.end()) { + baud_rate = this->original_baud_rate_; + } + ESP_LOGD(TAG, "Baud rate: %" PRIu32, baud_rate); + HTTPClient http; http.setTimeout(15000); // Yes 15 seconds.... Helps 8266s along bool begin_status = false; @@ -244,7 +253,7 @@ bool Nextion::upload_tft(bool exit_reparse) { // Tells the Nextion the content length of the tft file and baud rate it will be sent at // Once the Nextion accepts the command it will wait until the file is successfully uploaded // If it fails for any reason a power cycle of the display will be needed - sprintf(command, "whmi-wris %d,%d,1", this->content_length_, this->parent_->get_baud_rate()); + sprintf(command, "whmi-wris %d,%d,1", this->content_length_, baud_rate); // Clear serial receive buffer uint8_t d; @@ -254,6 +263,12 @@ bool Nextion::upload_tft(bool exit_reparse) { this->send_command_(command); + if (baud_rate != this->original_baud_rate_) { + ESP_LOGD(TAG, "Changing baud rate from %" PRIu32 " to %" PRIu32 " bps", this->original_baud_rate_, baud_rate); + this->parent_->set_baud_rate(baud_rate); + this->parent_->load_settings(); + } + App.feed_wdt(); std::string response; @@ -335,6 +350,14 @@ bool Nextion::upload_tft(bool exit_reparse) { bool Nextion::upload_end_(bool successful) { this->is_updating_ = false; + + uint32_t baud_rate = this->parent_->get_baud_rate(); + if (baud_rate != this->original_baud_rate_) { + ESP_LOGD(TAG, "Changing baud rate back from %" PRIu32 " to %" PRIu32 " bps", baud_rate, this->original_baud_rate_); + this->parent_->set_baud_rate(this->original_baud_rate_); + this->parent_->load_settings(); + } + ESP_LOGD(TAG, "Restarting Nextion"); this->soft_reset(); if (successful) { diff --git a/esphome/components/nextion/nextion_upload_idf.cpp b/esphome/components/nextion/nextion_upload_idf.cpp index d08f970551..163b497d3b 100644 --- a/esphome/components/nextion/nextion_upload_idf.cpp +++ b/esphome/components/nextion/nextion_upload_idf.cpp @@ -160,7 +160,7 @@ int Nextion::upload_range(const std::string &url, int range_start) { return range_end + 1; } -bool Nextion::upload_tft(bool exit_reparse) { +bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) { ESP_LOGD(TAG, "Nextion TFT upload requested"); ESP_LOGD(TAG, "Exit reparse: %s", YESNO(exit_reparse)); ESP_LOGD(TAG, "url: %s", this->tft_url_.c_str()); @@ -185,6 +185,15 @@ bool Nextion::upload_tft(bool exit_reparse) { } } + // Check if baud rate is supported + this->original_baud_rate_ = this->parent_->get_baud_rate(); + static const std::vector SUPPORTED_BAUD_RATES = {2400, 4800, 9600, 19200, 31250, 38400, 57600, + 115200, 230400, 250000, 256000, 512000, 921600}; + if (std::find(SUPPORTED_BAUD_RATES.begin(), SUPPORTED_BAUD_RATES.end(), baud_rate) == SUPPORTED_BAUD_RATES.end()) { + baud_rate = this->original_baud_rate_; + } + ESP_LOGD(TAG, "Baud rate: %" PRIu32, baud_rate); + // Define the configuration for the HTTP client ESP_LOGV(TAG, "Establishing connection to HTTP server"); ESP_LOGVV(TAG, "Available heap: %" PRIu32, esp_get_free_heap_size()); @@ -254,7 +263,7 @@ bool Nextion::upload_tft(bool exit_reparse) { // Tells the Nextion the content length of the tft file and baud rate it will be sent at // Once the Nextion accepts the command it will wait until the file is successfully uploaded // If it fails for any reason a power cycle of the display will be needed - sprintf(command, "whmi-wris %d,%" PRIu32 ",1", this->content_length_, this->parent_->get_baud_rate()); + sprintf(command, "whmi-wris %d,%" PRIu32 ",1", this->content_length_, baud_rate); // Clear serial receive buffer ESP_LOGV(TAG, "Clear serial receive buffer"); @@ -268,6 +277,12 @@ bool Nextion::upload_tft(bool exit_reparse) { ESP_LOGVV(TAG, "Available heap: %" PRIu32, esp_get_free_heap_size()); this->send_command_(command); + if (baud_rate != this->original_baud_rate_) { + ESP_LOGD(TAG, "Changing baud rate from %" PRIu32 " to %" PRIu32 " bps", this->original_baud_rate_, baud_rate); + this->parent_->set_baud_rate(baud_rate); + this->parent_->load_settings(); + } + std::string response; ESP_LOGV(TAG, "Waiting for upgrade response"); this->recv_ret_string_(response, 5000, true); // This can take some time to return @@ -308,6 +323,14 @@ bool Nextion::upload_tft(bool exit_reparse) { bool Nextion::upload_end(bool successful) { this->is_updating_ = false; + + uint32_t baud_rate = this->parent_->get_baud_rate(); + if (baud_rate != this->original_baud_rate_) { + ESP_LOGD(TAG, "Changing baud rate back from %" PRIu32 " to %" PRIu32 " bps", baud_rate, this->original_baud_rate_); + this->parent_->set_baud_rate(this->original_baud_rate_); + this->parent_->load_settings(); + } + ESP_LOGD(TAG, "Restarting Nextion"); this->soft_reset(); vTaskDelay(pdMS_TO_TICKS(1500)); // NOLINT