Remove GIF decoder and add swipe gesture support
Deleted GIF decoder files (app_gifdec.c/h) and blink emotion asset. Added swipe_gesture.c/h for new gesture handling. Updated CMakeLists and emotion_manager to reflect these changes. Added Geist font asset.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "main.c" "emotion_manager.c" "app_gifdec.c"
|
||||
SRCS "swipe_gesture.c" "main.c" "emotion_manager.c" "../assets/fonts/Geist.c"
|
||||
INCLUDE_DIRS "." ".."
|
||||
PRIV_REQUIRES esp_lcd lvgl esp_adc espressif__esp_io_expander espressif__esp_io_expander_tca9554 espressif__esp_lcd_touch espressif__esp_lcd_touch_ft5x06 esp_lcd_sh8601
|
||||
)
|
||||
|
||||
@@ -1,501 +0,0 @@
|
||||
#include "app_gifdec.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||
|
||||
typedef struct Entry {
|
||||
uint16_t length;
|
||||
uint16_t prefix;
|
||||
uint8_t suffix;
|
||||
} Entry;
|
||||
|
||||
typedef struct Table {
|
||||
int bulk;
|
||||
int nentries;
|
||||
Entry *entries;
|
||||
} Table;
|
||||
|
||||
/* Memory buffer read helpers */
|
||||
static size_t mem_read(app_gd_GIF *gif, void *buf, size_t count) {
|
||||
size_t avail = gif->data_size - gif->pos;
|
||||
size_t to_read = (count < avail) ? count : avail;
|
||||
memcpy(buf, gif->data + gif->pos, to_read);
|
||||
gif->pos += to_read;
|
||||
return to_read;
|
||||
}
|
||||
|
||||
static void mem_seek(app_gd_GIF *gif, size_t offset) { gif->pos = offset; }
|
||||
|
||||
static void mem_skip(app_gd_GIF *gif, size_t count) {
|
||||
gif->pos += count;
|
||||
if (gif->pos > gif->data_size)
|
||||
gif->pos = gif->data_size;
|
||||
}
|
||||
|
||||
static uint16_t read_num(app_gd_GIF *gif) {
|
||||
uint8_t bytes[2];
|
||||
mem_read(gif, bytes, 2);
|
||||
return bytes[0] + (((uint16_t)bytes[1]) << 8);
|
||||
}
|
||||
|
||||
app_gd_GIF *app_gd_open_gif_mem(const uint8_t *data, size_t size) {
|
||||
uint8_t sigver[3];
|
||||
uint16_t width, height, depth;
|
||||
uint8_t fdsz, bgidx, aspect;
|
||||
int i;
|
||||
uint8_t *bgcolor;
|
||||
int gct_sz;
|
||||
app_gd_GIF *gif;
|
||||
|
||||
/* Create gd_GIF Structure first to use mem_read */
|
||||
gif = calloc(1, sizeof(*gif));
|
||||
if (!gif)
|
||||
return NULL;
|
||||
|
||||
gif->data = data;
|
||||
gif->data_size = size;
|
||||
gif->pos = 0;
|
||||
|
||||
/* Header */
|
||||
mem_read(gif, sigver, 3);
|
||||
if (memcmp(sigver, "GIF", 3) != 0) {
|
||||
fprintf(stderr, "invalid signature\n");
|
||||
goto fail;
|
||||
}
|
||||
/* Version */
|
||||
mem_read(gif, sigver, 3);
|
||||
if (memcmp(sigver, "89a", 3) != 0) {
|
||||
fprintf(stderr, "invalid version\n");
|
||||
goto fail;
|
||||
}
|
||||
/* Width x Height */
|
||||
width = read_num(gif);
|
||||
height = read_num(gif);
|
||||
/* FDSZ */
|
||||
mem_read(gif, &fdsz, 1);
|
||||
/* Presence of GCT */
|
||||
if (!(fdsz & 0x80)) {
|
||||
fprintf(stderr, "no global color table\n");
|
||||
goto fail;
|
||||
}
|
||||
/* Color Space's Depth */
|
||||
depth = ((fdsz >> 4) & 7) + 1;
|
||||
/* Ignore Sort Flag. */
|
||||
/* GCT Size */
|
||||
gct_sz = 1 << ((fdsz & 0x07) + 1);
|
||||
/* Background Color Index */
|
||||
mem_read(gif, &bgidx, 1);
|
||||
/* Aspect Ratio */
|
||||
mem_read(gif, &aspect, 1);
|
||||
|
||||
gif->width = width;
|
||||
gif->height = height;
|
||||
gif->depth = depth;
|
||||
/* Read GCT */
|
||||
gif->gct.size = gct_sz;
|
||||
mem_read(gif, gif->gct.colors, 3 * gif->gct.size);
|
||||
gif->palette = &gif->gct;
|
||||
gif->bgindex = bgidx;
|
||||
gif->frame = calloc(4, width * height);
|
||||
if (!gif->frame) {
|
||||
goto fail;
|
||||
}
|
||||
gif->canvas = &gif->frame[width * height];
|
||||
if (gif->bgindex)
|
||||
memset(gif->frame, gif->bgindex, gif->width * gif->height);
|
||||
bgcolor = &gif->palette->colors[gif->bgindex * 3];
|
||||
if (bgcolor[0] || bgcolor[1] || bgcolor[2])
|
||||
for (i = 0; i < gif->width * gif->height; i++)
|
||||
memcpy(&gif->canvas[i * 3], bgcolor, 3);
|
||||
gif->anim_start = gif->pos;
|
||||
return gif;
|
||||
|
||||
fail:
|
||||
free(gif);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void discard_sub_blocks(app_gd_GIF *gif) {
|
||||
uint8_t size;
|
||||
|
||||
do {
|
||||
mem_read(gif, &size, 1);
|
||||
mem_skip(gif, size);
|
||||
} while (size);
|
||||
}
|
||||
|
||||
static void read_plain_text_ext(app_gd_GIF *gif) {
|
||||
/* Discard plain text metadata. */
|
||||
mem_skip(gif, 13);
|
||||
/* Discard plain text sub-blocks. */
|
||||
discard_sub_blocks(gif);
|
||||
}
|
||||
|
||||
static void read_graphic_control_ext(app_gd_GIF *gif) {
|
||||
uint8_t rdit;
|
||||
|
||||
/* Discard block size (always 0x04). */
|
||||
mem_skip(gif, 1);
|
||||
mem_read(gif, &rdit, 1);
|
||||
gif->gce.disposal = (rdit >> 2) & 3;
|
||||
gif->gce.input = rdit & 2;
|
||||
gif->gce.transparency = rdit & 1;
|
||||
gif->gce.delay = read_num(gif);
|
||||
mem_read(gif, &gif->gce.tindex, 1);
|
||||
/* Skip block terminator. */
|
||||
mem_skip(gif, 1);
|
||||
}
|
||||
|
||||
static void read_comment_ext(app_gd_GIF *gif) {
|
||||
/* Discard comment sub-blocks. */
|
||||
discard_sub_blocks(gif);
|
||||
}
|
||||
|
||||
static void read_application_ext(app_gd_GIF *gif) {
|
||||
char app_id[8];
|
||||
char app_auth_code[3];
|
||||
|
||||
/* Discard block size (always 0x0B). */
|
||||
mem_skip(gif, 1);
|
||||
/* Application Identifier. */
|
||||
mem_read(gif, app_id, 8);
|
||||
/* Application Authentication Code. */
|
||||
mem_read(gif, app_auth_code, 3);
|
||||
if (!strncmp(app_id, "NETSCAPE", sizeof(app_id))) {
|
||||
/* Discard block size (0x03) and constant byte (0x01). */
|
||||
mem_skip(gif, 2);
|
||||
gif->loop_count = read_num(gif);
|
||||
/* Skip block terminator. */
|
||||
mem_skip(gif, 1);
|
||||
} else {
|
||||
discard_sub_blocks(gif);
|
||||
}
|
||||
}
|
||||
|
||||
static void read_ext(app_gd_GIF *gif) {
|
||||
uint8_t label;
|
||||
|
||||
mem_read(gif, &label, 1);
|
||||
switch (label) {
|
||||
case 0x01:
|
||||
read_plain_text_ext(gif);
|
||||
break;
|
||||
case 0xF9:
|
||||
read_graphic_control_ext(gif);
|
||||
break;
|
||||
case 0xFE:
|
||||
read_comment_ext(gif);
|
||||
break;
|
||||
case 0xFF:
|
||||
read_application_ext(gif);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "unknown extension: %02X\n", label);
|
||||
}
|
||||
}
|
||||
|
||||
static Table *new_table(int key_size) {
|
||||
int key;
|
||||
int init_bulk = MAX(1 << (key_size + 1), 0x100);
|
||||
Table *table = malloc(sizeof(*table) + sizeof(Entry) * init_bulk);
|
||||
if (table) {
|
||||
table->bulk = init_bulk;
|
||||
table->nentries = (1 << key_size) + 2;
|
||||
table->entries = (Entry *)&table[1];
|
||||
for (key = 0; key < (1 << key_size); key++)
|
||||
table->entries[key] = (Entry){1, 0xFFF, key};
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
/* Add table entry. Return value:
|
||||
* 0 on success
|
||||
* +1 if key size must be incremented after this addition
|
||||
* -1 if could not realloc table */
|
||||
static int add_entry(Table **tablep, uint16_t length, uint16_t prefix,
|
||||
uint8_t suffix) {
|
||||
Table *table = *tablep;
|
||||
if (table->nentries == table->bulk) {
|
||||
table->bulk *= 2;
|
||||
table = realloc(table, sizeof(*table) + sizeof(Entry) * table->bulk);
|
||||
if (!table)
|
||||
return -1;
|
||||
table->entries = (Entry *)&table[1];
|
||||
*tablep = table;
|
||||
}
|
||||
table->entries[table->nentries] = (Entry){length, prefix, suffix};
|
||||
table->nentries++;
|
||||
if ((table->nentries & (table->nentries - 1)) == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint16_t get_key(app_gd_GIF *gif, int key_size, uint8_t *sub_len,
|
||||
uint8_t *shift, uint8_t *byte) {
|
||||
int bits_read;
|
||||
int rpad;
|
||||
int frag_size;
|
||||
uint16_t key;
|
||||
|
||||
key = 0;
|
||||
for (bits_read = 0; bits_read < key_size; bits_read += frag_size) {
|
||||
rpad = (*shift + bits_read) % 8;
|
||||
if (rpad == 0) {
|
||||
/* Update byte. */
|
||||
if (*sub_len == 0) {
|
||||
mem_read(gif, sub_len, 1); /* Must be nonzero! */
|
||||
if (*sub_len == 0)
|
||||
return 0x1000;
|
||||
}
|
||||
mem_read(gif, byte, 1);
|
||||
(*sub_len)--;
|
||||
}
|
||||
frag_size = MIN(key_size - bits_read, 8 - rpad);
|
||||
key |= ((uint16_t)((*byte) >> rpad)) << bits_read;
|
||||
}
|
||||
/* Clear extra bits to the left. */
|
||||
key &= (1 << key_size) - 1;
|
||||
*shift = (*shift + key_size) % 8;
|
||||
return key;
|
||||
}
|
||||
|
||||
/* Compute output index of y-th input line, in frame of height h. */
|
||||
static int interlaced_line_index(int h, int y) {
|
||||
int p; /* number of lines in current pass */
|
||||
|
||||
p = (h - 1) / 8 + 1;
|
||||
if (y < p) /* pass 1 */
|
||||
return y * 8;
|
||||
y -= p;
|
||||
p = (h - 5) / 8 + 1;
|
||||
if (y < p) /* pass 2 */
|
||||
return y * 8 + 4;
|
||||
y -= p;
|
||||
p = (h - 3) / 4 + 1;
|
||||
if (y < p) /* pass 3 */
|
||||
return y * 4 + 2;
|
||||
y -= p;
|
||||
/* pass 4 */
|
||||
return y * 2 + 1;
|
||||
}
|
||||
|
||||
/* Decompress image pixels.
|
||||
* Return 0 on success or -1 on out-of-memory (w.r.t. LZW code table). */
|
||||
static int read_image_data(app_gd_GIF *gif, int interlace) {
|
||||
uint8_t sub_len, shift, byte;
|
||||
int init_key_size, key_size, table_is_full = 0;
|
||||
int frm_off, frm_size, str_len = 0, i, p, x, y;
|
||||
uint16_t key, clear, stop;
|
||||
int ret;
|
||||
Table *table;
|
||||
Entry entry = {0};
|
||||
size_t start, end;
|
||||
|
||||
mem_read(gif, &byte, 1);
|
||||
key_size = (int)byte;
|
||||
if (key_size < 2 || key_size > 8)
|
||||
return -1;
|
||||
|
||||
start = gif->pos;
|
||||
discard_sub_blocks(gif);
|
||||
end = gif->pos;
|
||||
mem_seek(gif, start);
|
||||
clear = 1 << key_size;
|
||||
stop = clear + 1;
|
||||
table = new_table(key_size);
|
||||
key_size++;
|
||||
init_key_size = key_size;
|
||||
sub_len = shift = 0;
|
||||
key = get_key(gif, key_size, &sub_len, &shift, &byte); /* clear code */
|
||||
frm_off = 0;
|
||||
ret = 0;
|
||||
frm_size = gif->fw * gif->fh;
|
||||
while (frm_off < frm_size) {
|
||||
if (key == clear) {
|
||||
key_size = init_key_size;
|
||||
table->nentries = (1 << (key_size - 1)) + 2;
|
||||
table_is_full = 0;
|
||||
} else if (!table_is_full) {
|
||||
ret = add_entry(&table, str_len + 1, key, entry.suffix);
|
||||
if (ret == -1) {
|
||||
free(table);
|
||||
return -1;
|
||||
}
|
||||
if (table->nentries == 0x1000) {
|
||||
ret = 0;
|
||||
table_is_full = 1;
|
||||
}
|
||||
}
|
||||
key = get_key(gif, key_size, &sub_len, &shift, &byte);
|
||||
if (key == clear)
|
||||
continue;
|
||||
if (key == stop || key == 0x1000)
|
||||
break;
|
||||
if (ret == 1)
|
||||
key_size++;
|
||||
entry = table->entries[key];
|
||||
str_len = entry.length;
|
||||
for (i = 0; i < str_len; i++) {
|
||||
p = frm_off + entry.length - 1;
|
||||
x = p % gif->fw;
|
||||
y = p / gif->fw;
|
||||
if (interlace)
|
||||
y = interlaced_line_index((int)gif->fh, y);
|
||||
gif->frame[(gif->fy + y) * gif->width + gif->fx + x] = entry.suffix;
|
||||
if (entry.prefix == 0xFFF)
|
||||
break;
|
||||
else
|
||||
entry = table->entries[entry.prefix];
|
||||
}
|
||||
frm_off += str_len;
|
||||
if (key < table->nentries - 1 && !table_is_full)
|
||||
table->entries[table->nentries - 1].suffix = entry.suffix;
|
||||
}
|
||||
free(table);
|
||||
if (key == stop)
|
||||
mem_read(gif, &sub_len, 1); /* Must be zero! */
|
||||
mem_seek(gif, end);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read image.
|
||||
* Return 0 on success or -1 on out-of-memory (w.r.t. LZW code table). */
|
||||
static int read_image(app_gd_GIF *gif) {
|
||||
uint8_t fisrz;
|
||||
int interlace;
|
||||
|
||||
/* Image Descriptor. */
|
||||
gif->fx = read_num(gif);
|
||||
gif->fy = read_num(gif);
|
||||
|
||||
if (gif->fx >= gif->width || gif->fy >= gif->height)
|
||||
return -1;
|
||||
|
||||
gif->fw = read_num(gif);
|
||||
gif->fh = read_num(gif);
|
||||
|
||||
gif->fw = MIN(gif->fw, gif->width - gif->fx);
|
||||
gif->fh = MIN(gif->fh, gif->height - gif->fy);
|
||||
|
||||
mem_read(gif, &fisrz, 1);
|
||||
interlace = fisrz & 0x40;
|
||||
/* Ignore Sort Flag. */
|
||||
/* Local Color Table? */
|
||||
if (fisrz & 0x80) {
|
||||
/* Read LCT */
|
||||
gif->lct.size = 1 << ((fisrz & 0x07) + 1);
|
||||
mem_read(gif, gif->lct.colors, 3 * gif->lct.size);
|
||||
gif->palette = &gif->lct;
|
||||
} else
|
||||
gif->palette = &gif->gct;
|
||||
/* Image Data. */
|
||||
return read_image_data(gif, interlace);
|
||||
}
|
||||
|
||||
static void render_frame_rect(app_gd_GIF *gif, uint8_t *buffer) {
|
||||
int i, j, k;
|
||||
uint8_t index, *color;
|
||||
i = gif->fy * gif->width + gif->fx;
|
||||
for (j = 0; j < gif->fh; j++) {
|
||||
for (k = 0; k < gif->fw; k++) {
|
||||
index = gif->frame[(gif->fy + j) * gif->width + gif->fx + k];
|
||||
color = &gif->palette->colors[index * 3];
|
||||
if (!gif->gce.transparency || index != gif->gce.tindex)
|
||||
memcpy(&buffer[(i + k) * 3], color, 3);
|
||||
}
|
||||
i += gif->width;
|
||||
}
|
||||
}
|
||||
|
||||
static void dispose(app_gd_GIF *gif) {
|
||||
int i, j, k;
|
||||
uint8_t *bgcolor;
|
||||
switch (gif->gce.disposal) {
|
||||
case 2: /* Restore to background color. */
|
||||
bgcolor = &gif->palette->colors[gif->bgindex * 3];
|
||||
i = gif->fy * gif->width + gif->fx;
|
||||
for (j = 0; j < gif->fh; j++) {
|
||||
for (k = 0; k < gif->fw; k++)
|
||||
memcpy(&gif->canvas[(i + k) * 3], bgcolor, 3);
|
||||
i += gif->width;
|
||||
}
|
||||
break;
|
||||
case 3: /* Restore to previous, i.e., don't update canvas.*/
|
||||
break;
|
||||
default:
|
||||
/* Add frame non-transparent pixels to canvas. */
|
||||
render_frame_rect(gif, gif->canvas);
|
||||
}
|
||||
}
|
||||
|
||||
/* Return 1 if got a frame; 0 if got GIF trailer; -1 if error. */
|
||||
int app_gd_get_frame(app_gd_GIF *gif) {
|
||||
char sep;
|
||||
|
||||
dispose(gif);
|
||||
mem_read(gif, &sep, 1);
|
||||
while (sep != ',') {
|
||||
if (sep == ';')
|
||||
return 0;
|
||||
if (sep == '!')
|
||||
read_ext(gif);
|
||||
else
|
||||
return -1;
|
||||
mem_read(gif, &sep, 1);
|
||||
}
|
||||
if (read_image(gif) == -1)
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void app_gd_render_frame(app_gd_GIF *gif, uint8_t *buffer) {
|
||||
memcpy(buffer, gif->canvas, gif->width * gif->height * 3);
|
||||
render_frame_rect(gif, buffer);
|
||||
}
|
||||
|
||||
void app_gd_render_frame_to_argb(app_gd_GIF *gif, uint8_t *buffer) {
|
||||
/* 1. Copy Canvas (Background) to ARGB Buffer */
|
||||
uint32_t *dst = (uint32_t *)buffer;
|
||||
uint8_t *src = gif->canvas;
|
||||
int total_pixels = gif->width * gif->height;
|
||||
|
||||
for (int i = 0; i < total_pixels; i++) {
|
||||
/* Pack RGB to 0xAABBGGRR (Little Endian for 0xAABBGGRR -> 0xRR, 0xGG, 0xBB,
|
||||
0xAA in memory) LVGL ARGB8888 can be slightly different depending on
|
||||
config, usually 0xAARRGGBB or 0xBBGGRRAA The previous manual loop was: R,
|
||||
G, B, 0xFF (buffer[0]=R) If target is uint32, and we want R at lowest
|
||||
addr: 0xAA BB GG RR (on LE machine) -> [RR] [GG] [BB] [AA]
|
||||
*/
|
||||
dst[i] = (0xFF000000) | (src[2] << 16) | (src[1] << 8) | (src[0]);
|
||||
src += 3;
|
||||
}
|
||||
|
||||
/* 2. Render Current Frame Rect on top */
|
||||
int i = gif->fy * gif->width + gif->fx;
|
||||
for (int j = 0; j < gif->fh; j++) {
|
||||
for (int k = 0; k < gif->fw; k++) {
|
||||
uint8_t index = gif->frame[(gif->fy + j) * gif->width + gif->fx + k];
|
||||
if (!gif->gce.transparency || index != gif->gce.tindex) {
|
||||
uint8_t *color = &gif->palette->colors[index * 3];
|
||||
dst[i + k] =
|
||||
(0xFF000000) | (color[2] << 16) | (color[1] << 8) | (color[0]);
|
||||
}
|
||||
}
|
||||
i += gif->width;
|
||||
}
|
||||
}
|
||||
|
||||
int app_gd_is_bgcolor(app_gd_GIF *gif, uint8_t color[3]) {
|
||||
return !memcmp(&gif->palette->colors[gif->bgindex * 3], color, 3);
|
||||
}
|
||||
|
||||
void app_gd_rewind(app_gd_GIF *gif) { mem_seek(gif, gif->anim_start); }
|
||||
|
||||
void app_gd_close_gif(app_gd_GIF *gif) {
|
||||
free(gif->frame);
|
||||
free(gif);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
#ifndef APP_GIFDEC_H
|
||||
#define APP_GIFDEC_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct app_gd_Palette {
|
||||
int size;
|
||||
uint8_t colors[0x100 * 3];
|
||||
} app_gd_Palette;
|
||||
|
||||
typedef struct app_gd_GCE {
|
||||
uint16_t delay;
|
||||
uint8_t tindex;
|
||||
uint8_t disposal;
|
||||
int input;
|
||||
int transparency;
|
||||
} app_gd_GCE;
|
||||
|
||||
typedef struct app_gd_GIF {
|
||||
const uint8_t *data;
|
||||
size_t data_size;
|
||||
size_t pos;
|
||||
size_t anim_start;
|
||||
uint16_t width, height;
|
||||
uint16_t depth;
|
||||
uint16_t loop_count;
|
||||
app_gd_GCE gce;
|
||||
app_gd_Palette *palette;
|
||||
app_gd_Palette lct, gct;
|
||||
uint16_t fx, fy, fw, fh;
|
||||
uint8_t bgindex;
|
||||
uint8_t *canvas, *frame;
|
||||
} app_gd_GIF;
|
||||
|
||||
app_gd_GIF *app_gd_open_gif_mem(const uint8_t *data, size_t size);
|
||||
int app_gd_get_frame(app_gd_GIF *gif);
|
||||
void app_gd_render_frame(app_gd_GIF *gif, uint8_t *buffer);
|
||||
void app_gd_render_frame_to_argb(app_gd_GIF *gif, uint8_t *buffer);
|
||||
int app_gd_is_bgcolor(app_gd_GIF *gif, uint8_t color[3]);
|
||||
void app_gd_rewind(app_gd_GIF *gif);
|
||||
void app_gd_close_gif(app_gd_GIF *gif);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* APP_GIFDEC_H */
|
||||
@@ -1,142 +1,65 @@
|
||||
#include "emotion_manager.h"
|
||||
#include "app_gifdec.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Include the GIF assets */
|
||||
#include "../assets/emotions/blink.h"
|
||||
#include "lvgl.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static const char *TAG = "EMOTION_MANAGER";
|
||||
|
||||
static app_gd_GIF *current_gif = NULL;
|
||||
static lv_obj_t *img_obj = NULL;
|
||||
static lv_timer_t *update_timer = NULL;
|
||||
static uint8_t *render_buf_32 = NULL;
|
||||
static lv_img_dsc_t img_dsc;
|
||||
static lv_obj_t *emotion_screen_obj = NULL;
|
||||
static lv_obj_t *emotion_label = NULL;
|
||||
|
||||
typedef struct {
|
||||
const uint8_t *data;
|
||||
size_t size;
|
||||
} emotion_asset_t;
|
||||
|
||||
static const emotion_asset_t assets[EMOTION_COUNT] = {
|
||||
[EMOTION_BLINK] = {.data = blink, .size = sizeof(blink)},
|
||||
// Add placeholders for Smile and Sad if needed
|
||||
[EMOTION_SMILE] = {NULL, 0},
|
||||
[EMOTION_SAD] = {NULL, 0},
|
||||
static const char *emotion_names[EMOTION_COUNT] = {
|
||||
[EMOTION_BLINK] = "Heya!",
|
||||
[EMOTION_SMILE] = "SMILE",
|
||||
[EMOTION_SAD] = "SAD",
|
||||
};
|
||||
|
||||
static void timer_cb(lv_timer_t *t) {
|
||||
if (!current_gif)
|
||||
return;
|
||||
|
||||
int ret = app_gd_get_frame(current_gif);
|
||||
if (ret == 0) {
|
||||
/* End of GIF, loop back */
|
||||
app_gd_rewind(current_gif);
|
||||
ret = app_gd_get_frame(current_gif);
|
||||
}
|
||||
|
||||
if (ret > 0) {
|
||||
uint32_t start_tick = xTaskGetTickCount();
|
||||
|
||||
/* Render frame directly into 32-bit ARGB buffer */
|
||||
/* This saves memory bandwidth and CPU cycles by combining decoding and
|
||||
* swizzling */
|
||||
app_gd_render_frame_to_argb(current_gif, render_buf_32);
|
||||
|
||||
uint32_t end_tick = xTaskGetTickCount();
|
||||
uint32_t render_time_ms = (end_tick - start_tick) * portTICK_PERIOD_MS;
|
||||
|
||||
/* Refresh image */
|
||||
lv_obj_invalidate(img_obj);
|
||||
|
||||
/* Set timer period based on GIF delay */
|
||||
int delay = current_gif->gce.delay * 10;
|
||||
// ESP_LOGI(TAG, "GIF Delay form GCE: %d ms", delay);
|
||||
if (delay < 20)
|
||||
delay = 20; // Minimum delay safeguard (allow faster playback)
|
||||
|
||||
// Log Load
|
||||
ESP_LOGI(TAG, "GIF Render: %lu ms, Target Delay: %d ms, Load: %lu%%",
|
||||
render_time_ms, delay, (render_time_ms * 100) / delay);
|
||||
|
||||
lv_timer_set_period(update_timer, delay);
|
||||
}
|
||||
}
|
||||
extern const lv_font_t Geist;
|
||||
|
||||
void emotion_manager_init(lv_obj_t *parent) {
|
||||
if (img_obj)
|
||||
if (emotion_screen_obj)
|
||||
return;
|
||||
|
||||
ESP_LOGI(TAG, "Initializing emotion manager");
|
||||
ESP_LOGI(TAG, "Initializing Text-Based Emotion Manager");
|
||||
|
||||
/* Create the image object */
|
||||
img_obj = lv_img_create(parent);
|
||||
lv_obj_set_size(img_obj, 368, 460);
|
||||
lv_obj_center(img_obj);
|
||||
lv_obj_set_scrollbar_mode(img_obj, LV_SCROLLBAR_MODE_OFF);
|
||||
/* Create a container object to act as the main screen */
|
||||
emotion_screen_obj = lv_obj_create(parent);
|
||||
lv_obj_set_size(emotion_screen_obj, LV_PCT(100), LV_PCT(100));
|
||||
lv_obj_center(emotion_screen_obj);
|
||||
|
||||
/* Allocate buffers for frames in PSRAM */
|
||||
/* Only need 32-bit buffer now as we render directly to it */
|
||||
render_buf_32 = heap_caps_malloc(368 * 460 * 4, MALLOC_CAP_SPIRAM);
|
||||
if (!render_buf_32) {
|
||||
ESP_LOGE(TAG, "Failed to allocate render buffer in PSRAM");
|
||||
return;
|
||||
}
|
||||
/* Set background to Cream/Off-White (#FAF8F4) */
|
||||
lv_obj_set_style_bg_color(emotion_screen_obj, lv_color_hex(0xFAF8F4), 0);
|
||||
lv_obj_set_style_bg_opa(emotion_screen_obj, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_border_width(emotion_screen_obj, 0, 0);
|
||||
lv_obj_set_scrollbar_mode(emotion_screen_obj, LV_SCROLLBAR_MODE_OFF);
|
||||
|
||||
/* Setup image descriptor */
|
||||
img_dsc.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA;
|
||||
img_dsc.header.always_zero = 0;
|
||||
img_dsc.header.w = 368;
|
||||
img_dsc.header.h = 460;
|
||||
img_dsc.data_size = 368 * 460 * 4;
|
||||
img_dsc.data = render_buf_32;
|
||||
/* Allow touch events to pass through to the parent (which handles swipe
|
||||
* gestures) */
|
||||
lv_obj_clear_flag(emotion_screen_obj, LV_OBJ_FLAG_CLICKABLE);
|
||||
|
||||
lv_img_set_src(img_obj, &img_dsc);
|
||||
/* Create the label */
|
||||
emotion_label = lv_label_create(emotion_screen_obj);
|
||||
/* Use the custom Geist 48 font */
|
||||
lv_obj_set_style_text_font(emotion_label, &Geist, 0);
|
||||
/* Use 1x native size (removed zoom) */
|
||||
lv_obj_center(emotion_label);
|
||||
/* Text color Dark Gray (#2B2B2B) */
|
||||
lv_obj_set_style_text_color(emotion_label, lv_color_hex(0x2B2B2B), 0);
|
||||
|
||||
/* Create timer for animation */
|
||||
update_timer = lv_timer_create(timer_cb, 100, NULL);
|
||||
lv_timer_pause(update_timer);
|
||||
|
||||
/* Set default emotion */
|
||||
/* Initialize with default emotion */
|
||||
emotion_manager_set(EMOTION_BLINK);
|
||||
}
|
||||
|
||||
void emotion_manager_set(emotion_t emotion) {
|
||||
if (emotion >= EMOTION_COUNT)
|
||||
if (emotion >= EMOTION_COUNT || !emotion_label)
|
||||
return;
|
||||
if (!assets[emotion].data) {
|
||||
ESP_LOGW(TAG, "Asset for emotion %d not found", emotion);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Setting emotion to %d", emotion);
|
||||
ESP_LOGI(TAG, "Setting emotion to %s", emotion_names[emotion]);
|
||||
|
||||
lv_timer_pause(update_timer);
|
||||
|
||||
if (current_gif) {
|
||||
app_gd_close_gif(current_gif);
|
||||
current_gif = NULL;
|
||||
}
|
||||
|
||||
current_gif = app_gd_open_gif_mem(assets[emotion].data, assets[emotion].size);
|
||||
if (!current_gif) {
|
||||
ESP_LOGE(TAG, "Failed to open GIF for emotion %d", emotion);
|
||||
return;
|
||||
}
|
||||
|
||||
app_gd_rewind(current_gif);
|
||||
|
||||
/* Render first frame immediately to avoid garbage on screen */
|
||||
timer_cb(NULL);
|
||||
|
||||
lv_timer_resume(update_timer);
|
||||
lv_label_set_text(emotion_label, emotion_names[emotion]);
|
||||
lv_obj_center(emotion_label); /* Re-center after text change */
|
||||
}
|
||||
|
||||
void emotion_manager_update(void) {
|
||||
// This could be used for manual polling if timer is not used
|
||||
/* No periodic updates needed for static text */
|
||||
}
|
||||
|
||||
65
main/main.c
65
main/main.c
@@ -43,6 +43,7 @@
|
||||
/* Project Assets */
|
||||
#include "../assets/logoIroScreen.c"
|
||||
#include "emotion_manager.h"
|
||||
#include "swipe_gesture.h"
|
||||
|
||||
// Emotions handled by emotion_manager
|
||||
|
||||
@@ -206,7 +207,8 @@ static void start_watch_face(void) {
|
||||
|
||||
/* Initialize Emotion Manager */
|
||||
emotion_manager_init(scr);
|
||||
|
||||
/* Initialize swipe gesture handling */
|
||||
swipe_gesture_init(scr);
|
||||
/* Load the watch face screen immediately */
|
||||
lv_scr_load(scr);
|
||||
}
|
||||
@@ -397,9 +399,11 @@ static void ui_task(void *arg) {
|
||||
|
||||
/* Process Event Queue */
|
||||
ui_event_t ev;
|
||||
/*
|
||||
if (xQueueReceive(g_event_queue, &ev, 0) == pdTRUE) {
|
||||
/* Handle UI events here */
|
||||
// Handle UI events here
|
||||
}
|
||||
*/
|
||||
|
||||
lvgl_unlock();
|
||||
}
|
||||
@@ -417,7 +421,7 @@ static void ui_task(void *arg) {
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
void app_main(void) {
|
||||
esp_log_level_set("lcd_panel.io.i2c", ESP_LOG_INFO);
|
||||
esp_log_level_set("lcd_panel.io.i2c", ESP_LOG_DEBUG);
|
||||
esp_log_level_set("FT5x06", ESP_LOG_DEBUG);
|
||||
esp_log_level_set("esp_lcd_touch", ESP_LOG_DEBUG);
|
||||
|
||||
@@ -430,12 +434,29 @@ void app_main(void) {
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = PIN_NUM_TOUCH_SCL,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = 100 * 1000,
|
||||
.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));
|
||||
|
||||
/* Run a quick I2C scan to list devices on the bus for diagnostics */
|
||||
{
|
||||
ESP_LOGI("I2C_SCAN", "Starting I2C scan on port %d", TOUCH_HOST);
|
||||
for (int addr = 1; addr < 0x7F; addr++) {
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(TOUCH_HOST, cmd, pdMS_TO_TICKS(50));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI("I2C_SCAN", "Found device at 0x%02X", addr);
|
||||
}
|
||||
}
|
||||
ESP_LOGI("I2C_SCAN", "I2C scan complete");
|
||||
}
|
||||
|
||||
/* IO Expander Setup */
|
||||
esp_io_expander_handle_t io_expander = NULL;
|
||||
ESP_ERROR_CHECK(esp_io_expander_new_i2c_tca9554(
|
||||
@@ -445,17 +466,15 @@ void app_main(void) {
|
||||
IO_EXPANDER_PIN_NUM_2,
|
||||
IO_EXPANDER_OUTPUT);
|
||||
|
||||
/* Vendor Init Sequence: Toggle all pins Low -> High with 200ms delay */
|
||||
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);
|
||||
|
||||
/* Hard Reset Touch via IO Expander */
|
||||
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_2, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_2, 1);
|
||||
/* Add delay to allow Touch IC to wake up and stabilize after reset high */
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
|
||||
/* LCD Setup */
|
||||
@@ -488,21 +507,37 @@ void app_main(void) {
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_init(lcd_panel));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(lcd_panel, true));
|
||||
|
||||
/* Touch Driver Setup (FT5x06) */
|
||||
#if USE_TOUCH
|
||||
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
|
||||
const esp_lcd_panel_io_i2c_config_t tp_io_config =
|
||||
|
||||
/* Overriding default address (usually 0x38) with 0x34 found in scan */
|
||||
esp_lcd_panel_io_i2c_config_t tp_io_config =
|
||||
ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
|
||||
tp_io_config.dev_addr = 0x34;
|
||||
|
||||
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 = LCD_H_RES,
|
||||
.y_max = LCD_V_RES,
|
||||
.rst_gpio_num = PIN_NUM_TOUCH_RST,
|
||||
esp_lcd_touch_config_t tp_cfg = {
|
||||
.x_max = 368,
|
||||
.y_max = 448,
|
||||
.rst_gpio_num = -1, // Reset handled by IO Expander logic above
|
||||
.int_gpio_num = PIN_NUM_TOUCH_INT,
|
||||
.levels = {.reset = 0, .interrupt = 0},
|
||||
.flags = {.swap_xy = 0, .mirror_x = 0, .mirror_y = 0},
|
||||
.levels =
|
||||
{
|
||||
.reset = 0,
|
||||
.interrupt = 0,
|
||||
},
|
||||
.flags =
|
||||
{
|
||||
.swap_xy = 0,
|
||||
.mirror_x = 0,
|
||||
.mirror_y = 0,
|
||||
},
|
||||
};
|
||||
|
||||
esp_lcd_touch_handle_t tp = NULL;
|
||||
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
|
||||
#endif
|
||||
|
||||
|
||||
55
main/swipe_gesture.c
Normal file
55
main/swipe_gesture.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "swipe_gesture.h"
|
||||
#include "lvgl.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
// Pastel color definitions
|
||||
#define COLOR_PASTEL_GREEN lv_color_hex(0xA8E6CF)
|
||||
#define COLOR_PASTEL_RED lv_color_hex(0xFFB3BA)
|
||||
#define COLOR_BLACK lv_color_hex(0x000000)
|
||||
|
||||
static lv_point_t start_point;
|
||||
|
||||
static void swipe_gesture_cb(lv_event_t *e) {
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t *scr = lv_event_get_target(e);
|
||||
lv_indev_t *indev = lv_indev_get_act();
|
||||
if (!indev)
|
||||
return;
|
||||
|
||||
if (code == LV_EVENT_PRESSED) {
|
||||
lv_indev_get_point(indev, &start_point);
|
||||
} else if (code == LV_EVENT_PRESSING) {
|
||||
lv_point_t curr_point;
|
||||
lv_indev_get_point(indev, &curr_point);
|
||||
|
||||
lv_coord_t diff_x = curr_point.x - start_point.x;
|
||||
lv_coord_t scr_width = lv_obj_get_width(scr);
|
||||
if (scr_width <= 0)
|
||||
scr_width = 368; // LCD_H_RES fallback
|
||||
|
||||
float proportion = (float)abs(diff_x) / (float)scr_width;
|
||||
if (proportion > 1.0f)
|
||||
proportion = 1.0f;
|
||||
|
||||
// Target color based on swipe direction (Right = Green, Left = Red)
|
||||
lv_color_t target = (diff_x > 0) ? COLOR_PASTEL_GREEN : COLOR_PASTEL_RED;
|
||||
|
||||
// Blend target color with black based on swipe proportion
|
||||
lv_opa_t opa = (lv_opa_t)(proportion * 255);
|
||||
lv_color_t blended = lv_color_mix(target, COLOR_BLACK, opa);
|
||||
|
||||
lv_obj_set_style_bg_color(scr, blended, LV_PART_MAIN);
|
||||
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, LV_PART_MAIN);
|
||||
} else if (code == LV_EVENT_RELEASED || code == LV_EVENT_PRESS_LOST) {
|
||||
// Return to black on release
|
||||
lv_obj_set_style_bg_color(scr, COLOR_BLACK, LV_PART_MAIN);
|
||||
}
|
||||
}
|
||||
|
||||
void swipe_gesture_init(lv_obj_t *scr) {
|
||||
// Register event callbacks on the screen to track touch manually
|
||||
lv_obj_add_event_cb(scr, swipe_gesture_cb, LV_EVENT_PRESSED, NULL);
|
||||
lv_obj_add_event_cb(scr, swipe_gesture_cb, LV_EVENT_PRESSING, NULL);
|
||||
lv_obj_add_event_cb(scr, swipe_gesture_cb, LV_EVENT_RELEASED, NULL);
|
||||
lv_obj_add_event_cb(scr, swipe_gesture_cb, LV_EVENT_PRESS_LOST, NULL);
|
||||
}
|
||||
13
main/swipe_gesture.h
Normal file
13
main/swipe_gesture.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "lvgl.h"
|
||||
|
||||
/**
|
||||
* Initialize swipe gesture handling for a given screen object.
|
||||
* The function registers a gesture event callback that changes the
|
||||
* background color to a pastel green for a right swipe (positive) and
|
||||
* pastel red for a left swipe (negative). The color transition is
|
||||
* performed using LVGL's animation system for a smooth, Apple‑level
|
||||
* experience.
|
||||
*/
|
||||
void swipe_gesture_init(lv_obj_t *scr);
|
||||
Reference in New Issue
Block a user