diff --git a/esphome/components/touchscreen/__init__.py b/esphome/components/touchscreen/__init__.py index 8246b95187..125103e2b8 100644 --- a/esphome/components/touchscreen/__init__.py +++ b/esphome/components/touchscreen/__init__.py @@ -6,6 +6,8 @@ from esphome import automation from esphome.const import CONF_ON_TOUCH CODEOWNERS = ["@jesserockz"] +DEPENDENCIES = ["display"] + IS_PLATFORM_COMPONENT = True touchscreen_ns = cg.esphome_ns.namespace("touchscreen") diff --git a/esphome/components/touchscreen/binary_sensor/__init__.py b/esphome/components/touchscreen/binary_sensor/__init__.py index 73cbf1df7e..800bc4c2a9 100644 --- a/esphome/components/touchscreen/binary_sensor/__init__.py +++ b/esphome/components/touchscreen/binary_sensor/__init__.py @@ -1,7 +1,8 @@ import esphome.codegen as cg import esphome.config_validation as cv -from esphome.components import binary_sensor +from esphome.components import binary_sensor, display +from esphome.const import CONF_PAGE_ID from .. import touchscreen_ns, CONF_TOUCHSCREEN_ID, Touchscreen, TouchListener @@ -41,6 +42,7 @@ CONFIG_SCHEMA = cv.All( cv.Required(CONF_X_MAX): cv.int_range(min=0, max=2000), cv.Required(CONF_Y_MIN): cv.int_range(min=0, max=2000), cv.Required(CONF_Y_MAX): cv.int_range(min=0, max=2000), + cv.Optional(CONF_PAGE_ID): cv.use_id(display.DisplayPage), } ) .extend(cv.COMPONENT_SCHEMA), @@ -61,3 +63,7 @@ async def to_code(config): config[CONF_Y_MAX], ) ) + + if CONF_PAGE_ID in config: + page = await cg.get_variable(config[CONF_PAGE_ID]) + cg.add(var.set_page(page)) diff --git a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp index ba12aeeae0..583392cce3 100644 --- a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp +++ b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp @@ -6,6 +6,10 @@ namespace touchscreen { void TouchscreenBinarySensor::touch(TouchPoint tp) { bool touched = (tp.x >= this->x_min_ && tp.x <= this->x_max_ && tp.y >= this->y_min_ && tp.y <= this->y_max_); + if (this->page_ != nullptr) { + touched &= this->page_ == this->parent_->get_display()->get_active_page(); + } + if (touched) { this->publish_state(true); } else { diff --git a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h index 7b8cac5c4c..d7e53962e2 100644 --- a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h +++ b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h @@ -1,6 +1,7 @@ #pragma once #include "esphome/components/binary_sensor/binary_sensor.h" +#include "esphome/components/display/display_buffer.h" #include "esphome/components/touchscreen/touchscreen.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" @@ -23,11 +24,14 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor, this->y_max_ = y_max; } + void set_page(display::DisplayPage *page) { this->page_ = page; } + void touch(TouchPoint tp) override; void release() override; protected: int16_t x_min_, x_max_, y_min_, y_max_; + display::DisplayPage *page_{nullptr}; }; } // namespace touchscreen diff --git a/esphome/components/touchscreen/touchscreen.h b/esphome/components/touchscreen/touchscreen.h index 2c0ec9e268..0597759894 100644 --- a/esphome/components/touchscreen/touchscreen.h +++ b/esphome/components/touchscreen/touchscreen.h @@ -37,6 +37,7 @@ class Touchscreen { this->display_height_ = display->get_height_internal(); this->rotation_ = static_cast(display->get_rotation()); } + display::DisplayBuffer *get_display() const { return this->display_; } Trigger *get_touch_trigger() { return &this->touch_trigger_; }