forked from xiaozhi/xiaozhi-esp32
ajust code style
This commit is contained in:
@@ -11,7 +11,7 @@ static const char TAG[] = "Tcircles3AudioCodec";
|
|||||||
Tcircles3AudioCodec::Tcircles3AudioCodec(int input_sample_rate, int output_sample_rate,
|
Tcircles3AudioCodec::Tcircles3AudioCodec(int input_sample_rate, int output_sample_rate,
|
||||||
gpio_num_t mic_bclk, gpio_num_t mic_ws, gpio_num_t mic_data,
|
gpio_num_t mic_bclk, gpio_num_t mic_ws, gpio_num_t mic_data,
|
||||||
gpio_num_t spkr_bclk, gpio_num_t spkr_lrclk, gpio_num_t spkr_data,
|
gpio_num_t spkr_bclk, gpio_num_t spkr_lrclk, gpio_num_t spkr_data,
|
||||||
bool input_reference){
|
bool input_reference) {
|
||||||
duplex_ = true; // 是否双工
|
duplex_ = true; // 是否双工
|
||||||
input_reference_ = input_reference; // 是否使用参考输入,实现回声消除
|
input_reference_ = input_reference; // 是否使用参考输入,实现回声消除
|
||||||
input_channels_ = input_reference_ ? 2 : 1; // 输入通道数
|
input_channels_ = input_reference_ ? 2 : 1; // 输入通道数
|
||||||
@@ -34,7 +34,7 @@ Tcircles3AudioCodec::Tcircles3AudioCodec(int input_sample_rate, int output_sampl
|
|||||||
ESP_LOGI(TAG, "Tcircles3AudioCodec initialized");
|
ESP_LOGI(TAG, "Tcircles3AudioCodec initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tcircles3AudioCodec::~Tcircles3AudioCodec(){
|
Tcircles3AudioCodec::~Tcircles3AudioCodec() {
|
||||||
audio_codec_delete_codec_if(in_codec_if_);
|
audio_codec_delete_codec_if(in_codec_if_);
|
||||||
audio_codec_delete_ctrl_if(in_ctrl_if_);
|
audio_codec_delete_ctrl_if(in_ctrl_if_);
|
||||||
audio_codec_delete_codec_if(out_codec_if_);
|
audio_codec_delete_codec_if(out_codec_if_);
|
||||||
@@ -44,7 +44,7 @@ Tcircles3AudioCodec::~Tcircles3AudioCodec(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mic_ws, gpio_num_t mic_data,
|
void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mic_ws, gpio_num_t mic_data,
|
||||||
gpio_num_t spkr_bclk, gpio_num_t spkr_lrclk, gpio_num_t spkr_data){
|
gpio_num_t spkr_bclk, gpio_num_t spkr_lrclk, gpio_num_t spkr_data) {
|
||||||
|
|
||||||
i2s_chan_config_t mic_chan_config = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
|
i2s_chan_config_t mic_chan_config = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
|
||||||
mic_chan_config.auto_clear = true; // Auto clear the legacy data in the DMA buffer
|
mic_chan_config.auto_clear = true; // Auto clear the legacy data in the DMA buffer
|
||||||
@@ -54,7 +54,7 @@ void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mi
|
|||||||
ESP_ERROR_CHECK(i2s_new_channel(&mic_chan_config, NULL, &rx_handle_));
|
ESP_ERROR_CHECK(i2s_new_channel(&mic_chan_config, NULL, &rx_handle_));
|
||||||
ESP_ERROR_CHECK(i2s_new_channel(&spkr_chan_config, &tx_handle_, NULL));
|
ESP_ERROR_CHECK(i2s_new_channel(&spkr_chan_config, &tx_handle_, NULL));
|
||||||
|
|
||||||
i2s_std_config_t mic_config ={
|
i2s_std_config_t mic_config = {
|
||||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(input_sample_rate_)),
|
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(input_sample_rate_)),
|
||||||
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
|
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
|
||||||
.gpio_cfg ={
|
.gpio_cfg ={
|
||||||
@@ -63,7 +63,7 @@ void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mi
|
|||||||
.ws = mic_ws,
|
.ws = mic_ws,
|
||||||
.dout = I2S_GPIO_UNUSED,
|
.dout = I2S_GPIO_UNUSED,
|
||||||
.din = mic_data,
|
.din = mic_data,
|
||||||
.invert_flags ={
|
.invert_flags = {
|
||||||
.mclk_inv = false,
|
.mclk_inv = false,
|
||||||
.bclk_inv = false,
|
.bclk_inv = false,
|
||||||
.ws_inv = false,
|
.ws_inv = false,
|
||||||
@@ -71,7 +71,7 @@ void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mi
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
i2s_std_config_t spkr_config ={
|
i2s_std_config_t spkr_config = {
|
||||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(11025)),
|
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(11025)),
|
||||||
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
|
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
|
||||||
.gpio_cfg ={
|
.gpio_cfg ={
|
||||||
@@ -80,7 +80,7 @@ void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mi
|
|||||||
.ws = spkr_lrclk,
|
.ws = spkr_lrclk,
|
||||||
.dout = spkr_data,
|
.dout = spkr_data,
|
||||||
.din = I2S_GPIO_UNUSED,
|
.din = I2S_GPIO_UNUSED,
|
||||||
.invert_flags ={
|
.invert_flags = {
|
||||||
.mclk_inv = false,
|
.mclk_inv = false,
|
||||||
.bclk_inv = false,
|
.bclk_inv = false,
|
||||||
.ws_inv = false
|
.ws_inv = false
|
||||||
@@ -93,19 +93,16 @@ void Tcircles3AudioCodec::CreateVoiceHardware(gpio_num_t mic_bclk, gpio_num_t mi
|
|||||||
ESP_LOGI(TAG, "Voice hardware created");
|
ESP_LOGI(TAG, "Voice hardware created");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tcircles3AudioCodec::SetOutputVolume(int volume){
|
void Tcircles3AudioCodec::SetOutputVolume(int volume) {
|
||||||
volume_ = volume;
|
volume_ = volume;
|
||||||
AudioCodec::SetOutputVolume(volume);
|
AudioCodec::SetOutputVolume(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tcircles3AudioCodec::EnableInput(bool enable){
|
void Tcircles3AudioCodec::EnableInput(bool enable) {
|
||||||
if (enable){
|
|
||||||
}else{
|
|
||||||
}
|
|
||||||
AudioCodec::EnableInput(enable);
|
AudioCodec::EnableInput(enable);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tcircles3AudioCodec::EnableOutput(bool enable){
|
void Tcircles3AudioCodec::EnableOutput(bool enable) {
|
||||||
if (enable){
|
if (enable){
|
||||||
gpio_set_level(GPIO_NUM_45, 1);
|
gpio_set_level(GPIO_NUM_45, 1);
|
||||||
}else{
|
}else{
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
#include <esp_codec_dev.h>
|
#include <esp_codec_dev.h>
|
||||||
#include <esp_codec_dev_defaults.h>
|
#include <esp_codec_dev_defaults.h>
|
||||||
|
|
||||||
class Tcircles3AudioCodec : public AudioCodec
|
class Tcircles3AudioCodec : public AudioCodec {
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
const audio_codec_data_if_t *data_if_ = nullptr;
|
const audio_codec_data_if_t *data_if_ = nullptr;
|
||||||
const audio_codec_ctrl_if_t *out_ctrl_if_ = nullptr;
|
const audio_codec_ctrl_if_t *out_ctrl_if_ = nullptr;
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ static const char* TAG = "Button";
|
|||||||
|
|
||||||
Button::Button(gpio_num_t gpio_num, bool active_high) : gpio_num_(gpio_num) {
|
Button::Button(gpio_num_t gpio_num, bool active_high) : gpio_num_(gpio_num) {
|
||||||
if (gpio_num == GPIO_NUM_NC) {
|
if (gpio_num == GPIO_NUM_NC) {
|
||||||
button_handle_ = NULL;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
button_config_t button_config = {
|
button_config_t button_config = {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public:
|
|||||||
void OnDoubleClick(std::function<void()> callback);
|
void OnDoubleClick(std::function<void()> callback);
|
||||||
private:
|
private:
|
||||||
gpio_num_t gpio_num_;
|
gpio_num_t gpio_num_;
|
||||||
button_handle_t button_handle_;
|
button_handle_t button_handle_ = nullptr;
|
||||||
|
|
||||||
|
|
||||||
std::function<void()> on_press_down_;
|
std::function<void()> on_press_down_;
|
||||||
|
|||||||
@@ -16,32 +16,32 @@
|
|||||||
|
|
||||||
#define TAG "LilygoTCircleS3Board"
|
#define TAG "LilygoTCircleS3Board"
|
||||||
|
|
||||||
class Cst816x : public I2cDevice{
|
class Cst816x : public I2cDevice {
|
||||||
public:
|
public:
|
||||||
struct TouchPoint_t{
|
struct TouchPoint_t {
|
||||||
int num = 0;
|
int num = 0;
|
||||||
int x = -1;
|
int x = -1;
|
||||||
int y = -1;
|
int y = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
Cst816x(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr){
|
Cst816x(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||||
uint8_t chip_id = ReadReg(0xA7);
|
uint8_t chip_id = ReadReg(0xA7);
|
||||||
ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id);
|
ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id);
|
||||||
read_buffer_ = new uint8_t[6];
|
read_buffer_ = new uint8_t[6];
|
||||||
}
|
}
|
||||||
|
|
||||||
~Cst816x(){
|
~Cst816x() {
|
||||||
delete[] read_buffer_;
|
delete[] read_buffer_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateTouchPoint(){
|
void UpdateTouchPoint() {
|
||||||
ReadRegs(0x02, read_buffer_, 6);
|
ReadRegs(0x02, read_buffer_, 6);
|
||||||
tp_.num = read_buffer_[0] & 0x0F;
|
tp_.num = read_buffer_[0] & 0x0F;
|
||||||
tp_.x = ((read_buffer_[1] & 0x0F) << 8) | read_buffer_[2];
|
tp_.x = ((read_buffer_[1] & 0x0F) << 8) | read_buffer_[2];
|
||||||
tp_.y = ((read_buffer_[3] & 0x0F) << 8) | read_buffer_[4];
|
tp_.y = ((read_buffer_[3] & 0x0F) << 8) | read_buffer_[4];
|
||||||
}
|
}
|
||||||
|
|
||||||
const TouchPoint_t &GetTouchPoint(){
|
const TouchPoint_t &GetTouchPoint() {
|
||||||
return tp_;
|
return tp_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ private:
|
|||||||
TouchPoint_t tp_;
|
TouchPoint_t tp_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LilygoTCircleS3Board : public WifiBoard{
|
class LilygoTCircleS3Board : public WifiBoard {
|
||||||
private:
|
private:
|
||||||
i2c_master_bus_handle_t i2c_bus_;
|
i2c_master_bus_handle_t i2c_bus_;
|
||||||
Cst816x *cst816d_;
|
Cst816x *cst816d_;
|
||||||
@@ -74,20 +74,20 @@ private:
|
|||||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_));
|
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2cDetect(){
|
void I2cDetect() {
|
||||||
uint8_t address;
|
uint8_t address;
|
||||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
|
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
|
||||||
for (int i = 0; i < 128; i += 16){
|
for (int i = 0; i < 128; i += 16) {
|
||||||
printf("%02x: ", i);
|
printf("%02x: ", i);
|
||||||
for (int j = 0; j < 16; j++){
|
for (int j = 0; j < 16; j++) {
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
address = i + j;
|
address = i + j;
|
||||||
esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
|
esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
|
||||||
if (ret == ESP_OK){
|
if (ret == ESP_OK) {
|
||||||
printf("%02x ", address);
|
printf("%02x ", address);
|
||||||
}else if (ret == ESP_ERR_TIMEOUT){
|
} else if (ret == ESP_ERR_TIMEOUT) {
|
||||||
printf("UU ");
|
printf("UU ");
|
||||||
}else{
|
} else {
|
||||||
printf("-- ");
|
printf("-- ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,22 +95,22 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void touchpad_daemon(void *param){
|
static void touchpad_daemon(void *param) {
|
||||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||||
auto &board = (LilygoTCircleS3Board&)Board::GetInstance();
|
auto &board = (LilygoTCircleS3Board&)Board::GetInstance();
|
||||||
auto touchpad = board.GetTouchpad();
|
auto touchpad = board.GetTouchpad();
|
||||||
bool was_touched = false;
|
bool was_touched = false;
|
||||||
while (1){
|
while (1) {
|
||||||
touchpad->UpdateTouchPoint();
|
touchpad->UpdateTouchPoint();
|
||||||
if (touchpad->GetTouchPoint().num > 0){
|
if (touchpad->GetTouchPoint().num > 0){
|
||||||
// On press
|
// On press
|
||||||
if (!was_touched){
|
if (!was_touched) {
|
||||||
was_touched = true;
|
was_touched = true;
|
||||||
Application::GetInstance().ToggleChatState();
|
Application::GetInstance().ToggleChatState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// On release
|
// On release
|
||||||
else if (was_touched){
|
else if (was_touched) {
|
||||||
was_touched = false;
|
was_touched = false;
|
||||||
}
|
}
|
||||||
vTaskDelay(pdMS_TO_TICKS(50));
|
vTaskDelay(pdMS_TO_TICKS(50));
|
||||||
@@ -118,13 +118,13 @@ private:
|
|||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitCst816d(){
|
void InitCst816d() {
|
||||||
ESP_LOGI(TAG, "Init CST816x");
|
ESP_LOGI(TAG, "Init CST816x");
|
||||||
cst816d_ = new Cst816x(i2c_bus_, 0x15);
|
cst816d_ = new Cst816x(i2c_bus_, 0x15);
|
||||||
xTaskCreate(touchpad_daemon, "tp", 2048, NULL, 5, NULL);
|
xTaskCreate(touchpad_daemon, "tp", 2048, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitSpi(){
|
void InitSpi() {
|
||||||
spi_bus_config_t buscfg = {};
|
spi_bus_config_t buscfg = {};
|
||||||
buscfg.mosi_io_num = DISPLAY_MOSI;
|
buscfg.mosi_io_num = DISPLAY_MOSI;
|
||||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||||
@@ -135,7 +135,7 @@ private:
|
|||||||
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitGc9d01nDisplay(){
|
void InitGc9d01nDisplay() {
|
||||||
ESP_LOGI(TAG, "Init GC9D01N");
|
ESP_LOGI(TAG, "Init GC9D01N");
|
||||||
|
|
||||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||||
@@ -183,24 +183,24 @@ private:
|
|||||||
gpio_set_level(DISPLAY_BL, 0);
|
gpio_set_level(DISPLAY_BL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeButtons(){
|
void InitializeButtons() {
|
||||||
boot_button_.OnClick([this]()
|
boot_button_.OnClick([this]() {
|
||||||
{
|
|
||||||
auto& app = Application::GetInstance();
|
auto& app = Application::GetInstance();
|
||||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||||
ResetWifiConfiguration();
|
ResetWifiConfiguration();
|
||||||
}
|
}
|
||||||
app.ToggleChatState(); });
|
app.ToggleChatState();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 物联网初始化,添加对 AI 可见设备
|
// 物联网初始化,添加对 AI 可见设备
|
||||||
void InitializeIot(){
|
void InitializeIot() {
|
||||||
auto &thing_manager = iot::ThingManager::GetInstance();
|
auto &thing_manager = iot::ThingManager::GetInstance();
|
||||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LilygoTCircleS3Board() : boot_button_(BOOT_BUTTON_GPIO){
|
LilygoTCircleS3Board() : boot_button_(BOOT_BUTTON_GPIO) {
|
||||||
InitI2c();
|
InitI2c();
|
||||||
InitCst816d();
|
InitCst816d();
|
||||||
I2cDetect();
|
I2cDetect();
|
||||||
@@ -210,7 +210,7 @@ public:
|
|||||||
InitializeIot();
|
InitializeIot();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual AudioCodec *GetAudioCodec() override{
|
virtual AudioCodec *GetAudioCodec() override {
|
||||||
static Tcircles3AudioCodec *audio_codec = nullptr;
|
static Tcircles3AudioCodec *audio_codec = nullptr;
|
||||||
if (audio_codec == nullptr){
|
if (audio_codec == nullptr){
|
||||||
audio_codec = new Tcircles3AudioCodec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
|
audio_codec = new Tcircles3AudioCodec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
|
||||||
@@ -225,7 +225,7 @@ public:
|
|||||||
return display_;
|
return display_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cst816x *GetTouchpad(){
|
Cst816x *GetTouchpad() {
|
||||||
return cst816d_;
|
return cst816d_;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user