986 lines
27 KiB
C
986 lines
27 KiB
C
/**
|
|
* @file lottie_player.c
|
|
* @brief Lightweight Lottie animation player implementation for LVGL on ESP32
|
|
*
|
|
* This implementation provides a minimal Lottie JSON parser and renderer
|
|
* optimized for simple shape-based animations. It supports:
|
|
* - Rounded rectangle shapes
|
|
* - Position, size, and corner radius animation
|
|
* - Keyframe interpolation with bezier easing
|
|
* - Solid color fills
|
|
*
|
|
* The parser extracts only the data needed for rendering, minimizing memory
|
|
* usage on resource-constrained embedded systems.
|
|
*/
|
|
|
|
#include "lottie_player.h"
|
|
#include "cJSON.h"
|
|
#include "esp_heap_caps.h"
|
|
#include "esp_log.h"
|
|
#include "esp_random.h"
|
|
#include "esp_timer.h"
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static const char *TAG = "lottie_player";
|
|
|
|
// Maximum supported keyframes per property (memory optimization)
|
|
#define MAX_KEYFRAMES 16
|
|
|
|
// Maximum supported layers in animation
|
|
#define MAX_LAYERS 8
|
|
|
|
// Maximum supported shapes per layer
|
|
#define MAX_SHAPES 4
|
|
|
|
/**
|
|
* Keyframe data structure for animated properties
|
|
*/
|
|
typedef struct {
|
|
float time; // Frame time
|
|
float value[2]; // Value at this keyframe (x, y or single value)
|
|
float in_tangent[2]; // Bezier in-tangent
|
|
float out_tangent[2]; // Bezier out-tangent
|
|
bool hold; // If true, hold value until next keyframe
|
|
} lottie_keyframe_t;
|
|
|
|
/**
|
|
* Animated property that can have keyframes or static value
|
|
*/
|
|
typedef struct {
|
|
bool animated;
|
|
float static_value[2];
|
|
lottie_keyframe_t keyframes[MAX_KEYFRAMES];
|
|
uint8_t keyframe_count;
|
|
} lottie_property_t;
|
|
|
|
/**
|
|
* Rounded rectangle shape definition
|
|
*/
|
|
typedef struct {
|
|
lottie_property_t position; // Center position
|
|
lottie_property_t size; // Width and height
|
|
lottie_property_t corner_radius; // Corner radius
|
|
lv_color_t fill_color; // Fill color
|
|
uint8_t fill_opacity; // Fill opacity (0-100)
|
|
bool valid;
|
|
} lottie_rounded_rect_t;
|
|
|
|
/**
|
|
* Layer in the animation
|
|
*/
|
|
typedef struct {
|
|
int index; // Layer index
|
|
int parent; // Parent layer index (-1 if none)
|
|
int type; // Layer type (3=null, 4=shape)
|
|
lottie_property_t position; // Transform position
|
|
lottie_property_t anchor; // Anchor point
|
|
lottie_rounded_rect_t shapes[MAX_SHAPES];
|
|
uint8_t shape_count;
|
|
bool valid;
|
|
} lottie_layer_t;
|
|
|
|
/**
|
|
* Main player state structure
|
|
*/
|
|
struct lottie_player_t {
|
|
// Animation metadata
|
|
uint16_t width;
|
|
uint16_t height;
|
|
float framerate;
|
|
uint32_t total_frames;
|
|
uint32_t in_point; // Start frame
|
|
uint32_t out_point; // End frame
|
|
|
|
// Layers
|
|
lottie_layer_t layers[MAX_LAYERS];
|
|
uint8_t layer_count;
|
|
|
|
// Playback state
|
|
float current_frame;
|
|
bool playing;
|
|
bool loop;
|
|
int64_t start_time_us; // Animation start time in microseconds
|
|
|
|
// Natural blink mode
|
|
bool natural_blink_mode; // If true, shows idle state with random blinks
|
|
lv_timer_t *blink_timer; // Timer for triggering random blinks
|
|
|
|
// LVGL objects
|
|
lv_obj_t *canvas;
|
|
lv_color_t *canvas_buf;
|
|
lv_timer_t *timer;
|
|
};
|
|
|
|
// Forward declarations
|
|
static void lottie_parse_animation(lottie_player_handle_t handle,
|
|
const cJSON *root);
|
|
static void lottie_parse_layers(lottie_player_handle_t handle,
|
|
const cJSON *layers);
|
|
static void lottie_parse_layer(lottie_layer_t *layer, const cJSON *json);
|
|
static void lottie_parse_shapes(lottie_layer_t *layer, const cJSON *shapes);
|
|
static void lottie_parse_property(lottie_property_t *prop, const cJSON *json);
|
|
static float lottie_interpolate_property(const lottie_property_t *prop,
|
|
float frame, int index);
|
|
static void lottie_render_frame(lottie_player_handle_t handle);
|
|
static void lottie_timer_cb(lv_timer_t *timer);
|
|
static void lottie_blink_timer_cb(lv_timer_t *timer);
|
|
static void lottie_schedule_next_blink(lottie_player_handle_t handle);
|
|
static float lottie_bezier_interpolate(float t, float p0, float p1, float p2,
|
|
float p3);
|
|
|
|
/**
|
|
* Create a Lottie player from JSON data
|
|
*/
|
|
lottie_player_handle_t lottie_create_from_data(lv_obj_t *parent,
|
|
const char *json_data) {
|
|
if (!parent || !json_data) {
|
|
ESP_LOGE(TAG, "Invalid parameters");
|
|
return NULL;
|
|
}
|
|
|
|
// Parse JSON
|
|
cJSON *root = cJSON_Parse(json_data);
|
|
if (!root) {
|
|
ESP_LOGE(TAG, "Failed to parse JSON: %s", cJSON_GetErrorPtr());
|
|
return NULL;
|
|
}
|
|
|
|
// Allocate player structure
|
|
lottie_player_handle_t handle = calloc(1, sizeof(struct lottie_player_t));
|
|
if (!handle) {
|
|
ESP_LOGE(TAG, "Failed to allocate player");
|
|
cJSON_Delete(root);
|
|
return NULL;
|
|
}
|
|
|
|
// Set defaults
|
|
handle->playing = false;
|
|
handle->loop = true;
|
|
handle->current_frame = 0;
|
|
|
|
// Parse animation data
|
|
lottie_parse_animation(handle, root);
|
|
|
|
// Cleanup JSON (we've extracted what we need)
|
|
cJSON_Delete(root);
|
|
|
|
// Validate parsed data
|
|
if (handle->width == 0 || handle->height == 0 || handle->total_frames == 0) {
|
|
ESP_LOGE(TAG, "Invalid animation data");
|
|
free(handle);
|
|
return NULL;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Parsed animation: %dx%d, %lu frames at %.1f fps",
|
|
handle->width, handle->height, (unsigned long)handle->total_frames,
|
|
handle->framerate);
|
|
ESP_LOGI(TAG, "Loaded %d layers", handle->layer_count);
|
|
|
|
// Allocate canvas buffer
|
|
size_t buf_size = handle->width * handle->height * sizeof(lv_color_t);
|
|
handle->canvas_buf =
|
|
heap_caps_malloc(buf_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
if (!handle->canvas_buf) {
|
|
// Try regular RAM if SPIRAM not available
|
|
handle->canvas_buf = malloc(buf_size);
|
|
}
|
|
if (!handle->canvas_buf) {
|
|
ESP_LOGE(TAG, "Failed to allocate canvas buffer (%u bytes)",
|
|
(unsigned)buf_size);
|
|
free(handle);
|
|
return NULL;
|
|
}
|
|
|
|
// Create LVGL canvas
|
|
handle->canvas = lv_canvas_create(parent);
|
|
lv_canvas_set_buffer(handle->canvas, handle->canvas_buf, handle->width,
|
|
handle->height, LV_IMG_CF_TRUE_COLOR);
|
|
lv_obj_center(handle->canvas);
|
|
|
|
// Speed up animation for snappier blinks (2x speed)
|
|
// The canvas rendering is slow (~110ms per frame), so we need faster playback
|
|
handle->framerate *= 2.0f;
|
|
|
|
// Create timer for animation playback (use faster period for responsiveness)
|
|
uint32_t period_ms = 16; // ~60Hz refresh attempts
|
|
handle->timer = lv_timer_create(lottie_timer_cb, period_ms, handle);
|
|
lv_timer_pause(handle->timer);
|
|
|
|
// Enable natural blink mode by default
|
|
handle->natural_blink_mode = true;
|
|
handle->loop = false; // Don't loop in natural mode
|
|
|
|
// Show idle state (last frame = eyes open)
|
|
handle->current_frame = handle->total_frames - 1;
|
|
lottie_render_frame(handle);
|
|
|
|
// Create blink timer with random initial delay (2-5 seconds)
|
|
uint32_t initial_delay = 2000 + (esp_random() % 3000);
|
|
handle->blink_timer =
|
|
lv_timer_create(lottie_blink_timer_cb, initial_delay, handle);
|
|
lv_timer_set_repeat_count(handle->blink_timer,
|
|
1); // One-shot, will be recreated
|
|
|
|
ESP_LOGI(TAG, "Natural blink mode enabled, first blink in %lu ms",
|
|
(unsigned long)initial_delay);
|
|
|
|
return handle;
|
|
}
|
|
|
|
/**
|
|
* Parse top-level animation properties
|
|
*/
|
|
static void lottie_parse_animation(lottie_player_handle_t handle,
|
|
const cJSON *root) {
|
|
cJSON *item;
|
|
|
|
// Width
|
|
item = cJSON_GetObjectItem(root, "w");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
handle->width = (uint16_t)item->valueint;
|
|
}
|
|
|
|
// Height
|
|
item = cJSON_GetObjectItem(root, "h");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
handle->height = (uint16_t)item->valueint;
|
|
}
|
|
|
|
// Framerate
|
|
item = cJSON_GetObjectItem(root, "fr");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
handle->framerate = (float)item->valuedouble;
|
|
}
|
|
|
|
// In point (start frame)
|
|
item = cJSON_GetObjectItem(root, "ip");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
handle->in_point = (uint32_t)item->valueint;
|
|
}
|
|
|
|
// Out point (end frame)
|
|
item = cJSON_GetObjectItem(root, "op");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
handle->out_point = (uint32_t)item->valueint;
|
|
}
|
|
|
|
handle->total_frames = handle->out_point - handle->in_point;
|
|
|
|
// Parse layers
|
|
item = cJSON_GetObjectItem(root, "layers");
|
|
if (item && cJSON_IsArray(item)) {
|
|
lottie_parse_layers(handle, item);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parse layers array
|
|
*/
|
|
static void lottie_parse_layers(lottie_player_handle_t handle,
|
|
const cJSON *layers) {
|
|
int count = cJSON_GetArraySize(layers);
|
|
if (count > MAX_LAYERS) {
|
|
ESP_LOGW(TAG, "Truncating %d layers to %d", count, MAX_LAYERS);
|
|
count = MAX_LAYERS;
|
|
}
|
|
|
|
handle->layer_count = 0;
|
|
for (int i = 0; i < count; i++) {
|
|
cJSON *layer_json = cJSON_GetArrayItem(layers, i);
|
|
if (layer_json) {
|
|
lottie_layer_t *layer = &handle->layers[handle->layer_count];
|
|
memset(layer, 0, sizeof(lottie_layer_t));
|
|
layer->parent = -1;
|
|
lottie_parse_layer(layer, layer_json);
|
|
if (layer->valid) {
|
|
handle->layer_count++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parse a single layer
|
|
*/
|
|
static void lottie_parse_layer(lottie_layer_t *layer, const cJSON *json) {
|
|
cJSON *item;
|
|
|
|
// Layer index
|
|
item = cJSON_GetObjectItem(json, "ind");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
layer->index = item->valueint;
|
|
}
|
|
|
|
// Layer type
|
|
item = cJSON_GetObjectItem(json, "ty");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
layer->type = item->valueint;
|
|
}
|
|
|
|
// Parent index
|
|
item = cJSON_GetObjectItem(json, "parent");
|
|
if (item && cJSON_IsNumber(item)) {
|
|
layer->parent = item->valueint;
|
|
}
|
|
|
|
// Transform (ks)
|
|
cJSON *ks = cJSON_GetObjectItem(json, "ks");
|
|
if (ks) {
|
|
// Position
|
|
item = cJSON_GetObjectItem(ks, "p");
|
|
if (item) {
|
|
lottie_parse_property(&layer->position, item);
|
|
}
|
|
|
|
// Anchor point
|
|
item = cJSON_GetObjectItem(ks, "a");
|
|
if (item) {
|
|
lottie_parse_property(&layer->anchor, item);
|
|
}
|
|
}
|
|
|
|
// Shapes (for shape layers, type 4)
|
|
if (layer->type == 4) {
|
|
item = cJSON_GetObjectItem(json, "shapes");
|
|
if (item && cJSON_IsArray(item)) {
|
|
lottie_parse_shapes(layer, item);
|
|
}
|
|
}
|
|
|
|
layer->valid = true;
|
|
}
|
|
|
|
/**
|
|
* Parse shapes array for a layer
|
|
* Uses two-pass approach: first find fill color, then parse shapes
|
|
*/
|
|
static void lottie_parse_shapes(lottie_layer_t *layer, const cJSON *shapes) {
|
|
int count = cJSON_GetArraySize(shapes);
|
|
layer->shape_count = 0;
|
|
|
|
// PASS 1: Find fill color (it may come before or after shapes in the array)
|
|
lv_color_t fill_color = lv_color_white();
|
|
uint8_t fill_opacity = 100;
|
|
bool fill_found = false;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
cJSON *shape = cJSON_GetArrayItem(shapes, i);
|
|
if (!shape)
|
|
continue;
|
|
|
|
cJSON *ty = cJSON_GetObjectItem(shape, "ty");
|
|
if (!ty || !cJSON_IsString(ty))
|
|
continue;
|
|
|
|
const char *type = ty->valuestring;
|
|
|
|
// Fill color
|
|
if (strcmp(type, "fl") == 0) {
|
|
cJSON *c = cJSON_GetObjectItem(shape, "c");
|
|
if (c) {
|
|
cJSON *k = cJSON_GetObjectItem(c, "k");
|
|
if (k && cJSON_IsArray(k) && cJSON_GetArraySize(k) >= 3) {
|
|
float r = (float)cJSON_GetArrayItem(k, 0)->valuedouble;
|
|
float g = (float)cJSON_GetArrayItem(k, 1)->valuedouble;
|
|
float b = (float)cJSON_GetArrayItem(k, 2)->valuedouble;
|
|
fill_color = lv_color_make((uint8_t)(r * 255), (uint8_t)(g * 255),
|
|
(uint8_t)(b * 255));
|
|
fill_found = true;
|
|
ESP_LOGI(TAG, "Found fill: RGB(%.2f,%.2f,%.2f) -> (%d,%d,%d)", r, g,
|
|
b, (int)(r * 255), (int)(g * 255), (int)(b * 255));
|
|
}
|
|
}
|
|
cJSON *o = cJSON_GetObjectItem(shape, "o");
|
|
if (o) {
|
|
cJSON *k = cJSON_GetObjectItem(o, "k");
|
|
if (k && cJSON_IsNumber(k)) {
|
|
fill_opacity = (uint8_t)k->valueint;
|
|
}
|
|
}
|
|
break; // Found fill, stop searching
|
|
}
|
|
}
|
|
|
|
if (!fill_found) {
|
|
ESP_LOGW(TAG, "No fill found for layer, using default white");
|
|
}
|
|
|
|
// PASS 2: Parse shapes and apply the found fill color
|
|
for (int i = 0; i < count && layer->shape_count < MAX_SHAPES; i++) {
|
|
cJSON *shape = cJSON_GetArrayItem(shapes, i);
|
|
if (!shape)
|
|
continue;
|
|
|
|
cJSON *ty = cJSON_GetObjectItem(shape, "ty");
|
|
if (!ty || !cJSON_IsString(ty))
|
|
continue;
|
|
|
|
const char *type = ty->valuestring;
|
|
|
|
// Rounded rectangle
|
|
if (strcmp(type, "rc") == 0) {
|
|
lottie_rounded_rect_t *rect = &layer->shapes[layer->shape_count];
|
|
memset(rect, 0, sizeof(lottie_rounded_rect_t));
|
|
|
|
// Position
|
|
cJSON *p = cJSON_GetObjectItem(shape, "p");
|
|
if (p) {
|
|
lottie_parse_property(&rect->position, p);
|
|
}
|
|
|
|
// Size
|
|
cJSON *s = cJSON_GetObjectItem(shape, "s");
|
|
if (s) {
|
|
lottie_parse_property(&rect->size, s);
|
|
}
|
|
|
|
// Corner radius
|
|
cJSON *r = cJSON_GetObjectItem(shape, "r");
|
|
if (r) {
|
|
lottie_parse_property(&rect->corner_radius, r);
|
|
}
|
|
|
|
rect->fill_color = fill_color;
|
|
rect->fill_opacity = fill_opacity;
|
|
rect->valid = true;
|
|
layer->shape_count++;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parse an animated or static property
|
|
*/
|
|
static void lottie_parse_property(lottie_property_t *prop, const cJSON *json) {
|
|
memset(prop, 0, sizeof(lottie_property_t));
|
|
|
|
cJSON *a = cJSON_GetObjectItem(json, "a");
|
|
cJSON *k = cJSON_GetObjectItem(json, "k");
|
|
|
|
if (!k)
|
|
return;
|
|
|
|
// Check if animated
|
|
if (a && cJSON_IsNumber(a) && a->valueint == 1) {
|
|
prop->animated = true;
|
|
|
|
// Parse keyframes
|
|
if (cJSON_IsArray(k)) {
|
|
int count = cJSON_GetArraySize(k);
|
|
if (count > MAX_KEYFRAMES) {
|
|
count = MAX_KEYFRAMES;
|
|
}
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
cJSON *kf = cJSON_GetArrayItem(k, i);
|
|
if (!kf)
|
|
continue;
|
|
|
|
lottie_keyframe_t *keyframe = &prop->keyframes[prop->keyframe_count];
|
|
|
|
// Time
|
|
cJSON *t = cJSON_GetObjectItem(kf, "t");
|
|
if (t && cJSON_IsNumber(t)) {
|
|
keyframe->time = (float)t->valuedouble;
|
|
}
|
|
|
|
// Hold
|
|
cJSON *h = cJSON_GetObjectItem(kf, "h");
|
|
if (h && cJSON_IsNumber(h)) {
|
|
keyframe->hold = h->valueint == 1;
|
|
}
|
|
|
|
// Start value (s)
|
|
cJSON *s = cJSON_GetObjectItem(kf, "s");
|
|
if (s && cJSON_IsArray(s)) {
|
|
int vcount = cJSON_GetArraySize(s);
|
|
for (int v = 0; v < vcount && v < 2; v++) {
|
|
cJSON *val = cJSON_GetArrayItem(s, v);
|
|
if (val && cJSON_IsNumber(val)) {
|
|
keyframe->value[v] = (float)val->valuedouble;
|
|
}
|
|
}
|
|
}
|
|
|
|
// In tangent (i)
|
|
cJSON *in_tan = cJSON_GetObjectItem(kf, "i");
|
|
if (in_tan) {
|
|
cJSON *x = cJSON_GetObjectItem(in_tan, "x");
|
|
cJSON *y = cJSON_GetObjectItem(in_tan, "y");
|
|
if (x && cJSON_IsArray(x) && cJSON_GetArraySize(x) > 0) {
|
|
keyframe->in_tangent[0] =
|
|
(float)cJSON_GetArrayItem(x, 0)->valuedouble;
|
|
} else if (x && cJSON_IsNumber(x)) {
|
|
keyframe->in_tangent[0] = (float)x->valuedouble;
|
|
}
|
|
if (y && cJSON_IsArray(y) && cJSON_GetArraySize(y) > 0) {
|
|
keyframe->in_tangent[1] =
|
|
(float)cJSON_GetArrayItem(y, 0)->valuedouble;
|
|
} else if (y && cJSON_IsNumber(y)) {
|
|
keyframe->in_tangent[1] = (float)y->valuedouble;
|
|
}
|
|
}
|
|
|
|
// Out tangent (o)
|
|
cJSON *out_tan = cJSON_GetObjectItem(kf, "o");
|
|
if (out_tan) {
|
|
cJSON *x = cJSON_GetObjectItem(out_tan, "x");
|
|
cJSON *y = cJSON_GetObjectItem(out_tan, "y");
|
|
if (x && cJSON_IsArray(x) && cJSON_GetArraySize(x) > 0) {
|
|
keyframe->out_tangent[0] =
|
|
(float)cJSON_GetArrayItem(x, 0)->valuedouble;
|
|
} else if (x && cJSON_IsNumber(x)) {
|
|
keyframe->out_tangent[0] = (float)x->valuedouble;
|
|
}
|
|
if (y && cJSON_IsArray(y) && cJSON_GetArraySize(y) > 0) {
|
|
keyframe->out_tangent[1] =
|
|
(float)cJSON_GetArrayItem(y, 0)->valuedouble;
|
|
} else if (y && cJSON_IsNumber(y)) {
|
|
keyframe->out_tangent[1] = (float)y->valuedouble;
|
|
}
|
|
}
|
|
|
|
prop->keyframe_count++;
|
|
}
|
|
}
|
|
} else {
|
|
// Static value
|
|
prop->animated = false;
|
|
if (cJSON_IsArray(k)) {
|
|
int count = cJSON_GetArraySize(k);
|
|
for (int i = 0; i < count && i < 2; i++) {
|
|
cJSON *val = cJSON_GetArrayItem(k, i);
|
|
if (val && cJSON_IsNumber(val)) {
|
|
prop->static_value[i] = (float)val->valuedouble;
|
|
}
|
|
}
|
|
} else if (cJSON_IsNumber(k)) {
|
|
prop->static_value[0] = (float)k->valuedouble;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cubic bezier interpolation
|
|
*/
|
|
static float lottie_bezier_interpolate(float t, float p0, float p1, float p2,
|
|
float p3) {
|
|
float u = 1.0f - t;
|
|
float tt = t * t;
|
|
float uu = u * u;
|
|
float uuu = uu * u;
|
|
float ttt = tt * t;
|
|
|
|
float result = uuu * p0;
|
|
result += 3.0f * uu * t * p1;
|
|
result += 3.0f * u * tt * p2;
|
|
result += ttt * p3;
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Interpolate a property value at a given frame
|
|
*/
|
|
static float lottie_interpolate_property(const lottie_property_t *prop,
|
|
float frame, int index) {
|
|
if (!prop->animated) {
|
|
return prop->static_value[index];
|
|
}
|
|
|
|
if (prop->keyframe_count == 0) {
|
|
return 0.0f;
|
|
}
|
|
|
|
// Find surrounding keyframes
|
|
const lottie_keyframe_t *prev = NULL;
|
|
const lottie_keyframe_t *next = NULL;
|
|
|
|
for (int i = 0; i < prop->keyframe_count; i++) {
|
|
if (prop->keyframes[i].time <= frame) {
|
|
prev = &prop->keyframes[i];
|
|
}
|
|
if (prop->keyframes[i].time >= frame && next == NULL) {
|
|
next = &prop->keyframes[i];
|
|
}
|
|
}
|
|
|
|
// Clamp to boundaries
|
|
if (!prev) {
|
|
return prop->keyframes[0].value[index];
|
|
}
|
|
if (!next || prev == next) {
|
|
return prev->value[index];
|
|
}
|
|
|
|
// Check for hold keyframe
|
|
if (prev->hold) {
|
|
return prev->value[index];
|
|
}
|
|
|
|
// Calculate interpolation factor
|
|
float duration = next->time - prev->time;
|
|
if (duration <= 0) {
|
|
return prev->value[index];
|
|
}
|
|
|
|
float t = (frame - prev->time) / duration;
|
|
|
|
// Apply bezier easing if tangents are present
|
|
if (prev->out_tangent[0] != 0 || prev->out_tangent[1] != 0 ||
|
|
next->in_tangent[0] != 0 || next->in_tangent[1] != 0) {
|
|
// Approximate bezier curve for timing
|
|
t = lottie_bezier_interpolate(t, 0.0f, prev->out_tangent[1],
|
|
next->in_tangent[1], 1.0f);
|
|
t = fmaxf(0.0f, fminf(1.0f, t));
|
|
}
|
|
|
|
// Linear interpolation between values
|
|
return prev->value[index] + t * (next->value[index] - prev->value[index]);
|
|
}
|
|
|
|
/**
|
|
* Get layer by index
|
|
*/
|
|
static lottie_layer_t *lottie_get_layer_by_index(lottie_player_handle_t handle,
|
|
int index) {
|
|
for (int i = 0; i < handle->layer_count; i++) {
|
|
if (handle->layers[i].index == index) {
|
|
return &handle->layers[i];
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Calculate cumulative transform for a layer (following parent chain)
|
|
* In Lottie, transform = position - anchor for each layer in the hierarchy
|
|
*/
|
|
static void lottie_get_layer_transform(lottie_player_handle_t handle,
|
|
lottie_layer_t *layer, float frame,
|
|
float *offset_x, float *offset_y) {
|
|
*offset_x = 0;
|
|
*offset_y = 0;
|
|
|
|
lottie_layer_t *current = layer;
|
|
while (current) {
|
|
// Add this layer's position and subtract anchor
|
|
float pos_x = lottie_interpolate_property(¤t->position, frame, 0);
|
|
float pos_y = lottie_interpolate_property(¤t->position, frame, 1);
|
|
float anc_x = lottie_interpolate_property(¤t->anchor, frame, 0);
|
|
float anc_y = lottie_interpolate_property(¤t->anchor, frame, 1);
|
|
|
|
*offset_x += pos_x - anc_x;
|
|
*offset_y += pos_y - anc_y;
|
|
|
|
// Follow parent chain
|
|
if (current->parent >= 0) {
|
|
current = lottie_get_layer_by_index(handle, current->parent);
|
|
} else {
|
|
current = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Draw a rounded rectangle on the canvas
|
|
*/
|
|
static void lottie_draw_rounded_rect(lottie_player_handle_t handle,
|
|
lv_coord_t x, lv_coord_t y, lv_coord_t w,
|
|
lv_coord_t h, lv_coord_t radius,
|
|
lv_color_t color) {
|
|
lv_draw_rect_dsc_t rect_dsc;
|
|
lv_draw_rect_dsc_init(&rect_dsc);
|
|
rect_dsc.bg_color = color;
|
|
rect_dsc.bg_opa = LV_OPA_COVER;
|
|
rect_dsc.radius = radius;
|
|
rect_dsc.border_width = 0;
|
|
|
|
lv_area_t area;
|
|
area.x1 = x - w / 2;
|
|
area.y1 = y - h / 2;
|
|
area.x2 = area.x1 + w - 1;
|
|
area.y2 = area.y1 + h - 1;
|
|
|
|
lv_canvas_draw_rect(handle->canvas, area.x1, area.y1, w, h, &rect_dsc);
|
|
}
|
|
|
|
/**
|
|
* Render current frame to canvas
|
|
*/
|
|
static void lottie_render_frame(lottie_player_handle_t handle) {
|
|
static bool first_frame_logged = false;
|
|
|
|
// Clear canvas to black
|
|
lv_canvas_fill_bg(handle->canvas, lv_color_black(), LV_OPA_COVER);
|
|
|
|
float frame = handle->current_frame + handle->in_point;
|
|
|
|
// Render layers in reverse order (last layer = bottom)
|
|
for (int i = handle->layer_count - 1; i >= 0; i--) {
|
|
lottie_layer_t *layer = &handle->layers[i];
|
|
if (!layer->valid || layer->type != 4) {
|
|
continue;
|
|
}
|
|
|
|
// Get cumulative transform
|
|
float offset_x = 0, offset_y = 0;
|
|
lottie_get_layer_transform(handle, layer, frame, &offset_x, &offset_y);
|
|
|
|
// Render shapes
|
|
for (int s = 0; s < layer->shape_count; s++) {
|
|
lottie_rounded_rect_t *rect = &layer->shapes[s];
|
|
if (!rect->valid) {
|
|
continue;
|
|
}
|
|
|
|
// Get animated properties
|
|
float px = lottie_interpolate_property(&rect->position, frame, 0);
|
|
float py = lottie_interpolate_property(&rect->position, frame, 1);
|
|
float w = lottie_interpolate_property(&rect->size, frame, 0);
|
|
float h = lottie_interpolate_property(&rect->size, frame, 1);
|
|
float r = lottie_interpolate_property(&rect->corner_radius, frame, 0);
|
|
|
|
// Apply layer offset
|
|
px += offset_x;
|
|
py += offset_y;
|
|
|
|
// Debug logging for first frame
|
|
if (!first_frame_logged) {
|
|
ESP_LOGI(TAG,
|
|
"Layer %d shape %d: pos=(%.1f,%.1f) size=(%.1f,%.1f) "
|
|
"radius=%.1f offset=(%.1f,%.1f)",
|
|
layer->index, s, px - offset_x, py - offset_y, w, h, r,
|
|
offset_x, offset_y);
|
|
}
|
|
|
|
// Only draw if visible
|
|
if (w > 0 && h > 0) {
|
|
lottie_draw_rounded_rect(handle, (lv_coord_t)px, (lv_coord_t)py,
|
|
(lv_coord_t)w, (lv_coord_t)h, (lv_coord_t)r,
|
|
rect->fill_color);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!first_frame_logged) {
|
|
first_frame_logged = true;
|
|
}
|
|
|
|
// Invalidate canvas to trigger redraw
|
|
lv_obj_invalidate(handle->canvas);
|
|
}
|
|
|
|
/**
|
|
* Timer callback for animation playback
|
|
* Uses time-based animation for accurate playback speed
|
|
*/
|
|
static void lottie_timer_cb(lv_timer_t *timer) {
|
|
lottie_player_handle_t handle = (lottie_player_handle_t)timer->user_data;
|
|
if (!handle || !handle->playing) {
|
|
return;
|
|
}
|
|
|
|
// Calculate current frame based on elapsed time
|
|
int64_t now_us = esp_timer_get_time();
|
|
int64_t elapsed_us = now_us - handle->start_time_us;
|
|
float elapsed_sec = (float)elapsed_us / 1000000.0f;
|
|
float frame = elapsed_sec * handle->framerate;
|
|
|
|
// Debug: log first few frames
|
|
static int frame_log_count = 0;
|
|
if (frame_log_count < 5) {
|
|
ESP_LOGI(TAG, "Frame calc: elapsed=%.3fs, frame=%.1f/%lu", elapsed_sec,
|
|
frame, (unsigned long)handle->total_frames);
|
|
frame_log_count++;
|
|
}
|
|
|
|
// Handle end of animation
|
|
if (frame >= handle->total_frames) {
|
|
if (handle->loop) {
|
|
// Reset start time for smooth looping
|
|
float cycles = frame / handle->total_frames;
|
|
int full_cycles = (int)cycles;
|
|
handle->start_time_us += (int64_t)(full_cycles * handle->total_frames /
|
|
handle->framerate * 1000000.0f);
|
|
frame = frame - (full_cycles * handle->total_frames);
|
|
} else {
|
|
// Animation complete - return to idle state
|
|
ESP_LOGI(TAG, "Blink animation complete after %.3fs", elapsed_sec);
|
|
frame_log_count = 0; // Reset for next blink
|
|
|
|
handle->playing = false;
|
|
lv_timer_pause(handle->timer);
|
|
handle->current_frame = handle->total_frames - 1; // Eyes open
|
|
lottie_render_frame(handle);
|
|
|
|
// Schedule next blink if in natural mode
|
|
if (handle->natural_blink_mode) {
|
|
lottie_schedule_next_blink(handle);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
handle->current_frame = frame;
|
|
|
|
// Render the new frame
|
|
lottie_render_frame(handle);
|
|
}
|
|
|
|
/**
|
|
* Schedule next random blink
|
|
*/
|
|
static void lottie_schedule_next_blink(lottie_player_handle_t handle) {
|
|
if (!handle || !handle->natural_blink_mode) {
|
|
return;
|
|
}
|
|
|
|
// Random delay between 3-6 seconds for natural feel
|
|
uint32_t delay_ms = 3000 + (esp_random() % 3000);
|
|
|
|
// Create new timer (old one was already deleted or is NULL)
|
|
handle->blink_timer =
|
|
lv_timer_create(lottie_blink_timer_cb, delay_ms, handle);
|
|
lv_timer_set_repeat_count(handle->blink_timer, 1);
|
|
}
|
|
|
|
/**
|
|
* Blink timer callback - triggers a blink animation
|
|
*/
|
|
static void lottie_blink_timer_cb(lv_timer_t *timer) {
|
|
lottie_player_handle_t handle = (lottie_player_handle_t)timer->user_data;
|
|
|
|
// Mark timer as gone (one-shot timers auto-delete after callback returns)
|
|
if (handle) {
|
|
handle->blink_timer = NULL;
|
|
}
|
|
|
|
if (!handle || !handle->natural_blink_mode) {
|
|
return;
|
|
}
|
|
|
|
// Start blink animation from frame 0
|
|
ESP_LOGI(TAG, "Blink triggered! Starting animation from frame 0");
|
|
handle->current_frame = 0;
|
|
lottie_play(handle);
|
|
}
|
|
|
|
/**
|
|
* Start/resume playback
|
|
*/
|
|
void lottie_play(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return;
|
|
|
|
// Set start time for time-based animation
|
|
// Adjust for current frame position if resuming
|
|
int64_t frame_offset_us =
|
|
(int64_t)(handle->current_frame / handle->framerate * 1000000.0f);
|
|
handle->start_time_us = esp_timer_get_time() - frame_offset_us;
|
|
|
|
handle->playing = true;
|
|
lv_timer_resume(handle->timer);
|
|
}
|
|
|
|
/**
|
|
* Pause playback
|
|
*/
|
|
void lottie_pause(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return;
|
|
handle->playing = false;
|
|
lv_timer_pause(handle->timer);
|
|
}
|
|
|
|
/**
|
|
* Stop playback and reset to first frame
|
|
*/
|
|
void lottie_stop(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return;
|
|
handle->playing = false;
|
|
handle->current_frame = 0;
|
|
lv_timer_pause(handle->timer);
|
|
lottie_render_frame(handle);
|
|
}
|
|
|
|
/**
|
|
* Set loop mode
|
|
*/
|
|
void lottie_set_loop(lottie_player_handle_t handle, bool loop) {
|
|
if (!handle)
|
|
return;
|
|
handle->loop = loop;
|
|
}
|
|
|
|
/**
|
|
* Check if playing
|
|
*/
|
|
bool lottie_is_playing(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return false;
|
|
return handle->playing;
|
|
}
|
|
|
|
/**
|
|
* Get current frame number
|
|
*/
|
|
uint32_t lottie_get_current_frame(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return 0;
|
|
return (uint32_t)handle->current_frame;
|
|
}
|
|
|
|
/**
|
|
* Get total frame count
|
|
*/
|
|
uint32_t lottie_get_total_frames(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return 0;
|
|
return handle->total_frames;
|
|
}
|
|
|
|
/**
|
|
* Set current frame
|
|
*/
|
|
void lottie_set_frame(lottie_player_handle_t handle, uint32_t frame) {
|
|
if (!handle)
|
|
return;
|
|
if (frame >= handle->total_frames) {
|
|
frame = handle->total_frames - 1;
|
|
}
|
|
handle->current_frame = (float)frame;
|
|
lottie_render_frame(handle);
|
|
}
|
|
|
|
/**
|
|
* Destroy player and free resources
|
|
*/
|
|
void lottie_destroy(lottie_player_handle_t handle) {
|
|
if (!handle)
|
|
return;
|
|
|
|
// Stop timer
|
|
if (handle->timer) {
|
|
lv_timer_del(handle->timer);
|
|
}
|
|
|
|
// Delete canvas
|
|
if (handle->canvas) {
|
|
lv_obj_del(handle->canvas);
|
|
}
|
|
|
|
// Free canvas buffer
|
|
if (handle->canvas_buf) {
|
|
free(handle->canvas_buf);
|
|
}
|
|
|
|
// Free player structure
|
|
free(handle);
|
|
|
|
ESP_LOGI(TAG, "Lottie player destroyed");
|
|
}
|