优化c3上的内存使用

This commit is contained in:
Terrence
2025-02-18 03:05:00 +08:00
parent 93915cd624
commit ade1e3193d
5 changed files with 30 additions and 25 deletions

View File

@@ -54,7 +54,7 @@ void Application::CheckNewVersion() {
while (true) { while (true) {
if (ota_.CheckVersion()) { if (ota_.CheckVersion()) {
if (ota_.HasNewVersion()) { if (ota_.HasNewVersion()) {
Alert("OTA 升级", "正在升级系统", "happy", std::string(p3_upgrade_start, p3_upgrade_end - p3_upgrade_start)); Alert("OTA 升级", "正在升级系统", "happy", std::string_view(p3_upgrade_start, p3_upgrade_end - p3_upgrade_start));
// Wait for the chat state to be idle // Wait for the chat state to be idle
do { do {
vTaskDelay(pdMS_TO_TICKS(3000)); vTaskDelay(pdMS_TO_TICKS(3000));
@@ -123,33 +123,37 @@ void Application::ShowActivationCode() {
struct digit_sound { struct digit_sound {
char digit; char digit;
const char* sound_data_start; const char* data;
const char* sound_data_end; size_t size;
}; };
digit_sound digit_sounds[] = { static const std::array<digit_sound, 10> digit_sounds{{
{'0', p3_0_start, p3_0_end}, digit_sound{'0', p3_0_start, size_t(p3_0_end - p3_0_start)},
{'1', p3_1_start, p3_1_end}, digit_sound{'1', p3_1_start, size_t(p3_1_end - p3_1_start)},
{'2', p3_2_start, p3_2_end}, digit_sound{'2', p3_2_start, size_t(p3_2_end - p3_2_start)},
{'3', p3_3_start, p3_3_end}, digit_sound{'3', p3_3_start, size_t(p3_3_end - p3_3_start)},
{'4', p3_4_start, p3_4_end}, digit_sound{'4', p3_4_start, size_t(p3_4_end - p3_4_start)},
{'5', p3_5_start, p3_5_end}, digit_sound{'5', p3_5_start, size_t(p3_5_end - p3_5_start)},
{'6', p3_6_start, p3_6_end}, digit_sound{'6', p3_6_start, size_t(p3_6_end - p3_6_start)},
{'7', p3_7_start, p3_7_end}, digit_sound{'7', p3_7_start, size_t(p3_7_end - p3_7_start)},
{'8', p3_8_start, p3_8_end}, digit_sound{'8', p3_8_start, size_t(p3_8_end - p3_8_start)},
{'9', p3_9_start, p3_9_end}, digit_sound{'9', p3_9_start, size_t(p3_9_end - p3_9_start)}
}; }};
std::string sound = std::string(p3_activation_start, p3_activation_end - p3_activation_start);
// This sentence uses 9KB of SRAM, so we need to wait for it to finish
Alert("激活设备", message, "happy", std::string_view(p3_activation_start, p3_activation_end - p3_activation_start));
vTaskDelay(pdMS_TO_TICKS(1000));
background_task_->WaitForCompletion();
for (const auto& digit : code) { for (const auto& digit : code) {
auto it = std::find_if(digit_sounds, digit_sounds + sizeof(digit_sounds) / sizeof(digit_sound), auto it = std::find_if(digit_sounds.begin(), digit_sounds.end(),
[digit](const digit_sound& ds) { return ds.digit == digit; }); [digit](const digit_sound& ds) { return ds.digit == digit; });
if (it != digit_sounds + sizeof(digit_sounds) / sizeof(digit_sound)) { if (it != digit_sounds.end()) {
sound += std::string(it->sound_data_start, it->sound_data_end - it->sound_data_start); PlayLocalFile(it->data, it->size);
} }
} }
Alert("激活设备", message, "happy", sound);
} }
void Application::Alert(const std::string& status, const std::string& message, const std::string& emotion, const std::string& sound) { void Application::Alert(const std::string& status, const std::string& message, const std::string& emotion, const std::string_view& sound) {
ESP_LOGW(TAG, "Alert %s: %s [%s]", status.c_str(), message.c_str(), emotion.c_str()); ESP_LOGW(TAG, "Alert %s: %s [%s]", status.c_str(), message.c_str(), emotion.c_str());
auto display = Board::GetInstance().GetDisplay(); auto display = Board::GetInstance().GetDisplay();
display->SetStatus(status); display->SetStatus(status);

View File

@@ -56,7 +56,7 @@ public:
bool IsVoiceDetected() const { return voice_detected_; } bool IsVoiceDetected() const { return voice_detected_; }
void Schedule(std::function<void()> callback); void Schedule(std::function<void()> callback);
void SetDeviceState(DeviceState state); void SetDeviceState(DeviceState state);
void Alert(const std::string& status, const std::string& message, const std::string& emotion = "", const std::string& sound = ""); void Alert(const std::string& status, const std::string& message, const std::string& emotion = "", const std::string_view& sound = "");
void AbortSpeaking(AbortReason reason); void AbortSpeaking(AbortReason reason);
void ToggleChatState(); void ToggleChatState();
void StartListening(); void StartListening();

View File

@@ -48,10 +48,10 @@ void Ml307Board::WaitForNetworkReady() {
display->SetStatus("等待网络..."); display->SetStatus("等待网络...");
int result = modem_.WaitForNetworkReady(); int result = modem_.WaitForNetworkReady();
if (result == -1) { if (result == -1) {
application.Alert("PIN_ERROR", "请插入SIM卡", "sad", std::string(p3_err_pin_start, p3_err_pin_end - p3_err_pin_start)); application.Alert("PIN_ERROR", "请插入SIM卡", "sad", std::string_view(p3_err_pin_start, p3_err_pin_end - p3_err_pin_start));
return; return;
} else if (result == -2) { } else if (result == -2) {
application.Alert("REG_ERROR", "无法接入网络,请检查流量卡状态", "sad", std::string(p3_err_reg_start, p3_err_reg_end - p3_err_reg_start)); application.Alert("REG_ERROR", "无法接入网络,请检查流量卡状态", "sad", std::string_view(p3_err_reg_start, p3_err_reg_end - p3_err_reg_start));
return; return;
} }

View File

@@ -52,7 +52,7 @@ void WifiBoard::EnterWifiConfigMode() {
hint += "\n\n"; hint += "\n\n";
// 播报配置 WiFi 的提示 // 播报配置 WiFi 的提示
application.Alert("配网模式", hint, "", std::string(p3_wificonfig_start, p3_wificonfig_end - p3_wificonfig_start)); application.Alert("配网模式", hint, "", std::string_view(p3_wificonfig_start, p3_wificonfig_end - p3_wificonfig_start));
// Wait forever until reset after configuration // Wait forever until reset after configuration
while (true) { while (true) {

View File

@@ -21,6 +21,7 @@ CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=n CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=n
CONFIG_ESP_WIFI_IRAM_OPT=n CONFIG_ESP_WIFI_IRAM_OPT=n
CONFIG_ESP_WIFI_RX_IRAM_OPT=n CONFIG_ESP_WIFI_RX_IRAM_OPT=n
CONFIG_ESP_WIFI_DYNAMIC_RX_MGMT_BUFFER=y
CONFIG_CODEC_I2C_BACKWARD_COMPATIBLE=n CONFIG_CODEC_I2C_BACKWARD_COMPATIBLE=n