Bump to 1.3.0

This commit is contained in:
Terrence
2025-02-24 14:41:34 +08:00
parent c08a1a5310
commit a23a88cc5d
14 changed files with 84 additions and 22 deletions

View File

@@ -36,9 +36,24 @@ static const char* const STATE_STRINGS[] = {
Application::Application() {
event_group_ = xEventGroupCreate();
background_task_ = new BackgroundTask(4096 * 8);
esp_timer_create_args_t clock_timer_args = {
.callback = [](void* arg) {
Application* app = (Application*)arg;
app->OnClockTimer();
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "clock_timer"
};
esp_timer_create(&clock_timer_args, &clock_timer_handle_);
}
Application::~Application() {
if (clock_timer_handle_ != nullptr) {
esp_timer_stop(clock_timer_handle_);
esp_timer_delete(clock_timer_handle_);
}
if (background_task_ != nullptr) {
delete background_task_;
}
@@ -225,7 +240,6 @@ void Application::ToggleChatState() {
if (device_state_ == kDeviceStateIdle) {
SetDeviceState(kDeviceStateConnecting);
if (!protocol_->OpenAudioChannel()) {
Alert(Lang::Strings::ERROR, Lang::Strings::UNABLE_TO_ESTABLISH_AUDIO_CHANNEL, "sad");
SetDeviceState(kDeviceStateIdle);
return;
}
@@ -259,7 +273,6 @@ void Application::StartListening() {
SetDeviceState(kDeviceStateConnecting);
if (!protocol_->OpenAudioChannel()) {
SetDeviceState(kDeviceStateIdle);
Alert(Lang::Strings::ERROR, Lang::Strings::UNABLE_TO_ESTABLISH_AUDIO_CHANNEL, "sad");
return;
}
}
@@ -506,6 +519,33 @@ void Application::Start() {
#endif
SetDeviceState(kDeviceStateIdle);
esp_timer_start_periodic(clock_timer_handle_, 1000000);
}
void Application::OnClockTimer() {
static int count = 0;
count++;
// Print the debug info every 10 seconds
if (count % 10 == 0) {
// SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
ESP_LOGI(TAG, "Free internal: %u minimal internal: %u", free_sram, min_free_sram);
// If we have synchronized server time, set the status to clock "HH:MM" if the device is idle
if (ota_.HasServerTime()) {
Schedule([this]() {
if (device_state_ == kDeviceStateIdle) {
// Set status to clock "HH:MM"
time_t now = time(NULL);
char time_str[64];
strftime(time_str, sizeof(time_str), "%H:%M ", localtime(&now));
Board::GetInstance().GetDisplay()->SetStatus(time_str);
}
});
}
}
}
void Application::Schedule(std::function<void()> callback) {

View File

@@ -4,6 +4,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <esp_timer.h>
#include <string>
#include <mutex>
@@ -77,7 +78,8 @@ private:
std::mutex mutex_;
std::list<std::function<void()>> main_tasks_;
std::unique_ptr<Protocol> protocol_;
EventGroupHandle_t event_group_;
EventGroupHandle_t event_group_ = nullptr;
esp_timer_handle_t clock_timer_handle_ = nullptr;
volatile DeviceState device_state_ = kDeviceStateUnknown;
bool keep_listening_ = false;
bool aborted_ = false;
@@ -104,7 +106,7 @@ private:
void SetDecodeSampleRate(int sample_rate);
void CheckNewVersion();
void ShowActivationCode();
void OnClockTimer();
void PlayLocalFile(const char* data, size_t size);
};

View File

@@ -22,7 +22,7 @@
"LISTENING": "Listening...",
"SPEAKING": "Speaking...",
"UNABLE_TO_CONNECT_TO_SERVICE": "Unable to connect to service",
"UNABLE_TO_CONNECT_TO_SERVICE": "Unable to connect to service, please try again later",
"WAITING_FOR_RESPONSE_TIMEOUT": "Waiting for response timeout",
"SENDING_FAILED_PLEASE_CHECK_THE_NETWORK": "Sending failed, please check the network",

View File

@@ -37,7 +37,7 @@ private:
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Power Save Timer",
.name = "power_save_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&power_save_timer_args, &power_save_timer_));

View File

@@ -38,7 +38,7 @@ private:
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Power Save Timer",
.name = "power_save_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&power_save_timer_args, &power_save_timer_));

View File

@@ -27,7 +27,7 @@ Display::Display() {
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Notification Timer",
.name = "notification_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&notification_timer_args, &notification_timer_));
@@ -40,7 +40,7 @@ Display::Display() {
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Update Display Timer",
.name = "update_display_timer",
.skip_unhandled_events = true,
};
ESP_ERROR_CHECK(esp_timer_create(&update_display_timer_args, &update_timer_));

View File

@@ -38,7 +38,7 @@ CircularStrip::CircularStrip(gpio_num_t gpio, uint8_t max_leds) : max_leds_(max_
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Strip Timer",
.name = "strip_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&strip_timer_args, &strip_timer_));

View File

@@ -22,7 +22,7 @@ Led::Led(gpio_num_t gpio, uint8_t max_leds) {
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Led Strip Timer",
.name = "led_strip_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&led_strip_timer_args, &led_strip_timer_));

View File

@@ -34,7 +34,7 @@ SingleLed::SingleLed(gpio_num_t gpio) {
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "Blink Timer",
.name = "blink_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&blink_timer_args, &blink_timer_));

View File

@@ -26,13 +26,5 @@ extern "C" void app_main(void)
// Launch the application
Application::GetInstance().Start();
// Dump CPU usage every 10 second
while (true) {
vTaskDelay(10000 / portTICK_PERIOD_MS);
// SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
ESP_LOGI(TAG, "Free internal: %u minimal internal: %u", free_sram, min_free_sram);
}
// The main thread will exit and release the stack memory
}

View File

@@ -100,6 +100,29 @@ bool Ota::CheckVersion() {
has_mqtt_config_ = true;
}
has_server_time_ = false;
cJSON *server_time = cJSON_GetObjectItem(root, "server_time");
if (server_time != NULL) {
cJSON *timestamp = cJSON_GetObjectItem(server_time, "timestamp");
cJSON *timezone_offset = cJSON_GetObjectItem(server_time, "timezone_offset");
if (timestamp != NULL) {
// 设置系统时间
struct timeval tv;
double ts = timestamp->valuedouble;
// 如果有时区偏移,计算本地时间
if (timezone_offset != NULL) {
ts += (timezone_offset->valueint * 60 * 1000); // 转换分钟为毫秒
}
tv.tv_sec = (time_t)(ts / 1000); // 转换毫秒为秒
tv.tv_usec = (suseconds_t)((long long)ts % 1000) * 1000; // 剩余的毫秒转换为微秒
settimeofday(&tv, NULL);
has_server_time_ = true;
}
}
cJSON *firmware = cJSON_GetObjectItem(root, "firmware");
if (firmware == NULL) {
ESP_LOGE(TAG, "Failed to get firmware object");

View File

@@ -17,6 +17,7 @@ public:
bool HasNewVersion() { return has_new_version_; }
bool HasMqttConfig() { return has_mqtt_config_; }
bool HasActivationCode() { return has_activation_code_; }
bool HasServerTime() { return has_server_time_; }
void StartUpgrade(std::function<void(int progress, size_t speed)> callback);
void MarkCurrentVersionValid();
@@ -31,6 +32,7 @@ private:
std::string activation_code_;
bool has_new_version_ = false;
bool has_mqtt_config_ = false;
bool has_server_time_ = false;
bool has_activation_code_ = false;
std::string current_version_;
std::string firmware_version_;

View File

@@ -46,6 +46,9 @@ bool MqttProtocol::StartMqttClient() {
if (endpoint_.empty()) {
ESP_LOGE(TAG, "MQTT endpoint is not specified");
if (on_network_error_ != nullptr) {
on_network_error_(Lang::Strings::UNABLE_TO_CONNECT_TO_SERVICE);
}
return false;
}