Add optional display page for touchscreen binary sensors (#3247)

This commit is contained in:
Jesse Hills 2022-02-23 11:05:53 +13:00 committed by GitHub
parent c9094ca537
commit bf60e40d0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 1 deletions

View file

@ -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")

View file

@ -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))

View file

@ -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 {

View file

@ -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

View file

@ -37,6 +37,7 @@ class Touchscreen {
this->display_height_ = display->get_height_internal();
this->rotation_ = static_cast<TouchRotation>(display->get_rotation());
}
display::DisplayBuffer *get_display() const { return this->display_; }
Trigger<TouchPoint> *get_touch_trigger() { return &this->touch_trigger_; }