diff --git a/esphome/components/touchscreen/__init__.py b/esphome/components/touchscreen/__init__.py index 01a271a34e..26cb22d490 100644 --- a/esphome/components/touchscreen/__init__.py +++ b/esphome/components/touchscreen/__init__.py @@ -4,7 +4,9 @@ from esphome.components import display import esphome.config_validation as cv from esphome.const import ( CONF_CALIBRATION, + CONF_DIMENSIONS, CONF_DISPLAY, + CONF_HEIGHT, CONF_MIRROR_X, CONF_MIRROR_Y, CONF_ON_RELEASE, @@ -12,6 +14,7 @@ from esphome.const import ( CONF_ON_UPDATE, CONF_SWAP_XY, CONF_TRANSFORM, + CONF_WIDTH, ) from esphome.core import coroutine_with_priority @@ -94,6 +97,15 @@ def touchscreen_schema(default_touch_timeout=cv.UNDEFINED, calibration_required= cv.Range(max=cv.TimePeriod(milliseconds=65535)), ), calibration: CALIBRATION_SCHEMA, + cv.Optional(CONF_DIMENSIONS): cv.Any( + cv.dimensions, + cv.Schema( + { + cv.Required(CONF_WIDTH): cv.int_, + cv.Required(CONF_HEIGHT): cv.int_, + } + ), + ), cv.Optional(CONF_ON_TOUCH): 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), @@ -130,6 +142,14 @@ async def register_touchscreen(var, config): ) ) + if CONF_DIMENSIONS in config: + dimensions = config[CONF_DIMENSIONS] + if isinstance(dimensions, dict): + cg.add(var.set_dimensions(dimensions[CONF_WIDTH], dimensions[CONF_HEIGHT])) + else: + (width, height) = dimensions + cg.add(var.set_dimensions(width, height)) + if CONF_ON_TOUCH in config: await automation.build_automation( var.get_touch_trigger(), diff --git a/esphome/components/touchscreen/touchscreen.cpp b/esphome/components/touchscreen/touchscreen.cpp index dfe723aedf..2df6b72066 100644 --- a/esphome/components/touchscreen/touchscreen.cpp +++ b/esphome/components/touchscreen/touchscreen.cpp @@ -94,8 +94,16 @@ void Touchscreen::add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_r std::swap(x, y); } - tp.x = (uint16_t) ((int) x * this->display_width_ / 0x1000); - tp.y = (uint16_t) ((int) y * this->display_height_ / 0x1000); + if (this->touchscreen_width_ > 0) { + tp.x = (uint16_t) ((int) x * this->touchscreen_width_ / 0x1000); + } else { + tp.x = (uint16_t) ((int) x * this->display_width_ / 0x1000); + } + if (this->touchscreen_height_ > 0) { + tp.y = (uint16_t) ((int) y * this->touchscreen_height_ / 0x1000); + } else { + tp.y = (uint16_t) ((int) y * this->display_height_ / 0x1000); + } } else { tp.state |= STATE_CALIBRATE; } diff --git a/esphome/components/touchscreen/touchscreen.h b/esphome/components/touchscreen/touchscreen.h index 8016323d49..380ee10a55 100644 --- a/esphome/components/touchscreen/touchscreen.h +++ b/esphome/components/touchscreen/touchscreen.h @@ -59,6 +59,11 @@ class Touchscreen : public PollingComponent { this->y_raw_max_ = y_max; } + void set_dimensions(int16_t width, int16_t height) { + this->touchscreen_width_ = width; + this->touchscreen_height_ = height; + } + Trigger *get_touch_trigger() { return &this->touch_trigger_; } Trigger *get_update_trigger() { return &this->update_trigger_; } Trigger<> *get_release_trigger() { return &this->release_trigger_; } @@ -96,6 +101,7 @@ class Touchscreen : public PollingComponent { int16_t x_raw_min_{0}, x_raw_max_{0}, y_raw_min_{0}, y_raw_max_{0}; int16_t display_width_{0}, display_height_{0}; + int16_t touchscreen_width_{0}, touchscreen_height_{0}; uint16_t touch_timeout_{0}; bool invert_x_{false}, invert_y_{false}, swap_x_y_{false};