I started off with getting CYD code to work from here, with tweaks to meet my hardware
I want to use the “Button A” to toggle a boolean and make the screen change based on that boolean.
I create a boolean in GLOBAL.
globals:
- id: bool_A
type: bool
initial_value: 'false' #bool_A set to false
restore_value: no # Optional: defaults to no
Further down, I add a toggle in BINARY_SWITCH
binary_sensor:
- platform: touchscreen
name: Button A
x_min: 20
x_max: 140
y_min: 60
y_max: 180
on_press:
then:
- homeassistant.service:
service: bool_A.toggle #bool_A is toggled if Button A touched
- platform: touchscreen
name: Button B
x_min: 180
x_max: 300
y_min: 60
y_max: 180
And on my display, I create an IF statement
display:
- platform: ili9xxx
id: esp_display
model: ILI9341 #st7789v
#EXTRA CODE REMOVED FOR BREVITY
lambda: |- #if bool_A then fill rectangle. Else just outline
it.fill(id(Color::BLACK));
if (bool_A) {
it.filled_rectangle(20, 60, 120, 120, id(ha_blue));
} else {
it.rectangle(20, 60, 120, 120, id(ha_blue));
}
it.rectangle(180, 60, 120, 120, id(ha_blue));
it.print(80, 120, id(arimo96), TextAlign::CENTER, "A");
it.print(240, 120, id(arimo96), TextAlign::CENTER, "B");
No matter how many times I touch the Button A, the rectangle is always filled ? Why? What triggered bool_A to be true? Why is it always true?
Just in case, here is my full code below
esphome:
name: aliexp-cyd
friendly_name: AliExp-CYD
esp32:
board: esp32dev
framework:
type: esp-idf
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "I have more than 1 device"
ota:
- platform: esphome
password: "I am not smart enough to add multiple"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Aliexp-Cyd Fallback Hotspot"
password: "secrets in the secrets.yaml file"
captive_portal:
globals:
- id: bool_A
type: bool
initial_value: 'false' #bool_A set to false
restore_value: no # Optional: defaults to no
# ============================================================
# ESPHome Display related setup
#
# Create a font to use, add and remove glyphs as needed.
font:
- file: 'fonts/Arimo-Regular.ttf'
id: arimo96
size: 96
glyphs: "AB"
# Create a Home Assistant blue color
color:
- id: ha_blue
hex: 51c0f2
# ============================================================
# Home Assistant related setup
#
light:
- platform: monochromatic
output: backlight_pwm
name: Display Backlight
id: backlight
restore_mode: ALWAYS_ON
# Setup two binary sensors for the two areas for touch
binary_sensor:
- platform: touchscreen
name: Button A
x_min: 20
x_max: 140
y_min: 60
y_max: 180
on_press:
then:
- homeassistant.service:
service: bool_A.toggle #bool_A is toggled if Button A touched
- platform: touchscreen
name: Button B
x_min: 180
x_max: 300
y_min: 60
y_max: 180
# ============================================================
# Hardware related setup
#
# Setup SPI for the display. The ESP32-2432S028R uses separate SPI buses for display and touch
spi:
- id: spi_tft
clk_pin: GPIO14
mosi_pin: GPIO13
miso_pin: GPIO12
interface: spi2
- id: spi_touch
clk_pin: GPIO25
mosi_pin: GPIO32
miso_pin: GPIO39
interface: spi3
# Setup a pin to control the backlight
output:
- platform: ledc
pin: GPIO21
id: backlight_pwm
# Setup the ili9xxx platform
#
# Display is used as 240x320 by default so we rotate it to 90°
#
# We print date and time wth the strftime() function, see the ESPHome documentation to
# format date and atime to your locale.
#
display:
- platform: ili9xxx
id: esp_display
model: ILI9341
spi_id: spi_tft
cs_pin: GPIO15
dc_pin: GPIO2
invert_colors: false
spi_mode: MODE3
data_rate: 40MHz
# data_rate: 2MHz
# pixel_mode: 16bit
color_order: bgr
color_palette: 8BIT
# update_interval: 1000ms
# auto_clear_enabled: true
dimensions:
height: 240
width: 320
offset_height: 0
offset_width: 0
transform:
swap_xy: true
mirror_x: false #true false
# show_test_card: true
lambda: |- #if bool_A then fill rectangle. Else just outline
it.fill(id(Color::BLACK));
if (bool_A) {
it.filled_rectangle(20, 60, 120, 120, id(ha_blue));
} else {
it.rectangle(20, 60, 120, 120, id(ha_blue));
}
it.rectangle(180, 60, 120, 120, id(ha_blue));
it.print(80, 120, id(arimo96), TextAlign::CENTER, "A");
it.print(240, 120, id(arimo96), TextAlign::CENTER, "B");
# Set up the xpt2046 touch platform
touchscreen:
platform: xpt2046
id: esp_touchscreen
spi_id: spi_touch
cs_pin: GPIO33
interrupt_pin: GPIO36
data_rate: 2MHz
spi_mode: MODE0
threshold: 400
update_interval: 50ms
transform:
swap_xy: true
mirror_x: false
mirror_y: false
calibration:
x_min: 390
x_max: 3800
y_min: 210
y_max: 3800