forked from xiaozhi/xiaozhi-esp32
graceful code
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
#include "audio_codecs/cores3_audio_codec.h"
|
#include "audio_codecs/cores3_audio_codec.h"
|
||||||
#include "display/lcd_display.h"
|
#include "display/lcd_display.h"
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
#include "button.h"
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "i2c_device.h"
|
#include "i2c_device.h"
|
||||||
#include "iot/thing_manager.h"
|
#include "iot/thing_manager.h"
|
||||||
@@ -120,7 +119,6 @@ private:
|
|||||||
Aw9523* aw9523_;
|
Aw9523* aw9523_;
|
||||||
Ft6336* ft6336_;
|
Ft6336* ft6336_;
|
||||||
LcdDisplay* display_;
|
LcdDisplay* display_;
|
||||||
Button boot_button_;
|
|
||||||
esp_timer_handle_t touchpad_timer_;
|
esp_timer_handle_t touchpad_timer_;
|
||||||
|
|
||||||
void InitializeI2c() {
|
void InitializeI2c() {
|
||||||
@@ -273,16 +271,6 @@ private:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeButtons() {
|
|
||||||
boot_button_.OnClick([this]() {
|
|
||||||
auto& app = Application::GetInstance();
|
|
||||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
|
||||||
ResetWifiConfiguration();
|
|
||||||
}
|
|
||||||
app.ToggleChatState();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 物联网初始化,添加对 AI 可见设备
|
// 物联网初始化,添加对 AI 可见设备
|
||||||
void InitializeIot() {
|
void InitializeIot() {
|
||||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||||
@@ -290,14 +278,13 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
M5StackCoreS3Board() : boot_button_(GPIO_NUM_1) {
|
M5StackCoreS3Board() {
|
||||||
InitializeI2c();
|
InitializeI2c();
|
||||||
InitializeAxp2101();
|
InitializeAxp2101();
|
||||||
InitializeAw9523();
|
InitializeAw9523();
|
||||||
I2cDetect();
|
I2cDetect();
|
||||||
InitializeSpi();
|
InitializeSpi();
|
||||||
InitializeIli9342Display();
|
InitializeIli9342Display();
|
||||||
InitializeButtons();
|
|
||||||
InitializeIot();
|
InitializeIot();
|
||||||
InitializeFt6336TouchPad();
|
InitializeFt6336TouchPad();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,8 @@
|
|||||||
#include <wifi_station.h>
|
#include <wifi_station.h>
|
||||||
#include <esp_lcd_panel_io.h>
|
#include <esp_lcd_panel_io.h>
|
||||||
#include <esp_lcd_panel_ops.h>
|
#include <esp_lcd_panel_ops.h>
|
||||||
#include "esp_lcd_st77916.h"
|
#include <esp_lcd_st77916.h>
|
||||||
|
#include <esp_timer.h>
|
||||||
|
|
||||||
#define TAG "TaijiPiS3Board"
|
#define TAG "TaijiPiS3Board"
|
||||||
|
|
||||||
@@ -58,6 +59,7 @@ private:
|
|||||||
i2c_master_bus_handle_t i2c_bus_;
|
i2c_master_bus_handle_t i2c_bus_;
|
||||||
Cst816s* cst816s_;
|
Cst816s* cst816s_;
|
||||||
LcdDisplay* display_;
|
LcdDisplay* display_;
|
||||||
|
esp_timer_handle_t touchpad_timer_;
|
||||||
|
|
||||||
void InitializeI2c() {
|
void InitializeI2c() {
|
||||||
// Initialize I2C peripheral
|
// Initialize I2C peripheral
|
||||||
@@ -76,33 +78,53 @@ private:
|
|||||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void touchpad_daemon(void* param) {
|
static void touchpad_timer_callback(void* arg) {
|
||||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
|
||||||
auto& board = (TaijiPiS3Board&)Board::GetInstance();
|
auto& board = (TaijiPiS3Board&)Board::GetInstance();
|
||||||
auto touchpad = board.GetTouchpad();
|
auto touchpad = board.GetTouchpad();
|
||||||
bool was_touched = false;
|
static bool was_touched = false;
|
||||||
while (1) {
|
static int64_t touch_start_time = 0;
|
||||||
touchpad->UpdateTouchPoint();
|
const int64_t TOUCH_THRESHOLD_MS = 500; // 触摸时长阈值,超过500ms视为长按
|
||||||
if (touchpad->GetTouchPoint().num > 0) {
|
|
||||||
// On press
|
touchpad->UpdateTouchPoint();
|
||||||
if (!was_touched) {
|
auto touch_point = touchpad->GetTouchPoint();
|
||||||
was_touched = true;
|
|
||||||
Application::GetInstance().ToggleChatState();
|
// 检测触摸开始
|
||||||
}
|
if (touch_point.num > 0 && !was_touched) {
|
||||||
}
|
was_touched = true;
|
||||||
// On release
|
touch_start_time = esp_timer_get_time() / 1000; // 转换为毫秒
|
||||||
else if (was_touched) {
|
}
|
||||||
was_touched = false;
|
// 检测触摸释放
|
||||||
}
|
else if (touch_point.num == 0 && was_touched) {
|
||||||
vTaskDelay(pdMS_TO_TICKS(50));
|
was_touched = false;
|
||||||
|
int64_t touch_duration = (esp_timer_get_time() / 1000) - touch_start_time;
|
||||||
|
|
||||||
|
// 只有短触才触发
|
||||||
|
if (touch_duration < TOUCH_THRESHOLD_MS) {
|
||||||
|
auto& app = Application::GetInstance();
|
||||||
|
if (app.GetDeviceState() == kDeviceStateStarting &&
|
||||||
|
!WifiStation::GetInstance().IsConnected()) {
|
||||||
|
board.ResetWifiConfiguration();
|
||||||
|
}
|
||||||
|
app.ToggleChatState();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeCst816sTouchPad() {
|
void InitializeCst816sTouchPad() {
|
||||||
ESP_LOGI(TAG, "Init Cst816s");
|
ESP_LOGI(TAG, "Init Cst816s");
|
||||||
cst816s_ = new Cst816s(i2c_bus_, 0x15);
|
cst816s_ = new Cst816s(i2c_bus_, 0x15);
|
||||||
xTaskCreate(touchpad_daemon, "tp", 2048, NULL, 5, NULL);
|
|
||||||
|
// 创建定时器,10ms 间隔
|
||||||
|
esp_timer_create_args_t timer_args = {
|
||||||
|
.callback = touchpad_timer_callback,
|
||||||
|
.arg = NULL,
|
||||||
|
.dispatch_method = ESP_TIMER_TASK,
|
||||||
|
.name = "touchpad_timer",
|
||||||
|
.skip_unhandled_events = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &touchpad_timer_));
|
||||||
|
ESP_ERROR_CHECK(esp_timer_start_periodic(touchpad_timer_, 10 * 1000)); // 10ms = 10000us
|
||||||
}
|
}
|
||||||
|
|
||||||
void BspLcdBlSet(int brightness_percent)
|
void BspLcdBlSet(int brightness_percent)
|
||||||
@@ -195,24 +217,23 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtual AudioCodec* GetAudioCodec() override {
|
virtual AudioCodec* GetAudioCodec() override {
|
||||||
static NoAudioCodec* audio_codec = nullptr;
|
static NoAudioCodecSimplex audio_codec(
|
||||||
if (audio_codec == nullptr) {
|
AUDIO_INPUT_SAMPLE_RATE,
|
||||||
audio_codec = new NoAudioCodecSimplex(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
|
AUDIO_OUTPUT_SAMPLE_RATE,
|
||||||
AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT,
|
AUDIO_I2S_GPIO_BCLK,
|
||||||
AUDIO_MIC_SCK_PIN, AUDIO_MIC_WS_PIN, AUDIO_MIC_SD_PIN);
|
AUDIO_I2S_GPIO_WS,
|
||||||
}
|
AUDIO_I2S_GPIO_DOUT,
|
||||||
return audio_codec;
|
AUDIO_MIC_SCK_PIN,
|
||||||
|
AUDIO_MIC_WS_PIN,
|
||||||
|
AUDIO_MIC_SD_PIN
|
||||||
|
);
|
||||||
|
return &audio_codec;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Display* GetDisplay() override {
|
virtual Display* GetDisplay() override {
|
||||||
return display_;
|
return display_;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool GetBatteryLevel(int &level, bool& charging) override {
|
|
||||||
level = 100; charging = 1;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Cst816s* GetTouchpad() {
|
Cst816s* GetTouchpad() {
|
||||||
return cst816s_;
|
return cst816s_;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user