Initial commit
This commit is contained in:
78
main/boards/xingzhi-abs-2.0/README.md
Normal file
78
main/boards/xingzhi-abs-2.0/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# 无名科技星智 ABS 2.0
|
||||
|
||||
## 简介
|
||||
无名科技星智 ABS 2.0 是一款高性价比的 AI 语音交互开发板。它搭载 1.54 寸 LCD 屏幕,配备独立的物理按键,并采用 **ML307R 4G 通信模组**,让你在没有 Wi-Fi 的环境下也能随时随地与大模型对话。
|
||||
|
||||
## 核心特性
|
||||
- 双网络通信:支持 Wi-Fi 与 ML307R Cat.1 4G 双模切换,适配多场景使用
|
||||
- 显示系统:1.54 寸 240×240 LCD 屏幕,自定义 UI 布局优化,适配 1.54 寸方形屏显示效果
|
||||
- 物理按键交互:独立 Boot 功能键、音量加减键,支持单击、双击、长按、五击多阶操作
|
||||
- 扩展能力:内置 Micro SD 卡插槽,支持本地存储扩展;可预留震动马达,实现按键操作触觉反馈
|
||||
- 完善的电源管理:支持电池电量 ADC 检测、充电状态实时监测、自动休眠与深度睡眠节能控制
|
||||
- 生态兼容:完全适配小智 ESP32 项目固件,支持 Qwen/DeepSeek 等大模型,兼容 MCP 协议设备控制
|
||||
- 由于硬件差异,UI显示上对底部表情和文本的显示位置稍有改动
|
||||
|
||||
## 与铝合金版本(XINGZHI_METAL_1_54_WIFI)核心差异
|
||||
| 功能特性 | xingzhi-abs-2.0 | 铝合金版本 |
|
||||
|----------|------------------|------------|
|
||||
| 交互方式 | 独立物理按键(Boot/音量+/音量-) | CST816 触摸芯片+触摸交互 |
|
||||
| 外壳材质 | ABS 工程塑料 | 铝合金材质 |
|
||||
|
||||
>### 按键操作
|
||||
>- **开机**: 关机状态,长按电源键3秒后自动开机
|
||||
>- **关机**: 开机状态,长按电源键5秒后自动关机
|
||||
>- **唤醒/打断**: 正常通话/待机状态下,单击Boot功能键,可唤醒设备或打断正在进行的语音对话
|
||||
>- **重新配网**: 开机后1秒钟内,单击Boot功能键,设备会自动重启并进入配网界面
|
||||
>- **切换网络模式**: 开机状态下,双击Boot功能键,可切换Wi-Fi与4G网络模式
|
||||
>- **SD卡状态检测**: 开机状态下,五击Boot功能键,可检测并在屏幕显示SD卡挂载状态
|
||||
>- **增加音量**: 开机状态下,单击音量+按键,音量增加10%;长按音量+按键2s,音量直接递增至100%最大音量
|
||||
>- **减小音量**: 开机状态下,单击音量-按键,音量减小10%;长按音量-按键2s,音量直接递减至0%静音
|
||||
|
||||
>### 休眠操作
|
||||
>- **浅睡眠**: 开机后,维持待命状态60s后,进入浅睡眠(屏幕亮度调整到1%)
|
||||
>- **深睡眠**: 开机后,维持待命状态300s后,自动关机
|
||||
>- **唤醒**: 浅睡眠状态下,单击任意按键,唤醒设备(屏幕亮度回调)
|
||||
|
||||
# 编译配置命令
|
||||
|
||||
**克隆工程**
|
||||
|
||||
```bash
|
||||
git clone https://github.com/78/xiaozhi-esp32.git
|
||||
```
|
||||
|
||||
**进入工程**
|
||||
|
||||
```bash
|
||||
cd xiaozhi-esp32
|
||||
```
|
||||
|
||||
**配置编译目标为 ESP32S3**
|
||||
|
||||
```bash
|
||||
idf.py set-target esp32s3
|
||||
```
|
||||
|
||||
**打开 menuconfig**
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
**选择板子**
|
||||
|
||||
```bash
|
||||
- `Xiaozhi Assistant` → `Board Type` → 选择 `无名科技星智ABS 2.0`
|
||||
```
|
||||
|
||||
**编译**
|
||||
|
||||
```ba
|
||||
idf.py build
|
||||
```
|
||||
|
||||
**下载并打开串口终端**
|
||||
|
||||
```bash
|
||||
idf.py build flash monitor
|
||||
```
|
||||
70
main/boards/xingzhi-abs-2.0/config.h
Normal file
70
main/boards/xingzhi-abs-2.0/config.h
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
#ifndef _BOARD_CONFIG_H_
|
||||
#define _BOARD_CONFIG_H_
|
||||
|
||||
#include <driver/gpio.h>
|
||||
|
||||
// 音频
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 24000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
|
||||
|
||||
#define AUDIO_INPUT_REFERENCE true
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_4
|
||||
#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_5
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_6
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_17
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_15
|
||||
|
||||
#define AUDIO_CODEC_USE_PCA9557
|
||||
#define AUDIO_CODEC_I2C_PA_EN GPIO_NUM_21
|
||||
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_41
|
||||
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_42
|
||||
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
|
||||
|
||||
// 按键
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_45
|
||||
#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_2
|
||||
|
||||
// sd卡
|
||||
#define SD_DATA0 GPIO_NUM_39
|
||||
#define SD_CLK GPIO_NUM_38
|
||||
#define SD_CMD GPIO_NUM_40
|
||||
#define SD_CS GPIO_NUM_46
|
||||
#define SD_MOUNT_POINT "/sdcard"
|
||||
#define SD_SPI_HOST SPI2_HOST
|
||||
|
||||
// 震动马达
|
||||
#define VIBRATING_MOTOR_PIN GPIO_NUM_3
|
||||
|
||||
// 屏幕
|
||||
#define DISPLAY_SPI_HOST SPI3_HOST
|
||||
#define DISPLAY_SDA GPIO_NUM_10
|
||||
#define DISPLAY_SCL GPIO_NUM_9
|
||||
#define DISPLAY_DC GPIO_NUM_8
|
||||
#define DISPLAY_CS GPIO_NUM_14
|
||||
#define DISPLAY_RES GPIO_NUM_18
|
||||
#define DISPLAY_WIDTH 240
|
||||
#define DISPLAY_HEIGHT 240
|
||||
#define DISPLAY_SWAP_XY false
|
||||
#define DISPLAY_MIRROR_X false
|
||||
#define DISPLAY_MIRROR_Y false
|
||||
#define BACKLIGHT_INVERT false
|
||||
#define DISPLAY_OFFSET_X 0
|
||||
#define DISPLAY_OFFSET_Y 0
|
||||
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_13
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
|
||||
|
||||
// 4g
|
||||
#define ML307_RX_PIN GPIO_NUM_11
|
||||
#define ML307_TX_PIN GPIO_NUM_12
|
||||
|
||||
// 电源管理
|
||||
#define POWER_USB_IN GPIO_NUM_1
|
||||
#define Power_Control GPIO_NUM_47 // 电源控制引脚
|
||||
#define Power_Dec GPIO_NUM_48 // 电源键检测引脚
|
||||
#define POWER_CBS_ADC_UNIT ADC_UNIT_1 // adc检测公共unit GPIO1
|
||||
#define POWER_USBIN_ADC_CHANNEL ADC_CHANNEL_0 // 检测usb是否插入 GPIO1
|
||||
#define POWER_BATTERY_ADC_CHANNEL ADC_CHANNEL_6 // 电池电量检测 GPIO7
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
10
main/boards/xingzhi-abs-2.0/config.json
Normal file
10
main/boards/xingzhi-abs-2.0/config.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"target": "esp32s3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "xingzhi-abs-2.0",
|
||||
"sdkconfig_append": [
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
94
main/boards/xingzhi-abs-2.0/customlcddisplay.h
Normal file
94
main/boards/xingzhi-abs-2.0/customlcddisplay.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#ifndef _CUSTOMLCDDISPLAY_H_
|
||||
#define _CUSTOMLCDDISPLAY_H_
|
||||
|
||||
#include "display/lcd_display.h"
|
||||
#include "lvgl_theme.h"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_16_4);
|
||||
LV_FONT_DECLARE(font_awesome_16_4);
|
||||
|
||||
class CustomLcdDisplay : public SpiLcdDisplay {
|
||||
private:
|
||||
lv_timer_t* init_check_timer_ = nullptr;
|
||||
bool init_custom_display_ = false;
|
||||
|
||||
bool CreateCustomLcdDisplayImpl() {
|
||||
DisplayLockGuard lock(this);
|
||||
LvglTheme* lvgl_theme = static_cast<LvglTheme*>(current_theme_);
|
||||
if (lvgl_theme == nullptr) {
|
||||
ESP_LOGE("CustomLcdDisplay", "lvgl_theme is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto text_font = lvgl_theme->text_font()->font();
|
||||
auto icon_font = lvgl_theme->icon_font()->font();
|
||||
|
||||
if (emoji_box_ == nullptr) {
|
||||
ESP_LOGE("CustomLcdDisplay", "emoji_box_ is null, skip align");
|
||||
return false;
|
||||
}
|
||||
if (bottom_bar_ == nullptr) {
|
||||
ESP_LOGE("CustomLcdDisplay", "bottom_bar_ is null, skip align");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto screen = lv_screen_active();
|
||||
lv_obj_set_scrollbar_mode(screen, LV_SCROLLBAR_MODE_OFF);
|
||||
lv_obj_align(emoji_box_, LV_ALIGN_CENTER, 0, -5);
|
||||
|
||||
lv_obj_set_size(bottom_bar_, LV_HOR_RES, text_font->line_height + lvgl_theme->spacing(8)+8);
|
||||
lv_obj_align(bottom_bar_, LV_ALIGN_BOTTOM_MID, 0, -5);
|
||||
ESP_LOGI("CustomLcdDisplay", "Custom UI initialized successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
static void InitCheckTimerCb(lv_timer_t* timer) {
|
||||
CustomLcdDisplay* self = static_cast<CustomLcdDisplay*>(lv_timer_get_user_data(timer));
|
||||
if (self == nullptr) {
|
||||
lv_timer_del(timer);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->IsSetupUICalled() && !self->init_custom_display_) {
|
||||
if (self->CreateCustomLcdDisplayImpl()) {
|
||||
self->init_custom_display_ = true;
|
||||
lv_timer_del(timer);
|
||||
self->init_check_timer_ = nullptr;
|
||||
ESP_LOGI("CustomLcdDisplay", "Init timer stopped, custom UI ready");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
|
||||
esp_lcd_panel_handle_t panel_handle,
|
||||
int width,
|
||||
int height,
|
||||
int offset_x,
|
||||
int offset_y,
|
||||
bool mirror_x,
|
||||
bool mirror_y,
|
||||
bool swap_xy)
|
||||
: SpiLcdDisplay(io_handle, panel_handle,
|
||||
width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy) {
|
||||
init_check_timer_ = lv_timer_create(InitCheckTimerCb, 50, this);
|
||||
if (init_check_timer_ == nullptr) {
|
||||
ESP_LOGE("CustomLcdDisplay", "Failed to create init check timer");
|
||||
} else {
|
||||
ESP_LOGI("CustomLcdDisplay", "Init check timer created (50ms interval)");
|
||||
}
|
||||
}
|
||||
|
||||
~CustomLcdDisplay() override {
|
||||
if (init_check_timer_ != nullptr) {
|
||||
lv_timer_del(init_check_timer_);
|
||||
init_check_timer_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsSetupUICalled() const {
|
||||
return setup_ui_called_;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
305
main/boards/xingzhi-abs-2.0/power_manager.h
Normal file
305
main/boards/xingzhi-abs-2.0/power_manager.h
Normal file
@@ -0,0 +1,305 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <esp_timer.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_adc/adc_oneshot.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "button.h"
|
||||
#include "board.h"
|
||||
#include "config.h"
|
||||
#include "assets/lang_config.h"
|
||||
#include <esp_sleep.h>
|
||||
|
||||
class PowerManager {
|
||||
private:
|
||||
esp_timer_handle_t timer_handle_;
|
||||
esp_timer_handle_t power_timer_handle_;
|
||||
std::function<void(bool)> on_charging_status_changed_;
|
||||
std::function<void(bool)> on_low_battery_status_changed_;
|
||||
|
||||
gpio_num_t charging_pin_ = GPIO_NUM_NC;
|
||||
std::vector<uint16_t> adc_values_;
|
||||
uint32_t battery_level_ = 30;
|
||||
bool is_charging_ = false;
|
||||
bool is_low_battery_ = false;
|
||||
int ticks_ = 0;
|
||||
adc_oneshot_unit_handle_t adc_handle_;
|
||||
const int kBatteryAdcInterval = 60;
|
||||
const int kBatteryAdcDataCount = 3;
|
||||
const int kLowBatteryLevel = 20;
|
||||
|
||||
// power
|
||||
bool pressed = false; // 是否按下电源键
|
||||
int PowerControl_ticks_ = 0; // 开机时长
|
||||
int press_ticks_ = 0; // 按下时的tick
|
||||
bool is_first_boot = true; //
|
||||
uint8_t PowerDec_level_ = 0; // 电源键电平
|
||||
const int power_off_ticks_ = 20; // 按键按下X/5秒准备关机;
|
||||
bool new_charging_status = false; // 插入usb时无法软件关机
|
||||
bool is_shutting_down_ = false; // 标记是否进入关机流程
|
||||
int shutdown_delay_ticks_ = 0; // 关机延迟计数
|
||||
uint8_t shutdown_ticks = 5; // 5/5s后关机 预留的关机操作
|
||||
bool shutdown_first_ = true;
|
||||
|
||||
// 软件关机逻辑
|
||||
void PowrSwitch() {
|
||||
PowerDec_level_ = gpio_get_level(Power_Dec);
|
||||
|
||||
// 确保开机后电源键松开再检测关机
|
||||
if (PowerDec_level_ == 1) {
|
||||
is_first_boot = false;
|
||||
}
|
||||
|
||||
if (is_shutting_down_) {
|
||||
shutdown_delay_ticks_++;
|
||||
if (shutdown_delay_ticks_ >= shutdown_ticks) {
|
||||
shutdown();
|
||||
is_shutting_down_ = false;
|
||||
shutdown_delay_ticks_ = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_first_boot) {
|
||||
PowerControl_ticks_++;
|
||||
if (PowerDec_level_ == 0 && pressed == false) {
|
||||
press_ticks_ = PowerControl_ticks_;
|
||||
pressed = true;
|
||||
}
|
||||
if ((press_ticks_ != 0) &&( PowerControl_ticks_ - press_ticks_ == power_off_ticks_ )&& (!new_charging_status)) {
|
||||
if (timer_handle_) {
|
||||
esp_timer_stop(timer_handle_);
|
||||
esp_timer_delete(timer_handle_);
|
||||
}
|
||||
is_shutting_down_ = true;
|
||||
shutdown_delay_ticks_ = 0;
|
||||
}
|
||||
if (PowerDec_level_ == 1 && press_ticks_!= 0) {
|
||||
PowerDec_level_ = gpio_get_level(Power_Dec);
|
||||
if (PowerDec_level_ == 1) {
|
||||
pressed = false;
|
||||
press_ticks_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CheckBatteryStatus() {
|
||||
int usb_usb_adc_value0;
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, POWER_USBIN_ADC_CHANNEL, &usb_usb_adc_value0));
|
||||
new_charging_status = (1500 < usb_usb_adc_value0 && usb_usb_adc_value0 < 4000);
|
||||
// ESP_LOGE("powercontrol", "USB ADC VALUE 1: %d", usb_usb_adc_value0);
|
||||
|
||||
if (new_charging_status != is_charging_) {
|
||||
ReadBatteryAdcData();
|
||||
is_charging_ = new_charging_status;
|
||||
if (on_charging_status_changed_) {
|
||||
on_charging_status_changed_(is_charging_);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果电池电量数据不足,则读取电池电量数据
|
||||
if (adc_values_.size() < kBatteryAdcDataCount) {
|
||||
ReadBatteryAdcData();
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果电池电量数据充足,则每 kBatteryAdcInterval 个 tick 读取一次电池电量数据
|
||||
ticks_++;
|
||||
if (ticks_ % kBatteryAdcInterval == 0) {
|
||||
ReadBatteryAdcData();
|
||||
}
|
||||
}
|
||||
|
||||
void ReadBatteryAdcData() {
|
||||
int adc_value;
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, POWER_BATTERY_ADC_CHANNEL, &adc_value));
|
||||
|
||||
// 将 ADC 值添加到队列中
|
||||
adc_values_.push_back(adc_value);
|
||||
if (adc_values_.size() > kBatteryAdcDataCount) {
|
||||
adc_values_.erase(adc_values_.begin());
|
||||
}
|
||||
uint32_t average_adc = 0;
|
||||
for (auto value : adc_values_) {
|
||||
average_adc += value;
|
||||
}
|
||||
average_adc /= adc_values_.size();
|
||||
|
||||
// 定义电池电量区间
|
||||
const struct {
|
||||
uint16_t adc;
|
||||
uint8_t level;
|
||||
} levels[] = {
|
||||
{1970, 0},
|
||||
{2062, 20},
|
||||
{2154, 40},
|
||||
{2246, 60},
|
||||
{2338, 80},
|
||||
{2430, 100}
|
||||
};
|
||||
|
||||
// 低于最低值时
|
||||
if (average_adc < levels[0].adc) {
|
||||
battery_level_ = 0;
|
||||
}
|
||||
// 高于最高值时
|
||||
else if (average_adc >= levels[5].adc) {
|
||||
battery_level_ = 100;
|
||||
} else {
|
||||
// 线性插值计算中间值
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (average_adc >= levels[i].adc && average_adc < levels[i+1].adc) {
|
||||
float ratio = static_cast<float>(average_adc - levels[i].adc) / (levels[i+1].adc - levels[i].adc);
|
||||
battery_level_ = levels[i].level + ratio * (levels[i+1].level - levels[i].level);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check low battery status
|
||||
if (adc_values_.size() >= kBatteryAdcDataCount) {
|
||||
bool new_low_battery_status = battery_level_ <= kLowBatteryLevel;
|
||||
if (new_low_battery_status != is_low_battery_) {
|
||||
is_low_battery_ = new_low_battery_status;
|
||||
if (on_low_battery_status_changed_) {
|
||||
on_low_battery_status_changed_(is_low_battery_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI("PowerManager", "ADC value: %d average: %ld level: %ld", adc_value, average_adc, battery_level_);
|
||||
}
|
||||
|
||||
public:
|
||||
PowerManager(gpio_num_t pin) : charging_pin_(pin) {
|
||||
// 初始化电源键检测引脚
|
||||
gpio_config_t powerdecgpio_conf = {};
|
||||
powerdecgpio_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
powerdecgpio_conf.mode = GPIO_MODE_INPUT;
|
||||
powerdecgpio_conf.pin_bit_mask = (1ULL << Power_Dec);
|
||||
powerdecgpio_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
powerdecgpio_conf.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
gpio_config(&powerdecgpio_conf);
|
||||
|
||||
// 初始化电源控制引脚
|
||||
gpio_config_t powercontgpio_conf = {};
|
||||
powercontgpio_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
powercontgpio_conf.mode = GPIO_MODE_OUTPUT;
|
||||
powercontgpio_conf.pin_bit_mask = (1ULL << Power_Control);
|
||||
powercontgpio_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
|
||||
powercontgpio_conf.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
gpio_config(&powercontgpio_conf);
|
||||
gpio_set_level(Power_Control, 1);
|
||||
ESP_LOGI("powercontrol", "turnded on ...");
|
||||
|
||||
// 创建电源控制检查定时器
|
||||
esp_timer_create_args_t power_timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
PowerManager* self = static_cast<PowerManager*>(arg);
|
||||
self->PowrSwitch();
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "power_cotrol_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&power_timer_args, &power_timer_handle_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(power_timer_handle_, 200000));
|
||||
// 初始化充电引脚
|
||||
gpio_config_t io_conf = {};
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << charging_pin_);
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
// 创建电池电量检查定时器
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
PowerManager* self = static_cast<PowerManager*>(arg);
|
||||
self->CheckBatteryStatus();
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "battery_check_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
|
||||
|
||||
adc_oneshot_unit_init_cfg_t init_config = {
|
||||
.unit_id = POWER_CBS_ADC_UNIT,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_));
|
||||
|
||||
adc_oneshot_chan_cfg_t chan_config = {
|
||||
.atten = ADC_ATTEN_DB_12,
|
||||
.bitwidth = ADC_BITWIDTH_12,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, POWER_BATTERY_ADC_CHANNEL, &chan_config)); // 电池电量
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, POWER_USBIN_ADC_CHANNEL, &chan_config)); // usb
|
||||
}
|
||||
|
||||
~PowerManager() {
|
||||
if (timer_handle_) {
|
||||
esp_timer_stop(timer_handle_);
|
||||
esp_timer_delete(timer_handle_);
|
||||
}
|
||||
if (power_timer_handle_) {
|
||||
esp_timer_stop(power_timer_handle_);
|
||||
esp_timer_delete(power_timer_handle_);
|
||||
}
|
||||
if (adc_handle_ != nullptr) {
|
||||
adc_oneshot_del_unit(adc_handle_);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsCharging() {
|
||||
// 如果电量已经满了,则不再显示充电中
|
||||
if (battery_level_ == 100) {
|
||||
return false;
|
||||
}
|
||||
return is_charging_;
|
||||
}
|
||||
|
||||
bool IsDischarging() {
|
||||
// 没有区分充电和放电,所以直接返回相反状态
|
||||
return !is_charging_;
|
||||
}
|
||||
|
||||
uint8_t GetBatteryLevel() {
|
||||
return battery_level_;
|
||||
}
|
||||
|
||||
void OnLowBatteryStatusChanged(std::function<void(bool)> callback) {
|
||||
on_low_battery_status_changed_ = callback;
|
||||
}
|
||||
|
||||
void OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||
on_charging_status_changed_ = callback;
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
if (!new_charging_status && shutdown_first_) {
|
||||
shutdown_first_ = false;
|
||||
gpio_set_level(DISPLAY_BACKLIGHT_PIN, 0);
|
||||
for (int i=1;i<15;i++) {
|
||||
gpio_set_level(Power_Control, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(Power_Control, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
ESP_LOGI("PowerManager","触发开关机控制");
|
||||
}
|
||||
ESP_LOGI("PowerManager","关机失败,进入深睡眠");
|
||||
esp_deep_sleep_start();
|
||||
} else {
|
||||
ESP_LOGI("PowerManager","检测到插入usb,无法关机");
|
||||
}
|
||||
}
|
||||
};
|
||||
389
main/boards/xingzhi-abs-2.0/xingzhi-abs-2.0.cc
Normal file
389
main/boards/xingzhi-abs-2.0/xingzhi-abs-2.0.cc
Normal file
@@ -0,0 +1,389 @@
|
||||
#include "dual_network_board.h"
|
||||
#include "codecs/es8311_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "system_reset.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "i2c_device.h"
|
||||
#include <esp_log.h>
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include "power_manager.h"
|
||||
#include "power_save_timer.h"
|
||||
#include "led/single_led.h"
|
||||
#include "assets/lang_config.h"
|
||||
#include <driver/rtc_io.h>
|
||||
#include <esp_sleep.h>
|
||||
#include "esp_private/sdmmc_common.h"
|
||||
#include <esp_vfs_fat.h>
|
||||
#include <driver/sdspi_host.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "customlcddisplay.h"
|
||||
|
||||
#define TAG "XINGZHI_ABS_2_0"
|
||||
|
||||
typedef enum {
|
||||
VIBRATE_EVENT_TRIGGER = 0,
|
||||
VIBRATE_EVENT_NONE
|
||||
} VibrateEvent_t;
|
||||
|
||||
class XINGZHI_ABS_2_0 : public DualNetworkBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
Button boot_button_;
|
||||
Button volume_up_button_;
|
||||
Button volume_down_button_;
|
||||
// SpiLcdDisplay* display_;
|
||||
CustomLcdDisplay* display_;
|
||||
PowerSaveTimer* power_save_timer_;
|
||||
PowerManager* power_manager_;
|
||||
esp_lcd_panel_io_handle_t panel_io_ = nullptr;
|
||||
esp_lcd_panel_handle_t panel_ = nullptr;
|
||||
esp_err_t err;
|
||||
bool is_sdcard_found = false;
|
||||
QueueHandle_t vibrate_event_queue_;
|
||||
TaskHandle_t vibrate_task_handle_;
|
||||
|
||||
void VibrateMotor(uint32_t duration_ms) {
|
||||
ESP_LOGI(TAG, "Vibrate Motor");
|
||||
gpio_set_level(VIBRATING_MOTOR_PIN, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(duration_ms));
|
||||
gpio_set_level(VIBRATING_MOTOR_PIN, 0);
|
||||
}
|
||||
|
||||
static void VibrateTask(void* param) {
|
||||
XINGZHI_ABS_2_0* board = static_cast<XINGZHI_ABS_2_0*>(param);
|
||||
VibrateEvent_t event;
|
||||
|
||||
while (1) {
|
||||
if (xQueueReceive(board->vibrate_event_queue_, &event, portMAX_DELAY)) {
|
||||
if (event == VIBRATE_EVENT_TRIGGER) {
|
||||
board->VibrateMotor(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TriggerVibrateEvent() {
|
||||
VibrateEvent_t event = VIBRATE_EVENT_TRIGGER;
|
||||
xQueueSend(vibrate_event_queue_, &event, 0);
|
||||
}
|
||||
|
||||
void InitializePowerManager() {
|
||||
power_manager_ = new PowerManager(POWER_USB_IN);//USB是否插入
|
||||
power_manager_->OnChargingStatusChanged([this](bool is_charging) {
|
||||
if (is_charging) {
|
||||
power_save_timer_->SetEnabled(false);
|
||||
} else {
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void InitializePowerSaveTimer() {
|
||||
power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
|
||||
power_save_timer_->OnEnterSleepMode([this]() {
|
||||
ESP_LOGI(TAG, "Enabling sleep mode");
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("sleepy");
|
||||
GetBacklight()->SetBrightness(1);
|
||||
});
|
||||
power_save_timer_->OnExitSleepMode([this]() {
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("neutral");
|
||||
GetBacklight()->RestoreBrightness();
|
||||
});
|
||||
power_save_timer_->OnShutdownRequest([this]() {
|
||||
ESP_LOGI(TAG, "Shutting down");
|
||||
power_manager_->shutdown();
|
||||
});
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
|
||||
.scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.intr_priority = 0,
|
||||
.trans_queue_depth = 0,
|
||||
.flags = {
|
||||
.enable_internal_pullup = 1,
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
||||
for (uint8_t addr = 1; addr < 127; addr++) {
|
||||
err = i2c_master_probe(i2c_bus_, addr, 100);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Device found at address 0x%02X", addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeSpi() {
|
||||
spi_bus_config_t buscfg = {};
|
||||
buscfg.mosi_io_num = DISPLAY_SDA;
|
||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||
buscfg.sclk_io_num = DISPLAY_SCL;
|
||||
buscfg.quadwp_io_num = GPIO_NUM_NC;
|
||||
buscfg.quadhd_io_num = GPIO_NUM_NC;
|
||||
buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(DISPLAY_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||||
}
|
||||
|
||||
void InitializeSt7789Display() {
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
io_config.cs_gpio_num = DISPLAY_CS;
|
||||
io_config.dc_gpio_num = DISPLAY_DC;
|
||||
io_config.spi_mode = 3;
|
||||
io_config.pclk_hz = 80 * 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(DISPLAY_SPI_HOST, &io_config, &panel_io_));
|
||||
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
esp_lcd_panel_dev_config_t panel_config = {};
|
||||
panel_config.reset_gpio_num = DISPLAY_RES;
|
||||
panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
|
||||
panel_config.bits_per_pixel = 16;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io_, &panel_config, &panel_));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_, DISPLAY_SWAP_XY));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_, true));
|
||||
|
||||
display_ = new CustomLcdDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y,
|
||||
DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
|
||||
// display_ = new SpiLcdDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y,
|
||||
// DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
|
||||
}
|
||||
|
||||
void InitializeSDcardSpi() {
|
||||
spi_bus_config_t bus_cnf = {
|
||||
.mosi_io_num = SD_CMD,
|
||||
.miso_io_num = SD_DATA0,
|
||||
.sclk_io_num = SD_CLK,
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
.max_transfer_sz = 400000,
|
||||
};
|
||||
|
||||
esp_err_t err = spi_bus_initialize(SD_SPI_HOST, &bus_cnf, SPI_DMA_CH_AUTO);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SPI总线初始化失败: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
static sdspi_device_config_t slot_cnf = {
|
||||
.host_id = SD_SPI_HOST,
|
||||
.gpio_cs = SD_CS,
|
||||
.gpio_cd = SDSPI_SLOT_NO_CD,
|
||||
.gpio_wp = GPIO_NUM_NC,
|
||||
.gpio_int = GPIO_NUM_NC,
|
||||
};
|
||||
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_cnf = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 16 * 1024,
|
||||
};
|
||||
|
||||
sdmmc_card_t* card = NULL;
|
||||
|
||||
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
||||
err = esp_vfs_fat_sdspi_mount(SD_MOUNT_POINT, &host, &slot_cnf, &mount_cnf, &card);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SD卡挂载失败: %s", esp_err_to_name(err));
|
||||
is_sdcard_found = false;
|
||||
return;
|
||||
} else if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "SD卡挂载成功");
|
||||
is_sdcard_found = true;
|
||||
}
|
||||
// sdmmc_card_print_info(stdout, card); // 打印SD卡信息
|
||||
}
|
||||
|
||||
void InitializePhysicalButtons() {
|
||||
boot_button_.OnClick([this]() {
|
||||
this->TriggerVibrateEvent();
|
||||
power_save_timer_->WakeUp();
|
||||
auto& app = Application::GetInstance();
|
||||
if (GetNetworkType() == NetworkType::WIFI) {
|
||||
if (app.GetDeviceState() == kDeviceStateStarting) {
|
||||
// cast to WifiBoard
|
||||
auto& wifi_board = static_cast<WifiBoard&>(GetCurrentBoard());
|
||||
wifi_board.EnterWifiConfigMode();
|
||||
return;
|
||||
}
|
||||
}
|
||||
app.ToggleChatState();
|
||||
});
|
||||
boot_button_.OnDoubleClick([this]() {
|
||||
this->TriggerVibrateEvent();
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting || app.GetDeviceState() == kDeviceStateWifiConfiguring) {
|
||||
SwitchNetworkType();
|
||||
}
|
||||
});
|
||||
|
||||
boot_button_.OnMultipleClick(
|
||||
[this]() {
|
||||
ESP_LOGI(TAG, "Button OnFiveClick");
|
||||
this->TriggerVibrateEvent();
|
||||
if (is_sdcard_found) {
|
||||
display_->SetChatMessage("system", "开机检测到SD挂载成功");
|
||||
} else {
|
||||
display_->SetChatMessage("system", "开机检测到SD挂载失败");
|
||||
}
|
||||
}, 5);
|
||||
|
||||
volume_up_button_.OnClick([this]() {
|
||||
this->TriggerVibrateEvent();
|
||||
power_save_timer_->WakeUp();
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() + 10;
|
||||
if (volume > 100) {
|
||||
volume = 100;
|
||||
}
|
||||
codec->SetOutputVolume(volume);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
|
||||
});
|
||||
|
||||
volume_up_button_.OnLongPress([this]() {
|
||||
this->TriggerVibrateEvent();
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(100);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
||||
});
|
||||
|
||||
volume_down_button_.OnClick([this]() {
|
||||
this->TriggerVibrateEvent();
|
||||
power_save_timer_->WakeUp();
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() - 10;
|
||||
if (volume < 0) {
|
||||
volume = 0;
|
||||
}
|
||||
codec->SetOutputVolume(volume);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
|
||||
});
|
||||
|
||||
volume_down_button_.OnLongPress([this]() {
|
||||
this->TriggerVibrateEvent();
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(0);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeGpio() {
|
||||
gpio_config_t zhengdong = {};
|
||||
zhengdong.intr_type = GPIO_INTR_DISABLE;
|
||||
zhengdong.mode = GPIO_MODE_OUTPUT;
|
||||
zhengdong.pin_bit_mask = (1ULL << VIBRATING_MOTOR_PIN);
|
||||
zhengdong.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
zhengdong.pull_up_en = GPIO_PULLUP_DISABLE;
|
||||
gpio_config(&zhengdong);
|
||||
gpio_set_level(VIBRATING_MOTOR_PIN, 0);
|
||||
}
|
||||
|
||||
void InitializeVibrateTask() {
|
||||
vibrate_event_queue_ = xQueueCreate(10, sizeof(VibrateEvent_t));
|
||||
if (vibrate_event_queue_ == nullptr) {
|
||||
ESP_LOGE(TAG, "创建振动事件队列失败");
|
||||
return;
|
||||
}
|
||||
|
||||
BaseType_t ret = xTaskCreate(
|
||||
VibrateTask,
|
||||
"vibrate_task",
|
||||
2048,
|
||||
this,
|
||||
tskIDLE_PRIORITY + 1,
|
||||
&vibrate_task_handle_
|
||||
);
|
||||
|
||||
if (ret != pdPASS) {
|
||||
ESP_LOGE(TAG, "创建振动任务失败");
|
||||
} else {
|
||||
ESP_LOGI(TAG, "振动任务初始化成功");
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
XINGZHI_ABS_2_0() :
|
||||
DualNetworkBoard(ML307_TX_PIN, ML307_RX_PIN),
|
||||
boot_button_(BOOT_BUTTON_GPIO),
|
||||
volume_up_button_(VOLUME_UP_BUTTON_GPIO),
|
||||
volume_down_button_(VOLUME_DOWN_BUTTON_GPIO),
|
||||
vibrate_event_queue_(nullptr),
|
||||
vibrate_task_handle_(nullptr) {
|
||||
InitializeGpio();
|
||||
InitializePowerManager();
|
||||
InitializePowerSaveTimer();
|
||||
InitializeI2c();
|
||||
InitializeSpi();
|
||||
InitializeSt7789Display();
|
||||
InitializePhysicalButtons();
|
||||
InitializeSDcardSpi();
|
||||
InitializeVibrateTask();
|
||||
GetBacklight()->RestoreBrightness();
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static Es8311AudioCodec audio_codec(
|
||||
i2c_bus_,
|
||||
I2C_NUM_0,
|
||||
AUDIO_INPUT_SAMPLE_RATE,
|
||||
AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
AUDIO_I2S_GPIO_MCLK,
|
||||
AUDIO_I2S_GPIO_BCLK,
|
||||
AUDIO_I2S_GPIO_WS,
|
||||
AUDIO_I2S_GPIO_DOUT,
|
||||
AUDIO_I2S_GPIO_DIN,
|
||||
AUDIO_CODEC_I2C_PA_EN,
|
||||
AUDIO_CODEC_ES8311_ADDR,
|
||||
AUDIO_INPUT_REFERENCE);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
virtual Display* GetDisplay() override {
|
||||
return display_;
|
||||
}
|
||||
|
||||
virtual Backlight* GetBacklight() override {
|
||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
return &backlight;
|
||||
}
|
||||
|
||||
virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
|
||||
static bool last_discharging = false;
|
||||
charging = power_manager_->IsCharging();
|
||||
discharging = power_manager_->IsDischarging();
|
||||
if (discharging != last_discharging) {
|
||||
power_save_timer_->SetEnabled(discharging);
|
||||
last_discharging = discharging;
|
||||
}
|
||||
level = power_manager_->GetBatteryLevel();
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void SetPowerSaveLevel(PowerSaveLevel level) override {
|
||||
if (level != PowerSaveLevel::LOW_POWER) {
|
||||
power_save_timer_->WakeUp();
|
||||
}
|
||||
DualNetworkBoard::SetPowerSaveLevel(level);
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(XINGZHI_ABS_2_0);
|
||||
Reference in New Issue
Block a user