mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 22:28:14 +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:
parent
83e090cc7e
commit
10cea51739
@ -19,6 +19,7 @@ CONF_X_MIN = "x_min"
|
||||
CONF_X_MAX = "x_max"
|
||||
CONF_Y_MIN = "y_min"
|
||||
CONF_Y_MAX = "y_max"
|
||||
CONF_USE_RAW = "use_raw"
|
||||
|
||||
|
||||
def _validate_coords(config):
|
||||
@ -46,6 +47,7 @@ CONFIG_SCHEMA = cv.All(
|
||||
.extend(
|
||||
{
|
||||
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_MAX): 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_parented(var, config[CONF_TOUCHSCREEN_ID])
|
||||
|
||||
cg.add(var.set_use_raw(config[CONF_USE_RAW]))
|
||||
cg.add(
|
||||
var.set_area(
|
||||
config[CONF_X_MIN],
|
||||
|
@ -9,7 +9,13 @@ void TouchscreenBinarySensor::setup() {
|
||||
}
|
||||
|
||||
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()) {
|
||||
auto *current_page = this->parent_->get_display()->get_active_page();
|
||||
|
@ -25,6 +25,7 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor,
|
||||
this->y_min_ = y_min;
|
||||
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_max() { return this->x_max_; }
|
||||
int16_t get_y_min() { return this->y_min_; }
|
||||
@ -38,7 +39,8 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor,
|
||||
void release() override;
|
||||
|
||||
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_{};
|
||||
};
|
||||
|
||||
|
@ -47,3 +47,19 @@ def test_binary_sensor_config_value_internal_set(generate_main):
|
||||
# Then
|
||||
assert "bs_1->set_internal(true);" 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
|
||||
|
@ -2,8 +2,30 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp8266:
|
||||
board: d1_mini_lite
|
||||
esp32:
|
||||
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:
|
||||
- platform: gpio
|
||||
@ -11,10 +33,25 @@ binary_sensor:
|
||||
name: test bs1
|
||||
internal: true
|
||||
pin:
|
||||
number: D0
|
||||
number: GPIO32
|
||||
- platform: gpio
|
||||
id: bs_2
|
||||
name: test bs2
|
||||
internal: false
|
||||
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
|
||||
|
@ -34,3 +34,19 @@ touchscreen:
|
||||
on_release:
|
||||
- logger.log:
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user