The shake animation figures have been fixed. However, the petting emotion working with touch remains an issue. The Lottie JSON for the emotion is unable not parsable for some reason and my next step is to fix that for tomorrow's meeting.
351 lines
11 KiB
C
351 lines
11 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 "emotion-engine/shy/shy_emotion.h"
|
|
#include "sensors/shake_detector.h"
|
|
#include "sensors/glide_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";
|
|
|
|
// Track touch initialization status
|
|
static bool touchInitialized = false;
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// Glide Detection Callback
|
|
// Triggers shy emotion when a glide gesture is detected.
|
|
// Only triggers if currently in idle state for calm, subtle interactions.
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static void onGlideDetected(const GlideEvent *event, void *user_data) {
|
|
(void)event; // Unused parameter
|
|
(void)user_data; // Unused parameter
|
|
|
|
// Only trigger shy emotion if currently in idle state
|
|
if (emotionManagerGetCurrent() == EMOTION_IDLE) {
|
|
ESP_LOGI(TAG, "Glide gesture detected, triggering shy emotion");
|
|
if (displayDriverLock(500)) {
|
|
emotionManagerSetEmotion(EMOTION_SHY);
|
|
displayDriverUnlock();
|
|
} else {
|
|
ESP_LOGW(TAG, "Failed to acquire lock for SHY emotion");
|
|
}
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// Glide Detection Task
|
|
// Polls the touch controller for glide gestures and triggers shy emotion.
|
|
// Only triggers shy emotion when currently in idle state.
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static void glideTask(void *arg) {
|
|
ESP_LOGI(TAG, "Glide detection task started");
|
|
|
|
// Check if touch was initialized successfully
|
|
if (!touchInitialized) {
|
|
ESP_LOGW(TAG, "Touch controller not initialized, glide detection disabled");
|
|
vTaskDelete(NULL);
|
|
return;
|
|
}
|
|
|
|
esp_lcd_touch_handle_t touchHandle = touchDriverGetHandle();
|
|
if (touchHandle == NULL) {
|
|
ESP_LOGE(TAG, "Touch handle is NULL even though initialization succeeded, disabling glide detection");
|
|
vTaskDelete(NULL);
|
|
return;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Glide detection task running with touch handle %p", (void*)touchHandle);
|
|
|
|
while (1) {
|
|
uint16_t x, y;
|
|
uint8_t count = 0;
|
|
|
|
// Read touch data from controller
|
|
esp_lcd_touch_read_data(touchHandle);
|
|
bool pressed = esp_lcd_touch_get_coordinates(touchHandle, &x, &y, NULL, &count, 1);
|
|
|
|
// Feed touch events to glide detector
|
|
glideDetectorUpdate(x, y, pressed && count > 0);
|
|
|
|
// Poll at 60Hz for responsive touch detection
|
|
vTaskDelay(pdMS_TO_TICKS(16));
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// Shy Emotion Task
|
|
// Monitors shy emotion state and transitions back to idle when complete.
|
|
// The shy emotion is a one-shot animation that plays once then returns to idle.
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static void shyTask(void *arg) {
|
|
ESP_LOGI(TAG, "Shy emotion task started");
|
|
|
|
while (1) {
|
|
// Check if shy emotion is currently playing
|
|
if (shyEmotionIsPlaying()) {
|
|
// Update shy emotion state
|
|
bool animationComplete = false;
|
|
if (displayDriverLock(100)) {
|
|
animationComplete = shyEmotionUpdate();
|
|
displayDriverUnlock();
|
|
}
|
|
|
|
// Transition back to idle when animation completes
|
|
if (animationComplete) {
|
|
ESP_LOGI(TAG, "Shy animation complete, returning to idle");
|
|
if (displayDriverLock(500)) {
|
|
emotionManagerSetEmotion(EMOTION_IDLE);
|
|
displayDriverUnlock();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Poll at 20Hz for emotion state updates
|
|
vTaskDelay(pdMS_TO_TICKS(50));
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 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)");
|
|
touchInitialized = false;
|
|
} else {
|
|
touchInitialized = true;
|
|
ESP_LOGI(TAG, "Touch controller initialized successfully");
|
|
}
|
|
|
|
// Initialize shake detector
|
|
if (!shakeDetectorInit()) {
|
|
ESP_LOGW(TAG, "Shake detector initialization failed (continuing)");
|
|
}
|
|
|
|
// Initialize glide detector
|
|
if (!glideDetectorInit()) {
|
|
ESP_LOGW(TAG, "Glide detector initialization failed (continuing)");
|
|
} else {
|
|
// Register callback to trigger shy emotion on glide detection
|
|
glideDetectorRegisterCallback(onGlideDetected, NULL);
|
|
}
|
|
|
|
// 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);
|
|
|
|
// Start glide detection task only if touch controller is initialized
|
|
if (touchInitialized) {
|
|
xTaskCreate(glideTask, "glide_task", 4096, NULL, 5, NULL);
|
|
} else {
|
|
ESP_LOGW(TAG, "Skipping glide detection task - touch controller not initialized");
|
|
}
|
|
|
|
// Start shy emotion task
|
|
// Use 8KB stack (same as ui_entry_task) to handle complex shy animation
|
|
// Shy emotion has large JSON (~3KB) and 300 frames requiring more stack
|
|
xTaskCreate(shyTask, "shy_task", 8192, 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);
|
|
}
|