From 58e4ca6c03dd62c106acdb17f96fa75e9621e966 Mon Sep 17 00:00:00 2001 From: Joshua King Date: Wed, 4 Mar 2026 17:23:41 -0500 Subject: [PATCH] Enhance touchscreen functionality: Add buttons for medication control and simplify touch event handling --- esphome/cat-medication-tracker.yaml | 98 ++++++++++++++++------------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/esphome/cat-medication-tracker.yaml b/esphome/cat-medication-tracker.yaml index 607fc70..73cacd7 100644 --- a/esphome/cat-medication-tracker.yaml +++ b/esphome/cat-medication-tracker.yaml @@ -129,6 +129,7 @@ display: # XPT2046 Touchscreen touchscreen: - platform: xpt2046 + id: my_touchscreen spi_id: tft_spi cs_pin: GPIO33 interrupt_pin: GPIO36 @@ -138,34 +139,60 @@ touchscreen: x_max: 3850 y_min: 340 y_max: 3860 - on_release: + +# Touch buttons as binary sensors +binary_sensor: + - platform: touchscreen + touchscreen_id: my_touchscreen + name: "Penelope Button" + id: penelope_button + x_min: 30 + x_max: 210 + y_min: 60 + y_max: 140 + on_press: then: - - lambda: |- - // Ignore touches at edges (phantom touches usually report at extremes) - int touch_x = touch.x; - int touch_y = touch.y; - - // Filter out phantom touches (typically at corners/edges) - if (touch_x < 10 || touch_x > 230 || touch_y < 10 || touch_y > 310) { - return; - } - - ESP_LOGI("touch", "Valid touch at x=%d, y=%d", touch_x, touch_y); - - // Penelope button bounds: x=30-210, y=60-140 - if (touch_x >= 30 && touch_x <= 210 && touch_y >= 60 && touch_y <= 140) { - id(penelope_medicated).toggle(); - } - // Tess button bounds: x=30-210, y=155-235 - else if (touch_x >= 30 && touch_x <= 210 && touch_y >= 155 && touch_y <= 235) { - id(tess_medicated).toggle(); - } - // Reset button bounds: x=70-170, y=260-300 - else if (touch_x >= 70 && touch_x <= 170 && touch_y >= 260 && touch_y <= 300) { - id(penelope_medicated).turn_off(); - id(tess_medicated).turn_off(); - } - - script.execute: update_display + - switch.toggle: penelope_medicated + + - platform: touchscreen + touchscreen_id: my_touchscreen + name: "Tess Button" + id: tess_button + x_min: 30 + x_max: 210 + y_min: 155 + y_max: 235 + on_press: + then: + - switch.toggle: tess_medicated + + - platform: touchscreen + touchscreen_id: my_touchscreen + name: "Reset Button" + id: reset_button + x_min: 70 + x_max: 170 + y_min: 260 + y_max: 300 + on_press: + then: + - switch.turn_off: penelope_medicated + - switch.turn_off: tess_medicated + + - platform: template + name: "Penelope Medication Status" + lambda: 'return id(penelope_medicated).state;' + device_class: running + + - platform: template + name: "Tess Medication Status" + lambda: 'return id(tess_medicated).state;' + device_class: running + + - platform: template + name: "All Cats Medicated" + lambda: 'return id(penelope_medicated).state && id(tess_medicated).state;' + device_class: running # Backlight control output: @@ -210,23 +237,6 @@ switch: - switch.turn_off: penelope_medicated - switch.turn_off: tess_medicated -# Binary sensors for Home Assistant dashboard -binary_sensor: - - platform: template - name: "Penelope Medication Status" - lambda: 'return id(penelope_medicated).state;' - device_class: running - - - platform: template - name: "Tess Medication Status" - lambda: 'return id(tess_medicated).state;' - device_class: running - - - platform: template - name: "All Cats Medicated" - lambda: 'return id(penelope_medicated).state && id(tess_medicated).state;' - device_class: running - # Script to update display script: - id: update_display