update: shifted to LVGL's native GIF decoder
This commit is contained in:
134
main/main.c
134
main/main.c
@@ -16,7 +16,7 @@
|
||||
#include "esp_heap_caps.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
#include "gifdec.h"
|
||||
#include "src/extra/libs/gif/lv_gif.h"
|
||||
#include "face.h"
|
||||
|
||||
/* LCD size */
|
||||
@@ -52,117 +52,21 @@ static esp_lcd_panel_handle_t lcd_panel = NULL;
|
||||
/* LVGL display */
|
||||
static lv_display_t *lvgl_disp = NULL;
|
||||
|
||||
typedef struct {
|
||||
gd_GIF *gif;
|
||||
lv_obj_t *canvas;
|
||||
lv_timer_t *timer;
|
||||
uint16_t w;
|
||||
uint16_t h;
|
||||
uint8_t *rgb888;
|
||||
lv_color_t *rgb565;
|
||||
} gif_player_t;
|
||||
static lv_obj_t *g_lvgl_gif = NULL;
|
||||
static const lv_img_dsc_t g_lvgl_gif_src = {
|
||||
.header.always_zero = 0,
|
||||
.header.w = 240,
|
||||
.header.h = 135,
|
||||
.header.cf = LV_IMG_CF_RAW,
|
||||
.data_size = sizeof(videoplayback1_ezgif_com_optimize),
|
||||
.data = videoplayback1_ezgif_com_optimize,
|
||||
};
|
||||
|
||||
static gif_player_t g_gif = {0};
|
||||
|
||||
static inline void *lvgl_timer_user_data(lv_timer_t *t)
|
||||
static esp_err_t app_gif_init_lvgl(lv_obj_t *parent)
|
||||
{
|
||||
#if LVGL_VERSION_MAJOR >= 9
|
||||
return lv_timer_get_user_data(t);
|
||||
#else
|
||||
return t->user_data;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void gif_timer_cb(lv_timer_t *t)
|
||||
{
|
||||
gif_player_t *p = (gif_player_t *)lvgl_timer_user_data(t);
|
||||
if (!p || !p->gif) {
|
||||
return;
|
||||
}
|
||||
|
||||
int r = gd_get_frame(p->gif);
|
||||
if (r == 0) {
|
||||
gd_rewind(p->gif);
|
||||
r = gd_get_frame(p->gif);
|
||||
}
|
||||
if (r < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
gd_render_frame(p->gif, p->rgb888);
|
||||
|
||||
const size_t px = (size_t)p->w * (size_t)p->h;
|
||||
const uint8_t *src = p->rgb888;
|
||||
for (size_t i = 0; i < px; i++) {
|
||||
const uint8_t r8 = *src++;
|
||||
const uint8_t g8 = *src++;
|
||||
const uint8_t b8 = *src++;
|
||||
p->rgb565[i] = lv_color_make(r8, g8, b8);
|
||||
}
|
||||
|
||||
/* Schedule next frame based on GIF delay (centiseconds -> ms) */
|
||||
uint32_t delay_ms = (uint32_t)p->gif->gce.delay * 10U;
|
||||
if (delay_ms < 10U) delay_ms = 10U;
|
||||
lv_timer_set_period(t, delay_ms);
|
||||
|
||||
lv_obj_invalidate(p->canvas);
|
||||
}
|
||||
|
||||
static esp_err_t app_gif_init(lv_obj_t *parent)
|
||||
{
|
||||
g_gif.gif = gd_open_gif_mem(videoplayback1_ezgif_com_optimize, sizeof(videoplayback1_ezgif_com_optimize));
|
||||
if (!g_gif.gif) {
|
||||
ESP_LOGE(TAG, "Failed to open GIF from memory");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
g_gif.w = g_gif.gif->width;
|
||||
g_gif.h = g_gif.gif->height;
|
||||
|
||||
const size_t px = (size_t)g_gif.w * (size_t)g_gif.h;
|
||||
|
||||
g_gif.rgb888 = (uint8_t *)heap_caps_malloc(px * 3, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (!g_gif.rgb888) {
|
||||
ESP_LOGE(TAG, "No memory for GIF RGB888 buffer");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
g_gif.rgb565 = (lv_color_t *)heap_caps_aligned_alloc(16, px * sizeof(lv_color_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
if (!g_gif.rgb565) {
|
||||
ESP_LOGE(TAG, "No memory for GIF canvas buffer");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
g_gif.canvas = lv_canvas_create(parent);
|
||||
#if LVGL_VERSION_MAJOR >= 9
|
||||
lv_canvas_set_buffer(g_gif.canvas, g_gif.rgb565, g_gif.w, g_gif.h, LV_COLOR_FORMAT_RGB565);
|
||||
#else
|
||||
lv_canvas_set_buffer(g_gif.canvas, g_gif.rgb565, g_gif.w, g_gif.h, LV_IMG_CF_TRUE_COLOR);
|
||||
#endif
|
||||
lv_obj_set_style_border_width(g_gif.canvas, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_all(g_gif.canvas, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_bg_opa(g_gif.canvas, LV_OPA_TRANSP, LV_PART_MAIN);
|
||||
|
||||
/* First frame */
|
||||
if (gd_get_frame(g_gif.gif) <= 0) {
|
||||
gd_rewind(g_gif.gif);
|
||||
(void)gd_get_frame(g_gif.gif);
|
||||
}
|
||||
gd_render_frame(g_gif.gif, g_gif.rgb888);
|
||||
for (size_t i = 0; i < px; i++) {
|
||||
const uint8_t r8 = g_gif.rgb888[i * 3 + 0];
|
||||
const uint8_t g8 = g_gif.rgb888[i * 3 + 1];
|
||||
const uint8_t b8 = g_gif.rgb888[i * 3 + 2];
|
||||
g_gif.rgb565[i] = lv_color_make(r8, g8, b8);
|
||||
}
|
||||
|
||||
/* Place the GIF below the title */
|
||||
lv_obj_align(g_gif.canvas, LV_ALIGN_TOP_MID, 0, 45);
|
||||
|
||||
uint32_t delay_ms = (uint32_t)g_gif.gif->gce.delay * 10U;
|
||||
if (delay_ms < 10U) delay_ms = 10U;
|
||||
g_gif.timer = lv_timer_create(gif_timer_cb, delay_ms, &g_gif);
|
||||
|
||||
g_lvgl_gif = lv_gif_create(parent);
|
||||
lv_gif_set_src(g_lvgl_gif, &g_lvgl_gif_src);
|
||||
lv_obj_align(g_lvgl_gif, LV_ALIGN_TOP_MID, 0, 45);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -306,20 +210,14 @@ void app_main(void)
|
||||
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, LV_PART_MAIN);
|
||||
|
||||
/* Black text samples */
|
||||
lv_obj_t *title = lv_label_create(scr);
|
||||
lv_label_set_text(title, "Iro Bot.");
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_16, LV_PART_MAIN);
|
||||
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
|
||||
|
||||
/* Add GIF canvas */
|
||||
ESP_ERROR_CHECK(app_gif_init(scr));
|
||||
ESP_ERROR_CHECK(app_gif_init_lvgl(scr));
|
||||
|
||||
lv_obj_t *sample = lv_label_create(scr);
|
||||
lv_label_set_text(sample, "Hey Neil!\nHow are you today?");
|
||||
lv_obj_set_style_text_color(sample, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
|
||||
lv_obj_set_style_text_font(sample, &lv_font_montserrat_16, LV_PART_MAIN);
|
||||
lv_obj_align_to(sample, g_gif.canvas, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
|
||||
lv_obj_align_to(sample, g_lvgl_gif, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
|
||||
|
||||
lvgl_port_unlock();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user