From bf60e40d0bda314bccf1dd7c870ef9af6af87e56 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 23 Feb 2022 11:05:53 +1300 Subject: [PATCH] Add optional display page for touchscreen binary sensors (#3247) --- esphome/components/touchscreen/__init__.py | 2 ++ esphome/components/touchscreen/binary_sensor/__init__.py | 8 +++++++- .../binary_sensor/touchscreen_binary_sensor.cpp | 4 ++++ .../touchscreen/binary_sensor/touchscreen_binary_sensor.h | 4 ++++ esphome/components/touchscreen/touchscreen.h | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) 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_; }