#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/i2c_master.h"
#include "esp_check.h"
#include "esp_log.h"
#include "eye_frames.h"
#define OLED_I2C_PORT I2C_NUM_0
#define OLED_SDA_PIN 12
#define OLED_SCL_PIN 14
#define OLED_I2C_ADDR 0x3C
#define OLED_I2C_HZ 400000
#define OLED_TASK_STACK 4096
#define OLED_TASK_PRIORITY 4
static const char *TAG = "oled_eyes";
static i2c_master_dev_handle_t s_oled;
static QueueHandle_t s_state_queue;
typedef enum {
OLED_STATE_IDLE = 0,
OLED_STATE_WAKEUP,
OLED_STATE_LISTENING,
OLED_STATE_WAITING_SERVER,
OLED_STATE_SPEAKING,
} oled_state_t;
typedef struct {
const uint8_t *bitmap;
uint16_t duration_ms;
} oled_frame_t;
typedef struct {
const oled_frame_t *frames;
size_t count;
bool loop;
} oled_animation_t;
static const oled_frame_t anim_idle[] = {
{eye_normal, 1800}, {eye_half, 70}, {eye_closed, 120},
{eye_half, 70}, {eye_normal, 900},
};
static const oled_frame_t anim_wakeup[] = {
{eye_normal, 80}, {eye_surprised, 250}, {eye_happy, 300},
};
static const oled_frame_t anim_listening[] = {
{eye_focused, 450}, {eye_left, 350}, {eye_focused, 450}, {eye_right, 350},
};
static const oled_frame_t anim_waiting[] = {
{eye_left, 300}, {eye_normal, 180}, {eye_right, 300}, {eye_normal, 180},
};
static const oled_frame_t anim_speaking[] = {
{eye_speak1, 130}, {eye_speak2, 130}, {eye_happy, 150}, {eye_speak2, 130},
};
static const oled_animation_t animations[] = {
[OLED_STATE_IDLE] = {anim_idle, sizeof(anim_idle)/sizeof(anim_idle[0]), true},
[OLED_STATE_WAKEUP] = {anim_wakeup, sizeof(anim_wakeup)/sizeof(anim_wakeup[0]), false},
[OLED_STATE_LISTENING] = {anim_listening, sizeof(anim_listening)/sizeof(anim_listening[0]), true},
[OLED_STATE_WAITING_SERVER] = {anim_waiting, sizeof(anim_waiting)/sizeof(anim_waiting[0]), true},
[OLED_STATE_SPEAKING] = {anim_speaking, sizeof(anim_speaking)/sizeof(anim_speaking[0]), true},
};
static esp_err_t oled_cmd(const uint8_t *cmd, size_t len)
{
uint8_t tx[32];
if (len + 1 > sizeof(tx)) return ESP_ERR_INVALID_SIZE;
tx[0] = 0x00;
memcpy(&tx[1], cmd, len);
return i2c_master_transmit(s_oled, tx, len + 1, 100);
}
static esp_err_t oled_init(void)
{
i2c_master_bus_config_t bus_cfg = {
.i2c_port = OLED_I2C_PORT,
.sda_io_num = OLED_SDA_PIN,
.scl_io_num = OLED_SCL_PIN,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus;
ESP_RETURN_ON_ERROR(i2c_new_master_bus(&bus_cfg, &bus), TAG, "I2C bus");
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = OLED_I2C_ADDR,
.scl_speed_hz = OLED_I2C_HZ,
};
ESP_RETURN_ON_ERROR(i2c_master_bus_add_device(bus, &dev_cfg, &s_oled), TAG, "OLED device");
const uint8_t init_cmds[] = {
0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40,
0x8D, 0x14, 0x20, 0x00, 0xA1, 0xC8, 0xDA, 0x12,
0x81, 0x7F, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6,
0x2E, 0xAF
};
return oled_cmd(init_cmds, sizeof(init_cmds));
}
static esp_err_t oled_draw_native(const uint8_t *frame)
{
const uint8_t range[] = {0x21, 0, 127, 0x22, 0, 7};
ESP_RETURN_ON_ERROR(oled_cmd(range, sizeof(range)), TAG, "set range");
// Control byte 0x40 + 128 data bytes. Eight chunks avoid a large I2C transaction.
uint8_t tx[129];
tx[0] = 0x40;
for (int page = 0; page < 8; ++page) {
memcpy(&tx[1], frame + page * 128, 128);
ESP_RETURN_ON_ERROR(i2c_master_transmit(s_oled, tx, sizeof(tx), 100), TAG, "frame");
}
return ESP_OK;
}
void oled_set_state(oled_state_t state)
{
if (s_state_queue) xQueueOverwrite(s_state_queue, &state);
}
static void oled_animation_task(void *arg)
{
oled_state_t state = OLED_STATE_IDLE, new_state;
size_t frame = 0;
while (true) {
if (xQueueReceive(s_state_queue, &new_state, 0) == pdTRUE && new_state != state) {
state = new_state;
frame = 0;
ESP_LOGI(TAG, "state=%d", state);
}
const oled_animation_t *a = &animations[state];
ESP_ERROR_CHECK(oled_draw_native(a->frames[frame].bitmap));
TickType_t wait = pdMS_TO_TICKS(a->frames[frame].duration_ms);
if (xQueueReceive(s_state_queue, &new_state, wait) == pdTRUE) {
state = new_state;
frame = 0;
continue;
}
frame++;
if (frame >= a->count) {
frame = a->loop ? 0 : a->count - 1;
}
}
}
static void demo_task(void *arg)
{
const oled_state_t sequence[] = {
OLED_STATE_IDLE, OLED_STATE_WAKEUP, OLED_STATE_LISTENING,
OLED_STATE_WAITING_SERVER, OLED_STATE_SPEAKING,
};
const uint32_t duration_ms[] = {6000, 1200, 5000, 5000, 5000};
while (true) {
for (size_t i = 0; i < sizeof(sequence)/sizeof(sequence[0]); ++i) {
oled_set_state(sequence[i]);
vTaskDelay(pdMS_TO_TICKS(duration_ms[i]));
}
}
}
void app_main(void)
{
ESP_ERROR_CHECK(oled_init());
s_state_queue = xQueueCreate(1, sizeof(oled_state_t));
ESP_ERROR_CHECK(s_state_queue ? ESP_OK : ESP_ERR_NO_MEM);
oled_state_t initial = OLED_STATE_IDLE;
xQueueOverwrite(s_state_queue, &initial);
xTaskCreate(oled_animation_task, "oled_animation", OLED_TASK_STACK, NULL,
OLED_TASK_PRIORITY, NULL);
xTaskCreate(demo_task, "oled_demo", 2048, NULL, 2, NULL);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306