本帖最后由 wwjw 于 2025-10-24 12:51 编辑
我用esp32开发板(esp32dev)、编码器模块(ec11)、1.54寸tft屏幕(ST7789V)实现某些功能,编译正常并刷入了esp32,但是现在遇到2个问题:
1.HA里面不显示实体,按道理说集成后应该会自动发现;
2.屏幕没显示。代码想实现的是屏幕显示当前时间和DAC的输出百分比,但是屏幕显示是白色背景无任何内容。
个人技术不精,请大佬帮忙看看是哪里出了问题。另外,编码器的控制功能是正常的,旋转、按键均有正常的日志输出,可以控制屏幕亮度。
esphome:
name: esp32
friendly_name: ESP32
esp32:
board: esp32dev
framework:
type: esp-idf
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "U2utvgAJzotk+DlZkqo="
ota:
- platform: esphome
password: "f094c87da6b4d"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Fallback Hotspot"
password: "cS01"
captive_portal:
time:
- platform: homeassistant
id: ha_time
on_time_sync:
then:
- component.update: tft_display
font:
- file: "gfonts://Roboto"
id: large_font
size: 36
- file: "gfonts://Roboto"
id: small_font
size: 16
sensor:
- platform: uptime
id: internal_uptime
update_interval: 60s
- platform: rotary_encoder
id: ec11_encoder
name: "编码器"
pin_a: GPIO32
pin_b: GPIO33
resolution: 4
filters:
- debounce: 10ms
on_clockwise:
then:
- number.increment: dac_level
on_anticlockwise:
then:
- number.decrement: dac_level
binary_sensor:
- platform: gpio
id: encoder_button
name: "编码器按键"
pin:
number: GPIO27
mode: INPUT_PULLUP
inverted: false
filters:
- delayed_on: 50ms
on_press:
then:
- switch.toggle: main_switch
output:
- platform: esp32_dac
pin: GPIO25
id: dac_output
- platform: ledc
pin: GPIO17
id: backlight_output
frequency: 1000Hz
number:
- platform: template
name: "DAC输出"
id: dac_level
min_value: 0
max_value: 100
step: 1
unit_of_measurement: "%"
icon: mdi:gauge
optimistic: true
on_value:
then:
- output.set_level:
id: dac_output
level: !lambda 'return id(main_switch).state ? (id(dac_level).state / 100.0) : 0.0;'
- component.update: tft_display
switch:
- platform: gpio
name: "主控制开关"
id: main_switch
pin: GPIO15
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
then:
- output.set_level:
id: backlight_output
level: 1.0
- component.update: tft_display
on_turn_off:
then:
- output.set_level:
id: backlight_output
level: 0.1
- component.update: tft_display
spi:
clk_pin: GPIO18
mosi_pin: GPIO23
display:
- platform: mipi_spi
id: tft_display
model: ST7789V
dimensions: 240x240
cs_pin: GPIO5
dc_pin: GPIO16
reset_pin: GPIO21
rotation: 0
update_interval: never
lambda: |-
static float last_dac = -1;
static int last_hour = -1;
static int last_minute = -1;
static bool last_switch = false;
auto time = id(ha_time).now();
if (time.hour != last_hour || time.minute != last_minute) {
it.filled_ring(0, 60, 240, 40, Color::BLACK);
it.printf(120, 85, id(large_font), TextAlign::CENTER,
"%02d:%02d", time.hour, time.minute);
last_hour = time.hour;
last_minute = time.minute;
}
if (id(dac_level).state != last_dac) {
it.filled_ring(0, 130, 240, 20, Color::BLACK);
it.printf(120, 145, id(small_font), TextAlign::CENTER,
"DAC输出: %.0f%%", id(dac_level).state);
last_dac = id(dac_level).state;
}
|