[Touchscreen] Add expire of touch record. (#5986)

* Add expire of touch record.

* Implement suggested changes.

* Alternative implementation to detect touch release.

* add `cancel_timeout`.

* Add touch timeout as configurable element.

---------

Co-authored-by: Your Name <you@example.com>
This commit is contained in:
Fabian 2023-12-24 20:16:53 +01:00 committed by GitHub
parent 46fc37b691
commit 93ac765425
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 16 deletions

View file

@ -18,7 +18,7 @@ LilygoT547Touchscreen = lilygo_t5_47_ns.class_(
CONF_LILYGO_T5_47_TOUCHSCREEN_ID = "lilygo_t5_47_touchscreen_id" CONF_LILYGO_T5_47_TOUCHSCREEN_ID = "lilygo_t5_47_touchscreen_id"
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend( CONFIG_SCHEMA = touchscreen.touchscreen_schema("250ms").extend(
cv.Schema( cv.Schema(
{ {
cv.GenerateID(): cv.declare_id(LilygoT547Touchscreen), cv.GenerateID(): cv.declare_id(LilygoT547Touchscreen),

View file

@ -24,6 +24,7 @@ CONF_DISPLAY = "display"
CONF_TOUCHSCREEN_ID = "touchscreen_id" CONF_TOUCHSCREEN_ID = "touchscreen_id"
CONF_REPORT_INTERVAL = "report_interval" # not used yet: CONF_REPORT_INTERVAL = "report_interval" # not used yet:
CONF_ON_UPDATE = "on_update" CONF_ON_UPDATE = "on_update"
CONF_TOUCH_TIMEOUT = "touch_timeout"
CONF_MIRROR_X = "mirror_x" CONF_MIRROR_X = "mirror_x"
CONF_MIRROR_Y = "mirror_y" CONF_MIRROR_Y = "mirror_y"
@ -31,7 +32,8 @@ CONF_SWAP_XY = "swap_xy"
CONF_TRANSFORM = "transform" CONF_TRANSFORM = "transform"
TOUCHSCREEN_SCHEMA = cv.Schema( def touchscreen_schema(default_touch_timeout):
return cv.Schema(
{ {
cv.GenerateID(CONF_DISPLAY): cv.use_id(display.Display), cv.GenerateID(CONF_DISPLAY): cv.use_id(display.Display),
cv.Optional(CONF_TRANSFORM): cv.Schema( cv.Optional(CONF_TRANSFORM): cv.Schema(
@ -41,11 +43,18 @@ TOUCHSCREEN_SCHEMA = cv.Schema(
cv.Optional(CONF_MIRROR_Y, default=False): cv.boolean, cv.Optional(CONF_MIRROR_Y, default=False): cv.boolean,
} }
), ),
cv.Optional(CONF_TOUCH_TIMEOUT, default=default_touch_timeout): cv.All(
cv.positive_time_period_milliseconds,
cv.Range(max=cv.TimePeriod(milliseconds=65535)),
),
cv.Optional(CONF_ON_TOUCH): automation.validate_automation(single=True), cv.Optional(CONF_ON_TOUCH): automation.validate_automation(single=True),
cv.Optional(CONF_ON_UPDATE): automation.validate_automation(single=True), cv.Optional(CONF_ON_UPDATE): automation.validate_automation(single=True),
cv.Optional(CONF_ON_RELEASE): automation.validate_automation(single=True), cv.Optional(CONF_ON_RELEASE): automation.validate_automation(single=True),
} }
).extend(cv.polling_component_schema("50ms")) ).extend(cv.polling_component_schema("50ms"))
TOUCHSCREEN_SCHEMA = touchscreen_schema(cv.UNDEFINED)
async def register_touchscreen(var, config): async def register_touchscreen(var, config):
@ -54,6 +63,9 @@ async def register_touchscreen(var, config):
disp = await cg.get_variable(config[CONF_DISPLAY]) disp = await cg.get_variable(config[CONF_DISPLAY])
cg.add(var.set_display(disp)) cg.add(var.set_display(disp))
if CONF_TOUCH_TIMEOUT in config:
cg.add(var.set_touch_timeout(config[CONF_TOUCH_TIMEOUT]))
if CONF_TRANSFORM in config: if CONF_TRANSFORM in config:
transform = config[CONF_TRANSFORM] transform = config[CONF_TRANSFORM]
cg.add(var.set_swap_xy(transform[CONF_SWAP_XY])) cg.add(var.set_swap_xy(transform[CONF_SWAP_XY]))

View file

@ -47,6 +47,11 @@ void Touchscreen::loop() {
} else { } else {
this->store_.touched = false; this->store_.touched = false;
this->defer([this]() { this->send_touches_(); }); this->defer([this]() { this->send_touches_(); });
if (this->touch_timeout_ > 0) {
// Simulate a touch after <this->touch_timeout_> ms. This will reset any existing timeout operation.
// This is to detect touch release.
this->set_timeout(TAG, this->touch_timeout_, [this]() { this->store_.touched = true; });
}
} }
} }
} }
@ -90,6 +95,9 @@ void Touchscreen::add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_r
void Touchscreen::send_touches_() { void Touchscreen::send_touches_() {
if (!this->is_touched_) { if (!this->is_touched_) {
if (this->touch_timeout_ > 0) {
this->cancel_timeout(TAG);
}
this->release_trigger_.trigger(); this->release_trigger_.trigger();
for (auto *listener : this->touch_listeners_) for (auto *listener : this->touch_listeners_)
listener->release(); listener->release();

View file

@ -46,6 +46,7 @@ class Touchscreen : public PollingComponent {
void set_display(display::Display *display) { this->display_ = display; } void set_display(display::Display *display) { this->display_ = display; }
display::Display *get_display() const { return this->display_; } display::Display *get_display() const { return this->display_; }
void set_touch_timeout(uint16_t val) { this->touch_timeout_ = val; }
void set_mirror_x(bool invert_x) { this->invert_x_ = invert_x; } void set_mirror_x(bool invert_x) { this->invert_x_ = invert_x; }
void set_mirror_y(bool invert_y) { this->invert_y_ = invert_y; } void set_mirror_y(bool invert_y) { this->invert_y_ = invert_y; }
void set_swap_xy(bool swap) { this->swap_x_y_ = swap; } void set_swap_xy(bool swap) { this->swap_x_y_ = swap; }
@ -100,6 +101,7 @@ class Touchscreen : public PollingComponent {
display::Display *display_{nullptr}; display::Display *display_{nullptr};
int16_t x_raw_min_{0}, x_raw_max_{0}, y_raw_min_{0}, y_raw_max_{0}; int16_t x_raw_min_{0}, x_raw_max_{0}, y_raw_min_{0}, y_raw_max_{0};
uint16_t touch_timeout_{0};
bool invert_x_{false}, invert_y_{false}, swap_x_y_{false}; bool invert_x_{false}, invert_y_{false}, swap_x_y_{false};
Trigger<TouchPoint, const TouchPoints_t &> touch_trigger_; Trigger<TouchPoint, const TouchPoints_t &> touch_trigger_;