mirror of
https://github.com/esphome/esphome.git
synced 2025-10-26 20:53:50 +00:00
[touchscreen] Calibration fixes (#7704)
This commit is contained in:
@@ -1,21 +1,18 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
|
||||
from esphome.components import display
|
||||
from esphome import automation
|
||||
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import display
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_CALIBRATION,
|
||||
CONF_DISPLAY,
|
||||
CONF_ON_TOUCH,
|
||||
CONF_ON_RELEASE,
|
||||
CONF_ON_UPDATE,
|
||||
CONF_SWAP_XY,
|
||||
CONF_MIRROR_X,
|
||||
CONF_MIRROR_Y,
|
||||
CONF_ON_RELEASE,
|
||||
CONF_ON_TOUCH,
|
||||
CONF_ON_UPDATE,
|
||||
CONF_SWAP_XY,
|
||||
CONF_TRANSFORM,
|
||||
CONF_CALIBRATION,
|
||||
)
|
||||
|
||||
from esphome.core import coroutine_with_priority
|
||||
|
||||
CODEOWNERS = ["@jesserockz", "@nielsnl68"]
|
||||
@@ -43,51 +40,45 @@ CONF_Y_MIN = "y_min"
|
||||
CONF_Y_MAX = "y_max"
|
||||
|
||||
|
||||
def validate_calibration(config):
|
||||
if CONF_CALIBRATION in config:
|
||||
calibration_config = config[CONF_CALIBRATION]
|
||||
if (
|
||||
cv.int_([CONF_X_MIN]) != 0
|
||||
and cv.int_(calibration_config[CONF_X_MAX]) != 0
|
||||
and abs(
|
||||
cv.int_(calibration_config[CONF_X_MIN])
|
||||
- cv.int_(calibration_config[CONF_X_MAX])
|
||||
)
|
||||
< 10
|
||||
):
|
||||
raise cv.Invalid("Calibration X values difference must be more than 10")
|
||||
|
||||
if (
|
||||
cv.int_(calibration_config[CONF_Y_MIN]) != 0
|
||||
and cv.int_(calibration_config[CONF_Y_MAX]) != 0
|
||||
and abs(
|
||||
cv.int_(calibration_config[CONF_Y_MIN])
|
||||
- cv.int_(calibration_config[CONF_Y_MAX])
|
||||
)
|
||||
< 10
|
||||
):
|
||||
raise cv.Invalid("Calibration Y values difference must be more than 10")
|
||||
|
||||
return config
|
||||
def validate_calibration(calibration_config):
|
||||
x_min = calibration_config[CONF_X_MIN]
|
||||
x_max = calibration_config[CONF_X_MAX]
|
||||
y_min = calibration_config[CONF_Y_MIN]
|
||||
y_max = calibration_config[CONF_Y_MAX]
|
||||
if x_max < x_min:
|
||||
raise cv.Invalid(
|
||||
"x_min must be smaller than x_max. To mirror the direction use the 'transform' options"
|
||||
)
|
||||
if y_max < y_min:
|
||||
raise cv.Invalid(
|
||||
"y_min must be smaller than y_max. To mirror the direction use the 'transform' options"
|
||||
)
|
||||
x_delta = x_max - x_min
|
||||
y_delta = y_max - y_min
|
||||
if x_delta < 10 or y_delta < 10:
|
||||
raise cv.Invalid("Calibration value range must be greater than 10")
|
||||
return calibration_config
|
||||
|
||||
|
||||
def calibration_schema(default_max_values):
|
||||
return cv.Schema(
|
||||
CALIBRATION_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_X_MIN, default=0): cv.int_range(min=0, max=4095),
|
||||
cv.Optional(CONF_X_MAX, default=default_max_values): cv.int_range(
|
||||
min=0, max=4095
|
||||
),
|
||||
cv.Optional(CONF_Y_MIN, default=0): cv.int_range(min=0, max=4095),
|
||||
cv.Optional(CONF_Y_MAX, default=default_max_values): cv.int_range(
|
||||
min=0, max=4095
|
||||
),
|
||||
},
|
||||
validate_calibration,
|
||||
cv.Required(CONF_X_MIN): cv.int_range(min=0, max=4095),
|
||||
cv.Required(CONF_X_MAX): cv.int_range(min=0, max=4095),
|
||||
cv.Required(CONF_Y_MIN): cv.int_range(min=0, max=4095),
|
||||
cv.Required(CONF_Y_MAX): cv.int_range(min=0, max=4095),
|
||||
}
|
||||
),
|
||||
validate_calibration,
|
||||
)
|
||||
|
||||
|
||||
def touchscreen_schema(default_touch_timeout=cv.UNDEFINED, calibration_required=False):
|
||||
calibration = (
|
||||
cv.Required(CONF_CALIBRATION)
|
||||
if calibration_required
|
||||
else cv.Optional(CONF_CALIBRATION)
|
||||
)
|
||||
|
||||
|
||||
def touchscreen_schema(default_touch_timeout):
|
||||
return cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_DISPLAY): cv.use_id(display.Display),
|
||||
@@ -102,7 +93,7 @@ def touchscreen_schema(default_touch_timeout):
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=cv.TimePeriod(milliseconds=65535)),
|
||||
),
|
||||
cv.Optional(CONF_CALIBRATION): calibration_schema(0),
|
||||
calibration: CALIBRATION_SCHEMA,
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user