touchscreen:
  - platform: cst816
    id: tp
    i2c_id: i2c_bus  
    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
      y_max: 240
    on_touch:
      then:
        - lambda: |-
            int px = touch.x;
            int py = touch.y;
            ESP_LOGI("touch", "Pixel (%d,%d)", px, py);

            const int SCREEN_W   = ${SCREEN_W};
            const int SCREEN_H   = ${SCREEN_H};
            const int RIGHT_COL_W= ${RIGHT_COL_W};
            const int X_SPLIT    = SCREEN_W - RIGHT_COL_W;
            const int Y_MID      = SCREEN_H / 2;
            const int CX_RIGHT   = X_SPLIT + RIGHT_COL_W / 2;
            const int CY_UP      = Y_MID / 2;
            const int CY_DOWN    = Y_MID + (Y_MID / 2);
            const int RING_R     = 45;

            auto hit = [&](int cx, int cy) {
              int dx = px - cx;
              int dy = py - cy;
              return dx*dx + dy*dy <= RING_R*RING_R;
            };
          
            if (hit(CX_RIGHT, CY_UP)) {
              ESP_LOGI("touch", "VOLUME UP pressed");
              id(volume_up_trigger).execute();
            } else if (hit(CX_RIGHT, CY_DOWN)) {
              id(volume_down_trigger).execute();
              ESP_LOGI("touch", "VOLUME DOWN pressed");
            }
        - script.execute: wake_screen
   
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: true
    update_interval: never
    lambda: |-
      const int SCREEN_W   = ${SCREEN_W};
      const int SCREEN_H   = ${SCREEN_H};
      const int RIGHT_COL_W = ${RIGHT_COL_W};
      const int X_SPLIT     = SCREEN_W - RIGHT_COL_W;
      const int Y_MID       = SCREEN_H / 2;

      const int CX_RIGHT = X_SPLIT + RIGHT_COL_W / 2;
      const int CY_UP    = Y_MID / 2;
      const int CY_DOWN  = Y_MID + (Y_MID / 2);
      switch (id(current_image)) {
        case 1:
          it.image(0, 0, id(spellista_dolly));
          break;
        case 2:
          it.image(0, 0, id(spellista_godnattstund));
          break;
        case 3:
          it.image(0, 0, id(spellista_disco));
          break;
        case 4:
          it.image(0, 0, id(spellista_rosahelikopter));
          break;
        default:
          // valfri default-bild/blank
          it.image(0, 0, id(background));  // behåll din gamla default om du vill
          break;
      }
      // Svart background till höger
      it.filled_rectangle(X_SPLIT, 0, SCREEN_W - 1, SCREEN_H - 1, id(my_black));

      it.line(X_SPLIT, 0, X_SPLIT, SCREEN_H - 1);
      it.line(X_SPLIT, Y_MID, SCREEN_W - 1, Y_MID);
    
      
      // Rita minus-cirkel (volym up)
      it.print(CX_RIGHT, CY_UP, id(font_icon_spotify), id(my_white), TextAlign::CENTER, "\U000F0417");
      // Rita minus-cirkel (volym ned)
      it.print(CX_RIGHT, CY_DOWN, id(font_icon_spotify), id(my_white), TextAlign::CENTER, "\U000F0376");
  

      if (id(show_volume)) {                                                                           
        ESP_LOGI("display", "VOLUME is %f", id(local_vol));
        it.filled_circle(230, 122, 35, id(my_green));
        it.printf(230, 145, id(font40), id(my_black), TextAlign::BOTTOM_CENTER, "%.0f", id(local_vol) * 100.0f);

        // HA_value-> to slow?
        // it.printf(230, 145, id(font40), id(my_black), TextAlign::BOTTOM_CENTER, "%.0f", id(spotvol).state);
      }

color:
  - id: my_red
    red: 100%
    green: 0%
  - id: my_green
    red:   11.76%
    green: 84.31%
    blue:  37.65%
  - id: my_white
    red:   1.0000
    green: 1.0000
    blue:  1.0000
  - id: my_grey
    red:   0.6000
    green: 0.6000
    blue:  0.6000
  - id: my_black
    red:   0.0000
    green: 0.0000
    blue:  0.0000

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

  - file: 'gfonts://Roboto'
    id: font40
    size: 40
    glyphs: °.0123456789-%d
  
  - file: 'fonts/materialdesignicons-webfont.ttf'
    id: font_icon_spotify
    size: 75
    glyphs:
      - "\U000F040C" # play-circle
      - "\U000F03E5" # pause-circle
      - "\U000F0661" # skip-next-circle
      - "\U000F0376" # minus-circle
      - "\U000F0417" # plus-circle
