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

@@ -21,13 +21,17 @@
*/
#include "drivers/display_driver.h"
#include "drivers/nvs_driver.h"
#include "drivers/touch_driver.h"
#include "emotion-engine/emotion_manager.h"
#include "emotion-engine/shake/shake_emotion.h"
#include "sensors/shake_detector.h"
#include "ui/calibration_screen.h"
#include "ui/loading_anim.h"
#include "ui/splash_screen.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@@ -36,37 +40,75 @@ static const char *TAG = "main";
// ----------------------------------------------------------------------------
// Shake Detection Task
// Polls the accelerometer for shake events and triggers emotions.
// Implements 2-second holdoff after shaking stops before returning to idle.
// ----------------------------------------------------------------------------
// Holdoff time after shaking stops before transitioning (milliseconds)
#define SHAKE_HOLDOFF_MS 500
static void shakeTask(void *arg) {
ESP_LOGI(TAG, "Shake detection task started");
bool isShakingActive = false;
bool isTransitioning = false;
uint32_t shakeStoppedTime = 0;
while (1) {
ShakeEvent event = shakeDetectorUpdate();
uint32_t now = (uint32_t)(esp_timer_get_time() / 1000);
if (event != SHAKE_NONE) {
// Map shake events to emotions
EmotionType emotion = EMOTION_IDLE;
switch (event) {
case SHAKE_SURPRISED:
emotion = EMOTION_SURPRISED;
break;
case SHAKE_SAD:
emotion = EMOTION_SAD;
break;
case SHAKE_ANGRY:
emotion = EMOTION_ANGRY;
break;
default:
break;
// Handle transition completion (polling shakeEmotionUpdate)
if (isTransitioning) {
bool transitionComplete = false;
if (displayDriverLock(100)) {
transitionComplete = shakeEmotionUpdate();
displayDriverUnlock();
}
if (emotion != EMOTION_IDLE) {
// Acquire LVGL lock before modifying emotions
if (displayDriverLock(100)) {
emotionManagerSetEmotion(emotion);
if (transitionComplete) {
ESP_LOGI(TAG, "Shake transition complete, returning to idle");
if (displayDriverLock(500)) {
emotionManagerSetEmotion(EMOTION_IDLE);
displayDriverUnlock();
}
isTransitioning = false;
isShakingActive = false;
}
vTaskDelay(pdMS_TO_TICKS(50));
continue;
}
if (event == SHAKE_ACTIVE) {
// Start or continue shaking animation
if (!isShakingActive) {
ESP_LOGI(TAG, "Shake event: Starting shaking animation");
if (displayDriverLock(500)) {
emotionManagerSetEmotion(EMOTION_SHAKING);
displayDriverUnlock();
} else {
ESP_LOGW(TAG, "Failed to acquire lock for SHAKING");
}
isShakingActive = true;
}
// Reset stopped time while actively shaking
shakeStoppedTime = 0;
} else if (event == SHAKE_STOPPED) {
// Shaking just stopped, start holdoff timer
ESP_LOGI(TAG, "Shake event: Stopped, starting holdoff");
shakeStoppedTime = now;
} else if (isShakingActive && shakeStoppedTime != 0) {
// Check if holdoff period has elapsed
if (now - shakeStoppedTime >= SHAKE_HOLDOFF_MS) {
ESP_LOGI(TAG, "Holdoff complete, starting shake-stop transition");
if (displayDriverLock(500)) {
emotionManagerSetEmotion(EMOTION_SHAKE_STOPPING);
displayDriverUnlock();
isTransitioning = true;
shakeStoppedTime = 0;
} else {
ESP_LOGW(TAG, "Failed to acquire lock for SHAKE_STOPPING");
}
}
}
@@ -79,12 +121,17 @@ static void shakeTask(void *arg) {
// Application Entry Point
// ----------------------------------------------------------------------------
void app_main(void) {
ESP_LOGI(TAG, "Starting iroBot Display System");
// ----------------------------------------------------------------------------
// Main UI Task
// ----------------------------------------------------------------------------
static void ui_entry_task(void *arg) {
ESP_LOGI(TAG, "UI Entry Task started");
// Initialize display subsystem (I2C, SPI, LCD, LVGL)
if (!displayDriverInit()) {
ESP_LOGE(TAG, "Display initialization failed");
vTaskDelete(NULL);
return;
}
@@ -98,7 +145,48 @@ void app_main(void) {
ESP_LOGW(TAG, "Shake detector initialization failed (continuing)");
}
// Show splash screen (blocks for SPLASH_SCREEN_DURATION_MS)
// Initialize NVS (Critical for first boot check)
nvsDriverInit();
// Check for first boot experience
bool isFirstBoot = nvsDriverIsFirstBoot();
if (isFirstBoot) {
if (displayDriverLock(-1)) {
ESP_LOGI(TAG, "First boot detected! Showing loading animation.");
loadingAnimStart();
displayDriverUnlock();
// Allow animation to play
vTaskDelay(pdMS_TO_TICKS(3000));
if (displayDriverLock(-1)) {
loadingAnimStop();
displayDriverUnlock();
}
}
// Show IMU calibration screen
if (displayDriverLock(-1)) {
ESP_LOGI(TAG, "Showing IMU calibration screen");
calibrationScreenShow();
displayDriverUnlock();
}
// Mark device as provisioned after successful calibration
nvsDriverMarkProvisioned();
} else {
// Normal boot - load calibration from NVS
float calX, calY, calZ;
if (nvsDriverLoadImuCalibration(&calX, &calY, &calZ)) {
ESP_LOGI(TAG, "Applying stored IMU calibration");
shakeDetectorSetBaseline(calX, calY, calZ);
} else {
ESP_LOGW(TAG,
"No stored IMU calibration found, using runtime calibration");
}
}
// Show standard splash screen
if (displayDriverLock(-1)) {
ESP_LOGI(TAG, "Showing splash screen");
splashScreenShow();
@@ -108,10 +196,11 @@ void app_main(void) {
// Initialize emotion engine
if (!emotionManagerInit()) {
ESP_LOGE(TAG, "Emotion manager initialization failed");
vTaskDelete(NULL);
return;
}
// Start emotion engine (begins idle animation with natural blink)
// Start emotion engine
if (displayDriverLock(-1)) {
ESP_LOGI(TAG, "Starting emotion engine");
emotionManagerStart();
@@ -122,4 +211,14 @@ void app_main(void) {
xTaskCreate(shakeTask, "shake_task", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "iroBot Display System initialized successfully");
vTaskDelete(NULL);
}
void app_main(void) {
ESP_LOGI(TAG, "Starting iroBot Display System (Main)");
// Spawn a dedicated task with larger stack for UI/App logic
// Default main stack might be too small for LVGL recursive calls
// Using 8KB stack size to be safe
xTaskCreate(ui_entry_task, "ui_entry", 8192, NULL, 5, NULL);
}