Initial project import with ESP32 animated eyes

Add initial project files for ESP32 animated eyes, including main application sources, assets, CMake configuration, and supporting ESP-IDF components for LCD and touch functionality. Includes documentation, test apps, and configuration files.
This commit is contained in:
2026-01-18 21:32:07 +05:30
parent 03c8d43b7a
commit b71a39aae9
117 changed files with 14318 additions and 0 deletions

8
main/CMakeLists.txt Normal file
View File

@@ -0,0 +1,8 @@
idf_component_register(
SRCS "example_qspi_with_ram.c" "splash_screen.c" "animated_eyes.c" "animated_eyes_demo.c"
INCLUDE_DIRS ".")
set_source_files_properties(
${LV_DEMOS_SOURCES}
PROPERTIES COMPILE_OPTIONS
-DLV_LVGL_H_INCLUDE_SIMPLE)

603
main/animated_eyes.c Normal file
View File

@@ -0,0 +1,603 @@
/**
* @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_timer.h"
#include "esp_log.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "esp_heap_caps.h"
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;
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;
timer->is_expired = (get_millis() - timer->start_time) >= timer->interval;
}
// 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);
anim_set_trapezium(&eye->blink_anim, 0, 40, 100, 40, 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
static void draw_rect(lv_obj_t *canvas, int32_t x0, int32_t y0, int32_t x1, int32_t y1, lv_color_t color) {
int32_t l = MIN(x0, x1);
int32_t r = MAX(x0, x1);
int32_t t = MIN(y0, y1);
int32_t b = MAX(y0, y1);
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;
lv_canvas_draw_rect(canvas, l, t, r - l, b - t, &rect_dsc);
}
// 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;
// Update behavior
if (h->config.random_behavior) {
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);
}
}
// 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);
eye_draw(h->canvas, &h->left_eye, h->config.eye_color);
eye_draw(h->canvas, &h->right_eye, h->config.eye_color);
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);
timer_reset(&h->blink_timer);
}
void animated_eyes_set_emotion(animated_eyes_handle_t h, animated_eyes_emotion_t emotion) {
if (!h || emotion >= EMOTION_COUNT) return;
go_to_emotion(h, emotion);
}
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;
}

202
main/animated_eyes.h Normal file
View File

@@ -0,0 +1,202 @@
/**
* @file animated_eyes.h
* @brief Animated expressive eyes for LVGL displays
*
* Ported from esp32-eyes (https://github.com/playfultechnology/esp32-eyes)
* Original Copyright (c) 2020 Luis Llamas, 2023 Alastair Aitchison
* LVGL port for ESP-IDF
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License.
*/
#ifndef ANIMATED_EYES_H
#define ANIMATED_EYES_H
#include "lvgl.h"
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Available emotions for the animated face
*/
typedef enum {
EMOTION_NORMAL = 0,
EMOTION_ANGRY,
EMOTION_GLEE,
EMOTION_HAPPY,
EMOTION_SAD,
EMOTION_WORRIED,
EMOTION_FOCUSED,
EMOTION_ANNOYED,
EMOTION_SURPRISED,
EMOTION_SKEPTIC,
EMOTION_FRUSTRATED,
EMOTION_UNIMPRESSED,
EMOTION_SLEEPY,
EMOTION_SUSPICIOUS,
EMOTION_SQUINT,
EMOTION_FURIOUS,
EMOTION_SCARED,
EMOTION_AWE,
EMOTION_COUNT
} animated_eyes_emotion_t;
/**
* @brief Configuration for animated eyes
*/
typedef struct {
uint16_t screen_width; /**< Screen width in pixels */
uint16_t screen_height; /**< Screen height in pixels */
uint16_t eye_size; /**< Base eye size (will be scaled) */
uint16_t eye_spacing; /**< Distance between eyes */
lv_color_t eye_color; /**< Color of the eyes */
lv_color_t bg_color; /**< Background color */
bool random_behavior; /**< Enable random emotion changes */
bool random_look; /**< Enable random look-around */
bool random_blink; /**< Enable random blinking */
uint32_t behavior_interval; /**< Interval for behavior changes (ms) */
uint32_t look_interval; /**< Interval for look changes (ms) */
uint32_t blink_interval; /**< Interval for blinking (ms) */
} animated_eyes_config_t;
/**
* @brief Handle for animated eyes instance
*/
typedef struct animated_eyes_t* animated_eyes_handle_t;
/**
* @brief Default configuration macro
*/
#define ANIMATED_EYES_DEFAULT_CONFIG() { \
.screen_width = 368, \
.screen_height = 448, \
.eye_size = 120, \
.eye_spacing = 20, \
.eye_color = lv_color_white(), \
.bg_color = lv_color_black(), \
.random_behavior = true, \
.random_look = true, \
.random_blink = true, \
.behavior_interval = 5000, \
.look_interval = 4000, \
.blink_interval = 3500 \
}
/**
* @brief Create animated eyes on a parent LVGL object
*
* @param parent Parent LVGL object to attach eyes to
* @param config Configuration for the eyes
* @return Handle to the animated eyes instance, or NULL on failure
*/
animated_eyes_handle_t animated_eyes_create(lv_obj_t *parent, const animated_eyes_config_t *config);
/**
* @brief Delete animated eyes and free resources
*
* @param handle Handle to the animated eyes instance
*/
void animated_eyes_delete(animated_eyes_handle_t handle);
/**
* @brief Update the animated eyes (call this regularly, e.g., every frame)
*
* @param handle Handle to the animated eyes instance
*/
void animated_eyes_update(animated_eyes_handle_t handle);
/**
* @brief Trigger a blink animation
*
* @param handle Handle to the animated eyes instance
*/
void animated_eyes_blink(animated_eyes_handle_t handle);
/**
* @brief Set the current emotion
*
* @param handle Handle to the animated eyes instance
* @param emotion The emotion to set
*/
void animated_eyes_set_emotion(animated_eyes_handle_t handle, animated_eyes_emotion_t emotion);
/**
* @brief Get the current emotion
*
* @param handle Handle to the animated eyes instance
* @return Current emotion
*/
animated_eyes_emotion_t animated_eyes_get_emotion(animated_eyes_handle_t handle);
/**
* @brief Set the emotion probability weight
*
* @param handle Handle to the animated eyes instance
* @param emotion The emotion to set weight for
* @param weight Weight value (0.0 to disable, higher = more likely)
*/
void animated_eyes_set_emotion_weight(animated_eyes_handle_t handle, animated_eyes_emotion_t emotion, float weight);
/**
* @brief Make the eyes look in a specific direction
*
* @param handle Handle to the animated eyes instance
* @param x Horizontal direction (-1.0 = left, 0.0 = center, 1.0 = right)
* @param y Vertical direction (-1.0 = down, 0.0 = center, 1.0 = up)
*/
void animated_eyes_look_at(animated_eyes_handle_t handle, float x, float y);
/**
* @brief Make the eyes look at a screen position
*
* @param handle Handle to the animated eyes instance
* @param screen_x X coordinate on screen
* @param screen_y Y coordinate on screen
*/
void animated_eyes_look_at_point(animated_eyes_handle_t handle, int16_t screen_x, int16_t screen_y);
/**
* @brief Convenience methods for common look directions
*/
void animated_eyes_look_left(animated_eyes_handle_t handle);
void animated_eyes_look_right(animated_eyes_handle_t handle);
void animated_eyes_look_up(animated_eyes_handle_t handle);
void animated_eyes_look_down(animated_eyes_handle_t handle);
void animated_eyes_look_center(animated_eyes_handle_t handle);
/**
* @brief Enable or disable random behaviors
*
* @param handle Handle to the animated eyes instance
* @param behavior Enable random emotion changes
* @param look Enable random looking around
* @param blink Enable random blinking
*/
void animated_eyes_set_random(animated_eyes_handle_t handle, bool behavior, bool look, bool blink);
/**
* @brief Set the eye color
*
* @param handle Handle to the animated eyes instance
* @param color New eye color
*/
void animated_eyes_set_color(animated_eyes_handle_t handle, lv_color_t color);
/**
* @brief Set the background color
*
* @param handle Handle to the animated eyes instance
* @param color New background color
*/
void animated_eyes_set_bg_color(animated_eyes_handle_t handle, lv_color_t color);
#ifdef __cplusplus
}
#endif
#endif /* ANIMATED_EYES_H */

110
main/animated_eyes_demo.c Normal file
View File

@@ -0,0 +1,110 @@
/**
* @file animated_eyes_demo.c
* @brief Demo implementation for animated eyes
*/
#include "animated_eyes_demo.h"
#include "esp_log.h"
// FreeRTOS includes not needed for lv_timer
static const char *TAG = "eyes_demo";
static lv_timer_t *update_timer = NULL;
static lv_timer_t *emotion_timer = NULL;
static animated_eyes_handle_t eyes_handle = NULL;
static int current_emotion_idx = 0;
static const char *emotion_names[] = {
"NORMAL", "ANGRY", "GLEE", "HAPPY", "SAD", "WORRIED",
"FOCUSED", "ANNOYED", "SURPRISED", "SKEPTIC", "FRUSTRATED",
"UNIMPRESSED", "SLEEPY", "SUSPICIOUS", "SQUINT", "FURIOUS",
"SCARED", "AWE"
};
// Timer callback for updating eyes
static void update_timer_callback(lv_timer_t *timer) {
if (eyes_handle) {
animated_eyes_update(eyes_handle);
}
}
// Timer callback for cycling emotions
static void emotion_timer_callback(lv_timer_t *timer) {
if (eyes_handle) {
current_emotion_idx = (current_emotion_idx + 1) % EMOTION_COUNT;
animated_eyes_emotion_t next_emotion = (animated_eyes_emotion_t)current_emotion_idx;
ESP_LOGI(TAG, "Switching emotion to: %s", emotion_names[next_emotion]);
animated_eyes_set_emotion(eyes_handle, next_emotion);
}
}
animated_eyes_handle_t animated_eyes_demo_start(void) {
ESP_LOGI(TAG, "Starting animated eyes demo");
// Get the active screen
lv_obj_t *scr = lv_scr_act();
// Set black background
lv_obj_set_style_bg_color(scr, lv_color_black(), 0);
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0);
// Configure and create animated eyes
animated_eyes_config_t config = ANIMATED_EYES_DEFAULT_CONFIG();
// Customize for your screen (368x448)
config.screen_width = 368;
config.screen_height = 448;
config.eye_size = 120; // Larger eyes to fill the screen better
config.eye_spacing = 20; // Closer together
config.eye_color = lv_color_white();
config.bg_color = lv_color_black();
// Disable random movements for stability
config.random_behavior = false;
config.random_look = false;
config.random_blink = true; // Keep blinking for life
// Timing settings (unused when random features disabled, but kept for reference)
config.behavior_interval = 6000;
config.look_interval = 3000;
config.blink_interval = 4000;
eyes_handle = animated_eyes_create(scr, &config);
if (!eyes_handle) {
ESP_LOGE(TAG, "Failed to create animated eyes");
return NULL;
}
// Reset emotion index
current_emotion_idx = 0;
// Create timers
// 1. Update timer: 33ms (approx 30 FPS) for smooth animation
update_timer = lv_timer_create(update_timer_callback, 33, NULL);
// 2. Emotion cycle timer: 4000ms (4 seconds) to switch emotions
emotion_timer = lv_timer_create(emotion_timer_callback, 4000, NULL);
ESP_LOGI(TAG, "Animated eyes started successfully");
return eyes_handle;
}
void animated_eyes_demo_stop(animated_eyes_handle_t handle) {
if (update_timer) {
lv_timer_del(update_timer);
update_timer = NULL;
}
if (emotion_timer) {
lv_timer_del(emotion_timer);
emotion_timer = NULL;
}
if (handle) {
animated_eyes_delete(handle);
eyes_handle = NULL;
}
ESP_LOGI(TAG, "Animated eyes demo stopped");
}

36
main/animated_eyes_demo.h Normal file
View File

@@ -0,0 +1,36 @@
/**
* @file animated_eyes_demo.h
* @brief Demo functions for animated eyes
*/
#ifndef ANIMATED_EYES_DEMO_H
#define ANIMATED_EYES_DEMO_H
#include "animated_eyes.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Start the animated eyes demo
*
* Creates animated eyes on the current screen and starts
* the animation. Call animated_eyes_demo_stop() to clean up.
*
* @return Handle to the animated eyes, or NULL on failure
*/
animated_eyes_handle_t animated_eyes_demo_start(void);
/**
* @brief Stop the animated eyes demo and clean up
*
* @param handle Handle returned by animated_eyes_demo_start()
*/
void animated_eyes_demo_stop(animated_eyes_handle_t handle);
#ifdef __cplusplus
}
#endif
#endif /* ANIMATED_EYES_DEMO_H */

View File

@@ -0,0 +1,411 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/spi_master.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl.h"
#include "animated_eyes_demo.h"
#include "esp_lcd_sh8601.h"
#include "esp_lcd_touch_ft5x06.h"
#include "esp_io_expander_tca9554.h"
#include "splash_screen.h"
static const char *TAG = "example";
static SemaphoreHandle_t lvgl_mux = NULL;
#define LCD_HOST SPI2_HOST
#define TOUCH_HOST I2C_NUM_0
#if CONFIG_LV_COLOR_DEPTH == 32
#define LCD_BIT_PER_PIXEL (24)
#elif CONFIG_LV_COLOR_DEPTH == 16
#define LCD_BIT_PER_PIXEL (16)
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
#define EXAMPLE_PIN_NUM_LCD_CS (GPIO_NUM_12)
#define EXAMPLE_PIN_NUM_LCD_PCLK (GPIO_NUM_11)
#define EXAMPLE_PIN_NUM_LCD_DATA0 (GPIO_NUM_4)
#define EXAMPLE_PIN_NUM_LCD_DATA1 (GPIO_NUM_5)
#define EXAMPLE_PIN_NUM_LCD_DATA2 (GPIO_NUM_6)
#define EXAMPLE_PIN_NUM_LCD_DATA3 (GPIO_NUM_7)
#define EXAMPLE_PIN_NUM_LCD_RST (-1)
#define EXAMPLE_PIN_NUM_BK_LIGHT (-1)
// The pixel number in horizontal and vertical
#define EXAMPLE_LCD_H_RES 368
#define EXAMPLE_LCD_V_RES 448
#define EXAMPLE_USE_TOUCH 1
#if EXAMPLE_USE_TOUCH
#define EXAMPLE_PIN_NUM_TOUCH_SCL (GPIO_NUM_14)
#define EXAMPLE_PIN_NUM_TOUCH_SDA (GPIO_NUM_15)
#define EXAMPLE_PIN_NUM_TOUCH_RST (-1)
#define EXAMPLE_PIN_NUM_TOUCH_INT (GPIO_NUM_21)
esp_lcd_touch_handle_t tp = NULL;
#endif
#define EXAMPLE_LVGL_BUF_HEIGHT (EXAMPLE_LCD_V_RES / 4)
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
#define EXAMPLE_LVGL_TASK_MAX_DELAY_MS 500
#define EXAMPLE_LVGL_TASK_MIN_DELAY_MS 1
#define EXAMPLE_LVGL_TASK_STACK_SIZE (4 * 1024)
#define EXAMPLE_LVGL_TASK_PRIORITY 2
static const sh8601_lcd_init_cmd_t lcd_init_cmds[] = {
{0x11, (uint8_t[]){0x00}, 0, 120},
{0x44, (uint8_t[]){0x01, 0xD1}, 2, 0},
{0x35, (uint8_t[]){0x00}, 1, 0},
{0x53, (uint8_t[]){0x20}, 1, 10},
{0x2A, (uint8_t[]){0x00, 0x00, 0x01, 0x6F}, 4, 0},
{0x2B, (uint8_t[]){0x00, 0x00, 0x01, 0xBF}, 4, 0},
{0x51, (uint8_t[]){0x00}, 1, 10},
{0x29, (uint8_t[]){0x00}, 0, 10},
{0x51, (uint8_t[]){0xFF}, 1, 0},
};
static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
lv_disp_flush_ready(disp_driver);
return false;
}
static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
const int offsetx1 = area->x1;
const int offsetx2 = area->x2;
const int offsety1 = area->y1;
const int offsety2 = area->y2;
#if LCD_BIT_PER_PIXEL == 24
uint8_t *to = (uint8_t *)color_map;
uint8_t temp = 0;
uint16_t pixel_num = (offsetx2 - offsetx1 + 1) * (offsety2 - offsety1 + 1);
// Special dealing for first pixel
temp = color_map[0].ch.blue;
*to++ = color_map[0].ch.red;
*to++ = color_map[0].ch.green;
*to++ = temp;
// Normal dealing for other pixels
for (int i = 1; i < pixel_num; i++)
{
*to++ = color_map[i].ch.red;
*to++ = color_map[i].ch.green;
*to++ = color_map[i].ch.blue;
}
#endif
// copy a buffer's content to a specific area of the display
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
}
/* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */
static void example_lvgl_update_cb(lv_disp_drv_t *drv)
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
switch (drv->rotated)
{
case LV_DISP_ROT_NONE:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, true, false);
break;
case LV_DISP_ROT_90:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, true, true);
break;
case LV_DISP_ROT_180:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, false, true);
break;
case LV_DISP_ROT_270:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, false, false);
break;
}
}
void example_lvgl_rounder_cb(struct _lv_disp_drv_t *disp_drv, lv_area_t *area)
{
uint16_t x1 = area->x1;
uint16_t x2 = area->x2;
uint16_t y1 = area->y1;
uint16_t y2 = area->y2;
// round the start of coordinate down to the nearest 2M number
area->x1 = (x1 >> 1) << 1;
area->y1 = (y1 >> 1) << 1;
// round the end of coordinate up to the nearest 2N+1 number
area->x2 = ((x2 >> 1) << 1) + 1;
area->y2 = ((y2 >> 1) << 1) + 1;
}
#if EXAMPLE_USE_TOUCH
static void example_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
esp_lcd_touch_handle_t tp = (esp_lcd_touch_handle_t)drv->user_data;
assert(tp);
uint16_t tp_x;
uint16_t tp_y;
uint8_t tp_cnt = 0;
/* Read data from touch controller into memory */
esp_lcd_touch_read_data(tp);
/* Read data from touch controller */
bool tp_pressed = esp_lcd_touch_get_coordinates(tp, &tp_x, &tp_y, NULL, &tp_cnt, 1);
if (tp_pressed && tp_cnt > 0)
{
data->point.x = tp_x;
data->point.y = tp_y;
data->state = LV_INDEV_STATE_PRESSED;
ESP_LOGD(TAG, "Touch position: %d,%d", tp_x, tp_y);
}
else
{
data->state = LV_INDEV_STATE_RELEASED;
}
}
#endif
static void example_increase_lvgl_tick(void *arg)
{
/* Tell LVGL how many milliseconds has elapsed */
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static bool example_lvgl_lock(int timeout_ms)
{
assert(lvgl_mux && "bsp_display_start must be called first");
const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTake(lvgl_mux, timeout_ticks) == pdTRUE;
}
static void example_lvgl_unlock(void)
{
assert(lvgl_mux && "bsp_display_start must be called first");
xSemaphoreGive(lvgl_mux);
}
static void example_lvgl_port_task(void *arg)
{
ESP_LOGI(TAG, "Starting LVGL task");
uint32_t task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
while (1)
{
// Lock the mutex due to the LVGL APIs are not thread-safe
if (example_lvgl_lock(-1))
{
task_delay_ms = lv_timer_handler();
// Release the mutex
example_lvgl_unlock();
}
if (task_delay_ms > EXAMPLE_LVGL_TASK_MAX_DELAY_MS)
{
task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
}
else if (task_delay_ms < EXAMPLE_LVGL_TASK_MIN_DELAY_MS)
{
task_delay_ms = EXAMPLE_LVGL_TASK_MIN_DELAY_MS;
}
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
}
void app_main(void)
{
esp_log_level_set("lcd_panel.io.i2c", ESP_LOG_NONE);
esp_log_level_set("FT5x06", ESP_LOG_NONE);
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
static lv_disp_drv_t disp_drv; // contains callback functions
ESP_LOGI(TAG, "Initialize I2C bus");
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = EXAMPLE_PIN_NUM_TOUCH_SDA,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = EXAMPLE_PIN_NUM_TOUCH_SCL,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 200 * 1000,
};
ESP_ERROR_CHECK(i2c_param_config(TOUCH_HOST, &i2c_conf));
ESP_ERROR_CHECK(i2c_driver_install(TOUCH_HOST, i2c_conf.mode, 0, 0, 0));
esp_io_expander_handle_t io_expander = NULL;
esp_io_expander_new_i2c_tca9554(TOUCH_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander);
ESP_ERROR_CHECK(esp_io_expander_new_i2c_tca9554(TOUCH_HOST, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander));
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_2, IO_EXPANDER_OUTPUT);
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0, 0);
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_1, 0);
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_2, 0);
vTaskDelay(pdMS_TO_TICKS(200));
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0, 1);
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_1, 1);
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_2, 1);
#if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
ESP_LOGI(TAG, "Turn off LCD backlight");
gpio_config_t bk_gpio_config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT};
ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
#endif
ESP_LOGI(TAG, "Initialize SPI bus");
const spi_bus_config_t buscfg = SH8601_PANEL_BUS_QSPI_CONFIG(EXAMPLE_PIN_NUM_LCD_PCLK,
EXAMPLE_PIN_NUM_LCD_DATA0,
EXAMPLE_PIN_NUM_LCD_DATA1,
EXAMPLE_PIN_NUM_LCD_DATA2,
EXAMPLE_PIN_NUM_LCD_DATA3,
EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES * LCD_BIT_PER_PIXEL / 8);
ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
const esp_lcd_panel_io_spi_config_t io_config = SH8601_PANEL_IO_QSPI_CONFIG(EXAMPLE_PIN_NUM_LCD_CS,
example_notify_lvgl_flush_ready,
&disp_drv);
sh8601_vendor_config_t vendor_config = {
.init_cmds = lcd_init_cmds,
.init_cmds_size = sizeof(lcd_init_cmds) / sizeof(lcd_init_cmds[0]),
.flags = {
.use_qspi_interface = 1,
},
};
// Attach the LCD to the SPI bus
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));
esp_lcd_panel_handle_t panel_handle = NULL;
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
.bits_per_pixel = LCD_BIT_PER_PIXEL,
.vendor_config = &vendor_config,
};
ESP_LOGI(TAG, "Install SH8601 panel driver");
ESP_ERROR_CHECK(esp_lcd_new_panel_sh8601(io_handle, &panel_config, &panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
// user can flush pre-defined pattern to the screen before we turn on the screen or backlight
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
#if EXAMPLE_USE_TOUCH
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
const esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
// Attach the TOUCH to the I2C bus
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TOUCH_HOST, &tp_io_config, &tp_io_handle));
const esp_lcd_touch_config_t tp_cfg = {
.x_max = EXAMPLE_LCD_H_RES,
.y_max = EXAMPLE_LCD_V_RES,
.rst_gpio_num = EXAMPLE_PIN_NUM_TOUCH_RST,
.int_gpio_num = EXAMPLE_PIN_NUM_TOUCH_INT,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
};
ESP_LOGI(TAG, "Initialize touch controller");
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
#endif
#if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
ESP_LOGI(TAG, "Turn on LCD backlight");
gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
#endif
ESP_LOGI(TAG, "Initialize LVGL library");
lv_init();
// alloc draw buffers used by LVGL
// it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * EXAMPLE_LVGL_BUF_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf1);
lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * EXAMPLE_LVGL_BUF_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf2);
// initialize LVGL draw buffers
lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * EXAMPLE_LVGL_BUF_HEIGHT);
ESP_LOGI(TAG, "Register display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = EXAMPLE_LCD_H_RES;
disp_drv.ver_res = EXAMPLE_LCD_V_RES;
disp_drv.flush_cb = example_lvgl_flush_cb;
disp_drv.rounder_cb = example_lvgl_rounder_cb;
disp_drv.drv_update_cb = example_lvgl_update_cb;
disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_handle;
lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
ESP_LOGI(TAG, "Install LVGL tick timer");
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &example_increase_lvgl_tick,
.name = "lvgl_tick"};
esp_timer_handle_t lvgl_tick_timer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
#if EXAMPLE_USE_TOUCH
static lv_indev_drv_t indev_drv; // Input device driver (Touch)
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.disp = disp;
indev_drv.read_cb = example_lvgl_touch_cb;
indev_drv.user_data = tp;
lv_indev_drv_register(&indev_drv);
#endif
lvgl_mux = xSemaphoreCreateMutex();
assert(lvgl_mux);
xTaskCreate(example_lvgl_port_task, "LVGL", EXAMPLE_LVGL_TASK_STACK_SIZE, NULL, EXAMPLE_LVGL_TASK_PRIORITY, NULL);
// Lock the mutex due to the LVGL APIs are not thread-safe
if (example_lvgl_lock(-1))
{
// Show splash screen first
ESP_LOGI(TAG, "Showing splash screen");
splash_screen_show();
// Start animated eyes demo
ESP_LOGI(TAG, "Starting animated eyes");
animated_eyes_demo_start();
// Release the mutex
example_lvgl_unlock();
}
}

18
main/idf_component.yml Normal file
View File

@@ -0,0 +1,18 @@
version: "1.0.0"
dependencies:
espressif/esp_io_expander_tca9554: "^1.0.1"
idf: ">5.0.4,!=5.1.1"
esp_lcd_touch_ft5x06:
version: "*"
public: true
esp_lcd_sh8601:
path: "../components/esp_lcd_sh8601"
version: "*"
public: true
lvgl/lvgl:
version: "8.4.*"
public: true

55
main/splash_screen.c Normal file
View File

@@ -0,0 +1,55 @@
/**
* @file splash_screen.c
* @brief Splash screen implementation for LVGL
*/
#include "splash_screen.h"
#include "lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
// Include the logo image data
#include "../assets/logoIro.c"
static const char *TAG = "splash_screen";
// Splash screen display duration in milliseconds
#define SPLASH_SCREEN_DURATION_MS 2000
void splash_screen_show(void)
{
ESP_LOGI(TAG, "Showing splash screen");
// Create a new screen for the splash
lv_obj_t *splash_screen = lv_obj_create(NULL);
// Set background color and opacity using LVGL v8 style API (selector LV_PART_MAIN|LV_STATE_DEFAULT)
lv_obj_set_style_bg_color(splash_screen, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_opa(splash_screen, LV_OPA_COVER, LV_PART_MAIN | LV_STATE_DEFAULT);
// Removed redundant old style call (LVGL v7 API)
// Create the logo image
lv_obj_t *logo_img = lv_img_create(splash_screen);
lv_img_set_src(logo_img, &logoIro);
lv_obj_center(logo_img);
// Load the splash screen
lv_scr_load(splash_screen);
// Force a refresh to ensure the splash screen is displayed
lv_refr_now(NULL);
ESP_LOGI(TAG, "Splash screen displayed, waiting %d ms", SPLASH_SCREEN_DURATION_MS);
// Wait for the splash screen duration
vTaskDelay(pdMS_TO_TICKS(SPLASH_SCREEN_DURATION_MS));
ESP_LOGI(TAG, "Splash screen done");
// Clean up: Delete the logo image object before returning
// This ensures LVGL's internal state is clean before lv_demo_music() runs
lv_obj_del(logo_img);
// Note: We delete the image but keep the screen active.
// The next screen (lv_demo_music) will replace it automatically.
}

28
main/splash_screen.h Normal file
View File

@@ -0,0 +1,28 @@
/**
* @file splash_screen.h
* @brief Splash screen module for displaying logo on startup
*/
#ifndef SPLASH_SCREEN_H
#define SPLASH_SCREEN_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Display the splash screen with the logo image
*
* Creates a full-screen LVGL display showing the logo image,
* waits for the configured duration, then cleans up.
* This function blocks for the splash screen duration.
*
* @note Must be called after LVGL is initialized and the display driver is registered
*/
void splash_screen_show(void);
#ifdef __cplusplus
}
#endif
#endif /* SPLASH_SCREEN_H */