/**
 **************************************************
 *
 * @file        RTCCalibration.ino
 * @brief       Example showing how to Calibrate the RTC on Inkplate 5V2
 *              to be more precise and accurate.
 *              Uses LVGL in grayscale (L8) mode.
 *
 * For info on how to quickly get started with Inkplate 5V2 visit
 * https://soldered.com/documentation/inkplate/5/overview/
 *
 * @authors     Soldered
 * @date        November 2025
 ***************************************************/
#include <Inkplate-LVGL.h>

Inkplate inkplate(INKPLATE_1BIT); // LVGL-enabled Inkplate instance (Inkplate 5V2, grayscale / L8)

#define REFRESH_DELAY 1000 // Read RTC every second
unsigned long time1 = 0;

// RTC start values
uint8_t hours   = 0;
uint8_t minutes = 0;
uint8_t seconds = 0;

// Pointers to LVGL labels
lv_obj_t *mainLabel = nullptr;

// Update LVGL label and refresh display
void setLabel(const char *text)
{
    lv_label_set_text(mainLabel, text);
    lv_obj_align(mainLabel, LV_ALIGN_CENTER, 0, 0);

    // Let LVGL process timers and internal state,
    // then push a full grayscale (L8) frame to the Inkplate
    lv_timer_handler();
    inkplate.display();
}

void setup()
{
    Serial.begin(115200);

    // Initialize Inkplate in LVGL FULL render mode
    // (Inkplate 5V2, grayscale / L8 handled by the Inkplate-LVGL library config)
    inkplate.begin(LV_DISP_RENDER_MODE_FULL);

    // White background (L8 grayscale: 0xFFFFFF is treated as white)
    lv_obj_set_style_bg_color(lv_screen_active(),
                              lv_color_hex(0xFFFFFF),
                              LV_PART_MAIN);

    // Create primary LVGL label
    mainLabel = lv_label_create(lv_screen_active());
    lv_obj_set_style_text_font(mainLabel, &lv_font_montserrat_28, 0);
    lv_obj_set_style_text_color(mainLabel, lv_color_hex(0x000000), LV_PART_MAIN);

    // Initial message
    setLabel("RTC calibration\n\n"
             "Open Serial Monitor\n"
             "at 115200 baud.");

    // Configure wake-up button input
    // On Inkplate 5V2 the “WAKE” button is connected to GPIO 36 (same as many examples)
    pinMode(GPIO_NUM_36, INPUT);

    // Set internal RTC capacitor (12.5 pF recommended)
    inkplate.rtc.setInternalCapacitor(RTC_12_5PF);

    // Set RTC frequency offset (example values; adjust to your calibration)
    inkplate.rtc.setClockOffset(1, -63);

    Serial.println("Press the wake-up button to start RTC");

    setLabel("RTC calibration\n\n"
             "Press WAKE button to start.");

    // Wait for button press
    while (digitalRead(GPIO_NUM_36) == HIGH)
    {
        delay(10);
    }

    // Initialize RTC time
    inkplate.rtc.setTime(hours, minutes, seconds);

    setLabel("RTC started.\n"
             "Tracking time...\n"
             "See Serial Monitor at 115200 baud.");
}

void loop()
{
    // Print new time every second
    if (millis() - time1 > REFRESH_DELAY)
    {
        inkplate.rtc.getRtcData();

        seconds = inkplate.rtc.getSecond();
        minutes = inkplate.rtc.getMinute();
        hours   = inkplate.rtc.getHour();

        Serial.printf("%02d:%02d:%02d\n", hours, minutes, seconds);

        time1 = millis();
    }
}
