Login
1 branch 0 tags
Ben (Desktop/Arch) lvgl 67cbd91 1 month ago 12 Commits
moon / firmware / esp32 / main / esp32_main.c
#include "moon.h"
#include "audio.h"
#include "st7735.h"
#include "lvgl_port.h"

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_task_wdt.h"

static const char *TAG = "moon";

// Forward declarations from shared code
extern void setup(void);
extern void loop(void);

static int64_t last_tick_us = 0;

void app_main(void) {
    // Disable task watchdog for main task (LVGL rendering can be slow)
    esp_task_wdt_delete(xTaskGetCurrentTaskHandle());

    ESP_LOGI(TAG, "Initializing ST7735 display...");
    st7735_init();

    ESP_LOGI(TAG, "Initializing LVGL...");
    lvgl_port_init();

    ESP_LOGI(TAG, "Running setup...");
    setup();

    ESP_LOGI(TAG, "Initializing audio...");
    audio_init();

    last_tick_us = esp_timer_get_time();

    ESP_LOGI(TAG, "Entering main loop...");
    while (1) {
        // Calculate elapsed time for LVGL tick
        int64_t now_us = esp_timer_get_time();
        uint32_t elapsed_ms = (now_us - last_tick_us) / 1000;
        last_tick_us = now_us;

        // Update LVGL tick
        if (elapsed_ms > 0) {
            lv_tick_inc(elapsed_ms);
        }

        // Run application loop
        loop();

        // Handle LVGL tasks (includes rendering)
        lv_timer_handler();

        // Yield to other tasks (~30fps is plenty for UI)
        vTaskDelay(pdMS_TO_TICKS(10));
    }
}