Add shake emotion with persistent state and calibration

Introduces a new shake emotion animation with a shake-stop transition, including embedded Lottie animation data and a state machine for emotion transitions. Adds a non-volatile storage (NVS) driver for persistent state management, including first-boot detection, boot count, and IMU calibration storage. Updates emotion manager and related headers to support new shake emotion states, and integrates new UI components for calibration and loading animations.
This commit is contained in:
2026-01-24 20:02:04 +05:30
parent 8aeaed8a15
commit 4d68a788ad
22 changed files with 5255 additions and 93 deletions

View File

@@ -77,6 +77,17 @@ typedef struct {
bool valid;
} LottieLayer;
/**
* Cached shape bounds for dirty rect optimization
*/
typedef struct {
lv_coord_t x1, y1, x2, y2; // Bounding box corners
bool valid;
} ShapeBounds;
// Maximum cached shapes for dirty rect tracking
#define LOTTIE_MAX_CACHED_SHAPES (LOTTIE_MAX_LAYERS * LOTTIE_MAX_SHAPES)
/**
* Main player state structure
*/
@@ -85,6 +96,7 @@ struct lottie_player_t {
uint16_t width;
uint16_t height;
float framerate;
float nativeFramerate; // Original framerate from JSON
uint32_t totalFrames;
uint32_t inPoint;
uint32_t outPoint;
@@ -107,6 +119,11 @@ struct lottie_player_t {
lv_obj_t *canvas;
lv_color_t *canvasBuffer;
lv_timer_t *animTimer;
// Dirty rect optimization - cache previous frame shape bounds
ShapeBounds prevBounds[LOTTIE_MAX_CACHED_SHAPES];
int prevBoundsCount;
bool firstRender; // Skip dirty optimization on first frame
};
// ----------------------------------------------------------------------------
@@ -162,6 +179,8 @@ lottie_player_handle_t lottieCreateFromData(lv_obj_t *parent,
handle->playing = false;
handle->loop = true;
handle->currentFrame = 0;
handle->firstRender = true;
handle->prevBoundsCount = 0;
// Parse animation data
parseAnimation(handle, root);
@@ -262,6 +281,33 @@ void lottieSetLoop(lottie_player_handle_t handle, bool loop) {
handle->loop = loop;
}
void lottieDisableNaturalBlink(lottie_player_handle_t handle) {
if (!handle)
return;
// Delete the blink timer if it exists
if (handle->blinkTimer) {
lv_timer_del(handle->blinkTimer);
handle->blinkTimer = NULL;
}
handle->naturalBlinkMode = false;
ESP_LOGI(TAG, "Natural blink mode disabled");
}
void lottiePauseNaturalBlink(lottie_player_handle_t handle) {
if (!handle || !handle->blinkTimer)
return;
lv_timer_pause(handle->blinkTimer);
ESP_LOGI(TAG, "Natural blink timer paused");
}
void lottieResumeNaturalBlink(lottie_player_handle_t handle) {
if (!handle || !handle->blinkTimer)
return;
lv_timer_resume(handle->blinkTimer);
ESP_LOGI(TAG, "Natural blink timer resumed");
}
bool lottieIsPlaying(lottie_player_handle_t handle) {
if (!handle)
return false;
@@ -290,6 +336,15 @@ void lottieSetFrame(lottie_player_handle_t handle, uint32_t frame) {
renderFrame(handle);
}
void lottieSetSpeed(lottie_player_handle_t handle, float speed) {
if (!handle || speed <= 0)
return;
// Use native framerate as base and apply speed multiplier
handle->framerate = handle->nativeFramerate * speed;
ESP_LOGI(TAG, "Playback speed set to %.1fx (%.1f fps)", speed,
handle->framerate);
}
void lottieDestroy(lottie_player_handle_t handle) {
if (!handle)
return;
@@ -331,6 +386,8 @@ static void parseAnimation(lottie_player_handle_t handle, const cJSON *root) {
item = cJSON_GetObjectItem(root, "fr");
if (item && cJSON_IsNumber(item)) {
handle->framerate = (float)item->valuedouble;
handle->nativeFramerate =
handle->framerate; // Store native before any modifications
}
item = cJSON_GetObjectItem(root, "ip");
@@ -718,13 +775,143 @@ static void drawRoundedRect(lottie_player_handle_t handle, lv_coord_t x,
}
static void renderFrame(lottie_player_handle_t handle) {
static bool firstFrameLogged = false;
lv_canvas_fill_bg(handle->canvas, lv_color_black(), LV_OPA_COVER);
float frame = handle->currentFrame + handle->inPoint;
// Render layers in reverse order (last layer = bottom)
// Combined dirty area for partial invalidation
lv_area_t dirtyArea;
bool hasDirty = false;
// Helper macro to expand dirty area (parameters named differently to avoid
// conflict)
#define EXPAND_DIRTY(_ax1, _ay1, _ax2, _ay2) \
do { \
if (!hasDirty) { \
dirtyArea.x1 = (_ax1); \
dirtyArea.y1 = (_ay1); \
dirtyArea.x2 = (_ax2); \
dirtyArea.y2 = (_ay2); \
hasDirty = true; \
} else { \
if ((_ax1) < dirtyArea.x1) \
dirtyArea.x1 = (_ax1); \
if ((_ay1) < dirtyArea.y1) \
dirtyArea.y1 = (_ay1); \
if ((_ax2) > dirtyArea.x2) \
dirtyArea.x2 = (_ax2); \
if ((_ay2) > dirtyArea.y2) \
dirtyArea.y2 = (_ay2); \
} \
} while (0)
// Collect current frame shape bounds
ShapeBounds currBounds[LOTTIE_MAX_CACHED_SHAPES];
int currCount = 0;
// First pass: calculate all current shape bounds
for (int i = handle->layerCount - 1; i >= 0; i--) {
LottieLayer *layer = &handle->layers[i];
if (!layer->valid || layer->type != 4) {
continue;
}
float offsetX = 0, offsetY = 0;
getLayerTransform(handle, layer, frame, &offsetX, &offsetY);
for (int s = 0; s < layer->shapeCount; s++) {
LottieRoundedRect *rect = &layer->shapes[s];
if (!rect->valid || currCount >= LOTTIE_MAX_CACHED_SHAPES)
continue;
float px = interpolateProperty(&rect->position, frame, 0) + offsetX;
float py = interpolateProperty(&rect->position, frame, 1) + offsetY;
float w = interpolateProperty(&rect->size, frame, 0);
float h = interpolateProperty(&rect->size, frame, 1);
if (w > 0 && h > 0) {
currBounds[currCount].x1 = (lv_coord_t)(px - w / 2);
currBounds[currCount].y1 = (lv_coord_t)(py - h / 2);
currBounds[currCount].x2 = (lv_coord_t)(px + w / 2);
currBounds[currCount].y2 = (lv_coord_t)(py + h / 2);
currBounds[currCount].valid = true;
currCount++;
}
}
}
// On first render, do full clear and full invalidate
if (handle->firstRender) {
lv_canvas_fill_bg(handle->canvas, lv_color_black(), LV_OPA_COVER);
handle->firstRender = false;
lv_obj_invalidate(handle->canvas);
// Cache bounds for next frame
for (int i = 0; i < currCount && i < LOTTIE_MAX_CACHED_SHAPES; i++) {
handle->prevBounds[i] = currBounds[i];
}
handle->prevBoundsCount = currCount;
// Draw shapes
for (int i = handle->layerCount - 1; i >= 0; i--) {
LottieLayer *layer = &handle->layers[i];
if (!layer->valid || layer->type != 4)
continue;
float offsetX = 0, offsetY = 0;
getLayerTransform(handle, layer, frame, &offsetX, &offsetY);
for (int s = 0; s < layer->shapeCount; s++) {
LottieRoundedRect *rect = &layer->shapes[s];
if (!rect->valid)
continue;
float px = interpolateProperty(&rect->position, frame, 0) + offsetX;
float py = interpolateProperty(&rect->position, frame, 1) + offsetY;
float w = interpolateProperty(&rect->size, frame, 0);
float h = interpolateProperty(&rect->size, frame, 1);
float r = interpolateProperty(&rect->cornerRadius, frame, 0);
if (w > 0 && h > 0) {
drawRoundedRect(handle, (lv_coord_t)px, (lv_coord_t)py, (lv_coord_t)w,
(lv_coord_t)h, (lv_coord_t)r, rect->fillColor);
}
}
}
return;
}
// Clear previous shape positions and track dirty areas
lv_draw_rect_dsc_t clearDsc;
lv_draw_rect_dsc_init(&clearDsc);
clearDsc.bg_color = lv_color_black();
clearDsc.bg_opa = LV_OPA_COVER;
clearDsc.radius = 0;
clearDsc.border_width = 0;
for (int i = 0; i < handle->prevBoundsCount; i++) {
if (!handle->prevBounds[i].valid)
continue;
ShapeBounds *prev = &handle->prevBounds[i];
// Check if shape moved
bool needsClear = true;
if (i < currCount && currBounds[i].valid) {
ShapeBounds *curr = &currBounds[i];
if (prev->x1 == curr->x1 && prev->y1 == curr->y1 &&
prev->x2 == curr->x2 && prev->y2 == curr->y2) {
needsClear = false; // Shape didn't move
}
}
if (needsClear) {
lv_coord_t pw = prev->x2 - prev->x1;
lv_coord_t ph = prev->y2 - prev->y1;
if (pw > 0 && ph > 0) {
lv_canvas_draw_rect(handle->canvas, prev->x1, prev->y1, pw, ph,
&clearDsc);
EXPAND_DIRTY(prev->x1, prev->y1, prev->x2, prev->y2);
}
}
}
// Draw current shapes and track dirty areas
int shapeIdx = 0;
for (int i = handle->layerCount - 1; i >= 0; i--) {
LottieLayer *layer = &handle->layers[i];
if (!layer->valid || layer->type != 4) {
@@ -739,32 +926,60 @@ static void renderFrame(lottie_player_handle_t handle) {
if (!rect->valid)
continue;
float px = interpolateProperty(&rect->position, frame, 0);
float py = interpolateProperty(&rect->position, frame, 1);
float px = interpolateProperty(&rect->position, frame, 0) + offsetX;
float py = interpolateProperty(&rect->position, frame, 1) + offsetY;
float w = interpolateProperty(&rect->size, frame, 0);
float h = interpolateProperty(&rect->size, frame, 1);
float r = interpolateProperty(&rect->cornerRadius, frame, 0);
px += offsetX;
py += offsetY;
if (!firstFrameLogged) {
ESP_LOGI(TAG, "Layer %d shape %d: pos=(%.1f,%.1f) size=(%.1f,%.1f)",
layer->index, s, px, py, w, h);
}
if (w > 0 && h > 0) {
lv_coord_t cx1 = (lv_coord_t)(px - w / 2);
lv_coord_t cy1 = (lv_coord_t)(py - h / 2);
lv_coord_t cx2 = (lv_coord_t)(px + w / 2);
lv_coord_t cy2 = (lv_coord_t)(py + h / 2);
drawRoundedRect(handle, (lv_coord_t)px, (lv_coord_t)py, (lv_coord_t)w,
(lv_coord_t)h, (lv_coord_t)r, rect->fillColor);
// Check if this shape moved from previous position
if (shapeIdx < handle->prevBoundsCount &&
handle->prevBounds[shapeIdx].valid) {
ShapeBounds *prev = &handle->prevBounds[shapeIdx];
if (cx1 != prev->x1 || cy1 != prev->y1 || cx2 != prev->x2 ||
cy2 != prev->y2) {
EXPAND_DIRTY(cx1, cy1, cx2, cy2);
}
} else {
// New shape
EXPAND_DIRTY(cx1, cy1, cx2, cy2);
}
}
shapeIdx++;
}
}
if (!firstFrameLogged) {
firstFrameLogged = true;
// Cache current bounds for next frame
for (int i = 0; i < currCount && i < LOTTIE_MAX_CACHED_SHAPES; i++) {
handle->prevBounds[i] = currBounds[i];
}
handle->prevBoundsCount = currCount;
// Partial invalidation - only refresh the dirty area via DMA
if (hasDirty) {
// Clamp to canvas bounds
if (dirtyArea.x1 < 0)
dirtyArea.x1 = 0;
if (dirtyArea.y1 < 0)
dirtyArea.y1 = 0;
if (dirtyArea.x2 >= handle->width)
dirtyArea.x2 = handle->width - 1;
if (dirtyArea.y2 >= handle->height)
dirtyArea.y2 = handle->height - 1;
lv_obj_invalidate_area(handle->canvas, &dirtyArea);
}
lv_obj_invalidate(handle->canvas);
#undef EXPAND_DIRTY
}
// ----------------------------------------------------------------------------