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

184
main/drivers/nvs_driver.c Normal file
View File

@@ -0,0 +1,184 @@
/**
* @file nvs_driver.c
* @brief Non-Volatile Storage implementation
*
* Implements persistent state management using ESP-IDF NVS API.
*/
#include "drivers/nvs_driver.h"
#include "esp_log.h"
#include "nvs.h"
#include "nvs_flash.h"
static const char *TAG = "nvs_driver";
static const char *NAMESPACE = "storage";
static const char *KEY_PROVISIONED = "is_prov";
static const char *KEY_BOOT_COUNT = "boots";
bool nvsDriverInit(void) {
ESP_LOGI(TAG, "Initializing NVS");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_LOGW(TAG, "Erasing NVS partition due to corruption/version mismatch");
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
if (ret != ESP_OK) {
ESP_LOGE(TAG, "NVS Init failed: %s", esp_err_to_name(ret));
return false;
}
return true;
}
bool nvsDriverIsFirstBoot(void) {
nvs_handle_t handle;
esp_err_t err = nvs_open(NAMESPACE, NVS_READONLY, &handle);
if (err != ESP_OK) {
// If namespace doesn't exist, it's definitely first boot
return true;
}
uint8_t provisioned = 0;
err = nvs_get_u8(handle, KEY_PROVISIONED, &provisioned);
nvs_close(handle);
// If key not found (ESP_ERR_NVS_NOT_FOUND), it's first boot
if (err == ESP_ERR_NVS_NOT_FOUND) {
return true;
}
return (provisioned == 0);
}
bool nvsDriverMarkProvisioned(void) {
nvs_handle_t handle;
esp_err_t err = nvs_open(NAMESPACE, NVS_READWRITE, &handle);
if (err != ESP_OK)
return false;
err = nvs_set_u8(handle, KEY_PROVISIONED, 1);
if (err == ESP_OK) {
err = nvs_commit(handle);
}
nvs_close(handle);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Device marked as provisioned");
} else {
ESP_LOGE(TAG, "Failed to write provision flag: %s", esp_err_to_name(err));
}
return (err == ESP_OK);
}
uint32_t nvsDriverGetBootCount(void) {
nvs_handle_t handle;
if (nvs_open(NAMESPACE, NVS_READONLY, &handle) != ESP_OK)
return 1;
uint32_t count = 0;
esp_err_t err = nvs_get_u32(handle, KEY_BOOT_COUNT, &count);
nvs_close(handle);
return (err == ESP_OK) ? count : 1;
}
void nvsDriverIncrementBootCount(void) {
nvs_handle_t handle;
if (nvs_open(NAMESPACE, NVS_READWRITE, &handle) != ESP_OK)
return;
uint32_t count = 0;
nvs_get_u32(handle, KEY_BOOT_COUNT, &count);
count++;
nvs_set_u32(handle, KEY_BOOT_COUNT, count);
nvs_commit(handle);
nvs_close(handle);
ESP_LOGI(TAG, "Boot count updated: %ld", (long)count);
}
// IMU calibration keys
static const char *KEY_IMU_CAL_X = "imu_cal_x";
static const char *KEY_IMU_CAL_Y = "imu_cal_y";
static const char *KEY_IMU_CAL_Z = "imu_cal_z";
bool nvsDriverSaveImuCalibration(float x, float y, float z) {
nvs_handle_t handle;
esp_err_t err = nvs_open(NAMESPACE, NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to open NVS for calibration save");
return false;
}
// NVS doesn't have native float support, so we store as blobs
bool success = true;
if (nvs_set_blob(handle, KEY_IMU_CAL_X, &x, sizeof(float)) != ESP_OK) {
success = false;
}
if (nvs_set_blob(handle, KEY_IMU_CAL_Y, &y, sizeof(float)) != ESP_OK) {
success = false;
}
if (nvs_set_blob(handle, KEY_IMU_CAL_Z, &z, sizeof(float)) != ESP_OK) {
success = false;
}
if (success) {
err = nvs_commit(handle);
if (err != ESP_OK) {
success = false;
}
}
nvs_close(handle);
if (success) {
ESP_LOGI(TAG, "IMU calibration saved: x=%.3f y=%.3f z=%.3f", x, y, z);
} else {
ESP_LOGE(TAG, "Failed to save IMU calibration");
}
return success;
}
bool nvsDriverLoadImuCalibration(float *x, float *y, float *z) {
if (!x || !y || !z) {
return false;
}
nvs_handle_t handle;
esp_err_t err = nvs_open(NAMESPACE, NVS_READONLY, &handle);
if (err != ESP_OK) {
return false;
}
size_t size = sizeof(float);
bool success = true;
if (nvs_get_blob(handle, KEY_IMU_CAL_X, x, &size) != ESP_OK) {
success = false;
}
size = sizeof(float);
if (nvs_get_blob(handle, KEY_IMU_CAL_Y, y, &size) != ESP_OK) {
success = false;
}
size = sizeof(float);
if (nvs_get_blob(handle, KEY_IMU_CAL_Z, z, &size) != ESP_OK) {
success = false;
}
nvs_close(handle);
if (success) {
ESP_LOGI(TAG, "IMU calibration loaded: x=%.3f y=%.3f z=%.3f", *x, *y, *z);
}
return success;
}

84
main/drivers/nvs_driver.h Normal file
View File

@@ -0,0 +1,84 @@
/**
* @file nvs_driver.h
* @brief Non-Volatile Storage abstraction for persistent state
*
* Handles reading and writing system state to flash memory.
* Used for tracking first-boot status and other persistent flags.
*/
#ifndef NVS_DRIVER_H
#define NVS_DRIVER_H
#include "esp_err.h"
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize NVS flash
*
* Must be called before any other NVS operations.
* Handles partition initialization and potential recovery.
*
* @return true on success, false on failure
*/
bool nvsDriverInit(void);
/**
* @brief Check if this is the device's first boot
*
* Checks for the existence of a "provisioned" flag.
*
* @return true if first boot (flag not found), false otherwise
*/
bool nvsDriverIsFirstBoot(void);
/**
* @brief Mark the device as provisioned (not first boot)
*
* Writes the persistent flag to storage.
*
* @return true on success
*/
bool nvsDriverMarkProvisioned(void);
/**
* @brief Get the total boot count
*
* @return Number of times device has booted (defaults to 1 if read fails)
*/
uint32_t nvsDriverGetBootCount(void);
/**
* @brief Increment the total boot count
*/
void nvsDriverIncrementBootCount(void);
/**
* @brief Save IMU calibration data to NVS
*
* @param x X-axis baseline value
* @param y Y-axis baseline value
* @param z Z-axis baseline value
* @return true on success, false on failure
*/
bool nvsDriverSaveImuCalibration(float x, float y, float z);
/**
* @brief Load IMU calibration data from NVS
*
* @param x Pointer to store X-axis baseline
* @param y Pointer to store Y-axis baseline
* @param z Pointer to store Z-axis baseline
* @return true if calibration data exists and was loaded, false otherwise
*/
bool nvsDriverLoadImuCalibration(float *x, float *y, float *z);
#ifdef __cplusplus
}
#endif
#endif // NVS_DRIVER_H