Nextion on_touch trigger (#5833)

This commit is contained in:
Edward Firmo 2023-11-28 05:14:59 +01:00 committed by GitHub
parent a8bc5ef46f
commit d1be686c54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View File

@ -33,5 +33,14 @@ class PageTrigger : public Trigger<uint8_t> {
}
};
class TouchTrigger : public Trigger<uint8_t, uint8_t, bool> {
public:
explicit TouchTrigger(Nextion *nextion) {
nextion->add_touch_event_callback([this](uint8_t page_id, uint8_t component_id, bool touch_event) {
this->trigger(page_id, component_id, touch_event);
});
}
};
} // namespace nextion
} // namespace esphome

View File

@ -7,6 +7,7 @@ from esphome.const import (
CONF_LAMBDA,
CONF_BRIGHTNESS,
CONF_TRIGGER_ID,
CONF_ON_TOUCH,
)
from esphome.core import CORE
from . import Nextion, nextion_ns, nextion_ref
@ -31,6 +32,7 @@ SetupTrigger = nextion_ns.class_("SetupTrigger", automation.Trigger.template())
SleepTrigger = nextion_ns.class_("SleepTrigger", automation.Trigger.template())
WakeTrigger = nextion_ns.class_("WakeTrigger", automation.Trigger.template())
PageTrigger = nextion_ns.class_("PageTrigger", automation.Trigger.template())
TouchTrigger = nextion_ns.class_("TouchTrigger", automation.Trigger.template())
CONFIG_SCHEMA = (
display.BASIC_DISPLAY_SCHEMA.extend(
@ -58,6 +60,11 @@ CONFIG_SCHEMA = (
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PageTrigger),
}
),
cv.Optional(CONF_ON_TOUCH): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(TouchTrigger),
}
),
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,
@ -119,3 +126,15 @@ async def to_code(config):
for conf in config.get(CONF_ON_PAGE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(cg.uint8, "x")], conf)
for conf in config.get(CONF_ON_TOUCH, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger,
[
(cg.uint8, "page_id"),
(cg.uint8, "component_id"),
(cg.bool_, "touch_event"),
],
conf,
)

View File

@ -167,6 +167,10 @@ void Nextion::add_new_page_callback(std::function<void(uint8_t)> &&callback) {
this->page_callback_.add(std::move(callback));
}
void Nextion::add_touch_event_callback(std::function<void(uint8_t, uint8_t, bool)> &&callback) {
this->touch_callback_.add(std::move(callback));
}
void Nextion::update_all_components() {
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
return;
@ -440,6 +444,7 @@ void Nextion::process_nextion_commands_() {
for (auto *touch : this->touch_) {
touch->process_touch(page_id, component_id, touch_event != 0);
}
this->touch_callback_.call(page_id, component_id, touch_event != 0);
break;
}
case 0x66: { // Nextion initiated new page event return data.

View File

@ -732,6 +732,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*/
void add_new_page_callback(std::function<void(uint8_t)> &&callback);
/** Add a callback to be notified when Nextion has a touch event.
*
* @param callback The void() callback.
*/
void add_touch_event_callback(std::function<void(uint8_t, uint8_t, bool)> &&callback);
void update_all_components();
/**
@ -885,6 +891,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
CallbackManager<void()> sleep_callback_{};
CallbackManager<void()> wake_callback_{};
CallbackManager<void(uint8_t)> page_callback_{};
CallbackManager<void(uint8_t, uint8_t, bool)> touch_callback_{};
optional<nextion_writer_t> writer_;
float brightness_{1.0};