Enhance touchscreen functionality: Add buttons for medication control and simplify touch event handling

This commit is contained in:
Joshua King
2026-03-04 17:23:41 -05:00
parent 4aecb61673
commit 58e4ca6c03

View File

@@ -129,6 +129,7 @@ display:
# XPT2046 Touchscreen # XPT2046 Touchscreen
touchscreen: touchscreen:
- platform: xpt2046 - platform: xpt2046
id: my_touchscreen
spi_id: tft_spi spi_id: tft_spi
cs_pin: GPIO33 cs_pin: GPIO33
interrupt_pin: GPIO36 interrupt_pin: GPIO36
@@ -138,34 +139,60 @@ touchscreen:
x_max: 3850 x_max: 3850
y_min: 340 y_min: 340
y_max: 3860 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: then:
- lambda: |- - switch.toggle: penelope_medicated
// Ignore touches at edges (phantom touches usually report at extremes)
int touch_x = touch.x; - platform: touchscreen
int touch_y = touch.y; touchscreen_id: my_touchscreen
name: "Tess Button"
// Filter out phantom touches (typically at corners/edges) id: tess_button
if (touch_x < 10 || touch_x > 230 || touch_y < 10 || touch_y > 310) { x_min: 30
return; x_max: 210
} y_min: 155
y_max: 235
ESP_LOGI("touch", "Valid touch at x=%d, y=%d", touch_x, touch_y); on_press:
then:
// Penelope button bounds: x=30-210, y=60-140 - switch.toggle: tess_medicated
if (touch_x >= 30 && touch_x <= 210 && touch_y >= 60 && touch_y <= 140) {
id(penelope_medicated).toggle(); - platform: touchscreen
} touchscreen_id: my_touchscreen
// Tess button bounds: x=30-210, y=155-235 name: "Reset Button"
else if (touch_x >= 30 && touch_x <= 210 && touch_y >= 155 && touch_y <= 235) { id: reset_button
id(tess_medicated).toggle(); x_min: 70
} x_max: 170
// Reset button bounds: x=70-170, y=260-300 y_min: 260
else if (touch_x >= 70 && touch_x <= 170 && touch_y >= 260 && touch_y <= 300) { y_max: 300
id(penelope_medicated).turn_off(); on_press:
id(tess_medicated).turn_off(); then:
} - switch.turn_off: penelope_medicated
- script.execute: update_display - 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 # Backlight control
output: output:
@@ -210,23 +237,6 @@ switch:
- switch.turn_off: penelope_medicated - switch.turn_off: penelope_medicated
- switch.turn_off: tess_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 to update display
script: script:
- id: update_display - id: update_display