[nextion] Set alternative TFT update baud rate (#6587)

This commit is contained in:
Edward Firmo 2024-04-30 11:29:57 +02:00 committed by GitHub
parent 05fbb260ee
commit 74fd52e05f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 64 additions and 6 deletions

View file

@ -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;

View file

@ -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<uint32_t> 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) {

View file

@ -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<uint32_t> 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