/** * @file animated_eyes.c * @brief Animated expressive eyes for LVGL - Implementation * Ported from esp32-eyes for LVGL/ESP-IDF */ #include "animated_eyes.h" #include "esp_heap_caps.h" #include "esp_log.h" #include "esp_timer.h" #include #include #include static const char *TAG = "animated_eyes"; #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) // Get current time in milliseconds static inline uint32_t get_millis(void) { return (uint32_t)(esp_timer_get_time() / 1000); } // Eye configuration structure typedef struct { int16_t offset_x, offset_y; int16_t height, width; float slope_top, slope_bottom; int16_t radius_top, radius_bottom; } eye_config_t; // Transformation structure typedef struct { int16_t move_x, move_y; float scale_x, scale_y; } transformation_t; // Animation state typedef struct { uint32_t start_time; uint32_t interval; uint32_t t0, t1, t2, t3, t4; } animation_t; // Timer structure typedef struct { uint32_t start_time; uint32_t interval; bool is_active; bool is_expired; } async_timer_t; // Eye state typedef struct { eye_config_t config; eye_config_t final_config; eye_config_t transition_dest; transformation_t transform_origin, transform_current, transform_dest; eye_config_t variation_values1, variation_values2; animation_t transition_anim, transform_anim, variation_anim1, variation_anim2, blink_anim; int16_t center_x, center_y; bool is_mirrored; } eye_state_t; // Main animated eyes structure struct animated_eyes_t { lv_obj_t *canvas; lv_color_t *canvas_buf; animated_eyes_config_t config; eye_state_t left_eye, right_eye; animated_eyes_emotion_t current_emotion; float emotion_weights[EMOTION_COUNT]; async_timer_t behavior_timer, look_timer, blink_timer; // Temporary emotion state bool is_temporary_emotion; animated_eyes_emotion_t saved_emotion; async_timer_t temporary_timer; // Inertia state float inertia_x; float inertia_y; uint16_t center_x, center_y; }; // Eye presets for different emotions static const eye_config_t PRESET_NORMAL = {0, 0, 40, 40, 0, 0, 8, 8}; static const eye_config_t PRESET_HAPPY = {0, 0, 10, 40, 0, 0, 10, 0}; static const eye_config_t PRESET_GLEE = {0, 0, 8, 40, 0, 0, 8, 0}; static const eye_config_t PRESET_SAD = {0, 0, 15, 40, -0.5f, 0, 1, 10}; static const eye_config_t PRESET_WORRIED = {0, 0, 25, 40, -0.1f, 0, 6, 10}; static const eye_config_t PRESET_FOCUSED = {0, 0, 14, 40, 0.2f, 0, 3, 1}; static const eye_config_t PRESET_ANNOYED = {0, 0, 12, 40, 0, 0, 0, 10}; static const eye_config_t PRESET_SURPRISED = {-2, 0, 45, 45, 0, 0, 16, 16}; static const eye_config_t PRESET_ANGRY = {-3, 0, 20, 40, 0.3f, 0, 2, 12}; static const eye_config_t PRESET_FURIOUS = {-2, 0, 30, 40, 0.4f, 0, 2, 8}; static const eye_config_t PRESET_SCARED = {-3, 0, 40, 40, -0.1f, 0, 12, 8}; static const eye_config_t PRESET_AWE = {2, 0, 35, 45, -0.1f, 0.1f, 12, 12}; static const eye_config_t PRESET_SLEEPY = {0, -2, 14, 40, -0.5f, -0.5f, 3, 3}; static const eye_config_t PRESET_SUSPICIOUS = {0, 0, 22, 40, 0, 0, 8, 3}; static const eye_config_t PRESET_SKEPTIC = {0, 0, 40, 40, 0, 0, 10, 10}; static const eye_config_t PRESET_FRUSTRATED = {3, -5, 12, 40, 0, 0, 0, 10}; static const eye_config_t PRESET_UNIMPRESSED = {3, 0, 12, 40, 0, 0, 1, 10}; static const eye_config_t PRESET_SQUINT = {-10, -3, 35, 35, 0, 0, 8, 8}; // Timer functions static void timer_start(async_timer_t *timer, uint32_t interval) { timer->start_time = get_millis(); timer->interval = interval; timer->is_active = true; timer->is_expired = false; } static void timer_reset(async_timer_t *timer) { timer->start_time = get_millis(); timer->is_expired = false; } static void timer_update(async_timer_t *timer) { if (!timer->is_active) return; uint32_t now = get_millis(); if (now - timer->start_time >= timer->interval) { timer->is_expired = true; } } // Animation functions static void anim_restart(animation_t *anim) { anim->start_time = get_millis(); } static float anim_get_value(animation_t *anim) { uint32_t elapsed = get_millis() - anim->start_time; if (anim->interval == 0) return 0.0f; uint32_t pos = elapsed % anim->interval; if (pos < anim->t0) return 0.0f; pos -= anim->t0; if (pos < anim->t1 && anim->t1 > 0) return (float)pos / anim->t1; pos -= anim->t1; if (pos < anim->t2) return 1.0f; pos -= anim->t2; if (pos < anim->t3 && anim->t3 > 0) return 1.0f - (float)pos / anim->t3; return 0.0f; } static uint32_t anim_get_elapsed(animation_t *anim) { return get_millis() - anim->start_time; } static void anim_set_trapezium(animation_t *anim, uint32_t t0, uint32_t t1, uint32_t t2, uint32_t t3, uint32_t t4) { anim->t0 = t0; anim->t1 = t1; anim->t2 = t2; anim->t3 = t3; anim->t4 = t4; anim->interval = t0 + t1 + t2 + t3 + t4; } // Scale preset for larger screen static eye_config_t scale_preset(const eye_config_t *preset, float scale) { eye_config_t scaled = *preset; scaled.offset_x = (int16_t)(preset->offset_x * scale); scaled.offset_y = (int16_t)(preset->offset_y * scale); scaled.height = (int16_t)(preset->height * scale); scaled.width = (int16_t)(preset->width * scale); scaled.radius_top = (int16_t)(preset->radius_top * scale); scaled.radius_bottom = (int16_t)(preset->radius_bottom * scale); return scaled; } // Initialize eye state static void eye_init(eye_state_t *eye, bool is_mirrored) { memset(eye, 0, sizeof(eye_state_t)); eye->is_mirrored = is_mirrored; eye->transform_current.scale_x = 1.0f; eye->transform_current.scale_y = 1.0f; eye->transform_dest.scale_x = 1.0f; eye->transform_dest.scale_y = 1.0f; eye->transform_origin.scale_x = 1.0f; eye->transform_origin.scale_y = 1.0f; anim_set_trapezium(&eye->transition_anim, 0, 250, 0, 250, 0); anim_set_trapezium(&eye->transform_anim, 0, 100, 0, 100, 0); // Smoother blink: 150ms close, 50ms closed, 150ms open anim_set_trapezium(&eye->blink_anim, 0, 150, 50, 150, 0); anim_set_trapezium(&eye->variation_anim1, 200, 200, 200, 200, 0); anim_set_trapezium(&eye->variation_anim2, 0, 200, 200, 200, 200); } // Apply preset to eye static void eye_apply_preset(eye_state_t *eye, const eye_config_t *preset, float scale) { eye_config_t scaled = scale_preset(preset, scale); eye->config = scaled; if (eye->is_mirrored) { eye->config.offset_x = -scaled.offset_x; eye->config.slope_top = scaled.slope_top; eye->config.slope_bottom = scaled.slope_bottom; } else { eye->config.slope_top = -scaled.slope_top; eye->config.slope_bottom = -scaled.slope_bottom; } eye->transition_dest = eye->config; } // Transition eye to preset static void eye_transition_to(eye_state_t *eye, const eye_config_t *preset, float scale) { eye_config_t scaled = scale_preset(preset, scale); eye->transition_dest = scaled; if (eye->is_mirrored) { eye->transition_dest.offset_x = -scaled.offset_x; eye->transition_dest.slope_top = scaled.slope_top; eye->transition_dest.slope_bottom = scaled.slope_bottom; } else { eye->transition_dest.slope_top = -scaled.slope_top; eye->transition_dest.slope_bottom = -scaled.slope_bottom; } anim_restart(&eye->transition_anim); } // Update eye animations static void eye_update(eye_state_t *eye) { // Transition update float t = anim_get_value(&eye->transition_anim); eye->config.offset_x = eye->config.offset_x * (1.0f - t) + eye->transition_dest.offset_x * t; eye->config.offset_y = eye->config.offset_y * (1.0f - t) + eye->transition_dest.offset_y * t; eye->config.height = eye->config.height * (1.0f - t) + eye->transition_dest.height * t; eye->config.width = eye->config.width * (1.0f - t) + eye->transition_dest.width * t; eye->config.slope_top = eye->config.slope_top * (1.0f - t) + eye->transition_dest.slope_top * t; eye->config.slope_bottom = eye->config.slope_bottom * (1.0f - t) + eye->transition_dest.slope_bottom * t; eye->config.radius_top = eye->config.radius_top * (1.0f - t) + eye->transition_dest.radius_top * t; eye->config.radius_bottom = eye->config.radius_bottom * (1.0f - t) + eye->transition_dest.radius_bottom * t; // Transform update t = anim_get_value(&eye->transform_anim); eye->transform_current.move_x = (eye->transform_dest.move_x - eye->transform_origin.move_x) * t + eye->transform_origin.move_x; eye->transform_current.move_y = (eye->transform_dest.move_y - eye->transform_origin.move_y) * t + eye->transform_origin.move_y; eye->transform_current.scale_x = (eye->transform_dest.scale_x - eye->transform_origin.scale_x) * t + eye->transform_origin.scale_x; eye->transform_current.scale_y = (eye->transform_dest.scale_y - eye->transform_origin.scale_y) * t + eye->transform_origin.scale_y; // Apply transformation eye->final_config.offset_x = eye->config.offset_x + eye->transform_current.move_x; eye->final_config.offset_y = eye->config.offset_y - eye->transform_current.move_y; eye->final_config.width = eye->config.width * eye->transform_current.scale_x; eye->final_config.height = eye->config.height * eye->transform_current.scale_y; eye->final_config.slope_top = eye->config.slope_top; eye->final_config.slope_bottom = eye->config.slope_bottom; eye->final_config.radius_top = eye->config.radius_top; eye->final_config.radius_bottom = eye->config.radius_bottom; // Blink update t = anim_get_value(&eye->blink_anim); if (anim_get_elapsed(&eye->blink_anim) > eye->blink_anim.interval) t = 0.0f; t = t * t; int16_t blink_height = 4; eye->final_config.height = (blink_height - eye->final_config.height) * t + eye->final_config.height; eye->final_config.slope_top *= (1.0f - t); eye->final_config.slope_bottom *= (1.0f - t); eye->final_config.radius_top *= (1.0f - t); eye->final_config.radius_bottom *= (1.0f - t); } // Draw filled rectangle on canvas // Draw filled quadrilateral (polygon) static void draw_quad(lv_obj_t *canvas, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, lv_color_t color) { lv_point_t points[4]; points[0].x = x0; points[0].y = y0; points[1].x = x1; points[1].y = y1; points[2].x = x2; points[2].y = y2; points[3].x = x3; points[3].y = y3; lv_draw_rect_dsc_t rect_dsc; lv_draw_rect_dsc_init(&rect_dsc); rect_dsc.bg_color = color; rect_dsc.bg_opa = LV_OPA_COVER; rect_dsc.border_width = 0; lv_canvas_draw_polygon(canvas, points, 4, &rect_dsc); } // Draw filled ellipse corner static void draw_ellipse_corner(lv_obj_t *canvas, int corner, int16_t x0, int16_t y0, int32_t rx, int32_t ry, lv_color_t color) { if (rx < 2 || ry < 2) return; lv_draw_rect_dsc_t rect_dsc; lv_draw_rect_dsc_init(&rect_dsc); rect_dsc.bg_color = color; rect_dsc.bg_opa = LV_OPA_COVER; rect_dsc.border_width = 0; rect_dsc.radius = 0; int32_t x, y; int32_t rx2 = rx * rx, ry2 = ry * ry; int32_t fx2 = 4 * rx2, fy2 = 4 * ry2; int32_t s; // Draw using horizontal lines for each scanline of the ellipse for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++) { int16_t draw_x, draw_y, draw_w; switch (corner) { case 0: draw_x = x0; draw_y = y0 - y; draw_w = x; break; // TR case 1: draw_x = x0 - x; draw_y = y0 - y; draw_w = x; break; // TL case 2: draw_x = x0 - x; draw_y = y0 + y - 1; draw_w = x; break; // BL case 3: draw_x = x0; draw_y = y0 + y - 1; draw_w = x; break; // BR } if (draw_w > 0) lv_canvas_draw_rect(canvas, draw_x, draw_y, draw_w, 1, &rect_dsc); if (s >= 0) { s += fx2 * (1 - y); y--; } s += ry2 * ((4 * x) + 6); } for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++) { int16_t draw_x, draw_y, draw_w; switch (corner) { case 0: draw_x = x0; draw_y = y0 - y; draw_w = x; break; case 1: draw_x = x0 - x; draw_y = y0 - y; draw_w = x; break; case 2: draw_x = x0 - x; draw_y = y0 + y; draw_w = x; break; case 3: draw_x = x0; draw_y = y0 + y; draw_w = x; break; } if (draw_w > 0) lv_canvas_draw_rect(canvas, draw_x, draw_y, draw_w, 1, &rect_dsc); if (s >= 0) { s += fy2 * (1 - x); x--; } s += rx2 * ((4 * y) + 6); } } // Draw single eye static void eye_draw(lv_obj_t *canvas, eye_state_t *eye, lv_color_t color) { eye_config_t *cfg = &eye->final_config; int16_t cx = eye->center_x; int16_t cy = eye->center_y; int32_t delta_y_top = cfg->height * cfg->slope_top / 2.0f; int32_t delta_y_bottom = cfg->height * cfg->slope_bottom / 2.0f; int32_t total_height = cfg->height + delta_y_top - delta_y_bottom; int16_t rt = cfg->radius_top, rb = cfg->radius_bottom; if (rt > 0 && rb > 0 && total_height - 1 < rb + rt) { float ratio = (float)(total_height - 1) / (rb + rt); rt = (int16_t)(rt * ratio); rb = (int16_t)(rb * ratio); } int32_t TLc_x = cx + cfg->offset_x - cfg->width / 2 + rt; int32_t TLc_y = cy + cfg->offset_y - cfg->height / 2 + rt - delta_y_top; int32_t TRc_x = cx + cfg->offset_x + cfg->width / 2 - rt; int32_t TRc_y = cy + cfg->offset_y - cfg->height / 2 + rt + delta_y_top; int32_t BLc_x = cx + cfg->offset_x - cfg->width / 2 + rb; int32_t BLc_y = cy + cfg->offset_y + cfg->height / 2 - rb - delta_y_bottom; int32_t BRc_x = cx + cfg->offset_x + cfg->width / 2 - rb; int32_t BRc_y = cy + cfg->offset_y + cfg->height / 2 - rb + delta_y_bottom; // Fill center (Connecting the 4 corner centers) // Expand by 2px to ensure overlap and avoid gaps draw_quad(canvas, TLc_x - 2, TLc_y - 2, TRc_x + 2, TRc_y - 2, BRc_x + 2, BRc_y + 2, BLc_x - 2, BLc_y + 2, color); // Fill edges (Connecting corner centers to outer edges) // Right Edge draw_quad(canvas, TRc_x, TRc_y, TRc_x + rt, TRc_y, BRc_x + rb, BRc_y, BRc_x, BRc_y, color); // Top Edge draw_quad(canvas, TLc_x, TLc_y - rt, TRc_x, TRc_y - rt, TRc_x, TRc_y, TLc_x, TLc_y, color); // Left Edge draw_quad(canvas, TLc_x - rt, TLc_y, TLc_x, TLc_y, BLc_x, BLc_y, BLc_x - rb, BLc_y, color); // Bottom Edge draw_quad(canvas, BLc_x, BLc_y, BRc_x, BRc_y, BRc_x, BRc_y + rb, BLc_x, BLc_y + rb, color); // Draw corners if (rt > 0) { draw_ellipse_corner(canvas, 1, TLc_x, TLc_y, rt, rt, color); draw_ellipse_corner(canvas, 0, TRc_x, TRc_y, rt, rt, color); } if (rb > 0) { draw_ellipse_corner(canvas, 2, BLc_x, BLc_y, rb, rb, color); draw_ellipse_corner(canvas, 3, BRc_x, BRc_y, rb, rb, color); } } // Go to emotion static void go_to_emotion(animated_eyes_handle_t h, animated_eyes_emotion_t emotion) { float scale = (float)h->config.eye_size / 40.0f; const eye_config_t *preset = &PRESET_NORMAL; switch (emotion) { case EMOTION_NORMAL: preset = &PRESET_NORMAL; break; case EMOTION_ANGRY: preset = &PRESET_ANGRY; break; case EMOTION_GLEE: preset = &PRESET_GLEE; break; case EMOTION_HAPPY: preset = &PRESET_HAPPY; break; case EMOTION_SAD: preset = &PRESET_SAD; break; case EMOTION_WORRIED: preset = &PRESET_WORRIED; break; case EMOTION_FOCUSED: preset = &PRESET_FOCUSED; break; case EMOTION_ANNOYED: preset = &PRESET_ANNOYED; break; case EMOTION_SURPRISED: preset = &PRESET_SURPRISED; break; case EMOTION_SKEPTIC: preset = &PRESET_SKEPTIC; break; case EMOTION_FRUSTRATED: preset = &PRESET_FRUSTRATED; break; case EMOTION_UNIMPRESSED: preset = &PRESET_UNIMPRESSED; break; case EMOTION_SLEEPY: preset = &PRESET_SLEEPY; break; case EMOTION_SUSPICIOUS: preset = &PRESET_SUSPICIOUS; break; case EMOTION_SQUINT: preset = &PRESET_SQUINT; break; case EMOTION_FURIOUS: preset = &PRESET_FURIOUS; break; case EMOTION_SCARED: preset = &PRESET_SCARED; break; case EMOTION_AWE: preset = &PRESET_AWE; break; default: break; } eye_transition_to(&h->left_eye, preset, scale); eye_transition_to(&h->right_eye, preset, scale); h->current_emotion = emotion; } // Get random emotion based on weights static animated_eyes_emotion_t get_random_emotion(animated_eyes_handle_t h) { float sum = 0; for (int i = 0; i < EMOTION_COUNT; i++) sum += h->emotion_weights[i]; if (sum == 0) return EMOTION_NORMAL; float r = (float)(rand() % 1000) / 1000.0f * sum; float acc = 0; for (int i = 0; i < EMOTION_COUNT; i++) { if (h->emotion_weights[i] == 0) continue; acc += h->emotion_weights[i]; if (r <= acc) return (animated_eyes_emotion_t)i; } return EMOTION_NORMAL; } // Public API Implementation animated_eyes_handle_t animated_eyes_create(lv_obj_t *parent, const animated_eyes_config_t *config) { animated_eyes_handle_t h = calloc(1, sizeof(struct animated_eyes_t)); if (!h) return NULL; h->config = *config; h->center_x = config->screen_width / 2; h->center_y = config->screen_height / 2; // Create canvas // Use SPIRAM for large canvas buffer (internal RAM is likely too small for // full screen buffer) h->canvas_buf = heap_caps_malloc( config->screen_width * config->screen_height * sizeof(lv_color_t), MALLOC_CAP_SPIRAM); if (!h->canvas_buf) { ESP_LOGE(TAG, "Failed to allocate canvas buffer"); free(h); return NULL; } h->canvas = lv_canvas_create(parent); lv_canvas_set_buffer(h->canvas, h->canvas_buf, config->screen_width, config->screen_height, LV_IMG_CF_TRUE_COLOR); lv_obj_center(h->canvas); lv_canvas_fill_bg(h->canvas, config->bg_color, LV_OPA_COVER); // Initialize eyes eye_init(&h->left_eye, true); eye_init(&h->right_eye, false); float scale = (float)config->eye_size / 40.0f; h->left_eye.center_x = h->center_x - config->eye_size / 2 - config->eye_spacing; h->left_eye.center_y = h->center_y; h->right_eye.center_x = h->center_x + config->eye_size / 2 + config->eye_spacing; h->right_eye.center_y = h->center_y; eye_apply_preset(&h->left_eye, &PRESET_NORMAL, scale); eye_apply_preset(&h->right_eye, &PRESET_NORMAL, scale); // Initialize emotion weights for (int i = 0; i < EMOTION_COUNT; i++) h->emotion_weights[i] = 0; h->emotion_weights[EMOTION_NORMAL] = 1.0f; // Initialize timers timer_start(&h->behavior_timer, config->behavior_interval); timer_start(&h->look_timer, config->look_interval); timer_start(&h->blink_timer, config->blink_interval); ESP_LOGI(TAG, "Animated eyes created (%dx%d, eye_size=%d)", config->screen_width, config->screen_height, config->eye_size); return h; } void animated_eyes_delete(animated_eyes_handle_t h) { if (!h) return; if (h->canvas) lv_obj_del(h->canvas); if (h->canvas_buf) free(h->canvas_buf); free(h); } void animated_eyes_update(animated_eyes_handle_t h) { if (!h) return; if (!h) return; // Update temporary emotion if (h->is_temporary_emotion) { timer_update(&h->temporary_timer); if (h->temporary_timer.is_expired) { h->is_temporary_emotion = false; go_to_emotion(h, h->saved_emotion); } } // Decay inertia h->inertia_x *= 0.9f; // Slower decay for visibility h->inertia_y *= 0.9f; if (fabsf(h->inertia_x) < 1.0f) h->inertia_x = 0; if (fabsf(h->inertia_y) < 1.0f) h->inertia_y = 0; // Update behavior (only if not in temporary emotion) if (h->config.random_behavior && !h->is_temporary_emotion) { timer_update(&h->behavior_timer); if (h->behavior_timer.is_expired) { timer_reset(&h->behavior_timer); animated_eyes_emotion_t new_emotion = get_random_emotion(h); if (new_emotion != h->current_emotion) go_to_emotion(h, new_emotion); } } // Update look if (h->config.random_look) { timer_update(&h->look_timer); if (h->look_timer.is_expired) { timer_reset(&h->look_timer); float x = (float)(rand() % 100 - 50) / 100.0f; float y = (float)(rand() % 100 - 50) / 100.0f; animated_eyes_look_at(h, x, y); } } // Update blink if (h->config.random_blink) { timer_update(&h->blink_timer); if (h->blink_timer.is_expired) { animated_eyes_blink(h); // Randomize NEXT blink interval (3s to 8s for a more relaxed feel) uint32_t next_interval = 3000 + (rand() % 5000); h->blink_timer.interval = next_interval; timer_reset(&h->blink_timer); } } // Update eye animations eye_update(&h->left_eye); eye_update(&h->right_eye); // Redraw lv_canvas_fill_bg(h->canvas, h->config.bg_color, LV_OPA_COVER); // Apply inertia offset temporarily for drawing int16_t original_cx_l = h->left_eye.center_x; int16_t original_cy_l = h->left_eye.center_y; int16_t original_cx_r = h->right_eye.center_x; int16_t original_cy_r = h->right_eye.center_y; h->left_eye.center_x += (int16_t)h->inertia_x; h->left_eye.center_y += (int16_t)h->inertia_y; h->right_eye.center_x += (int16_t)h->inertia_x; h->right_eye.center_y += (int16_t)h->inertia_y; eye_draw(h->canvas, &h->left_eye, h->config.eye_color); eye_draw(h->canvas, &h->right_eye, h->config.eye_color); // Restore centers h->left_eye.center_x = original_cx_l; h->left_eye.center_y = original_cy_l; h->right_eye.center_x = original_cx_r; h->right_eye.center_y = original_cy_r; lv_obj_invalidate(h->canvas); } void animated_eyes_blink(animated_eyes_handle_t h) { if (!h) return; anim_restart(&h->left_eye.blink_anim); anim_restart(&h->right_eye.blink_anim); // Note: Timer reset and interval randomization is handled in update loop for // automatic blinks } void animated_eyes_set_emotion(animated_eyes_handle_t h, animated_eyes_emotion_t emotion) { if (!h || emotion >= EMOTION_COUNT) return; if (!h || emotion >= EMOTION_COUNT) return; // If we were in a temporary emotion, setting a new one manually cancels it h->is_temporary_emotion = false; go_to_emotion(h, emotion); } void animated_eyes_set_temporary_emotion(animated_eyes_handle_t h, animated_eyes_emotion_t emotion, uint32_t duration_ms) { if (!h || emotion >= EMOTION_COUNT) return; if (!h->is_temporary_emotion) { h->saved_emotion = h->current_emotion; h->is_temporary_emotion = true; } go_to_emotion(h, emotion); timer_start(&h->temporary_timer, duration_ms); ESP_LOGI(TAG, "Set temp emotion: %d for %d ms", emotion, duration_ms); } void animated_eyes_shake_inertia(animated_eyes_handle_t h, float intensity_x, float intensity_y) { if (!h) return; // Map intensity (G-force) to pixel offset // aggressive: 1G = 50 pixels displacement (X needs negative for inertia feel) h->inertia_x = intensity_x * -50.0f; h->inertia_y = intensity_y * 50.0f; } animated_eyes_emotion_t animated_eyes_get_emotion(animated_eyes_handle_t h) { return h ? h->current_emotion : EMOTION_NORMAL; } void animated_eyes_set_emotion_weight(animated_eyes_handle_t h, animated_eyes_emotion_t emotion, float weight) { if (!h || emotion >= EMOTION_COUNT) return; h->emotion_weights[emotion] = weight; } void animated_eyes_look_at(animated_eyes_handle_t h, float x, float y) { if (!h) return; float scale = (float)h->config.eye_size / 40.0f; int16_t move_x = (int16_t)(-25 * x * scale); int16_t move_y = (int16_t)(20 * y * scale); float scale_y = 1.0f - fabsf(y) * 0.4f; h->right_eye.transform_origin = h->right_eye.transform_current; h->right_eye.transform_dest.move_x = move_x; h->right_eye.transform_dest.move_y = move_y; h->right_eye.transform_dest.scale_x = 1.0f; h->right_eye.transform_dest.scale_y = scale_y * (1.0f - x * 0.2f); anim_restart(&h->right_eye.transform_anim); h->left_eye.transform_origin = h->left_eye.transform_current; h->left_eye.transform_dest.move_x = move_x; h->left_eye.transform_dest.move_y = move_y; h->left_eye.transform_dest.scale_x = 1.0f; h->left_eye.transform_dest.scale_y = scale_y * (1.0f + x * 0.2f); anim_restart(&h->left_eye.transform_anim); } void animated_eyes_look_at_point(animated_eyes_handle_t h, int16_t sx, int16_t sy) { if (!h) return; float x = (float)(sx - h->center_x) / (h->config.screen_width / 2); float y = (float)(h->center_y - sy) / (h->config.screen_height / 2); x = x < -1.0f ? -1.0f : (x > 1.0f ? 1.0f : x); y = y < -1.0f ? -1.0f : (y > 1.0f ? 1.0f : y); animated_eyes_look_at(h, x, y); } void animated_eyes_look_left(animated_eyes_handle_t h) { animated_eyes_look_at(h, 1.0f, 0); } void animated_eyes_look_right(animated_eyes_handle_t h) { animated_eyes_look_at(h, -1.0f, 0); } void animated_eyes_look_up(animated_eyes_handle_t h) { animated_eyes_look_at(h, 0, 1.0f); } void animated_eyes_look_down(animated_eyes_handle_t h) { animated_eyes_look_at(h, 0, -1.0f); } void animated_eyes_look_center(animated_eyes_handle_t h) { animated_eyes_look_at(h, 0, 0); } void animated_eyes_set_random(animated_eyes_handle_t h, bool behavior, bool look, bool blink) { if (!h) return; h->config.random_behavior = behavior; h->config.random_look = look; h->config.random_blink = blink; } void animated_eyes_set_color(animated_eyes_handle_t h, lv_color_t color) { if (h) h->config.eye_color = color; } void animated_eyes_set_bg_color(animated_eyes_handle_t h, lv_color_t color) { if (h) h->config.bg_color = color; }