Remove page jump on Nextion startup (#5673)

This commit is contained in:
Edward Firmo 2023-11-07 01:48:15 +01:00 committed by GitHub
parent defe8ac97b
commit a8a9c6192d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 1 deletions

View file

@ -21,6 +21,7 @@ CONF_ON_SETUP = "on_setup"
CONF_ON_PAGE = "on_page"
CONF_TOUCH_SLEEP_TIMEOUT = "touch_sleep_timeout"
CONF_WAKE_UP_PAGE = "wake_up_page"
CONF_START_UP_PAGE = "start_up_page"
CONF_AUTO_WAKE_ON_TOUCH = "auto_wake_on_touch"
CONF_WAVE_MAX_LENGTH = "wave_max_length"
CONF_BACKGROUND_COLOR = "background_color"

View file

@ -18,6 +18,7 @@ from .base_component import (
CONF_TFT_URL,
CONF_TOUCH_SLEEP_TIMEOUT,
CONF_WAKE_UP_PAGE,
CONF_START_UP_PAGE,
CONF_AUTO_WAKE_ON_TOUCH,
)
@ -59,6 +60,7 @@ CONFIG_SCHEMA = (
),
cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.int_range(min=3, max=65535),
cv.Optional(CONF_WAKE_UP_PAGE): cv.positive_int,
cv.Optional(CONF_START_UP_PAGE): cv.positive_int,
cv.Optional(CONF_AUTO_WAKE_ON_TOUCH, default=True): cv.boolean,
}
)
@ -95,6 +97,9 @@ async def to_code(config):
if CONF_WAKE_UP_PAGE in config:
cg.add(var.set_wake_up_page_internal(config[CONF_WAKE_UP_PAGE]))
if CONF_START_UP_PAGE in config:
cg.add(var.set_start_up_page_internal(config[CONF_START_UP_PAGE]))
if CONF_AUTO_WAKE_ON_TOUCH in config:
cg.add(var.set_auto_wake_on_touch_internal(config[CONF_AUTO_WAKE_ON_TOUCH]))

View file

@ -134,6 +134,10 @@ void Nextion::dump_config() {
if (this->wake_up_page_ != -1) {
ESP_LOGCONFIG(TAG, " Wake Up Page : %d", this->wake_up_page_);
}
if (this->start_up_page_ != -1) {
ESP_LOGCONFIG(TAG, " Start Up Page : %d", this->start_up_page_);
}
}
float Nextion::get_setup_priority() const { return setup_priority::DATA; }
@ -231,7 +235,11 @@ void Nextion::loop() {
this->send_command_("bkcmd=3"); // Always, returns 0x00 to 0x23 result of serial command.
this->set_backlight_brightness(this->brightness_);
this->goto_page("0");
// Check if a startup page has been set and send the command
if (this->start_up_page_ != -1) {
this->goto_page(this->start_up_page_);
}
this->set_auto_wake_on_touch(this->auto_wake_on_touch_);

View file

@ -334,6 +334,18 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* Switches to the page named `main`. Pages are named in the Nextion Editor.
*/
void goto_page(const char *page);
/**
* Show the page with a given id.
* @param page The id of the page.
*
* Example:
* ```cpp
* it.goto_page(2);
* ```
*
* Switches to the page named `main`. Pages are named in the Nextion Editor.
*/
void goto_page(uint8_t page);
/**
* Hide a component.
* @param component The component name.
@ -605,6 +617,20 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* The display will wake up to page 2.
*/
void set_wake_up_page(uint8_t page_id = 255);
/**
* Sets which page Nextion loads when connecting to ESPHome.
* @param page_id The page id, from 0 to the lage page in Nextion. Set 255 (not set to any existing page) to
* wakes up to current page.
*
* Example:
* ```cpp
* it.set_start_up_page(2);
* ```
*
* The display will go to page 2 when it establishes a connection to ESPHome.
*/
void set_start_up_page(uint8_t page_id = 255);
/**
* Sets if Nextion should auto-wake from sleep when touch press occurs.
* @param auto_wake True or false. When auto_wake is true and Nextion is in sleep mode,
@ -736,6 +762,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
this->touch_sleep_timeout_ = touch_sleep_timeout;
}
void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
void set_start_up_page_internal(uint8_t start_up_page) { this->start_up_page_ = start_up_page; }
void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
protected:
@ -758,6 +785,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
bool is_updating_ = false;
uint32_t touch_sleep_timeout_ = 0;
int wake_up_page_ = -1;
int start_up_page_ = -1;
bool auto_wake_on_touch_ = true;
/**

View file

@ -13,6 +13,8 @@ void Nextion::set_wake_up_page(uint8_t page_id) {
this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", page_id, true);
}
void Nextion::set_start_up_page(uint8_t page_id) { this->start_up_page_ = page_id; }
void Nextion::set_touch_sleep_timeout(uint16_t timeout) {
if (timeout < 3) {
ESP_LOGD(TAG, "Sleep timeout out of bounds, range 3-65535");
@ -124,6 +126,7 @@ void Nextion::set_component_text_printf(const char *component, const char *forma
// General Nextion
void Nextion::goto_page(const char *page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %s", page); }
void Nextion::goto_page(uint8_t page) { this->add_no_result_to_queue_with_printf_("goto_page", "page %i", page); }
void Nextion::set_backlight_brightness(float brightness) {
if (brightness < 0 || brightness > 1.0) {