#include "emotion_manager.h" #include "app_gifdec.h" #include "esp_heap_caps.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include /* Include the GIF assets */ #include "../assets/emotions/blink.h" static const char *TAG = "EMOTION_MANAGER"; static app_gd_GIF *current_gif = NULL; static lv_obj_t *img_obj = NULL; static lv_timer_t *update_timer = NULL; static uint8_t *render_buf_32 = NULL; static lv_img_dsc_t img_dsc; typedef struct { const uint8_t *data; size_t size; } emotion_asset_t; static const emotion_asset_t assets[EMOTION_COUNT] = { [EMOTION_BLINK] = {.data = blink, .size = sizeof(blink)}, // Add placeholders for Smile and Sad if needed [EMOTION_SMILE] = {NULL, 0}, [EMOTION_SAD] = {NULL, 0}, }; static void timer_cb(lv_timer_t *t) { if (!current_gif) return; int ret = app_gd_get_frame(current_gif); if (ret == 0) { /* End of GIF, loop back */ app_gd_rewind(current_gif); ret = app_gd_get_frame(current_gif); } if (ret > 0) { uint32_t start_tick = xTaskGetTickCount(); /* Render frame directly into 32-bit ARGB buffer */ /* This saves memory bandwidth and CPU cycles by combining decoding and * swizzling */ app_gd_render_frame_to_argb(current_gif, render_buf_32); uint32_t end_tick = xTaskGetTickCount(); uint32_t render_time_ms = (end_tick - start_tick) * portTICK_PERIOD_MS; /* Refresh image */ lv_obj_invalidate(img_obj); /* Set timer period based on GIF delay */ int delay = current_gif->gce.delay * 10; // ESP_LOGI(TAG, "GIF Delay form GCE: %d ms", delay); if (delay < 20) delay = 20; // Minimum delay safeguard (allow faster playback) // Log Load ESP_LOGI(TAG, "GIF Render: %lu ms, Target Delay: %d ms, Load: %lu%%", render_time_ms, delay, (render_time_ms * 100) / delay); lv_timer_set_period(update_timer, delay); } } void emotion_manager_init(lv_obj_t *parent) { if (img_obj) return; ESP_LOGI(TAG, "Initializing emotion manager"); /* Create the image object */ img_obj = lv_img_create(parent); lv_obj_set_size(img_obj, 368, 460); lv_obj_center(img_obj); lv_obj_set_scrollbar_mode(img_obj, LV_SCROLLBAR_MODE_OFF); /* Allocate buffers for frames in PSRAM */ /* Only need 32-bit buffer now as we render directly to it */ render_buf_32 = heap_caps_malloc(368 * 460 * 4, MALLOC_CAP_SPIRAM); if (!render_buf_32) { ESP_LOGE(TAG, "Failed to allocate render buffer in PSRAM"); return; } /* Setup image descriptor */ img_dsc.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA; img_dsc.header.always_zero = 0; img_dsc.header.w = 368; img_dsc.header.h = 460; img_dsc.data_size = 368 * 460 * 4; img_dsc.data = render_buf_32; lv_img_set_src(img_obj, &img_dsc); /* Create timer for animation */ update_timer = lv_timer_create(timer_cb, 100, NULL); lv_timer_pause(update_timer); /* Set default emotion */ emotion_manager_set(EMOTION_BLINK); } void emotion_manager_set(emotion_t emotion) { if (emotion >= EMOTION_COUNT) return; if (!assets[emotion].data) { ESP_LOGW(TAG, "Asset for emotion %d not found", emotion); return; } ESP_LOGI(TAG, "Setting emotion to %d", emotion); lv_timer_pause(update_timer); if (current_gif) { app_gd_close_gif(current_gif); current_gif = NULL; } current_gif = app_gd_open_gif_mem(assets[emotion].data, assets[emotion].size); if (!current_gif) { ESP_LOGE(TAG, "Failed to open GIF for emotion %d", emotion); return; } app_gd_rewind(current_gif); /* Render first frame immediately to avoid garbage on screen */ timer_cb(NULL); lv_timer_resume(update_timer); } void emotion_manager_update(void) { // This could be used for manual polling if timer is not used }