1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 07:08:20 +00:00

allow touchscreen buttons outside of display dimensions (#8296)

Co-authored-by: Dennis Marinus <dmarinus@apple.com>
Co-authored-by: clydebarrow <2366188+clydebarrow@users.noreply.github.com>
This commit is contained in:
Dennis Marinus 2025-03-07 15:41:54 -08:00 committed by GitHub
parent 83e090cc7e
commit 10cea51739
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 86 additions and 6 deletions

View File

@ -19,6 +19,7 @@ CONF_X_MIN = "x_min"
CONF_X_MAX = "x_max" CONF_X_MAX = "x_max"
CONF_Y_MIN = "y_min" CONF_Y_MIN = "y_min"
CONF_Y_MAX = "y_max" CONF_Y_MAX = "y_max"
CONF_USE_RAW = "use_raw"
def _validate_coords(config): def _validate_coords(config):
@ -46,6 +47,7 @@ CONFIG_SCHEMA = cv.All(
.extend( .extend(
{ {
cv.GenerateID(CONF_TOUCHSCREEN_ID): cv.use_id(Touchscreen), cv.GenerateID(CONF_TOUCHSCREEN_ID): cv.use_id(Touchscreen),
cv.Optional(CONF_USE_RAW, default=False): cv.boolean,
cv.Required(CONF_X_MIN): cv.int_range(min=0, max=2000), cv.Required(CONF_X_MIN): cv.int_range(min=0, max=2000),
cv.Required(CONF_X_MAX): cv.int_range(min=0, max=2000), 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_MIN): cv.int_range(min=0, max=2000),
@ -69,6 +71,7 @@ async def to_code(config):
await cg.register_component(var, config) await cg.register_component(var, config)
await cg.register_parented(var, config[CONF_TOUCHSCREEN_ID]) await cg.register_parented(var, config[CONF_TOUCHSCREEN_ID])
cg.add(var.set_use_raw(config[CONF_USE_RAW]))
cg.add( cg.add(
var.set_area( var.set_area(
config[CONF_X_MIN], config[CONF_X_MIN],

View File

@ -9,7 +9,13 @@ void TouchscreenBinarySensor::setup() {
} }
void TouchscreenBinarySensor::touch(TouchPoint tp) { 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_); bool touched;
if (this->use_raw_) {
touched =
(tp.x_raw >= this->x_min_ && tp.x_raw <= this->x_max_ && tp.y_raw >= this->y_min_ && tp.y_raw <= this->y_max_);
} else {
touched = (tp.x >= this->x_min_ && tp.x <= this->x_max_ && tp.y >= this->y_min_ && tp.y <= this->y_max_);
}
if (!this->pages_.empty()) { if (!this->pages_.empty()) {
auto *current_page = this->parent_->get_display()->get_active_page(); auto *current_page = this->parent_->get_display()->get_active_page();

View File

@ -25,6 +25,7 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor,
this->y_min_ = y_min; this->y_min_ = y_min;
this->y_max_ = y_max; this->y_max_ = y_max;
} }
void set_use_raw(bool use_raw) { this->use_raw_ = use_raw; }
int16_t get_x_min() { return this->x_min_; } int16_t get_x_min() { return this->x_min_; }
int16_t get_x_max() { return this->x_max_; } int16_t get_x_max() { return this->x_max_; }
int16_t get_y_min() { return this->y_min_; } int16_t get_y_min() { return this->y_min_; }
@ -38,7 +39,8 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor,
void release() override; void release() override;
protected: protected:
int16_t x_min_, x_max_, y_min_, y_max_; int16_t x_min_{}, x_max_{}, y_min_{}, y_max_{};
bool use_raw_{};
std::vector<display::DisplayPage *> pages_{}; std::vector<display::DisplayPage *> pages_{};
}; };

View File

@ -47,3 +47,19 @@ def test_binary_sensor_config_value_internal_set(generate_main):
# Then # Then
assert "bs_1->set_internal(true);" in main_cpp assert "bs_1->set_internal(true);" in main_cpp
assert "bs_2->set_internal(false);" in main_cpp assert "bs_2->set_internal(false);" in main_cpp
def test_binary_sensor_config_value_use_raw_set(generate_main):
"""
Test that the "use_raw" config value is correctly set
"""
# Given
# When
main_cpp = generate_main(
"tests/component_tests/binary_sensor/test_binary_sensor.yaml"
)
# Then
assert "bs_3->set_use_raw(true);" in main_cpp
assert "bs_4->set_use_raw(false);" in main_cpp

View File

@ -2,8 +2,30 @@
esphome: esphome:
name: test name: test
esp8266: esp32:
board: d1_mini_lite board: m5stack-core2
i2c:
sda: GPIO21
scl: GPIO22
spi:
clk_pin: GPIO18
mosi_pin: GPIO23
miso_pin: GPIO19
display:
platform: ili9xxx
id: lcd
model: M5STACK
dc_pin: GPIO15
cs_pin: GPIO5
invert_colors: true
touchscreen:
platform: ft63x6
id: touch
interrupt_pin: GPIO39
binary_sensor: binary_sensor:
- platform: gpio - platform: gpio
@ -11,10 +33,25 @@ binary_sensor:
name: test bs1 name: test bs1
internal: true internal: true
pin: pin:
number: D0 number: GPIO32
- platform: gpio - platform: gpio
id: bs_2 id: bs_2
name: test bs2 name: test bs2
internal: false internal: false
pin: pin:
number: D1 number: GPIO33
- platform: touchscreen
id: bs_3
name: test bs3
x_min: 100
x_max: 200
y_min: 300
y_max: 400
use_raw: true
- platform: touchscreen
id: bs_4
name: test bs4
x_min: 100
x_max: 200
y_min: 300
y_max: 400

View File

@ -34,3 +34,19 @@ touchscreen:
on_release: on_release:
- logger.log: - logger.log:
format: to released format: to released
calibration:
x_min: 0
x_max: 320
y_min: 0
y_max: 400
binary_sensor:
- platform: touchscreen
name: Bottom Left Touch
use_raw: true
x_min: 0
x_max: 100
y_min: 400
y_max: 480
on_press:
logger.log: Left pressed