forked from xiaozhi/xiaozhi-esp32
Compare commits
4 Commits
main
...
bed-operat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
737333e1a6 | ||
|
|
89a06eda81 | ||
|
|
846d7afb80 | ||
|
|
855e8c09b4 |
@@ -126,6 +126,11 @@ elseif(CONFIG_BOARD_TYPE_LICHUANG_DEV_S3)
|
||||
set(BUILTIN_TEXT_FONT font_puhui_basic_20_4)
|
||||
set(BUILTIN_ICON_FONT font_awesome_20_4)
|
||||
set(DEFAULT_EMOJI_COLLECTION twemoji_64)
|
||||
elseif(CONFIG_BOARD_TYPE_LICHUANG_DEV_S3_BED_OPERATOR)
|
||||
set(BOARD_TYPE "lichuang-dev-bed-operator")
|
||||
set(BUILTIN_TEXT_FONT font_puhui_basic_20_4)
|
||||
set(BUILTIN_ICON_FONT font_awesome_20_4)
|
||||
set(DEFAULT_EMOJI_COLLECTION twemoji_64)
|
||||
elseif(CONFIG_BOARD_TYPE_LICHUANG_DEV_C3)
|
||||
set(BOARD_TYPE "lichuang-c3-dev")
|
||||
set(BUILTIN_TEXT_FONT font_puhui_basic_20_4)
|
||||
|
||||
@@ -201,6 +201,9 @@ choice BOARD_TYPE
|
||||
config BOARD_TYPE_LICHUANG_DEV_S3
|
||||
bool "立创·实战派 ESP32-S3"
|
||||
depends on IDF_TARGET_ESP32S3
|
||||
config BOARD_TYPE_LICHUANG_DEV_S3_BED_OPERATOR
|
||||
bool "立创·实战派ESP32-S3开发板(床控)"
|
||||
depends on IDF_TARGET_ESP32S3
|
||||
config BOARD_TYPE_LICHUANG_DEV_C3
|
||||
bool "立创·实战派 ESP32-C3"
|
||||
depends on IDF_TARGET_ESP32C3
|
||||
@@ -660,7 +663,7 @@ config USE_DEVICE_AEC
|
||||
bool "Enable Device-Side AEC"
|
||||
default n
|
||||
depends on USE_AUDIO_PROCESSOR && (BOARD_TYPE_ESP_BOX_3 || BOARD_TYPE_ESP_BOX || BOARD_TYPE_ESP_BOX_LITE \
|
||||
|| BOARD_TYPE_LICHUANG_DEV_S3 || BOARD_TYPE_ESP_KORVO2_V3 || BOARD_TYPE_WAVESHARE_S3_TOUCH_AMOLED_1_75 || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_1_83\
|
||||
|| BOARD_TYPE_LICHUANG_DEV_S3 || BOARD_TYPE_LICHUANG_DEV_S3_BED_OPERATOR || BOARD_TYPE_ESP_KORVO2_V3 || BOARD_TYPE_WAVESHARE_S3_TOUCH_AMOLED_1_75 || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_1_83\
|
||||
|| BOARD_TYPE_WAVESHARE_S3_TOUCH_AMOLED_2_06 || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_4B || BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_4B || BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_7B \
|
||||
|| BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_XC || BOARD_TYPE_ESP_S3_LCD_EV_Board_2 || BOARD_TYPE_YUNLIAO_S3 \
|
||||
|| BOARD_TYPE_ECHOEAR || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_3_49)
|
||||
|
||||
@@ -2,26 +2,6 @@
|
||||
#include <esp_log.h>
|
||||
#include <cstring>
|
||||
|
||||
#define RATE_CVT_CFG(_src_rate, _dest_rate, _channel) \
|
||||
(esp_ae_rate_cvt_cfg_t) \
|
||||
{ \
|
||||
.src_rate = (uint32_t)(_src_rate), \
|
||||
.dest_rate = (uint32_t)(_dest_rate), \
|
||||
.channel = (uint8_t)(_channel), \
|
||||
.bits_per_sample = ESP_AUDIO_BIT16, \
|
||||
.complexity = 2, \
|
||||
.perf_type = ESP_AE_RATE_CVT_PERF_TYPE_SPEED, \
|
||||
}
|
||||
|
||||
#define OPUS_DEC_CFG(_sample_rate, _frame_duration_ms) \
|
||||
(esp_opus_dec_cfg_t) \
|
||||
{ \
|
||||
.sample_rate = (uint32_t)(_sample_rate), \
|
||||
.channel = ESP_AUDIO_MONO, \
|
||||
.frame_duration = (esp_opus_dec_frame_duration_t)AS_OPUS_GET_FRAME_DRU_ENUM(_frame_duration_ms), \
|
||||
.self_delimited = false, \
|
||||
}
|
||||
|
||||
#if CONFIG_USE_AUDIO_PROCESSOR
|
||||
#include "processors/afe_audio_processor.h"
|
||||
#else
|
||||
@@ -37,6 +17,7 @@
|
||||
|
||||
#define TAG "AudioService"
|
||||
|
||||
|
||||
AudioService::AudioService() {
|
||||
event_group_ = xEventGroupCreate();
|
||||
}
|
||||
@@ -45,51 +26,21 @@ AudioService::~AudioService() {
|
||||
if (event_group_ != nullptr) {
|
||||
vEventGroupDelete(event_group_);
|
||||
}
|
||||
if (opus_encoder_ != nullptr) {
|
||||
esp_opus_enc_close(opus_encoder_);
|
||||
}
|
||||
if (opus_decoder_ != nullptr) {
|
||||
esp_opus_dec_close(opus_decoder_);
|
||||
}
|
||||
if (input_resampler_ != nullptr) {
|
||||
esp_ae_rate_cvt_close(input_resampler_);
|
||||
}
|
||||
if (output_resampler_ != nullptr) {
|
||||
esp_ae_rate_cvt_close(output_resampler_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AudioService::Initialize(AudioCodec* codec) {
|
||||
codec_ = codec;
|
||||
codec_->Start();
|
||||
|
||||
esp_opus_dec_cfg_t opus_dec_cfg = OPUS_DEC_CFG(codec->output_sample_rate(), OPUS_FRAME_DURATION_MS);
|
||||
auto ret = esp_opus_dec_open(&opus_dec_cfg, sizeof(esp_opus_dec_cfg_t), &opus_decoder_);
|
||||
if (opus_decoder_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create audio decoder, error code: %d", ret);
|
||||
} else {
|
||||
decoder_sample_rate_ = codec->output_sample_rate();
|
||||
decoder_duration_ms_ = OPUS_FRAME_DURATION_MS;
|
||||
decoder_frame_size_ = decoder_sample_rate_ / 1000 * OPUS_FRAME_DURATION_MS;
|
||||
}
|
||||
esp_opus_enc_config_t opus_enc_cfg = AS_OPUS_ENC_CONFIG();
|
||||
ret = esp_opus_enc_open(&opus_enc_cfg, sizeof(esp_opus_enc_config_t), &opus_encoder_);
|
||||
if (opus_encoder_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create audio encoder, error code: %d", ret);
|
||||
} else {
|
||||
encoder_sample_rate_ = 16000;
|
||||
encoder_duration_ms_ = OPUS_FRAME_DURATION_MS;
|
||||
esp_opus_enc_get_frame_size(opus_encoder_, &encoder_frame_size_, &encoder_outbuf_size_);
|
||||
encoder_frame_size_ = encoder_frame_size_ / sizeof(int16_t);
|
||||
}
|
||||
/* Setup the audio codec */
|
||||
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(codec->output_sample_rate(), 1, OPUS_FRAME_DURATION_MS);
|
||||
opus_encoder_ = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
||||
opus_encoder_->SetComplexity(0);
|
||||
|
||||
if (codec->input_sample_rate() != 16000) {
|
||||
esp_ae_rate_cvt_cfg_t input_resampler_cfg = RATE_CVT_CFG(
|
||||
codec->input_sample_rate(), ESP_AUDIO_SAMPLE_RATE_16K, codec->input_channels());
|
||||
auto resampler_ret = esp_ae_rate_cvt_open(&input_resampler_cfg, &input_resampler_);
|
||||
if (input_resampler_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create input resampler, error code: %d", resampler_ret);
|
||||
}
|
||||
input_resampler_.Configure(codec->input_sample_rate(), 16000);
|
||||
reference_resampler_.Configure(codec->input_sample_rate(), 16000);
|
||||
}
|
||||
|
||||
#if CONFIG_USE_AUDIO_PROCESSOR
|
||||
@@ -163,7 +114,7 @@ void AudioService::Start() {
|
||||
AudioService* audio_service = (AudioService*)arg;
|
||||
audio_service->OpusCodecTask();
|
||||
vTaskDelete(NULL);
|
||||
}, "opus_codec", 2048 * 12, this, 2, &opus_codec_task_handle_);
|
||||
}, "opus_codec", 2048 * 13, this, 2, &opus_codec_task_handle_);
|
||||
}
|
||||
|
||||
void AudioService::Stop() {
|
||||
@@ -193,15 +144,25 @@ bool AudioService::ReadAudioData(std::vector<int16_t>& data, int sample_rate, in
|
||||
if (!codec_->InputData(data)) {
|
||||
return false;
|
||||
}
|
||||
if (input_resampler_ != nullptr) {
|
||||
uint32_t in_sample_num = data.size() / codec_->input_channels();
|
||||
uint32_t output_samples = 0;
|
||||
esp_ae_rate_cvt_get_max_out_sample_num(input_resampler_, in_sample_num, &output_samples);
|
||||
auto resampled = std::vector<int16_t>(output_samples * codec_->input_channels());
|
||||
uint32_t actual_output = output_samples;
|
||||
esp_ae_rate_cvt_process(input_resampler_, (esp_ae_sample_t)data.data(), in_sample_num,
|
||||
(esp_ae_sample_t)resampled.data(), &actual_output);
|
||||
resampled.resize(actual_output * codec_->input_channels());
|
||||
if (codec_->input_channels() == 2) {
|
||||
auto mic_channel = std::vector<int16_t>(data.size() / 2);
|
||||
auto reference_channel = std::vector<int16_t>(data.size() / 2);
|
||||
for (size_t i = 0, j = 0; i < mic_channel.size(); ++i, j += 2) {
|
||||
mic_channel[i] = data[j];
|
||||
reference_channel[i] = data[j + 1];
|
||||
}
|
||||
auto resampled_mic = std::vector<int16_t>(input_resampler_.GetOutputSamples(mic_channel.size()));
|
||||
auto resampled_reference = std::vector<int16_t>(reference_resampler_.GetOutputSamples(reference_channel.size()));
|
||||
input_resampler_.Process(mic_channel.data(), mic_channel.size(), resampled_mic.data());
|
||||
reference_resampler_.Process(reference_channel.data(), reference_channel.size(), resampled_reference.data());
|
||||
data.resize(resampled_mic.size() + resampled_reference.size());
|
||||
for (size_t i = 0, j = 0; i < resampled_mic.size(); ++i, j += 2) {
|
||||
data[j] = resampled_mic[i];
|
||||
data[j + 1] = resampled_reference[i];
|
||||
}
|
||||
} else {
|
||||
auto resampled = std::vector<int16_t>(input_resampler_.GetOutputSamples(data.size()));
|
||||
input_resampler_.Process(data.data(), data.size(), resampled.data());
|
||||
data = std::move(resampled);
|
||||
}
|
||||
} else {
|
||||
@@ -355,49 +316,25 @@ void AudioService::OpusCodecTask() {
|
||||
task->timestamp = packet->timestamp;
|
||||
|
||||
SetDecodeSampleRate(packet->sample_rate, packet->frame_duration);
|
||||
if (opus_decoder_ != nullptr) {
|
||||
task->pcm.resize(decoder_frame_size_);
|
||||
esp_audio_dec_in_raw_t raw = {
|
||||
.buffer = (uint8_t *)(packet->payload.data()),
|
||||
.len = (uint32_t)(packet->payload.size()),
|
||||
.consumed = 0,
|
||||
.frame_recover = ESP_AUDIO_DEC_RECOVERY_NONE,
|
||||
};
|
||||
esp_audio_dec_out_frame_t out_frame = {
|
||||
.buffer = (uint8_t *)(task->pcm.data()),
|
||||
.len = (uint32_t)(task->pcm.size() * sizeof(int16_t)),
|
||||
.decoded_size = 0,
|
||||
};
|
||||
esp_audio_dec_info_t dec_info = {};
|
||||
std::unique_lock<std::mutex> decoder_lock(decoder_mutex_);
|
||||
auto ret = esp_opus_dec_decode(opus_decoder_, &raw, &out_frame, &dec_info);
|
||||
decoder_lock.unlock();
|
||||
if (ret == ESP_AUDIO_ERR_OK) {
|
||||
task->pcm.resize(out_frame.decoded_size / sizeof(int16_t));
|
||||
if (decoder_sample_rate_ != codec_->output_sample_rate() && output_resampler_ != nullptr) {
|
||||
uint32_t target_size = 0;
|
||||
esp_ae_rate_cvt_get_max_out_sample_num(output_resampler_, task->pcm.size(), &target_size);
|
||||
std::vector<int16_t> resampled(target_size);
|
||||
uint32_t actual_output = target_size;
|
||||
esp_ae_rate_cvt_process(output_resampler_, (esp_ae_sample_t)task->pcm.data(), task->pcm.size(),
|
||||
(esp_ae_sample_t)resampled.data(), &actual_output);
|
||||
resampled.resize(actual_output);
|
||||
task->pcm = std::move(resampled);
|
||||
}
|
||||
lock.lock();
|
||||
audio_playback_queue_.push_back(std::move(task));
|
||||
audio_queue_cv_.notify_all();
|
||||
debug_statistics_.decode_count++;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to decode audio after resize, error code: %d", ret);
|
||||
lock.lock();
|
||||
if (opus_decoder_->Decode(std::move(packet->payload), task->pcm)) {
|
||||
// Resample if the sample rate is different
|
||||
if (opus_decoder_->sample_rate() != codec_->output_sample_rate()) {
|
||||
int target_size = output_resampler_.GetOutputSamples(task->pcm.size());
|
||||
std::vector<int16_t> resampled(target_size);
|
||||
output_resampler_.Process(task->pcm.data(), task->pcm.size(), resampled.data());
|
||||
task->pcm = std::move(resampled);
|
||||
}
|
||||
|
||||
lock.lock();
|
||||
audio_playback_queue_.push_back(std::move(task));
|
||||
audio_queue_cv_.notify_all();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Audio decoder is not configured");
|
||||
ESP_LOGE(TAG, "Failed to decode audio");
|
||||
lock.lock();
|
||||
}
|
||||
debug_statistics_.decode_count++;
|
||||
}
|
||||
|
||||
/* Encode the audio to send queue */
|
||||
if (!audio_encode_queue_.empty() && audio_send_queue_.size() < MAX_SEND_PACKETS_IN_QUEUE) {
|
||||
auto task = std::move(audio_encode_queue_.front());
|
||||
@@ -409,42 +346,24 @@ void AudioService::OpusCodecTask() {
|
||||
packet->frame_duration = OPUS_FRAME_DURATION_MS;
|
||||
packet->sample_rate = 16000;
|
||||
packet->timestamp = task->timestamp;
|
||||
|
||||
if (opus_encoder_ != nullptr && task->pcm.size() == encoder_frame_size_) {
|
||||
std::vector<uint8_t> buf(encoder_outbuf_size_);
|
||||
esp_audio_enc_in_frame_t in = {
|
||||
.buffer = (uint8_t *)(task->pcm.data()),
|
||||
.len = (uint32_t)(encoder_frame_size_ * sizeof(int16_t)),
|
||||
};
|
||||
esp_audio_enc_out_frame_t out = {
|
||||
.buffer = buf.data(),
|
||||
.len = (uint32_t)encoder_outbuf_size_,
|
||||
.encoded_bytes = 0,
|
||||
};
|
||||
auto ret = esp_opus_enc_process(opus_encoder_, &in, &out);
|
||||
if (ret == ESP_AUDIO_ERR_OK) {
|
||||
packet->payload.assign(buf.data(), buf.data() + out.encoded_bytes);
|
||||
|
||||
if (task->type == kAudioTaskTypeEncodeToSendQueue) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock2(audio_queue_mutex_);
|
||||
audio_send_queue_.push_back(std::move(packet));
|
||||
}
|
||||
if (callbacks_.on_send_queue_available) {
|
||||
callbacks_.on_send_queue_available();
|
||||
}
|
||||
} else if (task->type == kAudioTaskTypeEncodeToTestingQueue) {
|
||||
std::lock_guard<std::mutex> lock2(audio_queue_mutex_);
|
||||
audio_testing_queue_.push_back(std::move(packet));
|
||||
}
|
||||
debug_statistics_.encode_count++;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to encode audio, error code: %d", ret);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to encode audio: encoder not configured or invalid frame size (got %u, expected %u)",
|
||||
task->pcm.size(), encoder_frame_size_);
|
||||
if (!opus_encoder_->Encode(std::move(task->pcm), packet->payload)) {
|
||||
ESP_LOGE(TAG, "Failed to encode audio");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (task->type == kAudioTaskTypeEncodeToSendQueue) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(audio_queue_mutex_);
|
||||
audio_send_queue_.push_back(std::move(packet));
|
||||
}
|
||||
if (callbacks_.on_send_queue_available) {
|
||||
callbacks_.on_send_queue_available();
|
||||
}
|
||||
} else if (task->type == kAudioTaskTypeEncodeToTestingQueue) {
|
||||
std::lock_guard<std::mutex> lock(audio_queue_mutex_);
|
||||
audio_testing_queue_.push_back(std::move(packet));
|
||||
}
|
||||
debug_statistics_.encode_count++;
|
||||
lock.lock();
|
||||
}
|
||||
}
|
||||
@@ -453,38 +372,17 @@ void AudioService::OpusCodecTask() {
|
||||
}
|
||||
|
||||
void AudioService::SetDecodeSampleRate(int sample_rate, int frame_duration) {
|
||||
if (decoder_sample_rate_ == sample_rate && decoder_duration_ms_ == frame_duration) {
|
||||
if (opus_decoder_->sample_rate() == sample_rate && opus_decoder_->duration_ms() == frame_duration) {
|
||||
return;
|
||||
}
|
||||
std::unique_lock<std::mutex> decoder_lock(decoder_mutex_);
|
||||
if (opus_decoder_ != nullptr) {
|
||||
esp_opus_dec_close(opus_decoder_);
|
||||
opus_decoder_ = nullptr;
|
||||
}
|
||||
decoder_lock.unlock();
|
||||
esp_opus_dec_cfg_t opus_dec_cfg = OPUS_DEC_CFG(sample_rate, frame_duration);
|
||||
auto ret = esp_opus_dec_open(&opus_dec_cfg, sizeof(esp_opus_dec_cfg_t), &opus_decoder_);
|
||||
if (opus_decoder_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create audio decoder, error code: %d", ret);
|
||||
return;
|
||||
}
|
||||
decoder_sample_rate_ = sample_rate;
|
||||
decoder_duration_ms_ = frame_duration;
|
||||
decoder_frame_size_ = decoder_sample_rate_ / 1000 * frame_duration;
|
||||
|
||||
opus_decoder_.reset();
|
||||
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(sample_rate, 1, frame_duration);
|
||||
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
if (decoder_sample_rate_ != codec->output_sample_rate()) {
|
||||
ESP_LOGI(TAG, "Resampling audio from %d to %d", decoder_sample_rate_, codec->output_sample_rate());
|
||||
if (output_resampler_ != nullptr) {
|
||||
esp_ae_rate_cvt_close(output_resampler_);
|
||||
output_resampler_ = nullptr;
|
||||
}
|
||||
esp_ae_rate_cvt_cfg_t output_resampler_cfg = RATE_CVT_CFG(
|
||||
decoder_sample_rate_, codec->output_sample_rate(), ESP_AUDIO_MONO);
|
||||
auto resampler_ret = esp_ae_rate_cvt_open(&output_resampler_cfg, &output_resampler_);
|
||||
if (output_resampler_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create output resampler, error code: %d", resampler_ret);
|
||||
}
|
||||
if (opus_decoder_->sample_rate() != codec->output_sample_rate()) {
|
||||
ESP_LOGI(TAG, "Resampling audio from %d to %d", opus_decoder_->sample_rate(), codec->output_sample_rate());
|
||||
output_resampler_.Configure(opus_decoder_->sample_rate(), codec->output_sample_rate());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,6 +390,7 @@ void AudioService::PushTaskToEncodeQueue(AudioTaskType type, std::vector<int16_t
|
||||
auto task = std::make_unique<AudioTask>();
|
||||
task->type = type;
|
||||
task->pcm = std::move(pcm);
|
||||
|
||||
/* Push the task to the encode queue */
|
||||
std::unique_lock<std::mutex> lock(audio_queue_mutex_);
|
||||
|
||||
@@ -681,16 +580,18 @@ void AudioService::PlaySound(const std::string_view& ogg) {
|
||||
// 解析OpusHead包
|
||||
if (pkt_len >= 19 && std::memcmp(pkt_ptr, "OpusHead", 8) == 0) {
|
||||
seen_head = true;
|
||||
|
||||
// OpusHead结构:[0-7] "OpusHead", [8] version, [9] channel_count, [10-11] pre_skip
|
||||
// [12-15] input_sample_rate, [16-17] output_gain, [18] mapping_family
|
||||
if (pkt_len >= 12) {
|
||||
uint8_t version = pkt_ptr[8];
|
||||
uint8_t channel_count = pkt_ptr[9];
|
||||
|
||||
if (pkt_len >= 16) {
|
||||
// 读取输入采样率 (little-endian)
|
||||
sample_rate = pkt_ptr[12] | (pkt_ptr[13] << 8) |
|
||||
sample_rate = pkt_ptr[12] | (pkt_ptr[13] << 8) |
|
||||
(pkt_ptr[14] << 16) | (pkt_ptr[15] << 24);
|
||||
ESP_LOGI(TAG, "OpusHead: version=%d, channels=%d, sample_rate=%d",
|
||||
ESP_LOGI(TAG, "OpusHead: version=%d, channels=%d, sample_rate=%d",
|
||||
version, channel_count, sample_rate);
|
||||
}
|
||||
}
|
||||
@@ -725,11 +626,7 @@ bool AudioService::IsIdle() {
|
||||
|
||||
void AudioService::ResetDecoder() {
|
||||
std::lock_guard<std::mutex> lock(audio_queue_mutex_);
|
||||
std::unique_lock<std::mutex> decoder_lock(decoder_mutex_);
|
||||
if (opus_decoder_ != nullptr) {
|
||||
esp_opus_dec_reset(opus_decoder_);
|
||||
}
|
||||
decoder_lock.unlock();
|
||||
opus_decoder_->ResetState();
|
||||
timestamp_queue_.clear();
|
||||
audio_decode_queue_.clear();
|
||||
audio_playback_queue_.clear();
|
||||
|
||||
@@ -12,11 +12,10 @@
|
||||
#include <freertos/event_groups.h>
|
||||
#include <esp_timer.h>
|
||||
#include <model_path.h>
|
||||
#include "esp_audio_enc.h"
|
||||
#include "esp_opus_enc.h"
|
||||
#include "esp_opus_dec.h"
|
||||
#include "esp_ae_rate_cvt.h"
|
||||
#include "esp_audio_types.h"
|
||||
|
||||
#include <opus_encoder.h>
|
||||
#include <opus_decoder.h>
|
||||
#include <opus_resampler.h>
|
||||
|
||||
#include "audio_codec.h"
|
||||
#include "audio_processor.h"
|
||||
@@ -47,34 +46,12 @@
|
||||
#define AUDIO_POWER_TIMEOUT_MS 15000
|
||||
#define AUDIO_POWER_CHECK_INTERVAL_MS 1000
|
||||
|
||||
|
||||
#define AS_EVENT_AUDIO_TESTING_RUNNING (1 << 0)
|
||||
#define AS_EVENT_WAKE_WORD_RUNNING (1 << 1)
|
||||
#define AS_EVENT_AUDIO_PROCESSOR_RUNNING (1 << 2)
|
||||
#define AS_EVENT_PLAYBACK_NOT_EMPTY (1 << 3)
|
||||
|
||||
#define AS_OPUS_GET_FRAME_DRU_ENUM(duration_ms) \
|
||||
((duration_ms) == 5 ? ESP_OPUS_ENC_FRAME_DURATION_5_MS : \
|
||||
(duration_ms) == 10 ? ESP_OPUS_ENC_FRAME_DURATION_10_MS : \
|
||||
(duration_ms) == 20 ? ESP_OPUS_ENC_FRAME_DURATION_20_MS : \
|
||||
(duration_ms) == 40 ? ESP_OPUS_ENC_FRAME_DURATION_40_MS : \
|
||||
(duration_ms) == 60 ? ESP_OPUS_ENC_FRAME_DURATION_60_MS : \
|
||||
(duration_ms) == 80 ? ESP_OPUS_ENC_FRAME_DURATION_80_MS : \
|
||||
(duration_ms) == 100 ? ESP_OPUS_ENC_FRAME_DURATION_100_MS : \
|
||||
(duration_ms) == 120 ? ESP_OPUS_ENC_FRAME_DURATION_120_MS : -1)
|
||||
|
||||
#define AS_OPUS_ENC_CONFIG() { \
|
||||
.sample_rate = ESP_AUDIO_SAMPLE_RATE_16K, \
|
||||
.channel = ESP_AUDIO_MONO, \
|
||||
.bits_per_sample = ESP_AUDIO_BIT16, \
|
||||
.bitrate = ESP_OPUS_BITRATE_AUTO, \
|
||||
.frame_duration = (esp_opus_enc_frame_duration_t)AS_OPUS_GET_FRAME_DRU_ENUM(OPUS_FRAME_DURATION_MS), \
|
||||
.application_mode = ESP_OPUS_ENC_APPLICATION_AUDIO, \
|
||||
.complexity = 0, \
|
||||
.enable_fec = false, \
|
||||
.enable_dtx = true, \
|
||||
.enable_vbr = true, \
|
||||
}
|
||||
|
||||
struct AudioServiceCallbacks {
|
||||
std::function<void(void)> on_send_queue_available;
|
||||
std::function<void(const std::string&)> on_wake_word_detected;
|
||||
@@ -139,20 +116,11 @@ private:
|
||||
std::unique_ptr<AudioProcessor> audio_processor_;
|
||||
std::unique_ptr<WakeWord> wake_word_;
|
||||
std::unique_ptr<AudioDebugger> audio_debugger_;
|
||||
void* opus_encoder_ = nullptr;
|
||||
void* opus_decoder_ = nullptr;
|
||||
std::mutex decoder_mutex_;
|
||||
esp_ae_rate_cvt_handle_t input_resampler_ = nullptr;
|
||||
esp_ae_rate_cvt_handle_t output_resampler_ = nullptr;
|
||||
|
||||
// Encoder/Decoder state
|
||||
int encoder_sample_rate_ = 16000;
|
||||
int encoder_duration_ms_ = OPUS_FRAME_DURATION_MS;
|
||||
int encoder_frame_size_ = 0;
|
||||
int encoder_outbuf_size_ = 0;
|
||||
int decoder_sample_rate_ = 0;
|
||||
int decoder_duration_ms_ = OPUS_FRAME_DURATION_MS;
|
||||
int decoder_frame_size_ = 0;
|
||||
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
|
||||
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
|
||||
OpusResampler input_resampler_;
|
||||
OpusResampler reference_resampler_;
|
||||
OpusResampler output_resampler_;
|
||||
DebugStatistics debug_statistics_;
|
||||
srmodel_list_t* models_list_ = nullptr;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "afe_wake_word.h"
|
||||
#include "audio_service.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <sstream>
|
||||
|
||||
@@ -156,7 +157,7 @@ void AfeWakeWord::StoreWakeWordData(const int16_t* data, size_t samples) {
|
||||
}
|
||||
|
||||
void AfeWakeWord::EncodeWakeWordData() {
|
||||
const size_t stack_size = 4096 * 6;
|
||||
const size_t stack_size = 4096 * 7;
|
||||
wake_word_opus_.clear();
|
||||
if (wake_word_encode_task_stack_ == nullptr) {
|
||||
wake_word_encode_task_stack_ = (StackType_t*)heap_caps_malloc(stack_size, MALLOC_CAP_SPIRAM);
|
||||
@@ -171,62 +172,20 @@ void AfeWakeWord::EncodeWakeWordData() {
|
||||
auto this_ = (AfeWakeWord*)arg;
|
||||
{
|
||||
auto start_time = esp_timer_get_time();
|
||||
// Create encoder
|
||||
esp_opus_enc_config_t opus_enc_cfg = AS_OPUS_ENC_CONFIG();
|
||||
void* encoder_handle = nullptr;
|
||||
auto ret = esp_opus_enc_open(&opus_enc_cfg, sizeof(esp_opus_enc_config_t), &encoder_handle);
|
||||
if (encoder_handle == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create audio encoder, error code: %d", ret);
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.push_back(std::vector<uint8_t>());
|
||||
this_->wake_word_cv_.notify_all();
|
||||
return;
|
||||
}
|
||||
|
||||
// Get frame size
|
||||
int frame_size = 0;
|
||||
int outbuf_size = 0;
|
||||
esp_opus_enc_get_frame_size(encoder_handle, &frame_size, &outbuf_size);
|
||||
frame_size = frame_size / sizeof(int16_t);
|
||||
|
||||
// Encode all PCM data
|
||||
auto encoder = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
||||
encoder->SetComplexity(0); // 0 is the fastest
|
||||
|
||||
int packets = 0;
|
||||
std::vector<int16_t> in_buffer;
|
||||
esp_audio_enc_in_frame_t in = {};
|
||||
esp_audio_enc_out_frame_t out = {};
|
||||
|
||||
for (auto& pcm: this_->wake_word_pcm_) {
|
||||
if (in_buffer.empty()) {
|
||||
in_buffer = std::move(pcm);
|
||||
} else {
|
||||
in_buffer.reserve(in_buffer.size() + pcm.size());
|
||||
in_buffer.insert(in_buffer.end(), pcm.begin(), pcm.end());
|
||||
}
|
||||
|
||||
while (in_buffer.size() >= frame_size) {
|
||||
std::vector<uint8_t> opus_buf(outbuf_size);
|
||||
in.buffer = (uint8_t *)(in_buffer.data());
|
||||
in.len = (uint32_t)(frame_size * sizeof(int16_t));
|
||||
out.buffer = opus_buf.data();
|
||||
out.len = outbuf_size;
|
||||
out.encoded_bytes = 0;
|
||||
|
||||
ret = esp_opus_enc_process(encoder_handle, &in, &out);
|
||||
if (ret == ESP_AUDIO_ERR_OK) {
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.emplace_back(opus_buf.data(), opus_buf.data() + out.encoded_bytes);
|
||||
this_->wake_word_cv_.notify_all();
|
||||
packets++;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to encode audio, error code: %d", ret);
|
||||
}
|
||||
|
||||
in_buffer.erase(in_buffer.begin(), in_buffer.begin() + frame_size);
|
||||
}
|
||||
encoder->Encode(std::move(pcm), [this_](std::vector<uint8_t>&& opus) {
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.emplace_back(std::move(opus));
|
||||
this_->wake_word_cv_.notify_all();
|
||||
});
|
||||
packets++;
|
||||
}
|
||||
this_->wake_word_pcm_.clear();
|
||||
// Close encoder
|
||||
esp_opus_enc_close(encoder_handle);
|
||||
|
||||
auto end_time = esp_timer_get_time();
|
||||
ESP_LOGI(TAG, "Encode wake word opus %d packets in %ld ms", packets, (long)((end_time - start_time) / 1000));
|
||||
|
||||
|
||||
@@ -9,8 +9,10 @@
|
||||
#include <esp_mn_speech_commands.h>
|
||||
#include <cJSON.h>
|
||||
|
||||
|
||||
#define TAG "CustomWakeWord"
|
||||
|
||||
|
||||
CustomWakeWord::CustomWakeWord()
|
||||
: wake_word_pcm_(), wake_word_opus_() {
|
||||
}
|
||||
@@ -216,56 +218,20 @@ void CustomWakeWord::EncodeWakeWordData() {
|
||||
auto this_ = (CustomWakeWord*)arg;
|
||||
{
|
||||
auto start_time = esp_timer_get_time();
|
||||
// Create encoder
|
||||
esp_opus_enc_config_t opus_enc_cfg = AS_OPUS_ENC_CONFIG();
|
||||
void* encoder_handle = nullptr;
|
||||
auto ret = esp_opus_enc_open(&opus_enc_cfg, sizeof(esp_opus_enc_config_t), &encoder_handle);
|
||||
if (encoder_handle == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create audio encoder, error code: %d", ret);
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.push_back(std::vector<uint8_t>());
|
||||
this_->wake_word_cv_.notify_all();
|
||||
return;
|
||||
}
|
||||
// Get frame size
|
||||
int frame_size = 0;
|
||||
int outbuf_size = 0;
|
||||
esp_opus_enc_get_frame_size(encoder_handle, &frame_size, &outbuf_size);
|
||||
frame_size = frame_size / sizeof(int16_t);
|
||||
// Encode all PCM data
|
||||
auto encoder = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
||||
encoder->SetComplexity(0); // 0 is the fastest
|
||||
|
||||
int packets = 0;
|
||||
std::vector<int16_t> in_buffer;
|
||||
esp_audio_enc_in_frame_t in = {};
|
||||
esp_audio_enc_out_frame_t out = {};
|
||||
for (auto& pcm: this_->wake_word_pcm_) {
|
||||
if (in_buffer.empty()) {
|
||||
in_buffer = std::move(pcm);
|
||||
} else {
|
||||
in_buffer.reserve(in_buffer.size() + pcm.size());
|
||||
in_buffer.insert(in_buffer.end(), pcm.begin(), pcm.end());
|
||||
}
|
||||
while (in_buffer.size() >= frame_size) {
|
||||
std::vector<uint8_t> opus_buf(outbuf_size);
|
||||
in.buffer = (uint8_t *)(in_buffer.data());
|
||||
in.len = (uint32_t)(frame_size * sizeof(int16_t));
|
||||
out.buffer = opus_buf.data();
|
||||
out.len = outbuf_size;
|
||||
out.encoded_bytes = 0;
|
||||
ret = esp_opus_enc_process(encoder_handle, &in, &out);
|
||||
if (ret == ESP_AUDIO_ERR_OK) {
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.emplace_back(opus_buf.data(), opus_buf.data() + out.encoded_bytes);
|
||||
this_->wake_word_cv_.notify_all();
|
||||
packets++;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to encode audio, error code: %d", ret);
|
||||
}
|
||||
in_buffer.erase(in_buffer.begin(), in_buffer.begin() + frame_size);
|
||||
}
|
||||
encoder->Encode(std::move(pcm), [this_](std::vector<uint8_t>&& opus) {
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.emplace_back(std::move(opus));
|
||||
this_->wake_word_cv_.notify_all();
|
||||
});
|
||||
packets++;
|
||||
}
|
||||
this_->wake_word_pcm_.clear();
|
||||
// Close encoder
|
||||
esp_opus_enc_close(encoder_handle);
|
||||
|
||||
auto end_time = esp_timer_get_time();
|
||||
ESP_LOGI(TAG, "Encode wake word opus %d packets in %ld ms", packets, (long)((end_time - start_time) / 1000));
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <font_awesome.h>
|
||||
#include <opus_encoder.h>
|
||||
#include <utility>
|
||||
|
||||
static const char *TAG = "Ml307Board";
|
||||
|
||||
62
main/boards/lichuang-dev-bed-operator/config.h
Normal file
62
main/boards/lichuang-dev-bed-operator/config.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#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_MCLK GPIO_NUM_38
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_13
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_14
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_12
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_45
|
||||
|
||||
#define AUDIO_CODEC_USE_PCA9557
|
||||
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_1
|
||||
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_2
|
||||
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
|
||||
#define AUDIO_CODEC_ES7210_ADDR 0x82
|
||||
|
||||
#define BUILTIN_LED_GPIO GPIO_NUM_48
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_NC
|
||||
#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_NC
|
||||
|
||||
#define DISPLAY_WIDTH 320
|
||||
#define DISPLAY_HEIGHT 240
|
||||
#define DISPLAY_MIRROR_X true
|
||||
#define DISPLAY_MIRROR_Y false
|
||||
#define DISPLAY_SWAP_XY true
|
||||
|
||||
#define DISPLAY_OFFSET_X 0
|
||||
#define DISPLAY_OFFSET_Y 0
|
||||
|
||||
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_42
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT true
|
||||
|
||||
/* Camera pins */
|
||||
#define CAMERA_PIN_PWDN GPIO_NUM_NC
|
||||
#define CAMERA_PIN_RESET GPIO_NUM_NC
|
||||
#define CAMERA_PIN_XCLK GPIO_NUM_5
|
||||
#define CAMERA_PIN_SIOD GPIO_NUM_1
|
||||
#define CAMERA_PIN_SIOC GPIO_NUM_2
|
||||
|
||||
#define CAMERA_PIN_D7 GPIO_NUM_9
|
||||
#define CAMERA_PIN_D6 GPIO_NUM_4
|
||||
#define CAMERA_PIN_D5 GPIO_NUM_6
|
||||
#define CAMERA_PIN_D4 GPIO_NUM_15
|
||||
#define CAMERA_PIN_D3 GPIO_NUM_17
|
||||
#define CAMERA_PIN_D2 GPIO_NUM_8
|
||||
#define CAMERA_PIN_D1 GPIO_NUM_18
|
||||
#define CAMERA_PIN_D0 GPIO_NUM_16
|
||||
#define CAMERA_PIN_VSYNC GPIO_NUM_3
|
||||
#define CAMERA_PIN_HREF GPIO_NUM_46
|
||||
#define CAMERA_PIN_PCLK GPIO_NUM_7
|
||||
|
||||
#define XCLK_FREQ_HZ 20000000
|
||||
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
14
main/boards/lichuang-dev-bed-operator/config.json
Normal file
14
main/boards/lichuang-dev-bed-operator/config.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"target": "esp32s3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "lichuang-dev-bed-operator",
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_USE_DEVICE_AEC=y",
|
||||
"CONFIG_CAMERA_GC0308=y",
|
||||
"CONFIG_CAMERA_GC0308_AUTO_DETECT_DVP_INTERFACE_SENSOR=y",
|
||||
"CONFIG_CAMERA_GC0308_DVP_YUV422_640X480_16FPS=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
414
main/boards/lichuang-dev-bed-operator/lichuang_dev_board.cc
Normal file
414
main/boards/lichuang-dev-bed-operator/lichuang_dev_board.cc
Normal file
@@ -0,0 +1,414 @@
|
||||
#include "wifi_board.h"
|
||||
#include "codecs/box_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "display/emote_display.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "i2c_device.h"
|
||||
#include "esp32_camera.h"
|
||||
#include "assets/lang_config.h"
|
||||
#include "mcp_server.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_system.h>
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include <esp_lcd_touch_ft5x06.h>
|
||||
#include <esp_lvgl_port.h>
|
||||
#include <lvgl.h>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
#define TAG "LichuangDevBoard"
|
||||
|
||||
class Pca9557 : public I2cDevice {
|
||||
public:
|
||||
Pca9557(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
WriteReg(0x01, 0x03);
|
||||
WriteReg(0x03, 0xf8);
|
||||
}
|
||||
|
||||
void SetOutputState(uint8_t bit, uint8_t level) {
|
||||
uint8_t data = ReadReg(0x01);
|
||||
data = (data & ~(1 << bit)) | (level << bit);
|
||||
WriteReg(0x01, data);
|
||||
}
|
||||
};
|
||||
|
||||
class CustomAudioCodec : public BoxAudioCodec {
|
||||
private:
|
||||
Pca9557* pca9557_;
|
||||
|
||||
public:
|
||||
CustomAudioCodec(i2c_master_bus_handle_t i2c_bus, Pca9557* pca9557)
|
||||
: BoxAudioCodec(i2c_bus,
|
||||
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,
|
||||
GPIO_NUM_NC,
|
||||
AUDIO_CODEC_ES8311_ADDR,
|
||||
AUDIO_CODEC_ES7210_ADDR,
|
||||
AUDIO_INPUT_REFERENCE),
|
||||
pca9557_(pca9557) {
|
||||
}
|
||||
|
||||
virtual void EnableOutput(bool enable) override {
|
||||
BoxAudioCodec::EnableOutput(enable);
|
||||
if (enable) {
|
||||
pca9557_->SetOutputState(1, 1);
|
||||
} else {
|
||||
pca9557_->SetOutputState(1, 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief PCF8575 I/O 扩展芯片,地址为 0x20。
|
||||
*/
|
||||
class Pcf8575 : public I2cDevice {
|
||||
private:
|
||||
uint16_t data_ = 0x0000;
|
||||
bool initialized_ = false;
|
||||
|
||||
public:
|
||||
Pcf8575(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
// 复位所有位
|
||||
if (i2c_master_transmit(i2c_device_, (uint8_t*)&data_, 2, 100) != ESP_OK) {
|
||||
initialized_ = false;
|
||||
} else {
|
||||
initialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SetBit(uint8_t bit, uint8_t level) {
|
||||
if (initialized_) {
|
||||
data_ = (data_ & ~(1 << bit)) | (level << bit);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_transmit(i2c_device_, (uint8_t*)&data_, 2, 100));
|
||||
}
|
||||
}
|
||||
|
||||
bool IsInitialized() const {
|
||||
return initialized_;
|
||||
}
|
||||
};
|
||||
|
||||
class LichuangDevBoard : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
i2c_master_dev_handle_t pca9557_handle_;
|
||||
Button boot_button_;
|
||||
LcdDisplay* display_;
|
||||
Pca9557* pca9557_ = nullptr;
|
||||
Pcf8575* pcf8575_ = nullptr;
|
||||
Esp32Camera* camera_;
|
||||
std::atomic<bool> bed_operating_{false};
|
||||
|
||||
/**
|
||||
* @brief 控制床的某个功能
|
||||
* @param bit 要控制的位
|
||||
* @param duration_ms 持续时间,单位毫秒
|
||||
* @return 返回值
|
||||
*/
|
||||
ReturnValue ControlBed(int bit, int duration_ms = 12000) {
|
||||
if (bed_operating_) {
|
||||
throw std::runtime_error("Bed is already operating");
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "ControlBed(%d, %d)", bit, duration_ms);
|
||||
bed_operating_ = true;
|
||||
std::thread([this, bit, duration_ms]() {
|
||||
// High level to trigger
|
||||
pcf8575_->SetBit(bit, 1);
|
||||
// Duration in milliseconds
|
||||
int count = duration_ms / 100;
|
||||
for (int i = 0; i < count && bed_operating_; i++) {
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
// Low level to stop
|
||||
pcf8575_->SetBit(bit, 0);
|
||||
bed_operating_ = false;
|
||||
}).detach();
|
||||
|
||||
return "{\"success\": true, \"message\": \"Bed is operating now\"}";
|
||||
}
|
||||
|
||||
void InitializeTools() {
|
||||
// 初始化工具
|
||||
auto& mcp_server = McpServer::GetInstance();
|
||||
mcp_server.AddTool("bed.adjust", "床位调整\n"
|
||||
"Args: \n"
|
||||
" action: 动作,支持以下动作:raise_back(升高靠背),lower_back(降低靠背),raise_leg(升高腿部),lower_leg(降低腿部),lean_left(靠左倾斜),lean_right(靠右倾斜)\n"
|
||||
" full_adjust: 是否为完整调整(持续12秒),否则为单次调整\n",
|
||||
PropertyList({
|
||||
Property("action", kPropertyTypeString),
|
||||
Property("full_adjust", kPropertyTypeBoolean, false),
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
auto action = properties["action"].value<std::string>();
|
||||
auto full_adjust = properties["full_adjust"].value<bool>();
|
||||
int duration_ms = 2000;
|
||||
if (full_adjust) {
|
||||
duration_ms = 12000;
|
||||
}
|
||||
if (action == "raise_back") {
|
||||
return ControlBed(0, duration_ms);
|
||||
} else if (action == "lower_back") {
|
||||
return ControlBed(1, duration_ms);
|
||||
} else if (action == "raise_leg") {
|
||||
return ControlBed(2, duration_ms);
|
||||
} else if (action == "lower_leg") {
|
||||
return ControlBed(3, duration_ms);
|
||||
} else if (action == "lean_left") {
|
||||
return ControlBed(4, duration_ms);
|
||||
} else if (action == "lean_right") {
|
||||
return ControlBed(5, duration_ms);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid action: " + action);
|
||||
}
|
||||
});
|
||||
mcp_server.AddTool("bed.open_toilet", "便盆打开", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
return ControlBed(6);
|
||||
});
|
||||
mcp_server.AddTool("bed.close_toilet", "便盆关闭", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
return ControlBed(7);
|
||||
});
|
||||
mcp_server.AddTool("bed.auto_flip_a", "自动翻身A", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
return ControlBed(8, 1000);
|
||||
});
|
||||
mcp_server.AddTool("bed.auto_flip_b", "自动翻身B", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
return ControlBed(9, 1000);
|
||||
});
|
||||
mcp_server.AddTool("bed.stop", "停止操作。如用户要求停下来或取消当前操作,必须先调用后回答", PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
if (!bed_operating_) {
|
||||
return "{\"success\": false, \"message\": \"No operation is in progress\"}";
|
||||
}
|
||||
bed_operating_ = false;
|
||||
return "{\"success\": true, \"message\": \"Operation cancelled\"}";
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = (i2c_port_t)1,
|
||||
.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_));
|
||||
|
||||
// Initialize PCA9557
|
||||
pca9557_ = new Pca9557(i2c_bus_, 0x19);
|
||||
pcf8575_ = new Pcf8575(i2c_bus_, 0x20);
|
||||
}
|
||||
|
||||
void InitializeSpi() {
|
||||
spi_bus_config_t buscfg = {};
|
||||
buscfg.mosi_io_num = GPIO_NUM_40;
|
||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||
buscfg.sclk_io_num = GPIO_NUM_41;
|
||||
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(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||||
}
|
||||
|
||||
void InitializeButtons() {
|
||||
boot_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
// During startup (before connected), pressing BOOT button enters Wi-Fi config mode without reboot
|
||||
if (app.GetDeviceState() == kDeviceStateStarting) {
|
||||
EnterWifiConfigMode();
|
||||
return;
|
||||
}
|
||||
app.ToggleChatState();
|
||||
});
|
||||
|
||||
#if CONFIG_USE_DEVICE_AEC
|
||||
boot_button_.OnDoubleClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateIdle) {
|
||||
app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff);
|
||||
}
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
void InitializeSt7789Display() {
|
||||
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 = GPIO_NUM_NC;
|
||||
io_config.dc_gpio_num = GPIO_NUM_39;
|
||||
io_config.spi_mode = 2;
|
||||
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(SPI3_HOST, &io_config, &panel_io));
|
||||
|
||||
// 初始化液晶屏驱动芯片ST7789
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
esp_lcd_panel_dev_config_t panel_config = {};
|
||||
panel_config.reset_gpio_num = GPIO_NUM_NC;
|
||||
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_lcd_panel_reset(panel);
|
||||
pca9557_->SetOutputState(0, 0);
|
||||
|
||||
esp_lcd_panel_init(panel);
|
||||
esp_lcd_panel_invert_color(panel, true);
|
||||
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
|
||||
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||||
esp_lcd_panel_disp_on_off(panel, true);
|
||||
|
||||
#if CONFIG_USE_EMOTE_MESSAGE_STYLE
|
||||
display_ = new emote::EmoteDisplay(panel, panel_io, DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
||||
#else
|
||||
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);
|
||||
#endif
|
||||
}
|
||||
|
||||
void InitializeTouch()
|
||||
{
|
||||
esp_lcd_touch_handle_t tp;
|
||||
esp_lcd_touch_config_t tp_cfg = {
|
||||
.x_max = DISPLAY_HEIGHT,
|
||||
.y_max = DISPLAY_WIDTH,
|
||||
.rst_gpio_num = GPIO_NUM_NC, // Shared with LCD reset
|
||||
.int_gpio_num = GPIO_NUM_NC,
|
||||
.levels = {
|
||||
.reset = 0,
|
||||
.interrupt = 0,
|
||||
},
|
||||
.flags = {
|
||||
.swap_xy = 1,
|
||||
.mirror_x = 1,
|
||||
.mirror_y = 0,
|
||||
},
|
||||
};
|
||||
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
|
||||
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
|
||||
tp_io_config.scl_speed_hz = 400000;
|
||||
|
||||
esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle);
|
||||
esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp);
|
||||
assert(tp);
|
||||
|
||||
/* Add touch input (for selected screen) */
|
||||
const lvgl_port_touch_cfg_t touch_cfg = {
|
||||
.disp = lv_display_get_default(),
|
||||
.handle = tp,
|
||||
};
|
||||
|
||||
if(touch_cfg.disp) {
|
||||
lvgl_port_add_touch(&touch_cfg);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Touch display is not initialized");
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeCamera() {
|
||||
// Open camera power
|
||||
pca9557_->SetOutputState(2, 0);
|
||||
|
||||
static esp_cam_ctlr_dvp_pin_config_t dvp_pin_config = {
|
||||
.data_width = CAM_CTLR_DATA_WIDTH_8,
|
||||
.data_io = {
|
||||
[0] = CAMERA_PIN_D0,
|
||||
[1] = CAMERA_PIN_D1,
|
||||
[2] = CAMERA_PIN_D2,
|
||||
[3] = CAMERA_PIN_D3,
|
||||
[4] = CAMERA_PIN_D4,
|
||||
[5] = CAMERA_PIN_D5,
|
||||
[6] = CAMERA_PIN_D6,
|
||||
[7] = CAMERA_PIN_D7,
|
||||
},
|
||||
.vsync_io = CAMERA_PIN_VSYNC,
|
||||
.de_io = CAMERA_PIN_HREF,
|
||||
.pclk_io = CAMERA_PIN_PCLK,
|
||||
.xclk_io = CAMERA_PIN_XCLK,
|
||||
};
|
||||
|
||||
esp_video_init_sccb_config_t sccb_config = {
|
||||
.init_sccb = false,
|
||||
.i2c_handle = i2c_bus_,
|
||||
.freq = 100000,
|
||||
};
|
||||
|
||||
esp_video_init_dvp_config_t dvp_config = {
|
||||
.sccb_config = sccb_config,
|
||||
.reset_pin = CAMERA_PIN_RESET,
|
||||
.pwdn_pin = CAMERA_PIN_PWDN,
|
||||
.dvp_pin = dvp_pin_config,
|
||||
.xclk_freq = XCLK_FREQ_HZ,
|
||||
};
|
||||
|
||||
esp_video_init_config_t video_config = {
|
||||
.dvp = &dvp_config,
|
||||
};
|
||||
|
||||
camera_ = new Esp32Camera(video_config);
|
||||
}
|
||||
|
||||
public:
|
||||
LichuangDevBoard() : boot_button_(BOOT_BUTTON_GPIO) {
|
||||
InitializeI2c();
|
||||
InitializeSpi();
|
||||
InitializeSt7789Display();
|
||||
InitializeTouch();
|
||||
InitializeButtons();
|
||||
InitializeCamera();
|
||||
GetBacklight()->RestoreBrightness();
|
||||
|
||||
if (pcf8575_->IsInitialized()) {
|
||||
InitializeTools();
|
||||
} else {
|
||||
// PCF8575 initialization failed, show error and reboot after 30 seconds
|
||||
ESP_LOGE(TAG, "PCF8575 initialization failed, will reboot in 30 seconds");
|
||||
display_->SetStatus(Lang::Strings::ERROR);
|
||||
display_->SetEmotion("triangle_exclamation");
|
||||
display_->SetChatMessage("system", "PCF8575 not connected\nReboot in 30s...");
|
||||
vTaskDelay(pdMS_TO_TICKS(30000));
|
||||
esp_restart();
|
||||
}
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static CustomAudioCodec audio_codec(
|
||||
i2c_bus_,
|
||||
pca9557_);
|
||||
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 Camera* GetCamera() override {
|
||||
return camera_;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(LichuangDevBoard);
|
||||
@@ -19,8 +19,7 @@ dependencies:
|
||||
espressif/esp_lcd_panel_io_additions: ^1.0.1
|
||||
78/esp_lcd_nv3023: ~1.0.0
|
||||
78/esp-wifi-connect: ~3.0.2
|
||||
espressif/esp_audio_effects: ~1.2.0
|
||||
espressif/esp_audio_codec: ~2.4.0
|
||||
78/esp-opus-encoder: ~2.4.1
|
||||
78/esp-ml307: ~3.5.3
|
||||
78/xiaozhi-fonts: ~1.5.5
|
||||
espressif/led_strip: ~3.0.1
|
||||
|
||||
@@ -278,7 +278,7 @@ if __name__ == "__main__":
|
||||
|
||||
# Compile mode
|
||||
board_type_input: str = args.board
|
||||
name_filter: Optional[str] = args.name
|
||||
name_filter: str | None = args.name
|
||||
|
||||
# Check board_type in CMakeLists
|
||||
if board_type_input != "all" and not _board_type_exists(board_type_input):
|
||||
|
||||
Reference in New Issue
Block a user