Files
iroBotAMOLED_Test/main/main.c
Neil Arora 4d68a788ad 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.
2026-01-24 20:02:04 +05:30

225 lines
6.8 KiB
C

/**
* @file main.c
* @brief Application entry point for iroBot display system
*
* This is the main entry point for the iroBot display firmware.
* It initializes all subsystems in the proper order and starts
* the main application loop.
*
* Architecture Overview:
* - drivers/: Hardware abstraction (display, touch)
* - sensors/: Motion detection (shake detector)
* - ui/: Visual components (splash screen, lottie player)
* - emotion-engine/: Emotion management and animations
*
* Startup Sequence:
* 1. Initialize display driver (I2C, SPI, LCD, LVGL)
* 2. Initialize touch driver
* 3. Initialize shake detector sensor
* 4. Show splash screen
* 5. Initialize and start emotion engine
*/
#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"
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);
// Handle transition completion (polling shakeEmotionUpdate)
if (isTransitioning) {
bool transitionComplete = false;
if (displayDriverLock(100)) {
transitionComplete = shakeEmotionUpdate();
displayDriverUnlock();
}
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");
}
}
}
// Poll at 20Hz for responsive shake detection
vTaskDelay(pdMS_TO_TICKS(50));
}
}
// ----------------------------------------------------------------------------
// Application Entry Point
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// 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;
}
// Initialize touch controller
if (!touchDriverInit()) {
ESP_LOGW(TAG, "Touch initialization failed (continuing)");
}
// Initialize shake detector
if (!shakeDetectorInit()) {
ESP_LOGW(TAG, "Shake detector initialization failed (continuing)");
}
// 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();
displayDriverUnlock();
}
// Initialize emotion engine
if (!emotionManagerInit()) {
ESP_LOGE(TAG, "Emotion manager initialization failed");
vTaskDelete(NULL);
return;
}
// Start emotion engine
if (displayDriverLock(-1)) {
ESP_LOGI(TAG, "Starting emotion engine");
emotionManagerStart();
displayDriverUnlock();
}
// Start shake detection task
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);
}