# https://cdn.shopifycdn.net/s/files/1/0617/7190/7253/files/T-display-pin-diagram_1024x1024.jpg
# https://www.aqmd.gov/docs/default-source/aq-spec/resources-page/plantower-pms5003-manual_v2-3.pdf
esphome: 
  name: "br-air-quality"
  platform: ESP32
  board: featheresp32
  on_boot:
    priority: 590
    then:
      - script.execute: on_wifi_disconnect

logger:
  level: DEBUG
  logs:
    sensor: INFO
    pmsx003: INFO

wifi:
  networks:
    - ssid: !secret wifi_ssid
      password: !secret wifi_password  
  on_connect:
    then:
    - script.execute: on_wifi_connect
  on_disconnect:
    then:
      - script.execute: on_wifi_disconnect

ota:
  platform: esphome
  password: !secret ota_password_1

mqtt:
  id: mqtt_client
  broker: 'mqtt2.bigasterisk.com'
                                  
    # pms pin 1 (blk) to 5V
    # pms pin 2 (red) to gnd NEXT TO ESP PIN 12!!
    # pms pin 3 (blk) NC
# original setup
# GPIO13/RXD2 num 16 = pms tx (pin 5 blk)
# GPIO15/TXD2 num 17 = pms rx (pin 4 red)
# or on T-Display ESP32, 
#   pms tx pin 5 to esp 13
#   pms rx pin 4 tp esp 17
#   led strip: +5/gnd like pms; DIN to esp 27
uart:
  rx_pin: 13
  baud_rate: 9600

sensor:
  - platform: pmsx003
    type: PMS5003T
    pm_1_0:
      name: "Particulate Matter <1.0µm Concentration"
    pm_2_5:
      name: "Particulate Matter <2.5µm Concentration"
      id: pm_2_5
    pm_10_0:
      name: "Particulate Matter <10.0µm Concentration"
    temperature:
      name: "air temperature C"
      id: air_temp_c

  - platform: wifi_signal
    id: my_wifi_signal
    name: wifi_signal
    update_interval: 4s
    internal: true # no mqtt

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

font:
  - file: 'fonts/PassionOne-Regular.ttf'
    id: font_14
    size: 34
    glyphs: "%-.°0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
 
color:
  - { id: color_title,  hex: 88888a }
  - { id: color_wifi_h, hex: 5998de }
  - { id: color_wifi_b, hex: aec9e2 }
  - { id: color_pm_h,   hex: 5998de }
  - { id: color_pm_b,   hex: aec9e2 }
  - { id: color_temp_h, hex: 5998de }
  - { id: color_temp_b, hex: aec9e2 }

output:
  - platform: ledc
    pin: GPIO04
    id: gpio_04_backlight_pwm

light:
  - platform: monochromatic
    output: gpio_04_backlight_pwm
    name: "Display Backlight"
    id: back_light
    restore_mode: ALWAYS_ON

  - platform: fastled_clockless
    chipset: WS2812
    pin: GPIO27
    num_leds: 8
    rgb_order: GRB
    name: "strip1"
    id: strip1
    effects:
      - pulse:
          name: "wifi_connecting_effect"
          transition_length: 0.5s
          update_interval: 0.5s
          min_brightness: 0%
          max_brightness: 20%

script:
 - id: on_wifi_disconnect
   then:
      - light.turn_on:
          id: strip1
          effect: "wifi_connecting_effect"
          color_brightness: 100%
          red: 100%
          green: 0%
          blue: 0%
 - id: on_wifi_connect
   then: 
      - light.turn_off:
          id: strip1
      
display:
  # see note at https://esphome.io/components/display/ili9xxx#:~:text=For-,Lilygo%20TTGO%20Boards,-if%20you%20move
  - platform: ili9xxx 
    model: st7789v
    dimensions:
      height: 240
      width: 135
      offset_height: 40
      offset_width: 52
    rotation: 0°
    invert_colors: true
    cs_pin: GPIO5
    dc_pin: GPIO16
    reset_pin: GPIO23
    update_interval: 2s
    pixel_mode: 16bit
    lambda: |-
      unsigned int row = 0;
      unsigned int rh = 32;
      unsigned int x0 = 0;
      unsigned int x1 = 15;
      
      it.print(135 / 2,    (row++)*rh, id(font_14), id(color_title), TextAlign::TOP_CENTER, "BR");
      it.line(0, rh, 135, rh, id(color_title));

      it.print(x0,    (row++)*rh, id(font_14), id(color_wifi_h), "WIFI");
      if (std::isnan(id(my_wifi_signal).state)) {
        it.print(x1,   (row++)*rh, id(font_14), id(color_wifi_b), "...");
      } else {
        it.printf(x1,   (row++)*rh, id(font_14), id(color_wifi_b), "%.0f%%", min(max(2 * (id(my_wifi_signal).state + 100.0), 0.0), 100.0));
      } 
      it.print(x0,    (row++)*rh, id(font_14), id(color_pm_h), "PM2.5");
      it.printf(x1,   (row++)*rh, id(font_14), id(color_pm_b), "%.1f", id(pm_2_5).state);
      it.print(x0,    (row++)*rh, id(font_14), id(color_temp_h), "TEMP °F");
      it.printf(x1,   (row++)*rh, id(font_14), id(color_temp_b), "%.1f", 32 + (id(air_temp_c).state * 9 / 5));
