Home » Softronics » Guition 4.3″ Homeassistant Timer module & Pinpad

Guition 4.3″ Homeassistant Timer module & Pinpad

Code and stuffs to create a home assistant timer for the Guition JC4827W543C 4.3″ 480*272 capacitive board.

# Board: Guition JC4827W543C 4.3" 480*272
# Definition: definitions/boards/guition_jc4827543c/manifest.yaml

esphome:
  name: dash1
  friendly_name: PinpadWithTimerCapacitiveWorking
  platformio_options:
    board_build.flash_mode: dio

esp32:
  board: esp32-s3-devkitc-1
  variant: esp32s3
  flash_size: 4MB
  framework:
    type: esp-idf

logger:

  # JC4827W543 has 8MB octal PSRAM
psram:
  mode: octal
  speed: 80MHz

api:
  encryption:
    key: "VERY_SECRET_KEY"

ota:
  - platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: Dash1 Fallback Hotspot
    password: "Ryr4DdrvJ9kv"

captive_portal:
# ================= BUS CONFIGURATION =================
# Display: QSPI (NV3041A)
spi:
  id: tft_spi
  type: quad
  clk_pin: GPIO47
  data_pins: [GPIO21, GPIO48, GPIO40, GPIO39]
 
# Touch: GT911 on I2C
i2c:
  - id: bus_a
    sda: GPIO8
    scl: GPIO4
    scan: true
 
globals:
  - id: target_timestamp
    type: double
    initial_value: '0.0'
  - id: current_page
    type: std::string
    initial_value: '"TIMER"'
  - id: entered_code
    type: std::string
    initial_value: '""'
  - id: pad_status
    type: std::string
    initial_value: '"Enter PIN"'
 
text_sensor:
  - platform: homeassistant
    id: s_timer_main_state
    entity_id: timer.s_timer
    on_value:
      then:
        - component.update: my_display
  - platform: homeassistant
    id: s_timer_state
    entity_id: timer.s_timer
    attribute: remaining
    on_value:
      then:
        - component.update: my_display
  - platform: homeassistant
    id: s_timer_duration
    entity_id: timer.s_timer
    attribute: duration
    on_value:
      then:
        - component.update: my_display
 
sensor:
  - platform: homeassistant
    id: s_timer_finishes_epoch
    entity_id: timer.s_timer
    attribute: finishes_at
    device_class: timestamp
    on_value:
      then:
        - lambda: |-
            id(target_timestamp) = x;
        - component.update: my_display
 
binary_sensor:
  - platform: template
    name: "PinPad Security Access"
    id: s_pin_success
    device_class: lock
 
font:
  - file: gfonts://Roboto
    id: timer_font
    size: 72
  - file: gfonts://Roboto
    id: pad_font
    size: 64
  - file: gfonts://Roboto
    id: status_font
    size: 26
 
output:
  - platform: ledc
    id: backlight_pwm
    frequency: 1000Hz
    pin: GPIO1
 
light:
  - platform: monochromatic
    name: Display Backlight
    id: tft_backlight
    output: backlight_pwm
    restore_mode: ALWAYS_ON
 
script:
  # 1. Verifies the security code entered by the user
  - id: verify_pin
    then:
      - lambda: |-
          if (id(entered_code) == "1234") {
            id(pad_status) = "Success!";
            id(s_pin_success).publish_state(true);
          } else {
            id(pad_status) = "Invalid PIN!";
          }
      - component.update: my_display
      - delay: 2s
      - lambda: |-
          id(s_pin_success).publish_state(false);
          id(entered_code) = "";
          id(pad_status) = "Enter PIN";
          id(current_page) = "TIMER";
      - component.update: my_display
 
  # 2. Inactivity watchdog timer for the PIN Pad
  - id: pinpad_timeout_watchdog
    mode: restart
    then:
      - delay: 30s
      - lambda: |-
          if (id(current_page) == "PINPAD") {
            id(entered_code) = "";
            id(pad_status) = "Enter PIN";
            id(current_page) = "TIMER";
            id(my_display).update();
          }
 
display:
  - platform: qspi_dbi
    id: my_display
    spi_id: tft_spi
    model: CUSTOM    
    cs_pin:
      number: 45
      ignore_strapping_warning: true
    dimensions:
      width: 480
      height: 272
    data_rate: 20MHz
    invert_colors: true
    rotation: 180
    auto_clear_enabled: false
    update_interval: 1s
    init_sequence:
      - [0xff,0xa5]
      - [0x36,0xc0]
      - [0x3A,0x01]
      - [0x41,0x03]
      - [0x44,0x15]
      - [0x45,0x15]
      - [0x7d,0x03]
      - [0xc1,0xbb]
      - [0xc2,0x05]
      - [0xc3,0x10]
      - [0xc6,0x3e]
      - [0xc7,0x25]
      - [0xc8,0x11]
      - [0x7a,0x5f]
      - [0x6f,0x44]
      - [0x78,0x70]
      - [0xc9,0x00]
      - [0x67,0x21]
      - [0x51,0x0a]
      - [0x52,0x76]
      - [0x53,0x0a]
      - [0x54,0x76]
      - [0x46,0x0a]
      - [0x47,0x2a]
      - [0x48,0x0a]
      - [0x49,0x1a]
      - [0x56,0x43]
      - [0x57,0x42]
      - [0x58,0x3c]
      - [0x59,0x64]
      - [0x5a,0x41]
      - [0x5b,0x3c]
      - [0x5c,0x02]
      - [0x5d,0x3c]
      - [0x5e,0x1f]
      - [0x60,0x80]
      - [0x61,0x3f]
      - [0x62,0x21]
      - [0x63,0x07]
      - [0x64,0xe0]
      - [0x65,0x02]
      - [0xca,0x20]
      - [0xcb,0x52]
      - [0xcc,0x10]
      - [0xcd,0x42]
      - [0xd0,0x20]
      - [0xd1,0x52]
      - [0xd2,0x10]
      - [0xd3,0x42]
      - [0xd4,0x0a]
      - [0xd5,0x32]
      - [0x80,0x00]
      - [0xa0,0x00]
      - [0x81,0x07]
      - [0xa1,0x06]
      - [0x82,0x02]
      - [0xa2,0x01]
      - [0x86,0x11]
      - [0xa6,0x10]
      - [0x87,0x27]
      - [0xa7,0x27]
      - [0x83,0x37]
      - [0xa3,0x37]
      - [0x84,0x35]
      - [0xa4,0x35]
      - [0x85,0x3f]
      - [0xa5,0x3f]
      - [0x88,0x0b]
      - [0xa8,0x0b]
      - [0x89,0x14]
      - [0xa9,0x14]
      - [0x8a,0x1a]
      - [0xaa,0x1a]
      - [0x8b,0x0a]
      - [0xab,0x0a]
      - [0x8c,0x14]
      - [0xac,0x08]
      - [0x8d,0x17]
      - [0xad,0x07]
      - [0x8e,0x16]
      - [0xae,0x06]
      - [0x8f,0x1B]
      - [0xaf,0x07]
      - [0x90,0x04]
      - [0xb0,0x04]
      - [0x91,0x0a]
      - [0xb1,0x0a]
      - [0x92,0x16]
      - [0xb2,0x15]
      - [0xff,0x00]
      - [0x11,0x00]
      - [0x29,0x00]
    lambda: |-
      // ================= PAGE 1: TIMER INTERFACE =================
      if (id(current_page) == "TIMER") {
        Color bg_color = Color(20, 20, 20);
        Color text_active = Color(0, 255, 100);
        Color text_paused = Color(255, 140, 0);
        Color text_idle = Color(120, 120, 120);
        Color bar_fill_color = Color(0, 150, 255);
        Color bar_bg_color = Color(60, 60, 60);
 
        it.fill(bg_color);
 
        std::string state = id(s_timer_main_state).state;
        std::string remaining = id(s_timer_state).state;
        std::string duration_str = id(s_timer_duration).state;
 
        long total_seconds = 0;
        long remaining_seconds = 0;
        int dur_h = 0, dur_m = 0, dur_s = 0;
 
        if (sscanf(duration_str.c_str(), "%d:%d:%d", &dur_h, &dur_m, &dur_s) >= 2) {
            total_seconds = (dur_h * 3600) + (dur_m * 60) + dur_s;
        }
 
        if (state == "active") {
            double target_epoch = id(target_timestamp);
            double now_epoch = (double)::time(nullptr);
 
            if (target_epoch > 0) {
                remaining_seconds = (long)(target_epoch - now_epoch);
                if (remaining_seconds < 0) remaining_seconds = 0;
 
                int total_m = remaining_seconds / 60;
                int s = remaining_seconds % 60;
                it.printf(240, 105, id(timer_font), text_active, TextAlign::CENTER, "%02d:%02d", total_m, s);
            } else {
                it.print(240, 105, id(timer_font), text_active, TextAlign::CENTER, remaining.c_str());
            }
        } else if (state == "paused") {
            int rem_h = 0, rem_m = 0, rem_s = 0;
            if (sscanf(remaining.c_str(), "%d:%d:%d", &rem_h, &rem_m, &rem_s) >= 2) {
                remaining_seconds = (rem_h * 3600) + (rem_m * 60) + rem_s;
            }
            int total_paused_m = (rem_h * 60) + rem_m;
            it.printf(240, 105, id(timer_font), text_paused, TextAlign::CENTER, "%02d:%02d", total_paused_m, rem_s);
        } else {
            it.print(240, 125, id(timer_font), text_idle, TextAlign::CENTER, "Idle");
        }
 
        if ((state == "active" || state == "paused") && total_seconds > 0 && remaining_seconds <= total_seconds) {
            int bar_x = 0;
            int bar_y = 232;
            int bar_width = 480;
            int bar_height = 40;
            float progress = (float)remaining_seconds / (float)total_seconds;
            if (progress > 1.0f) progress = 1.0f;
            if (progress < 0.0f) progress = 0.0f;
            int fill_width = (int)(bar_width * progress);
 
            it.rectangle(bar_x, bar_y, bar_width, bar_height, bar_bg_color);
            if (fill_width > 0) {
                it.filled_rectangle(bar_x, bar_y, fill_width, bar_height, bar_fill_color);
            }
        }
      }
 
      // ================= PAGE 2: PIN PAD INTERFACE =================
      else if (id(current_page) == "PINPAD") {
        Color bg_color = Color(15, 15, 15);
        Color text_color = Color(255, 255, 255);
        Color btn_default = Color(40, 40, 40);
        Color red = Color(255, 0, 50);
 
        it.fill(bg_color);
 
        // Left panel: status + masked PIN
        std::string masked(id(entered_code).length(), '*');
        it.print(100, 70, id(status_font), text_color, TextAlign::CENTER, id(pad_status).c_str());
        it.print(100, 150, id(pad_font), red, TextAlign::CENTER, masked.c_str());
 
        // Right panel: keypad, 3 columns x 4 rows
        // columns x = 200, 290, 380 (w 84); rows y = 8, 72, 136, 200 (h 58)
        const char *labels[12] = {"1","2","3","4","5","6","7","8","9","CLR","0","ENT"};
        for (int r = 0; r < 4; r++) {
          for (int c = 0; c < 3; c++) {
            int bx = 200 + c * 90;
            int by = 8 + r * 64;
            it.filled_rectangle(bx, by, 84, 58, btn_default);
            it.print(bx + 42, by + 29, id(status_font), text_color, TextAlign::CENTER, labels[r * 3 + c]);
          }
        }
      }
 
touchscreen:
  - platform: gt911
    id: cyd_touch
    i2c_id: bus_a
    interrupt_pin:
      number: 3
      ignore_strapping_warning: true
    reset_pin: GPIO38
    transform:
      mirror_x: false
      mirror_y: false
    on_touch:
      - script.execute: pinpad_timeout_watchdog
      - lambda: |-
          if (id(current_page) == "TIMER") {
            id(current_page) = "PINPAD";
            id(entered_code) = "";
            id(pad_status) = "Enter PIN";
            id(my_display).update();
            return;
          }
 
          static uint32_t last_click = 0;
          uint32_t now = millis();
          if (now - last_click < 200) {
            return;
          }
 
          int x = touch.x;
          int y = touch.y;
 
          // Hit-test the keypad grid (pitch 90 x 64, buttons 84 x 58)
          int col = -1, row = -1;
          if (x >= 200 && x < 470) {
            int cx = x - 200;
            if (cx % 90 < 84) col = cx / 90;
          }
          if (y >= 8 && y < 264) {
            int ry = y - 8;
            if (ry % 64 < 58) row = ry / 64;
          }
          if (col < 0 || col > 2 || row < 0 || row > 3) return;
 
          int idx = row * 3 + col;   // 0..11
          last_click = now;
 
          if (idx == 9) {            // CLR
            id(entered_code) = "";
            id(pad_status) = "Enter PIN";
            id(my_display).update();
          } else if (idx == 11) {    // ENT
            id(pad_status) = "Verifying...";
            id(verify_pin).execute();
          } else {
            const char digits[] = {'1','2','3','4','5','6','7','8','9','*','0','#'};
            if (id(entered_code).length() < 8) id(entered_code) += digits[idx];
            if (id(entered_code).length() == 4) {
              id(pad_status) = "Verifying...";
              id(verify_pin).execute();
            } else {
              id(my_display).update();
            }
          }
 
time:
  - platform: homeassistant
    id: homeassistant_time