48f208b2e6
2. 试着测试了一下LVGL_GIF渲染+音乐播放+语音识别的组合简单优化后,
发现lvgl渲染略显卡顿,语音识别有缓冲区空警告,不过无伤大雅,还需要进一步深度优化。
212 lines
7.3 KiB
C++
212 lines
7.3 KiB
C++
//
|
|
// Created by misaki on 2025/9/2.
|
|
//
|
|
#include "CppHandle.h"
|
|
|
|
#include "OTAClass.h"
|
|
#include "PetBaseClass.h"
|
|
#include "PetDao.h"
|
|
#include <iostream>
|
|
|
|
void testPetSystem() {
|
|
std::cout << "Test point1" << std::endl;
|
|
// 创建阶段策略
|
|
auto stageStrategy = std::make_unique<PetStageStrategy>();
|
|
stageStrategy->addStage(PetStageType::PET_STAGE_YOUNG, "models/young.obj");
|
|
stageStrategy->addStage(PetStageType::PET_STAGE_ADULT, "models/adult.obj");
|
|
stageStrategy->addStage(PetStageType::PET_STAGE_OLD, "models/old.obj");
|
|
stageStrategy->addStageAudio(PetStageType::PET_STAGE_YOUNG, "audio/young.mp3");
|
|
stageStrategy->addStageAudio(PetStageType::PET_STAGE_ADULT, "audio/adult.mp3");
|
|
stageStrategy->addStageAudio(PetStageType::PET_STAGE_OLD, "audio/old.mp3");
|
|
|
|
// 创建动作策略
|
|
auto actionStrategy = std::make_unique<PetActionStrategy>();
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_EAT, "models/eat.obj");
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_HAPPY, "models/happy.obj");
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_SLEEP, "models/sleep.obj");
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_ANGRY, "models/angry.obj");
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_SAD, "models/sad.obj");
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_EVOLVE, "models/evolve.obj");
|
|
actionStrategy->addAction(PetActionType::PET_ACTION_TOUCH, "models/touch.obj");
|
|
|
|
actionStrategy->addActionAudio(PetActionType::PET_ACTION_EAT, "audio/eat.mp3");
|
|
actionStrategy->addActionAudio(PetActionType::PET_ACTION_HAPPY, "audio/happy.mp3");
|
|
actionStrategy->addActionAudio(PetActionType::PET_ACTION_SLEEP, "audio/sleep.mp3");
|
|
std::cout << "Test point2" << std::endl;
|
|
|
|
// 创建宠物信息
|
|
PetBaseInfo info;
|
|
info.pet_name = "芝士雪豹";
|
|
info.pet_hp = 100;
|
|
info.pet_density = 50;
|
|
info.pet_identity = "我是顶真";
|
|
|
|
// 创建宠物
|
|
auto pet = std::make_shared<PetBase>(info, std::move(stageStrategy), std::move(actionStrategy));
|
|
std::cout << "Test point3" << std::endl;
|
|
|
|
// 创建音频观察者
|
|
auto audioStrategy = std::make_shared<PetAudioStrategy>();
|
|
audioStrategy->setAudioCallback([](const std::string& audioPath) {
|
|
std::cout << "Playing audio: " << audioPath << std::endl;
|
|
});
|
|
audioStrategy->subscribe(pet);
|
|
std::cout << "Test point4" << std::endl;
|
|
|
|
// 创建渲染器观察者
|
|
auto rendererStrategy = std::make_shared<PetRendererStrategy>(
|
|
pet->getStageStrategy(),
|
|
pet->getActionStrategy()
|
|
);
|
|
rendererStrategy->setRenderCallback([](const std::string& modelPath) {
|
|
std::cout << "Rendering model: " << modelPath << std::endl;
|
|
});
|
|
rendererStrategy->subscribe(pet);
|
|
std::cout << "Test point5" << std::endl;
|
|
|
|
// 执行一些动作
|
|
std::cout << "=== Testing basic actions ===" << std::endl;
|
|
pet->feed();
|
|
pet->play();
|
|
pet->touch();
|
|
std::cout << "Test point6" << std::endl;
|
|
|
|
// 检查当前状态
|
|
std::cout << "Current HP: " << pet->getPetInfo().pet_hp << std::endl;
|
|
std::cout << "Current density: " << pet->getPetInfo().pet_density << std::endl;
|
|
std::cout << "Current stage: " << static_cast<int>(pet->getCurrentStage()) << std::endl;
|
|
std::cout << "Current action: " << static_cast<int>(pet->getCurrentAction()) << std::endl;
|
|
std::cout << "Test point7" << std::endl;
|
|
|
|
// 测试进化
|
|
std::cout << "\n=== Testing evolution ===" << std::endl;
|
|
// 直接修改亲密度来测试进化
|
|
PetBaseInfo newInfo = pet->getPetInfo();
|
|
newInfo.pet_density = 100; // 达到进化条件
|
|
pet->setPetInfo(newInfo);
|
|
std::cout << "Test point8" << std::endl;
|
|
|
|
if (pet->checkEvolution()) {
|
|
std::cout << "Evolution successful!" << std::endl;
|
|
} else {
|
|
std::cout << "Evolution failed!" << std::endl;
|
|
}
|
|
|
|
// 进一步增加亲密度到150,尝试再次进化
|
|
newInfo.pet_density = 150;
|
|
pet->setPetInfo(newInfo);
|
|
if (pet->checkEvolution()) {
|
|
std::cout << "Second evolution successful!" << std::endl;
|
|
} else {
|
|
std::cout << "Second evolution failed!" << std::endl;
|
|
}
|
|
|
|
std::cout << "Final stage: " << static_cast<int>(pet->getCurrentStage()) << std::endl;
|
|
|
|
PetDAO petDAO(SDFileManager::getInstance());
|
|
petDAO.savePet(pet, "my_pet.json");
|
|
// 列出所有宠物文件
|
|
auto petFiles = petDAO.listPetFiles();
|
|
for (const auto& file : petFiles) {
|
|
std::cout << "Pet file: " << file << std::endl;
|
|
}
|
|
std::cout << SDFileManager::getInstance()->catCommand("/sdcard/pet_data/my_pet.json") << std::endl;
|
|
}
|
|
|
|
|
|
#include "SpeechRecognizer.h"
|
|
#include <nvs.h>
|
|
#include <nvs_flash.h>
|
|
// 命令回调函数
|
|
void commandCallback(int command_id, const std::string& phrase, float probability) {
|
|
ESP_LOGI("Example", "Received command: ID=%d, Phrase='%s', Probability=%.2f",
|
|
command_id, phrase.c_str(), probability);
|
|
|
|
// 根据命令执行相应操作
|
|
switch (command_id) {
|
|
case 0:
|
|
ESP_LOGI("Example", "执行命令0");
|
|
// 执行命令0的操作
|
|
break;
|
|
case 1:
|
|
ESP_LOGI("Example", "执行命令1");
|
|
// 执行命令1的操作
|
|
break;
|
|
case 2:
|
|
ESP_LOGI("Example", "执行命令2");
|
|
// 执行命令2的操作
|
|
break;
|
|
default:
|
|
ESP_LOGI("Example", "未知的命令ID: %d", command_id);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 状态回调函数
|
|
void stateCallback(const std::string& state) {
|
|
ESP_LOGI("Example", "状态改变到: %s", state.c_str());
|
|
}
|
|
#include "SDFileManager.h"
|
|
void testMIC() {
|
|
// 初始化NVS
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
// 初始化SD卡管理器
|
|
SDFileManager::getInstance()->tryInitSDCard();
|
|
|
|
// 获取SpeechRecognizer实例
|
|
SpeechRecognizer* recognizer = SpeechRecognizer::getInstance();
|
|
|
|
// 配置识别器
|
|
SpeechRecognizerConfig config;
|
|
config.enable_vad = true;
|
|
config.vad_mode = VAD_MODE_3; // 更高的VAD灵敏度
|
|
config.model_path = "/sdcard/srmodels";
|
|
|
|
// 初始化
|
|
if (!recognizer->init(config)) {
|
|
ESP_LOGE("main", "Failed to initialize speech recognizer");
|
|
return;
|
|
}
|
|
|
|
// 添加自定义命令
|
|
std::vector<std::pair<int, std::string>> commands = {
|
|
{0, "kai deng"}, // 开灯
|
|
{1, "guan deng"}, // 关灯
|
|
{2, "ti gao liang du"}, // 提高亮度
|
|
{3, "jiang di liang du"}, // 降低亮度
|
|
{4, "bo fang yin yue"}, // 播放音乐
|
|
{5, "ting zhi bo fang"} // 停止播放
|
|
};
|
|
|
|
if (!recognizer->addCommands(commands)) {
|
|
ESP_LOGE("main", "Failed to add some commands");
|
|
}
|
|
|
|
// 注册回调函数
|
|
recognizer->registerCommandCallback(commandCallback);
|
|
recognizer->registerStateCallback(stateCallback);
|
|
|
|
// 开始识别
|
|
if (!recognizer->start()) {
|
|
ESP_LOGE("main", "Failed to start speech recognition");
|
|
return;
|
|
}
|
|
|
|
ESP_LOGI("main", "Speech recognition system started successfully");
|
|
|
|
}
|
|
|
|
void Cpp_Hand() {
|
|
// testMIC();
|
|
// testPetSystem();
|
|
|
|
OTAClass oc;
|
|
oc.Init();
|
|
}
|