Files
esp/services/screen.yaml
T
2026-12-03 17:15:27 +07:00

248 lines
6.8 KiB
YAML

globals:
# 0 ready, 1 listening, 2 thinking, 3 speaking, 4 error, 5 offline
- id: screen_state
type: int
restore_value: no
initial_value: "5"
# ---------------------------------------------------------------- hardware --
i2c:
- id: i2c_bus
sda: GPIO11
scl: GPIO10
pca9554:
- id: io_expander # TCA9554PWR; EXIO2 drives the LCD reset
address: 0x20
spi:
- id: display_qspi
type: quad
clk_pin: GPIO40
data_pins: [GPIO46, GPIO45, GPIO42, GPIO41]
display:
- platform: mipi_spi
id: screen
model: WAVESHARE-ESP32-S3-TOUCH-LCD-1.46
spi_id: display_qspi
auto_clear_enabled: false
update_interval: never
output:
- platform: ledc
id: backlight_pwm
pin: GPIO5
light:
- platform: monochromatic
id: backlight
name: Screen Backlight
internal: true
output: backlight_pwm
restore_mode: ALWAYS_OFF # lit only by screen_wake during a conversation
default_transition_length: 300ms
switch:
- platform: template
name: Screen
id: screen_switch
icon: mdi:monitor
optimistic: true
restore_mode: RESTORE_DEFAULT_ON
on_turn_on:
# Enabled: light up now if a conversation is in progress, else stay dark.
- if:
condition:
lambda: 'return id(screen_state) >= 1 && id(screen_state) <= 3;'
then:
- script.execute: screen_wake
else:
- script.execute:
id: screen_sleep
delay_ms: 0
on_turn_off:
- script.stop: screen_sleep
- light.turn_off: backlight
- lvgl.pause:
- id: !extend stop_listen
on_turn_on:
- script.execute: screen_refresh
on_turn_off:
- script.execute: screen_refresh
# ---------------------------------------------------------------------- UI --
lvgl:
displays: [screen]
buffer_size: 25%
bg_color: 0x000000
default_font: montserrat_24
pages:
- id: main_page
bg_color: 0x000000
scrollable: false
widgets:
- obj:
id: status_ring
align: CENTER
width: 396
height: 396
radius: 198
bg_opa: TRANSP
border_width: 14
border_color: 0x555555
scrollable: false
- label:
id: status_label
align: CENTER
y: -40
text_font: montserrat_48
text_color: 0x555555
text_align: CENTER
text: "Offline"
- label:
id: transcript_label
align: CENTER
y: 60
width: 290
long_mode: WRAP
text_align: CENTER
text_color: 0xBBBBBB
text: ""
script:
# Redraw ring + status text from screen_state and the Stop Listen switch.
- id: screen_refresh
mode: queued
then:
- lvgl.label.update:
id: status_label
text: !lambda |-
if (id(screen_state) == 5) return "Offline";
if (id(screen_state) == 4) return "Error";
if (id(stop_listen).state) return "Muted";
switch (id(screen_state)) {
case 1: return "Listening";
case 2: return "Thinking";
case 3: return "Speaking";
default: return "Ready";
}
text_color: !lambda |-
if (id(screen_state) >= 4) return lv_color_hex(0xE53935);
if (id(stop_listen).state) return lv_color_hex(0x555555);
switch (id(screen_state)) {
case 1: return lv_color_hex(0x1E88E5);
case 2: return lv_color_hex(0x00BCD4);
case 3: return lv_color_hex(0xFFFFFF);
default: return lv_color_hex(0x2E7D32);
}
- lvgl.obj.update:
id: status_ring
border_color: !lambda |-
if (id(screen_state) >= 4) return lv_color_hex(0xE53935);
if (id(stop_listen).state) return lv_color_hex(0x333333);
switch (id(screen_state)) {
case 1: return lv_color_hex(0x1E88E5);
case 2: return lv_color_hex(0x00BCD4);
case 3: return lv_color_hex(0xFFFFFF);
default: return lv_color_hex(0x1B5E20);
}
- id: screen_set_transcript
mode: queued
parameters:
text: string
then:
- lvgl.label.update:
id: transcript_label
text: !lambda return text;
- script.execute: screen_clear_transcript
# Clear the command/reply text a while after the last update.
- id: screen_clear_transcript
mode: restart
then:
- delay: 20s
- lvgl.label.update:
id: transcript_label
text: ""
# Light the screen for a conversation (only if the Screen switch is on).
# Also arms a 60 s fallback so it can never stay lit if on_end is missed.
- id: screen_wake
mode: restart
then:
- if:
condition:
switch.is_on: screen_switch
then:
- lvgl.resume:
- light.turn_on: backlight
- script.execute:
id: screen_sleep
delay_ms: 60000
# Turn the screen off after delay_ms; a new call restarts the countdown.
- id: screen_sleep
mode: restart
parameters:
delay_ms: int
then:
- delay: !lambda return delay_ms;
- light.turn_off: backlight
- delay: 300ms # let the fade finish before freezing the UI
- lvgl.pause:
# ------------------------------------------- hooks into services/voice.yaml --
voice_assistant:
on_listening:
- lambda: id(screen_state) = 1;
- script.execute: screen_refresh
- script.execute:
id: screen_set_transcript
text: ""
- script.execute: screen_wake
on_stt_vad_end:
- lambda: id(screen_state) = 2;
- script.execute: screen_refresh
on_stt_end:
- lambda: id(screen_state) = 2;
- script.execute: screen_refresh
- script.execute:
id: screen_set_transcript
text: !lambda 'return "\"" + x + "\"";'
on_tts_start:
then:
- lambda: id(screen_state) = 3;
- script.execute: screen_refresh
- script.execute:
id: screen_set_transc
ript
text: !lambda return x;
on_end:
- lambda: id(screen_state) = 0;
- script.execute: screen_refresh
- script.execute:
id: screen_sleep
delay_ms: 5000
on_error:
- lambda: |-
if (message.find("duplicate_wake_up_detected") == std::string::npos)
id(screen_state) = 4;
- script.execute: screen_refresh
- script.execute:
id: screen_sleep
delay_ms: 5000
on_client_connected:
- lambda: id(screen_state) = 0;
- script.execute: screen_refresh
on_client_disconnected:
- lambda: id(screen_state) = 5;
- script.execute: screen_refresh
- script.execute:
id: screen_sleep
delay_ms: 0