diff --git a/esphome/components/touchscreen/binary_sensor/__init__.py b/esphome/components/touchscreen/binary_sensor/__init__.py index 45fefbf814..5ce0defb31 100644 --- a/esphome/components/touchscreen/binary_sensor/__init__.py +++ b/esphome/components/touchscreen/binary_sensor/__init__.py @@ -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], diff --git a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp index 6cd12d4d0d..0662cebf87 100644 --- a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp +++ b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp @@ -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(); diff --git a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h index 862f41064c..79055e6c95 100644 --- a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h +++ b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h @@ -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 pages_{}; }; diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.py b/tests/component_tests/binary_sensor/test_binary_sensor.py index 514bc6ee5f..32d74027ba 100644 --- a/tests/component_tests/binary_sensor/test_binary_sensor.py +++ b/tests/component_tests/binary_sensor/test_binary_sensor.py @@ -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 diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.yaml b/tests/component_tests/binary_sensor/test_binary_sensor.yaml index 8842dda837..60f9472cfb 100644 --- a/tests/component_tests/binary_sensor/test_binary_sensor.yaml +++ b/tests/component_tests/binary_sensor/test_binary_sensor.yaml @@ -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 diff --git a/tests/components/ft63x6/common.yaml b/tests/components/ft63x6/common.yaml index e91266123e..1fae6da5f4 100644 --- a/tests/components/ft63x6/common.yaml +++ b/tests/components/ft63x6/common.yaml @@ -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