forked from xiaozhi/xiaozhi-esp32
feat: add new board esp-hi (#666)
* feat: add new board esp-hi * feat(esp-hi): update servo_dog_ctrl --------- Co-authored-by: Li Junru <lijunru@espressif.com> Co-authored-by: Xiaoxia <terrence@tenclass.com>
This commit is contained in:
44
main/boards/esp-hi/config.h
Normal file
44
main/boards/esp-hi/config.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef _BOARD_CONFIG_H_
|
||||
#define _BOARD_CONFIG_H_
|
||||
|
||||
#include <driver/gpio.h>
|
||||
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 16000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
|
||||
|
||||
#define AUDIO_ADC_MIC_CHANNEL 2
|
||||
#define AUDIO_PDM_SPEAK_P_GPIO GPIO_NUM_6
|
||||
#define AUDIO_PDM_SPEAK_N_GPIO GPIO_NUM_7
|
||||
#define AUDIO_PA_CTL_GPIO GPIO_NUM_3
|
||||
|
||||
#define BUILTIN_LED_GPIO GPIO_NUM_NC
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_9
|
||||
#define MOVE_WAKE_BUTTON_GPIO GPIO_NUM_0
|
||||
#define AUDIO_WAKE_BUTTON_GPIO GPIO_NUM_1
|
||||
|
||||
#define DISPLAY_MOSI_PIN GPIO_NUM_4
|
||||
#define DISPLAY_CLK_PIN GPIO_NUM_5
|
||||
#define DISPLAY_DC_PIN GPIO_NUM_10
|
||||
#define DISPLAY_RST_PIN GPIO_NUM_NC
|
||||
#define DISPLAY_CS_PIN GPIO_NUM_NC
|
||||
|
||||
#define FL_GPIO_NUM GPIO_NUM_21
|
||||
#define FR_GPIO_NUM GPIO_NUM_19
|
||||
#define BL_GPIO_NUM GPIO_NUM_20
|
||||
#define BR_GPIO_NUM GPIO_NUM_18
|
||||
|
||||
#define LCD_TYPE_ST7789_SERIAL
|
||||
#define DISPLAY_WIDTH 160
|
||||
#define DISPLAY_HEIGHT 80
|
||||
#define DISPLAY_MIRROR_X false
|
||||
#define DISPLAY_MIRROR_Y true
|
||||
#define DISPLAY_SWAP_XY true
|
||||
|
||||
#define DISPLAY_INVERT_COLOR true
|
||||
#define DISPLAY_RGB_ORDER LCD_RGB_ELEMENT_ORDER_RGB
|
||||
#define DISPLAY_OFFSET_X 0
|
||||
#define DISPLAY_OFFSET_Y 0
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
|
||||
#define DISPLAY_SPI_MODE 0
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
37
main/boards/esp-hi/config.json
Normal file
37
main/boards/esp-hi/config.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"target": "esp32c3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "esp-hi",
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_IDF_TARGET=\"esp32c3\"",
|
||||
"CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y",
|
||||
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions_hi.csv\"",
|
||||
"CONFIG_BOARD_TYPE_ESP_HI=y",
|
||||
"CONFIG_SR_WN_WN9S_HILEXIN=y",
|
||||
"CONFIG_FL_ANGLE_NEUTRAL=78",
|
||||
"CONFIG_FR_ANGLE_NEUTRAL=108",
|
||||
"CONFIG_BR_ANGLE_NEUTRAL=64",
|
||||
"CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=3",
|
||||
"CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=4",
|
||||
"CONFIG_ESP_WIFI_AMPDU_TX_ENABLED=n",
|
||||
"CONFIG_ESP_WIFI_RX_BA_WIN=4",
|
||||
"CONFIG_ESP_WIFI_ENABLE_WPA3_SAE=n",
|
||||
"CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=0",
|
||||
"CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT=n",
|
||||
"CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y",
|
||||
"CONFIG_ESP_MAIN_TASK_STACK_SIZE=6144",
|
||||
"CONFIG_FREERTOS_HZ=1000",
|
||||
"CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=768",
|
||||
"CONFIG_LWIP_MAX_SOCKETS=10",
|
||||
"CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16",
|
||||
"CONFIG_LWIP_IPV6=n",
|
||||
"CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=2048",
|
||||
"CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA=y",
|
||||
"CONFIG_NEWLIB_NANO_FORMAT=y",
|
||||
"CONFIG_MMAP_FILE_NAME_LENGTH=25",
|
||||
"CONFIG_ESP_CONSOLE_NONE=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
64
main/boards/esp-hi/dog_action extra.cc
Normal file
64
main/boards/esp-hi/dog_action extra.cc
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_log.h>
|
||||
#include "servo_dog_ctrl.h"
|
||||
|
||||
#define TAG "Message"
|
||||
|
||||
namespace iot {
|
||||
|
||||
class DogAction_extra : public Thing {
|
||||
private:
|
||||
bool is_moving_ = false;
|
||||
|
||||
void InitializePlayer()
|
||||
{
|
||||
ESP_LOGI(TAG, "Dog action initialized");
|
||||
}
|
||||
|
||||
public:
|
||||
DogAction_extra() : Thing("DogAction_extra", "机器人扩展动作控制")
|
||||
{
|
||||
InitializePlayer();
|
||||
|
||||
// 定义设备的属性
|
||||
properties_.AddBooleanProperty("is_moving", "机器人是否正在移动", [this]() -> bool {
|
||||
return is_moving_;
|
||||
});
|
||||
|
||||
// 定义设备可以被远程执行的指令
|
||||
methods_.AddMethod("retract_legs", "机器人收回腿部", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_RETRACT_LEGS, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("stop", "立即停止机器人当前动作", ParameterList(), [this](const ParameterList & parameters) {
|
||||
if (is_moving_) {
|
||||
is_moving_ = false;
|
||||
servo_dog_ctrl_send(DOG_STATE_IDLE, NULL);
|
||||
}
|
||||
});
|
||||
|
||||
methods_.AddMethod("shake_hand", "机器人做握手动作", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_SHAKE_HAND, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("shake_back_legs", "机器人伸懒腰", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_SHAKE_BACK_LEGS, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("jump_forward", "机器人向前跳跃", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_JUMP_FORWARD, NULL);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(DogAction_extra);
|
||||
75
main/boards/esp-hi/dog_action_basic.cc
Normal file
75
main/boards/esp-hi/dog_action_basic.cc
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_log.h>
|
||||
#include "servo_dog_ctrl.h"
|
||||
|
||||
#define TAG "Message"
|
||||
|
||||
namespace iot {
|
||||
|
||||
class DogAction_basic : public Thing {
|
||||
private:
|
||||
bool is_moving_ = false;
|
||||
|
||||
void InitializePlayer()
|
||||
{
|
||||
ESP_LOGI(TAG, "Dog action initialized");
|
||||
}
|
||||
|
||||
public:
|
||||
DogAction_basic() : Thing("DogAction_basic", "机器人基础动作控制")
|
||||
{
|
||||
InitializePlayer();
|
||||
|
||||
// 定义设备的属性
|
||||
properties_.AddBooleanProperty("is_moving", "机器人是否正在移动", [this]() -> bool {
|
||||
return is_moving_;
|
||||
});
|
||||
|
||||
// 定义设备可以被远程执行的指令
|
||||
methods_.AddMethod("forward", "机器人向前移动", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_FORWARD, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("backward", "机器人向后移动", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_BACKWARD, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("sway_back_forth", "机器人做前后摇摆动作", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_SWAY_BACK_FORTH, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("turn_left", "机器人向左转", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_TURN_LEFT, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("turn_right", "机器人向右转", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_TURN_RIGHT, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("lay_down", "机器人趴下", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
servo_dog_ctrl_send(DOG_STATE_LAY_DOWN, NULL);
|
||||
});
|
||||
|
||||
methods_.AddMethod("sway", "机器人做左右摇摆动作", ParameterList(), [this](const ParameterList & parameters) {
|
||||
is_moving_ = true;
|
||||
dog_action_args_t args = {
|
||||
.repeat_count = 4,
|
||||
};
|
||||
servo_dog_ctrl_send(DOG_STATE_SWAY, &args);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(DogAction_basic);
|
||||
113
main/boards/esp-hi/dog_light.cc
Normal file
113
main/boards/esp-hi/dog_light.cc
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "iot/thing.h"
|
||||
#include "board.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_log.h>
|
||||
#include "driver/rmt_tx.h"
|
||||
#include "led_strip.h"
|
||||
|
||||
#define TAG "Light"
|
||||
|
||||
static led_strip_handle_t led_strip;
|
||||
|
||||
static const led_strip_config_t bsp_strip_config = {
|
||||
.strip_gpio_num = GPIO_NUM_8,
|
||||
.max_leds = 4,
|
||||
.led_model = LED_MODEL_WS2812,
|
||||
.flags = {
|
||||
.invert_out = false
|
||||
}
|
||||
};
|
||||
|
||||
static const led_strip_rmt_config_t bsp_rmt_config = {
|
||||
.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||
.resolution_hz = 10 * 1000 * 1000,
|
||||
.flags = {
|
||||
.with_dma = false
|
||||
}
|
||||
};
|
||||
|
||||
esp_err_t bsp_led_init()
|
||||
{
|
||||
ESP_LOGI(TAG, "BLINK_GPIO setting %d", bsp_strip_config.strip_gpio_num);
|
||||
|
||||
ESP_ERROR_CHECK(led_strip_new_rmt_device(&bsp_strip_config, &bsp_rmt_config, &led_strip));
|
||||
led_strip_set_pixel(led_strip, 0, 0x00, 0x00, 0x00);
|
||||
led_strip_set_pixel(led_strip, 1, 0x00, 0x00, 0x00);
|
||||
led_strip_set_pixel(led_strip, 2, 0x00, 0x00, 0x00);
|
||||
led_strip_set_pixel(led_strip, 3, 0x00, 0x00, 0x00);
|
||||
led_strip_refresh(led_strip);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t bsp_led_rgb_set(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
|
||||
ret |= led_strip_set_pixel(led_strip, 0, r, g, b);
|
||||
ret |= led_strip_set_pixel(led_strip, 1, r, g, b);
|
||||
ret |= led_strip_set_pixel(led_strip, 2, r, g, b);
|
||||
ret |= led_strip_set_pixel(led_strip, 3, r, g, b);
|
||||
ret |= led_strip_refresh(led_strip);
|
||||
return ret;
|
||||
}
|
||||
|
||||
namespace iot {
|
||||
class DogLight : public Thing {
|
||||
private:
|
||||
bool power_ = false;
|
||||
|
||||
void InitializeGpio()
|
||||
{
|
||||
bsp_led_init();
|
||||
bsp_led_rgb_set(0x00, 0x00, 0x00);
|
||||
ESP_LOGI(TAG, "lamp InitializeGpio");
|
||||
}
|
||||
|
||||
public:
|
||||
DogLight() : Thing("DogLight", "机器人头灯"), power_(false)
|
||||
{
|
||||
InitializeGpio();
|
||||
|
||||
properties_.AddBooleanProperty("power", "灯是否打开", [this]() -> bool {
|
||||
return power_;
|
||||
});
|
||||
|
||||
methods_.AddMethod("TurnOn", "打开灯", ParameterList(), [this](const ParameterList & parameters) {
|
||||
power_ = true;
|
||||
bsp_led_rgb_set(0xFF, 0xFF, 0xFF);
|
||||
ESP_LOGI(TAG, "lamp TurnOn");
|
||||
});
|
||||
|
||||
methods_.AddMethod("TurnOff", "关闭灯", ParameterList(), [this](const ParameterList & parameters) {
|
||||
power_ = false;
|
||||
bsp_led_rgb_set(0x00, 0x00, 0x00);
|
||||
ESP_LOGI(TAG, "lamp TurnOff");
|
||||
});
|
||||
|
||||
methods_.AddMethod("SetRGB", "设置RGB颜色",
|
||||
ParameterList({
|
||||
Parameter("r", "红色值(0-255)", kValueTypeNumber, true),
|
||||
Parameter("g", "绿色值(0-255)", kValueTypeNumber, true),
|
||||
Parameter("b", "蓝色值(0-255)", kValueTypeNumber, true)
|
||||
}), [this](const ParameterList & parameters) {
|
||||
int r = parameters["r"].number();
|
||||
int g = parameters["g"].number();
|
||||
int b = parameters["b"].number();
|
||||
|
||||
r = std::max(0, std::min(255, r));
|
||||
g = std::max(0, std::min(255, g));
|
||||
b = std::max(0, std::min(255, b));
|
||||
|
||||
power_ = true;
|
||||
bsp_led_rgb_set(r, g, b);
|
||||
ESP_LOGI(TAG, "lamp SetRGB: r=%d, g=%d, b=%d", r, g, b);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace iot
|
||||
|
||||
DECLARE_THING(DogLight);
|
||||
171
main/boards/esp-hi/emoji_display.cc
Normal file
171
main/boards/esp-hi/emoji_display.cc
Normal file
@@ -0,0 +1,171 @@
|
||||
#include <cstring>
|
||||
#include "display/lcd_display.h"
|
||||
#include <esp_log.h>
|
||||
#include "mmap_generate_emoji.h"
|
||||
#include "emoji_display.h"
|
||||
|
||||
#include <esp_lcd_panel_io.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/queue.h>
|
||||
#include <freertos/event_groups.h>
|
||||
|
||||
static const char *TAG = "emoji";
|
||||
|
||||
namespace anim {
|
||||
|
||||
bool EmojiPlayer::OnFlushIoReady(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
|
||||
{
|
||||
auto* disp_drv = static_cast<anim_player_handle_t*>(user_ctx);
|
||||
anim_player_flush_ready(disp_drv);
|
||||
return true;
|
||||
}
|
||||
|
||||
void EmojiPlayer::OnFlush(anim_player_handle_t handle, int x_start, int y_start, int x_end, int y_end, const void *color_data)
|
||||
{
|
||||
auto* panel = static_cast<esp_lcd_panel_handle_t>(anim_player_get_user_data(handle));
|
||||
esp_lcd_panel_draw_bitmap(panel, x_start, y_start, x_end, y_end, color_data);
|
||||
}
|
||||
|
||||
EmojiPlayer::EmojiPlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io)
|
||||
{
|
||||
ESP_LOGI(TAG, "Create EmojiPlayer, panel: %p, panel_io: %p", panel, panel_io);
|
||||
const mmap_assets_config_t assets_cfg = {
|
||||
.partition_label = "assets_A",
|
||||
.max_files = MMAP_EMOJI_FILES,
|
||||
.checksum = MMAP_EMOJI_CHECKSUM,
|
||||
.flags = {.mmap_enable = true, .full_check = true}
|
||||
};
|
||||
|
||||
mmap_assets_new(&assets_cfg, &assets_handle_);
|
||||
|
||||
anim_player_config_t player_cfg = {
|
||||
.flush_cb = OnFlush,
|
||||
.update_cb = NULL,
|
||||
.user_data = panel,
|
||||
.flags = {.swap = true},
|
||||
.task = ANIM_PLAYER_INIT_CONFIG()
|
||||
};
|
||||
|
||||
player_handle_ = anim_player_init(&player_cfg);
|
||||
|
||||
const esp_lcd_panel_io_callbacks_t cbs = {
|
||||
.on_color_trans_done = OnFlushIoReady,
|
||||
};
|
||||
esp_lcd_panel_io_register_event_callbacks(panel_io, &cbs, player_handle_);
|
||||
StartPlayer(MMAP_EMOJI_CONNECTING_AAF, true, 15);
|
||||
}
|
||||
|
||||
EmojiPlayer::~EmojiPlayer()
|
||||
{
|
||||
if (player_handle_) {
|
||||
anim_player_update(player_handle_, PLAYER_ACTION_STOP);
|
||||
anim_player_deinit(player_handle_);
|
||||
player_handle_ = nullptr;
|
||||
}
|
||||
|
||||
if (assets_handle_) {
|
||||
mmap_assets_del(assets_handle_);
|
||||
assets_handle_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void EmojiPlayer::StartPlayer(int aaf, bool repeat, int fps)
|
||||
{
|
||||
if (player_handle_) {
|
||||
uint32_t start, end;
|
||||
const void *src_data;
|
||||
size_t src_len;
|
||||
|
||||
src_data = mmap_assets_get_mem(assets_handle_, aaf);
|
||||
src_len = mmap_assets_get_size(assets_handle_, aaf);
|
||||
|
||||
anim_player_set_src_data(player_handle_, src_data, src_len);
|
||||
anim_player_get_segment(player_handle_, &start, &end);
|
||||
if(MMAP_EMOJI_WAKE_AAF == aaf){
|
||||
start = 7;
|
||||
}
|
||||
anim_player_set_segment(player_handle_, start, end, fps, true);
|
||||
anim_player_update(player_handle_, PLAYER_ACTION_START);
|
||||
}
|
||||
}
|
||||
|
||||
void EmojiPlayer::StopPlayer()
|
||||
{
|
||||
if (player_handle_) {
|
||||
anim_player_update(player_handle_, PLAYER_ACTION_STOP);
|
||||
}
|
||||
}
|
||||
|
||||
EmojiWidget::EmojiWidget(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io)
|
||||
{
|
||||
InitializePlayer(panel, panel_io);
|
||||
}
|
||||
|
||||
EmojiWidget::~EmojiWidget()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void EmojiWidget::SetEmotion(const char* emotion)
|
||||
{
|
||||
if (!player_) {
|
||||
return;
|
||||
}
|
||||
|
||||
using Param = std::tuple<int, bool, int>;
|
||||
static const std::unordered_map<std::string, Param> emotion_map = {
|
||||
{"happy", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"laughing", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"funny", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"loving", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"embarrassed", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"confident", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"delicious", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"sad", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
|
||||
{"crying", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
|
||||
{"sleepy", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
|
||||
{"silly", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
|
||||
{"angry", {MMAP_EMOJI_ANGER_LOOP_AAF, true, 25}},
|
||||
{"surprised", {MMAP_EMOJI_PANIC_LOOP_AAF, true, 25}},
|
||||
{"shocked", {MMAP_EMOJI_PANIC_LOOP_AAF, true, 25}},
|
||||
{"thinking", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
|
||||
{"winking", {MMAP_EMOJI_BLINK_QUICK_AAF, true, 5}},
|
||||
{"relaxed", {MMAP_EMOJI_SCORN_LOOP_AAF, true, 25}},
|
||||
{"confused", {MMAP_EMOJI_SCORN_LOOP_AAF, true, 25}},
|
||||
};
|
||||
|
||||
auto it = emotion_map.find(emotion);
|
||||
if (it != emotion_map.end()) {
|
||||
const auto& [aaf, repeat, fps] = it->second;
|
||||
player_->StartPlayer(aaf, repeat, fps);
|
||||
} else if (strcmp(emotion, "neutral") == 0) {
|
||||
}
|
||||
}
|
||||
|
||||
void EmojiWidget::SetStatus(const char* status)
|
||||
{
|
||||
if (player_) {
|
||||
if (strcmp(status, "聆听中...") == 0) {
|
||||
player_->StartPlayer(MMAP_EMOJI_ASKING_AAF, true, 15);
|
||||
} else if (strcmp(status, "待命") == 0) {
|
||||
player_->StartPlayer(MMAP_EMOJI_WAKE_AAF, true, 15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EmojiWidget::InitializePlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io)
|
||||
{
|
||||
player_ = std::make_unique<EmojiPlayer>(panel, panel_io);
|
||||
}
|
||||
|
||||
bool EmojiWidget::Lock(int timeout_ms)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void EmojiWidget::Unlock()
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace anim
|
||||
54
main/boards/esp-hi/emoji_display.h
Normal file
54
main/boards/esp-hi/emoji_display.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include "display/lcd_display.h"
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
#include "anim_player.h"
|
||||
#include "mmap_generate_emoji.h"
|
||||
|
||||
namespace anim {
|
||||
|
||||
class EmojiPlayer;
|
||||
|
||||
using FlushIoReadyCallback = std::function<bool(esp_lcd_panel_io_handle_t, esp_lcd_panel_io_event_data_t*, void*)>;
|
||||
using FlushCallback = std::function<void(anim_player_handle_t, int, int, int, int, const void*)>;
|
||||
|
||||
class EmojiPlayer {
|
||||
public:
|
||||
EmojiPlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io);
|
||||
~EmojiPlayer();
|
||||
|
||||
void StartPlayer(int aaf, bool repeat, int fps);
|
||||
void StopPlayer();
|
||||
|
||||
private:
|
||||
static bool OnFlushIoReady(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx);
|
||||
static void OnFlush(anim_player_handle_t handle, int x_start, int y_start, int x_end, int y_end, const void *color_data);
|
||||
|
||||
anim_player_handle_t player_handle_;
|
||||
mmap_assets_handle_t assets_handle_;
|
||||
};
|
||||
|
||||
class EmojiWidget : public Display {
|
||||
public:
|
||||
EmojiWidget(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io);
|
||||
virtual ~EmojiWidget();
|
||||
|
||||
virtual void SetEmotion(const char* emotion) override;
|
||||
virtual void SetStatus(const char* status) override;
|
||||
anim::EmojiPlayer* GetPlayer()
|
||||
{
|
||||
return player_.get();
|
||||
}
|
||||
|
||||
private:
|
||||
void InitializePlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io);
|
||||
virtual bool Lock(int timeout_ms = 0) override;
|
||||
virtual void Unlock() override;
|
||||
|
||||
std::unique_ptr<anim::EmojiPlayer> player_;
|
||||
};
|
||||
|
||||
} // namespace anim
|
||||
242
main/boards/esp-hi/esp_hi.cc
Normal file
242
main/boards/esp-hi/esp_hi.cc
Normal file
@@ -0,0 +1,242 @@
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codecs/adc_pdm_audio_codec.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "iot/thing_manager.h"
|
||||
#include <wifi_station.h>
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
|
||||
#include "display/lcd_display.h"
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
#include "esp_lcd_ili9341.h"
|
||||
|
||||
#include "assets/lang_config.h"
|
||||
#include "anim_player.h"
|
||||
#include "emoji_display.h"
|
||||
#include "servo_dog_ctrl.h"
|
||||
|
||||
#define TAG "ESP_HI"
|
||||
|
||||
static const ili9341_lcd_init_cmd_t vendor_specific_init[] = {
|
||||
{0x11, NULL, 0, 120}, // Sleep out, Delay 120ms
|
||||
{0xB1, (uint8_t []){0x05, 0x3A, 0x3A}, 3, 0},
|
||||
{0xB2, (uint8_t []){0x05, 0x3A, 0x3A}, 3, 0},
|
||||
{0xB3, (uint8_t []){0x05, 0x3A, 0x3A, 0x05, 0x3A, 0x3A}, 6, 0},
|
||||
{0xB4, (uint8_t []){0x03}, 1, 0}, // Dot inversion
|
||||
{0xC0, (uint8_t []){0x44, 0x04, 0x04}, 3, 0},
|
||||
{0xC1, (uint8_t []){0xC0}, 1, 0},
|
||||
{0xC2, (uint8_t []){0x0D, 0x00}, 2, 0},
|
||||
{0xC3, (uint8_t []){0x8D, 0x6A}, 2, 0},
|
||||
{0xC4, (uint8_t []){0x8D, 0xEE}, 2, 0},
|
||||
{0xC5, (uint8_t []){0x08}, 1, 0},
|
||||
{0xE0, (uint8_t []){0x0F, 0x10, 0x03, 0x03, 0x07, 0x02, 0x00, 0x02, 0x07, 0x0C, 0x13, 0x38, 0x0A, 0x0E, 0x03, 0x10}, 16, 0},
|
||||
{0xE1, (uint8_t []){0x10, 0x0B, 0x04, 0x04, 0x10, 0x03, 0x00, 0x03, 0x03, 0x09, 0x17, 0x33, 0x0B, 0x0C, 0x06, 0x10}, 16, 0},
|
||||
{0x35, (uint8_t []){0x00}, 1, 0},
|
||||
{0x3A, (uint8_t []){0x05}, 1, 0},
|
||||
{0x36, (uint8_t []){0xC8}, 1, 0},
|
||||
{0x29, NULL, 0, 0}, // Display on
|
||||
{0x2C, NULL, 0, 0}, // Memory write
|
||||
};
|
||||
|
||||
class EspHi : public WifiBoard {
|
||||
private:
|
||||
Button boot_button_;
|
||||
Button audio_wake_button_;
|
||||
Button move_wake_button_;
|
||||
anim::EmojiWidget* display_ = nullptr;
|
||||
|
||||
void HandleMoveWakePressDown(int64_t current_time, int64_t &last_trigger_time, int &gesture_state)
|
||||
{
|
||||
int64_t interval = last_trigger_time == 0 ? 0 : current_time - last_trigger_time;
|
||||
last_trigger_time = current_time;
|
||||
|
||||
if (interval > 1000) {
|
||||
gesture_state = 0;
|
||||
} else {
|
||||
switch (gesture_state) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
if (interval > 300) {
|
||||
gesture_state = 2;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (interval > 100) {
|
||||
gesture_state = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMoveWakePressUp(int64_t current_time, int64_t &last_trigger_time, int &gesture_state)
|
||||
{
|
||||
int64_t interval = current_time - last_trigger_time;
|
||||
|
||||
if (interval > 1000) {
|
||||
gesture_state = 0;
|
||||
} else {
|
||||
switch (gesture_state) {
|
||||
case 0:
|
||||
if (interval > 300) {
|
||||
gesture_state = 1;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
break;
|
||||
case 2:
|
||||
if (interval < 100) {
|
||||
ESP_LOGI(TAG, "gesture detected");
|
||||
gesture_state = 0;
|
||||
auto &app = Application::GetInstance();
|
||||
app.ToggleChatState();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeButtons()
|
||||
{
|
||||
static int64_t last_trigger_time = 0;
|
||||
static int gesture_state = 0; // 0: init, 1: wait second long interval, 2: wait oscillation
|
||||
|
||||
boot_button_.OnClick([this]() {
|
||||
auto &app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
});
|
||||
|
||||
audio_wake_button_.OnPressDown([this]() {
|
||||
});
|
||||
|
||||
audio_wake_button_.OnPressUp([this]() {
|
||||
});
|
||||
|
||||
move_wake_button_.OnPressDown([this]() {
|
||||
int64_t current_time = esp_timer_get_time() / 1000;
|
||||
HandleMoveWakePressDown(current_time, last_trigger_time, gesture_state);
|
||||
});
|
||||
|
||||
move_wake_button_.OnPressUp([this]() {
|
||||
int64_t current_time = esp_timer_get_time() / 1000;
|
||||
HandleMoveWakePressUp(current_time, last_trigger_time, gesture_state);
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeIot()
|
||||
{
|
||||
ESP_LOGI(TAG, "Initialize Iot");
|
||||
auto &thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("DogLight"));
|
||||
thing_manager.AddThing(iot::CreateThing("DogAction_basic"));
|
||||
thing_manager.AddThing(iot::CreateThing("DogAction_extra"));
|
||||
}
|
||||
|
||||
void InitializeSpi()
|
||||
{
|
||||
spi_bus_config_t buscfg = {};
|
||||
buscfg.mosi_io_num = DISPLAY_MOSI_PIN;
|
||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||
buscfg.sclk_io_num = DISPLAY_CLK_PIN;
|
||||
buscfg.quadwp_io_num = GPIO_NUM_NC;
|
||||
buscfg.quadhd_io_num = GPIO_NUM_NC;
|
||||
buscfg.max_transfer_sz = DISPLAY_WIDTH * 10 * sizeof(uint16_t);
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||||
}
|
||||
|
||||
void InitializeLcdDisplay()
|
||||
{
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
|
||||
// 液晶屏控制IO初始化
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
io_config.cs_gpio_num = DISPLAY_CS_PIN;
|
||||
io_config.dc_gpio_num = DISPLAY_DC_PIN;
|
||||
io_config.spi_mode = DISPLAY_SPI_MODE;
|
||||
io_config.pclk_hz = 40 * 1000 * 1000;
|
||||
io_config.trans_queue_depth = 10;
|
||||
io_config.lcd_cmd_bits = 8;
|
||||
io_config.lcd_param_bits = 8;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io));
|
||||
|
||||
// 初始化液晶屏驱动芯片
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
const ili9341_vendor_config_t vendor_config = {
|
||||
.init_cmds = &vendor_specific_init[0],
|
||||
.init_cmds_size = sizeof(vendor_specific_init) / sizeof(ili9341_lcd_init_cmd_t),
|
||||
};
|
||||
|
||||
esp_lcd_panel_dev_config_t panel_config = {};
|
||||
panel_config.reset_gpio_num = DISPLAY_RST_PIN;
|
||||
panel_config.rgb_ele_order = DISPLAY_RGB_ORDER;
|
||||
panel_config.bits_per_pixel = 16;
|
||||
panel_config.vendor_config = (void *) &vendor_config;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(panel_io, &panel_config, &panel));
|
||||
|
||||
esp_lcd_panel_reset(panel);
|
||||
esp_lcd_panel_init(panel);
|
||||
esp_lcd_panel_invert_color(panel, DISPLAY_INVERT_COLOR);
|
||||
esp_lcd_panel_invert_color(panel, false);
|
||||
esp_lcd_panel_set_gap(panel, 0, 24);
|
||||
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||||
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
|
||||
ESP_LOGI(TAG, "LCD panel create success, %p", panel);
|
||||
|
||||
esp_lcd_panel_disp_on_off(panel, true);
|
||||
|
||||
ESP_LOGI(TAG, "Create emoji widget, panel: %p, panel_io: %p", panel, panel_io);
|
||||
display_ = new anim::EmojiWidget(panel, panel_io);
|
||||
|
||||
servo_dog_ctrl_config_t config = {
|
||||
.fl_gpio_num = FL_GPIO_NUM,
|
||||
.fr_gpio_num = FR_GPIO_NUM,
|
||||
.bl_gpio_num = BL_GPIO_NUM,
|
||||
.br_gpio_num = BR_GPIO_NUM,
|
||||
};
|
||||
#if CONFIG_ESP_CONSOLE_NONE
|
||||
servo_dog_ctrl_init(&config);
|
||||
#endif
|
||||
}
|
||||
|
||||
public:
|
||||
EspHi() : boot_button_(BOOT_BUTTON_GPIO),
|
||||
audio_wake_button_(AUDIO_WAKE_BUTTON_GPIO),
|
||||
move_wake_button_(MOVE_WAKE_BUTTON_GPIO)
|
||||
{
|
||||
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
InitializeSpi();
|
||||
InitializeLcdDisplay();
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override
|
||||
{
|
||||
static AdcPdmAudioCodec audio_codec(
|
||||
AUDIO_INPUT_SAMPLE_RATE,
|
||||
AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
AUDIO_ADC_MIC_CHANNEL,
|
||||
AUDIO_PDM_SPEAK_P_GPIO,
|
||||
AUDIO_PDM_SPEAK_N_GPIO,
|
||||
AUDIO_PA_CTL_GPIO);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
virtual Display* GetDisplay() override
|
||||
{
|
||||
return display_;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(EspHi);
|
||||
Reference in New Issue
Block a user