# =====================================================================
#  示例 1：单页仪表盘 UI
#  布局：顶栏设备名+时钟 / 中央大字温度 / 湿度 / 底部电池进度条
#  硬件依赖：显示屏、SHTC3、电池 ADC、BOOT 按键
# =====================================================================

esphome:
  name: esp32-s3-rlcd-42
  friendly_name: ESP32-S3-RLCD-4.2

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: esp-idf

psram:
  mode: octal
  speed: 80MHz

external_components:
  - source: github://kylehase/ESPHome-ST7305-RLCD
    components: [st7305_rlcd]

logger:

api:
  encryption:
    key: "7J7XVVoREEncoG39FrOxlb/ZVMRS/u7RmTc4WK2Ej7w="

ota:
  - platform: esphome
    password: "d9a7ee09077e2b8c27f2563e475e66ea"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Esp32-S3-Rlcd-42"
    password: "Wa3PMQZVj5kn"

captive_portal:

# ---------- 总线 ----------
i2c:
  - id: bus_a
    sda: GPIO13
    scl: GPIO14
    scan: true

spi:
  - id: spi_lcd
    clk_pin: GPIO11
    mosi_pin: GPIO12

# ---------- 时间（HA 同步，用于顶栏时钟）----------
time:
  - platform: homeassistant
    id: ha_time

# ---------- 传感器 ----------
sensor:
  - platform: shtcx
    address: 0x70
    i2c_id: bus_a
    update_interval: 30s
    temperature:
      name: "Temperature"
      id: temp_sensor
    humidity:
      name: "Humidity"
      id: hum_sensor

  - platform: adc
    id: bat_voltage
    name: "Battery Voltage"
    pin: GPIO4
    attenuation: 12db
    update_interval: 30s
    filters:
      - multiply: 3.0

  - platform: copy
    source_id: bat_voltage
    id: bat_level
    name: "Battery Level"
    unit_of_measurement: "%"
    filters:
      - calibrate_linear:
          - 2.5 -> 0.0
          - 4.2 -> 100.0
      - clamp:
          min_value: 0
          max_value: 100

# ---------- 字体（°、% 需要显式声明字符集） ----------
font:
  - file: "gfonts://Roboto"
    id: font_xs
    size: 14
    glyphs: &glyphs "!\"#%()+,-_.:°0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/ "
  - file: "gfonts://Roboto"
    id: font_sm
    size: 18
    glyphs: *glyphs
  - file: "gfonts://Roboto"
    id: font_md
    size: 28
    glyphs: *glyphs
  - file: "gfonts://Roboto@700"
    id: font_xl
    size: 96
    glyphs: *glyphs

# ---------- 显示屏 ----------
display:
  - platform: st7305_rlcd
    id: my_display
    model: WAVESHARE_400X300
    width: 400
    height: 300
    cs_pin: GPIO40
    dc_pin: GPIO5
    reset_pin: GPIO41
    data_rate: 1MHz
    update_interval: 30s
    lambda: |-
      // 顶部状态栏
      it.print(10, 6, id(font_xs), "ESP32-S3-RLCD-4.2");
      it.strftime(390, 6, id(font_xs), TextAlign::TOP_RIGHT, "%H:%M", id(ha_time).now());
      it.line(0, 28, 400, 28);

      // 中央温度（巨字号）
      it.printf(180, 50, id(font_xl), TextAlign::TOP_CENTER, "%.1f", id(temp_sensor).state);
      it.print(330, 80, id(font_md), "°C");

      // 湿度
      it.printf(200, 170, id(font_md), TextAlign::TOP_CENTER,
                "%.0f %%  RH", id(hum_sensor).state);

      // 底部电池条
      it.line(0, 225, 400, 225);
      it.print(10, 235, id(font_sm), "Battery");
      it.printf(390, 235, id(font_sm), TextAlign::TOP_RIGHT,
                "%.2f V    %.0f %%", id(bat_voltage).state, id(bat_level).state);
      int bx = 10, by = 270, bw = 380, bh = 20;
      it.rectangle(bx, by, bw, bh);
      int fill = (int)((bw - 4) * id(bat_level).state / 100.0);
      if (fill > 0) it.filled_rectangle(bx + 2, by + 2, fill, bh - 4);

# ---------- 按键：BOOT 立即刷屏 ----------
binary_sensor:
  - platform: gpio
    name: "Boot Button"
    pin:
      number: GPIO0
      inverted: true
      mode: INPUT
    on_press:
      - component.update: my_display
