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:
3522
main/ui/assets/fonts/Geist.c
Normal file
3522
main/ui/assets/fonts/Geist.c
Normal file
File diff suppressed because it is too large
Load Diff
25
main/ui/assets/fonts/Geist.h
Normal file
25
main/ui/assets/fonts/Geist.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* @file Geist.h
|
||||
* @brief Geist font declaration for LVGL
|
||||
*
|
||||
* 48px Geist Regular font converted for LVGL usage.
|
||||
* Provides a clean, modern typeface for UI text elements.
|
||||
*/
|
||||
|
||||
#ifndef GEIST_H
|
||||
#define GEIST_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "lvgl.h"
|
||||
|
||||
// External font declaration - defined in Geist.c
|
||||
extern const lv_font_t Geist;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // GEIST_H
|
||||
174
main/ui/calibration_screen.c
Normal file
174
main/ui/calibration_screen.c
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* @file calibration_screen.c
|
||||
* @brief IMU calibration screen implementation
|
||||
*
|
||||
* This module provides the first-boot calibration experience. It displays
|
||||
* a full-screen UI with the Geist font prompting the user to place the
|
||||
* device on a flat surface and tap to calibrate. The calibration captures
|
||||
* the IMU baseline and stores it to NVS for persistence.
|
||||
*/
|
||||
|
||||
#include "ui/calibration_screen.h"
|
||||
|
||||
#include "drivers/display_driver.h"
|
||||
#include "drivers/nvs_driver.h"
|
||||
#include "sensors/shake_detector.h"
|
||||
#include "ui/assets/fonts/Geist.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
static const char *TAG = "calibration_screen";
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private State
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static volatile bool tapDetected = false;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private Functions
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Touch event callback for detecting user tap
|
||||
*/
|
||||
static void onScreenTap(lv_event_t *event) {
|
||||
lv_event_code_t code = lv_event_get_code(event);
|
||||
if (code == LV_EVENT_CLICKED) {
|
||||
tapDetected = true;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API Implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
bool calibrationScreenShow(void) {
|
||||
ESP_LOGI(TAG, "Showing calibration screen");
|
||||
|
||||
tapDetected = false;
|
||||
|
||||
// Create calibration screen
|
||||
lv_obj_t *calibScreen = lv_obj_create(NULL);
|
||||
if (calibScreen == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create calibration screen");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Black background with no border
|
||||
lv_obj_set_style_bg_color(calibScreen, lv_color_black(),
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_bg_opa(calibScreen, LV_OPA_COVER,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_border_width(calibScreen, 0,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_pad_all(calibScreen, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
|
||||
// Make the screen clickable to detect taps
|
||||
lv_obj_add_flag(calibScreen, LV_OBJ_FLAG_CLICKABLE);
|
||||
lv_obj_add_event_cb(calibScreen, onScreenTap, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
// Main instruction text - minimalist white on black
|
||||
lv_obj_t *mainLabel = lv_label_create(calibScreen);
|
||||
lv_label_set_text(mainLabel, "Place device on\na flat surface");
|
||||
lv_obj_set_style_text_font(mainLabel, &Geist,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_color(mainLabel, lv_color_white(),
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_align(mainLabel, LV_TEXT_ALIGN_CENTER,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_line_space(mainLabel, 12,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_align(mainLabel, LV_ALIGN_CENTER, 0, -60);
|
||||
|
||||
// Sub instruction text - subtle gray
|
||||
lv_obj_t *subLabel = lv_label_create(calibScreen);
|
||||
lv_label_set_text(subLabel, "Tap to calibrate");
|
||||
lv_obj_set_style_text_font(subLabel, &Geist, LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_color(subLabel, lv_color_make(100, 100, 100),
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_text_align(subLabel, LV_TEXT_ALIGN_CENTER,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_align(subLabel, LV_ALIGN_CENTER, 0, 80);
|
||||
|
||||
// Load and display the calibration screen
|
||||
lv_scr_load(calibScreen);
|
||||
lv_refr_now(NULL);
|
||||
|
||||
ESP_LOGI(TAG, "Waiting for user tap...");
|
||||
|
||||
// Release the display lock so LVGL timer handler can process touch events
|
||||
displayDriverUnlock();
|
||||
|
||||
// Wait for tap - LVGL timer handler runs in background task
|
||||
while (!tapDetected) {
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
|
||||
// Reacquire lock for UI updates
|
||||
displayDriverLock(-1);
|
||||
|
||||
ESP_LOGI(TAG, "Tap detected, starting calibration...");
|
||||
|
||||
// Update UI to show calibrating state
|
||||
lv_label_set_text(mainLabel, "Calibrating...");
|
||||
lv_obj_align(mainLabel, LV_ALIGN_CENTER, 0, 0);
|
||||
lv_obj_add_flag(subLabel, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_refr_now(NULL);
|
||||
|
||||
// Release lock during calibration (sensor reads)
|
||||
displayDriverUnlock();
|
||||
|
||||
// Perform calibration
|
||||
bool calibSuccess = shakeDetectorCalibrateBaseline();
|
||||
|
||||
// Reacquire lock for final UI updates
|
||||
displayDriverLock(-1);
|
||||
|
||||
if (calibSuccess) {
|
||||
// Get the calibrated baseline values
|
||||
float baseX, baseY, baseZ;
|
||||
shakeDetectorGetBaseline(&baseX, &baseY, &baseZ);
|
||||
|
||||
ESP_LOGI(TAG, "Calibration complete: x=%.3f y=%.3f z=%.3f", baseX, baseY,
|
||||
baseZ);
|
||||
|
||||
// Store to NVS
|
||||
if (nvsDriverSaveImuCalibration(baseX, baseY, baseZ)) {
|
||||
ESP_LOGI(TAG, "Calibration saved to NVS");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to save calibration to NVS");
|
||||
}
|
||||
|
||||
// Show success
|
||||
lv_label_set_text(mainLabel, "Done!");
|
||||
lv_refr_now(NULL);
|
||||
|
||||
displayDriverUnlock();
|
||||
vTaskDelay(pdMS_TO_TICKS(800));
|
||||
displayDriverLock(-1);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Calibration failed");
|
||||
lv_label_set_text(mainLabel, "Calibration failed");
|
||||
lv_refr_now(NULL);
|
||||
|
||||
displayDriverUnlock();
|
||||
vTaskDelay(pdMS_TO_TICKS(1500));
|
||||
displayDriverLock(-1);
|
||||
}
|
||||
|
||||
// Clean up - create blank screen before deleting calibration screen
|
||||
lv_obj_t *blankScreen = lv_obj_create(NULL);
|
||||
if (blankScreen != NULL) {
|
||||
lv_obj_set_style_bg_color(blankScreen, lv_color_black(), 0);
|
||||
lv_obj_set_style_bg_opa(blankScreen, LV_OPA_COVER, 0);
|
||||
lv_scr_load(blankScreen);
|
||||
}
|
||||
lv_obj_del(calibScreen);
|
||||
|
||||
ESP_LOGI(TAG, "Calibration screen complete");
|
||||
return calibSuccess;
|
||||
}
|
||||
41
main/ui/calibration_screen.h
Normal file
41
main/ui/calibration_screen.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file calibration_screen.h
|
||||
* @brief IMU calibration screen for first-boot experience
|
||||
*
|
||||
* Displays a full-screen UI prompting the user to place the device
|
||||
* on a flat surface and tap to calibrate the IMU. Calibration values
|
||||
* are stored in NVS for use on subsequent boots.
|
||||
*/
|
||||
|
||||
#ifndef CALIBRATION_SCREEN_H
|
||||
#define CALIBRATION_SCREEN_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Display the calibration screen and perform IMU calibration
|
||||
*
|
||||
* This is a blocking function that:
|
||||
* 1. Displays instructions to place device on flat surface
|
||||
* 2. Waits for user tap
|
||||
* 3. Performs IMU baseline calibration
|
||||
* 4. Stores calibration data to NVS
|
||||
* 5. Cleans up and returns
|
||||
*
|
||||
* @return true on successful calibration, false on failure
|
||||
*/
|
||||
bool calibrationScreenShow(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // CALIBRATION_SCREEN_H
|
||||
99
main/ui/loading_anim.c
Normal file
99
main/ui/loading_anim.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @file loading_anim.c
|
||||
* @brief Minimalist loading animation implementation
|
||||
*
|
||||
* Uses LVGL animators to create a "breathing" or "bouncing" 3-dot effect.
|
||||
*/
|
||||
|
||||
#include "ui/loading_anim.h"
|
||||
#include "esp_log.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
static const char *TAG = "loading_anim";
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private State
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static lv_obj_t *loadingScreen = NULL;
|
||||
static lv_obj_t *dots[3];
|
||||
static lv_anim_t anims[3];
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Animation Callbacks
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void anim_y_cb(void *var, int32_t v) {
|
||||
lv_obj_set_y((lv_obj_t *)var, v);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API Implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void loadingAnimStart(void) {
|
||||
if (loadingScreen != NULL)
|
||||
return;
|
||||
|
||||
ESP_LOGI(TAG, "Starting loading animation");
|
||||
|
||||
// Create opaque black screen
|
||||
loadingScreen = lv_obj_create(NULL);
|
||||
lv_obj_set_style_bg_color(loadingScreen, lv_color_black(), 0);
|
||||
lv_obj_set_style_bg_opa(loadingScreen, LV_OPA_COVER, 0);
|
||||
|
||||
// Create 3 dots
|
||||
// Center is (0,0) for alignment
|
||||
const int32_t dot_size = 20;
|
||||
const int32_t dot_gap = 40;
|
||||
const int32_t base_y = 0;
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
dots[i] = lv_obj_create(loadingScreen);
|
||||
lv_obj_set_size(dots[i], dot_size, dot_size);
|
||||
lv_obj_set_style_radius(dots[i], LV_RADIUS_CIRCLE, 0);
|
||||
lv_obj_set_style_bg_color(dots[i], lv_color_white(), 0);
|
||||
lv_obj_set_style_border_width(dots[i], 0, 0);
|
||||
|
||||
// Position centered horizontally with gaps
|
||||
lv_obj_align(dots[i], LV_ALIGN_CENTER, (i - 1) * dot_gap, base_y);
|
||||
|
||||
// Setup bounce animation
|
||||
lv_anim_init(&anims[i]);
|
||||
lv_anim_set_var(&anims[i], dots[i]);
|
||||
lv_anim_set_values(&anims[i], base_y, base_y - 30);
|
||||
lv_anim_set_time(&anims[i], 600);
|
||||
lv_anim_set_playback_time(&anims[i], 600);
|
||||
lv_anim_set_repeat_count(&anims[i], LV_ANIM_REPEAT_INFINITE);
|
||||
lv_anim_set_path_cb(&anims[i], lv_anim_path_ease_in_out);
|
||||
lv_anim_set_exec_cb(&anims[i], anim_y_cb);
|
||||
|
||||
// Stagger the animations
|
||||
lv_anim_set_delay(&anims[i], i * 200);
|
||||
|
||||
lv_anim_start(&anims[i]);
|
||||
}
|
||||
|
||||
lv_scr_load(loadingScreen);
|
||||
}
|
||||
|
||||
void loadingAnimStop(void) {
|
||||
if (loadingScreen == NULL)
|
||||
return;
|
||||
|
||||
ESP_LOGI(TAG, "Stopping loading animation");
|
||||
|
||||
// LVGL requires a valid active screen at all times. Before deleting the
|
||||
// loading screen, we must create and load a temporary blank screen.
|
||||
// The next UI component (splash screen) will replace this.
|
||||
lv_obj_t *blankScreen = lv_obj_create(NULL);
|
||||
if (blankScreen != NULL) {
|
||||
lv_obj_set_style_bg_color(blankScreen, lv_color_black(), 0);
|
||||
lv_obj_set_style_bg_opa(blankScreen, LV_OPA_COVER, 0);
|
||||
lv_scr_load(blankScreen);
|
||||
}
|
||||
|
||||
// Now safe to delete the loading screen since we have a new active screen
|
||||
lv_obj_del(loadingScreen);
|
||||
loadingScreen = NULL;
|
||||
}
|
||||
42
main/ui/loading_anim.h
Normal file
42
main/ui/loading_anim.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @file loading_anim.h
|
||||
* @brief Minimalist loading animation
|
||||
*
|
||||
* Displays a simple 3-dots animation using LVGL primitive objects.
|
||||
* Used primarily for the first-boot experience.
|
||||
*/
|
||||
|
||||
#ifndef LOADING_ANIM_H
|
||||
#define LOADING_ANIM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Start the loading animation
|
||||
*
|
||||
* Creates a modal screen with a 3-dot animation.
|
||||
* This function blocks the caller for the specified duration?
|
||||
* No, it typically runs async, but for splash/boot it might be blocking.
|
||||
*
|
||||
* @param duration_ms Time to show the animation (0 for indefinite)
|
||||
*/
|
||||
void loadingAnimStart(void);
|
||||
|
||||
/**
|
||||
* @brief Stop the loading animation and clean up
|
||||
*/
|
||||
void loadingAnimStop(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // LOADING_ANIM_H
|
||||
@@ -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
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
@@ -95,6 +95,35 @@ void lottieStop(lottie_player_handle_t handle);
|
||||
*/
|
||||
void lottieSetLoop(lottie_player_handle_t handle, bool loop);
|
||||
|
||||
/**
|
||||
* @brief Disable natural blink mode
|
||||
*
|
||||
* By default, all animations enable natural blink mode. Call this
|
||||
* to disable it for non-blink animations like shake.
|
||||
*
|
||||
* @param handle Handle to the Lottie player instance
|
||||
*/
|
||||
void lottieDisableNaturalBlink(lottie_player_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Pause the natural blink timer
|
||||
*
|
||||
* Temporarily stops the blink timer without disabling natural blink mode.
|
||||
* Use lottieResumeNaturalBlink to resume.
|
||||
*
|
||||
* @param handle Handle to the Lottie player instance
|
||||
*/
|
||||
void lottiePauseNaturalBlink(lottie_player_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Resume the natural blink timer
|
||||
*
|
||||
* Resumes the blink timer that was paused with lottiePauseNaturalBlink.
|
||||
*
|
||||
* @param handle Handle to the Lottie player instance
|
||||
*/
|
||||
void lottieResumeNaturalBlink(lottie_player_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Check if animation is currently playing
|
||||
*
|
||||
@@ -119,6 +148,14 @@ uint32_t lottieGetCurrentFrame(lottie_player_handle_t handle);
|
||||
*/
|
||||
uint32_t lottieGetTotalFrames(lottie_player_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Set the playback speed multiplier
|
||||
*
|
||||
* @param handle Handle to the Lottie player instance
|
||||
* @param speed Speed multiplier (1.0 = native, 2.0 = 2x speed, etc.)
|
||||
*/
|
||||
void lottieSetSpeed(lottie_player_handle_t handle, float speed);
|
||||
|
||||
/**
|
||||
* @brief Set the current frame manually
|
||||
*
|
||||
|
||||
@@ -27,6 +27,11 @@ void splashScreenShow(void) {
|
||||
|
||||
// Create a temporary screen for the splash
|
||||
lv_obj_t *splashScreen = lv_obj_create(NULL);
|
||||
if (splashScreen == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create splash screen object");
|
||||
return;
|
||||
}
|
||||
|
||||
lv_obj_set_style_bg_color(splashScreen, lv_color_black(),
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_bg_opa(splashScreen, LV_OPA_COVER,
|
||||
@@ -34,6 +39,11 @@ void splashScreenShow(void) {
|
||||
|
||||
// Create and center the logo image
|
||||
lv_obj_t *logoImage = lv_img_create(splashScreen);
|
||||
if (logoImage == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create logo image object");
|
||||
lv_obj_del(splashScreen);
|
||||
return;
|
||||
}
|
||||
lv_img_set_src(logoImage, &logoIro);
|
||||
lv_obj_center(logoImage);
|
||||
|
||||
@@ -47,8 +57,27 @@ void splashScreenShow(void) {
|
||||
// Block for the display duration
|
||||
vTaskDelay(pdMS_TO_TICKS(SPLASH_SCREEN_DURATION_MS));
|
||||
|
||||
// Clean up the logo image before transitioning
|
||||
lv_obj_del(logoImage);
|
||||
// Create a new main screen for the application to use
|
||||
lv_obj_t *mainScreen = lv_obj_create(NULL);
|
||||
if (mainScreen == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create main screen");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Splash screen complete");
|
||||
// Set black background for main screen
|
||||
lv_obj_set_style_bg_color(mainScreen, lv_color_black(),
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_bg_opa(mainScreen, LV_OPA_COVER,
|
||||
LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
|
||||
// Switch to the main screen (this deactivates splash screen)
|
||||
lv_scr_load(mainScreen);
|
||||
|
||||
// Force an immediate refresh to ensure the transition is complete
|
||||
lv_refr_now(NULL);
|
||||
|
||||
// Now safe to delete the splash screen and its children
|
||||
lv_obj_del(splashScreen);
|
||||
|
||||
ESP_LOGI(TAG, "Splash screen complete, main screen active");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user