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:
@@ -18,6 +18,7 @@
|
||||
#include "drivers/display_driver.h"
|
||||
#include "esp_log.h"
|
||||
#include "ui/lottie_player.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
static const char *TAG = "shake_emotion";
|
||||
|
||||
@@ -257,8 +258,7 @@ static const char *shakeStopAnimationData =
|
||||
typedef enum {
|
||||
SHAKE_STATE_IDLE = 0,
|
||||
SHAKE_STATE_SHAKING,
|
||||
SHAKE_STATE_FINISHING_LOOP,
|
||||
SHAKE_STATE_PLAYING_STOP_ANIM,
|
||||
SHAKE_STATE_SLOWING_DOWN,
|
||||
SHAKE_STATE_COMPLETE
|
||||
} ShakeState;
|
||||
|
||||
@@ -271,6 +271,10 @@ static lottie_player_handle_t stopPlayer = NULL;
|
||||
static bool initialized = false;
|
||||
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
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -330,8 +334,11 @@ void shakeEmotionBeginStop(void) {
|
||||
lottieSetLoop(shakePlayer, false);
|
||||
}
|
||||
|
||||
state = SHAKE_STATE_FINISHING_LOOP;
|
||||
ESP_LOGI(TAG, "Finishing shake loop before stop animation");
|
||||
// Record the start time for progressive deceleration
|
||||
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) {
|
||||
@@ -339,44 +346,48 @@ bool shakeEmotionUpdate(void) {
|
||||
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
|
||||
if (shakePlayer != NULL && !lottieIsPlaying(shakePlayer)) {
|
||||
// Destroy shake player
|
||||
lottieDestroy(shakePlayer);
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
state = SHAKE_STATE_COMPLETE;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (state == SHAKE_STATE_PLAYING_STOP_ANIM) {
|
||||
// Check if stop animation has completed
|
||||
if (stopPlayer != NULL && !lottieIsPlaying(stopPlayer)) {
|
||||
lottieDestroy(stopPlayer);
|
||||
stopPlayer = NULL;
|
||||
state = SHAKE_STATE_COMPLETE;
|
||||
ESP_LOGI(TAG, "Shake-stop transition complete");
|
||||
ESP_LOGI(TAG, "Progressive deceleration complete");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Calculate current speed using linear interpolation (1.0 -> 0.0)
|
||||
if (elapsed_ms >= slowdown_duration_ms) {
|
||||
// Recovery phase complete, stop the animation
|
||||
if (shakePlayer != NULL) {
|
||||
lottiePause(shakePlayer);
|
||||
lottieDestroy(shakePlayer);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -404,6 +415,5 @@ void shakeEmotionStop(void) {
|
||||
}
|
||||
|
||||
bool shakeEmotionIsPlaying(void) {
|
||||
return state == SHAKE_STATE_SHAKING || state == SHAKE_STATE_FINISHING_LOOP ||
|
||||
state == SHAKE_STATE_PLAYING_STOP_ANIM;
|
||||
return state == SHAKE_STATE_SHAKING || state == SHAKE_STATE_SLOWING_DOWN;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#define SHAKE_EMOTION_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
||||
1
main/emotion-engine/shy/shy.json
Normal file
1
main/emotion-engine/shy/shy.json
Normal file
File diff suppressed because one or more lines are too long
@@ -1388,6 +1388,12 @@ static void scheduleNextBlink(lottie_player_handle_t handle) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete existing timer if it exists
|
||||
if (handle->blinkTimer) {
|
||||
lv_timer_del(handle->blinkTimer);
|
||||
handle->blinkTimer = NULL;
|
||||
}
|
||||
|
||||
uint32_t delayMs =
|
||||
LOTTIE_BLINK_MIN_MS +
|
||||
(esp_random() % (LOTTIE_BLINK_MAX_MS - LOTTIE_BLINK_MIN_MS));
|
||||
|
||||
Reference in New Issue
Block a user