1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-13 22:28:14 +00:00

allow setting touchscreen dimensions

This commit is contained in:
Dennis Marinus 2025-02-21 15:14:03 -08:00
parent c281351732
commit d59a6fdc57
3 changed files with 36 additions and 2 deletions

View File

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

View File

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

View File

@ -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<TouchPoint, const TouchPoints_t &> *get_touch_trigger() { return &this->touch_trigger_; }
Trigger<const TouchPoints_t &> *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};