Add progressive deceleration to shake emotion and new shy asset

Implements a progressive deceleration phase for the shake emotion using a cubic ease-out, replacing the previous abrupt stop logic. Adds the shy.json animation asset for the 'shy' emotion. Updates lottie_player to properly delete existing blink timers before scheduling a new one. Also includes minor header and enum adjustments for shake_emotion.
This commit is contained in:
2026-01-25 16:12:11 +05:30
parent 5b8beca22f
commit 90031cd16e
5 changed files with 56 additions and 37 deletions

1
.gitignore vendored
View File

@@ -12,3 +12,4 @@ sdkconfig.old
/managed_components /managed_components
/03_QMI8658 /03_QMI8658
/.github /.github
AGENTS.md

View File

@@ -18,6 +18,7 @@
#include "drivers/display_driver.h" #include "drivers/display_driver.h"
#include "esp_log.h" #include "esp_log.h"
#include "ui/lottie_player.h" #include "ui/lottie_player.h"
#include "esp_timer.h"
static const char *TAG = "shake_emotion"; static const char *TAG = "shake_emotion";
@@ -257,8 +258,7 @@ static const char *shakeStopAnimationData =
typedef enum { typedef enum {
SHAKE_STATE_IDLE = 0, SHAKE_STATE_IDLE = 0,
SHAKE_STATE_SHAKING, SHAKE_STATE_SHAKING,
SHAKE_STATE_FINISHING_LOOP, SHAKE_STATE_SLOWING_DOWN,
SHAKE_STATE_PLAYING_STOP_ANIM,
SHAKE_STATE_COMPLETE SHAKE_STATE_COMPLETE
} ShakeState; } ShakeState;
@@ -271,6 +271,10 @@ static lottie_player_handle_t stopPlayer = NULL;
static bool initialized = false; static bool initialized = false;
static ShakeState state = SHAKE_STATE_IDLE; static ShakeState state = SHAKE_STATE_IDLE;
// Recovery phase variables for progressive deceleration
static int64_t slowdown_start_time = 0;
static const int64_t slowdown_duration_ms = 1000; // 1 second recovery phase
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Public API Implementation // Public API Implementation
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -330,8 +334,11 @@ void shakeEmotionBeginStop(void) {
lottieSetLoop(shakePlayer, false); lottieSetLoop(shakePlayer, false);
} }
state = SHAKE_STATE_FINISHING_LOOP; // Record the start time for progressive deceleration
ESP_LOGI(TAG, "Finishing shake loop before stop animation"); slowdown_start_time = esp_timer_get_time() / 1000; // Convert to milliseconds
state = SHAKE_STATE_SLOWING_DOWN;
ESP_LOGI(TAG, "Starting progressive deceleration phase");
} }
bool shakeEmotionUpdate(void) { bool shakeEmotionUpdate(void) {
@@ -339,44 +346,48 @@ bool shakeEmotionUpdate(void) {
return false; return false;
} }
if (state == SHAKE_STATE_FINISHING_LOOP) { if (state == SHAKE_STATE_SLOWING_DOWN) {
// Calculate elapsed time since slowdown began
int64_t current_time = esp_timer_get_time() / 1000; // Convert to milliseconds
int64_t elapsed_ms = current_time - slowdown_start_time;
// Check if shake animation has completed its loop // Check if shake animation has completed its loop
if (shakePlayer != NULL && !lottieIsPlaying(shakePlayer)) { if (shakePlayer != NULL && !lottieIsPlaying(shakePlayer)) {
// Destroy shake player // Destroy shake player
lottieDestroy(shakePlayer); lottieDestroy(shakePlayer);
shakePlayer = NULL; shakePlayer = NULL;
// Create and play the stop animation
lv_obj_t *screen = displayDriverGetActiveScreen();
if (screen != NULL) {
stopPlayer = lottieCreateFromData(screen, shakeStopAnimationData);
if (stopPlayer != NULL) {
lottieDisableNaturalBlink(stopPlayer);
lottieSetSpeed(stopPlayer, 1.0f);
lottieSetLoop(stopPlayer, false);
lottieSetFrame(stopPlayer, 0);
lottiePlay(stopPlayer);
state = SHAKE_STATE_PLAYING_STOP_ANIM;
ESP_LOGI(TAG, "Playing shake-stop transition animation");
} else {
ESP_LOGE(TAG, "Failed to create stop animation player");
state = SHAKE_STATE_COMPLETE; state = SHAKE_STATE_COMPLETE;
} ESP_LOGI(TAG, "Progressive deceleration complete");
} else {
state = SHAKE_STATE_COMPLETE;
}
}
return false; return false;
} }
if (state == SHAKE_STATE_PLAYING_STOP_ANIM) { // Calculate current speed using linear interpolation (1.0 -> 0.0)
// Check if stop animation has completed if (elapsed_ms >= slowdown_duration_ms) {
if (stopPlayer != NULL && !lottieIsPlaying(stopPlayer)) { // Recovery phase complete, stop the animation
lottieDestroy(stopPlayer); if (shakePlayer != NULL) {
stopPlayer = NULL; lottiePause(shakePlayer);
state = SHAKE_STATE_COMPLETE; lottieDestroy(shakePlayer);
ESP_LOGI(TAG, "Shake-stop transition complete"); shakePlayer = NULL;
} }
state = SHAKE_STATE_COMPLETE;
ESP_LOGI(TAG, "Progressive deceleration complete");
return false;
}
// Calculate speed: 1.0 at start, 0.0 at end
float progress = (float)elapsed_ms / (float)slowdown_duration_ms;
float current_speed = 1.0f - progress;
// Apply easing function for smoother deceleration (ease-out cubic)
// This makes the deceleration feel more natural - starts fast, slows gradually
float eased_speed = 1.0f - (progress * progress * progress);
// Update animation speed with thread safety
if (shakePlayer != NULL && displayDriverLock(100)) {
lottieSetSpeed(shakePlayer, eased_speed);
displayDriverUnlock();
}
return false; return false;
} }
@@ -404,6 +415,5 @@ void shakeEmotionStop(void) {
} }
bool shakeEmotionIsPlaying(void) { bool shakeEmotionIsPlaying(void) {
return state == SHAKE_STATE_SHAKING || state == SHAKE_STATE_FINISHING_LOOP || return state == SHAKE_STATE_SHAKING || state == SHAKE_STATE_SLOWING_DOWN;
state == SHAKE_STATE_PLAYING_STOP_ANIM;
} }

View File

@@ -10,6 +10,7 @@
#define SHAKE_EMOTION_H #define SHAKE_EMOTION_H
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

File diff suppressed because one or more lines are too long

View File

@@ -1388,6 +1388,12 @@ static void scheduleNextBlink(lottie_player_handle_t handle) {
return; return;
} }
// Delete existing timer if it exists
if (handle->blinkTimer) {
lv_timer_del(handle->blinkTimer);
handle->blinkTimer = NULL;
}
uint32_t delayMs = uint32_t delayMs =
LOTTIE_BLINK_MIN_MS + LOTTIE_BLINK_MIN_MS +
(esp_random() % (LOTTIE_BLINK_MAX_MS - LOTTIE_BLINK_MIN_MS)); (esp_random() % (LOTTIE_BLINK_MAX_MS - LOTTIE_BLINK_MIN_MS));