Login
1 branch 0 tags
Ben (Desktop/Arch) Resized roboto 12->14 a1253f9 1 month ago 58 Commits
moon / esp32 / main / esp32_main.c
#include "moon.h"
#include "audio_player.h"
#include "buttons.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";

// Stub implementations - ESP32 uses ST7735 driver directly, not blit_buf
void blit_buf(const void *buf, int yStart, int yEnd) {
    (void)buf; (void)yStart; (void)yEnd;
}

void blit_buf_rect(const void *buf, int xStart, int yStart, int xEnd, int yEnd) {
    (void)buf; (void)xStart; (void)yStart; (void)xEnd; (void)yEnd;
}

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

static int64_t last_tick_us = 0;

void app_main(void) {
    // Subscribe main task to watchdog so we can feed it during long renders
    esp_task_wdt_add(xTaskGetCurrentTaskHandle());

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

    ESP_LOGI(TAG, "Initializing buttons...");
    buttons_init();

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

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

    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);
        }

        // Poll button state
        buttons_update();

        // Run application loop
        loop();

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

        // Feed watchdog after LVGL rendering (which can be slow with CJK font)
        esp_task_wdt_reset();

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