#include "devices/Power.h"
#include "devices/Display.h"

#include "PwmBacklight.h"
#include <Tactility/SystemEvents.h>
#include <Tactility/TactilityCore.h>
#include <tactility/log.h>

#define TAG "tdisplay-s3"

static bool powerOn() {
    gpio_config_t power_signal_config = {
        .pin_bit_mask = BIT64(TDISPLAY_S3_POWERON_GPIO),
        .mode = GPIO_MODE_OUTPUT,
        .pull_up_en = GPIO_PULLUP_DISABLE,
        .pull_down_en = GPIO_PULLDOWN_DISABLE,
        .intr_type = GPIO_INTR_DISABLE,
    };

    if (gpio_config(&power_signal_config) != ESP_OK) {
        return false;
    }

    if (gpio_set_level(TDISPLAY_S3_POWERON_GPIO, 1) != ESP_OK) {
        return false;
    }

    return true;
}

bool initBoot() {
    LOG_I(TAG, "Powering on");
    if (!powerOn()) {
        LOG_E(TAG, "Power on failed");
        return false;
    }

    if (!driver::pwmbacklight::init(DISPLAY_BL, 30000)) {
        LOG_E(TAG, "Backlight init failed");
        return false;
    }

    return true;
}