esphome:
  name: nfc-playbox-1
  friendly_name: NFC Playbox_1

esp32:
  board: esp32-s3-devkitc-1
  flash_size: 16MB
  framework:
    type: esp-idf

psram:
  mode: octal
  speed: 80MHz

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "OQlmeshTtUZg/iavLExjuNt1H7ywTohWAYozqNym+9M="

ota:
  - platform: esphome
    password: "14106c281b3e5b1ac1da204b7ff99728"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  power_save_mode: none      # stabilare link
  output_power: 17dB         # maxa sändarnivån
  use_address: 10.0.3.29
  domain: .milvert.com
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nfc-Playbox-1 Fallback Hotspot"
    password: "kQAdTCPYabwd"

captive_portal:

sensor:
  - platform: wifi_signal
    name: "NFC Playbox WiFi RSSI"
    update_interval: 30s

  - platform: uptime
    name: "NFC Playbox Uptime"

script:
  - id: wake_screen
    mode: restart
    then:
      - light.turn_on:
          id: backlight
          brightness: 100%
      # - delay: 60s
      # - light.turn_off: backlight

image:
  - file: "images/play.png"
    id: img_playpause
    type: BINARY

  - file: "images/low-volume.png"
    id: img_voldown
    type: BINARY

  - file: "images/high-volume.png"
    id: img_volup
    type: BINARY
  

globals:
  - id: touch_x
    type: int
    initial_value: "0"

  - id: touch_y
    type: int
    initial_value: "0"

# ---- SPI-buss till LCD
spi:
  - id: spi_bus
    clk_pin: 39      # LCD_SCLK
    mosi_pin: 38     # LCD_MOSI
    miso_pin: 40     # (MISO finns men används ej av LCD)

  - id: spi_bus_rc522
    clk_pin: 13
    mosi_pin: 14
    miso_pin: 9

rc522_spi:
  spi_id: spi_bus_rc522
  cs_pin: 12
  reset_pin: 11
  update_interval: 1s
  on_tag:
    then:
      - logger.log:
          format: "RFID Tag UID: %s"
          args: [ 'x.c_str()' ]
      - homeassistant.tag_scanned: !lambda 'return x;'

# ---- Bakgrundsbelysning (GPIO1 via PWM)
output:
  - platform: ledc
    pin: 1
    id: lcd_bl

light:
  - platform: monochromatic
    name: "LCD Backlight"
    id: backlight
    output: lcd_bl
    restore_mode: ALWAYS_ON

# ---- I2C för touch (CST816D)
i2c:
  sda: 48          # TP_SDA
  scl: 47          # TP_SCL
  id: i2c_bus
  scan: true
  frequency: 400kHz

touchscreen:
  - platform: cst816
    id: tp
    interrupt_pin: 46   # TP_INT
    reset_pin: 21       # TP_RESET
    transform:
      mirror_x: false
      mirror_y: true
      swap_xy: true
    calibration:
      x_min: 0
      x_max: 320
      y_min: 30         # offset för min display
      y_max: 240
    on_touch:
      then:
        - lambda: |-
            const int CELL_SIZE = 40;
            const int ICON_R = 22; // radie på cirkeln runt ikonen

            // Hjälpfunktion för att kolla träff
            auto hit_circle = [&](int col, int row, int px, int py) {
              int cx = (col + 1) * CELL_SIZE;
              int cy = (row + 1) * CELL_SIZE;
              int dx = px - cx;
              int dy = py - cy;
              return (dx*dx + dy*dy) <= (ICON_R * ICON_R);
            };
        - script.execute: wake_screen
        - logger.log:
            format: "Touch coordinates: (%d, %d)"
            args: ["touch.x", "touch.y"]

substitutions:
  SCREEN_W: "320"   # från loggen: ili9xxx Dimensions: 320 x 240
  SCREEN_H: "240"
  ICON_W:   "32"
  ICON_H:   "32"
  MARGIN:   "8"
  HIT_PAD:  "6"

# ---- Display (ST7789T3/ ST7789V 240x320) via moderna 'ili9xxx'
display:
  - platform: ili9xxx
    id: display_main
    model: ST7789V
    cs_pin: 45          # LCD_CS
    dc_pin: 42          # LCD_DC
    reset_pin: 0        # LCD_RST
    spi_id: spi_bus
    dimensions:
      width: 240
      height: 320
    rotation: 90        # matchar Waveshares exempel (~EXAMPLE_LCD_ROTATION=1)
    invert_colors: false
    update_interval: 500ms
    lambda: |-
      const int CELL_SIZE = 40;

      auto draw_centered_2x2 = [&](int col, int row, const esphome::display::BaseImage *img) {
        const int center_x = (col + 1) * CELL_SIZE;
        const int center_y = (row + 1) * CELL_SIZE;

        // cirkel runt ikonen
        it.circle(center_x, center_y, 22);

        // rita ikonen (32x32 → offset 16)
        it.image(center_x - 16, center_y - 16,
                 const_cast<esphome::display::BaseImage*>(img));
      };

      // PLAY i blocket (1,2)-(2,3)
      draw_centered_2x2(1, 2, id(img_playpause));

      // VOL UP i blocket (3,2)-(4,3)
      draw_centered_2x2(3, 2, id(img_volup));

      // VOL DOWN i blocket (5,2)-(6,3)
      draw_centered_2x2(5, 2, id(img_voldown));

# ---- Klocka och font (för att visa något)
time:
  - platform: sntp
    id: esptime
    timezone: "Europe/Stockholm"

font:
  - file: "gfonts://Roboto"
    id: font_small
    size: 16
