| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #ifndef _APPLICATION_H_
- #define _APPLICATION_H_
- #include <freertos/FreeRTOS.h>
- #include <freertos/event_groups.h>
- #include <freertos/task.h>
- #include <esp_timer.h>
- #include <string>
- #include <mutex>
- #include <deque>
- #include <vector>
- #include <memory>
- #include "protocol.h"
- #include "ota.h"
- #include "audio_service.h"
- #include "device_state_event.h"
- #define MAIN_EVENT_SCHEDULE (1 << 0)
- #define MAIN_EVENT_SEND_AUDIO (1 << 1)
- #define MAIN_EVENT_WAKE_WORD_DETECTED (1 << 2)
- #define MAIN_EVENT_VAD_CHANGE (1 << 3)
- #define MAIN_EVENT_ERROR (1 << 4)
- #define MAIN_EVENT_CHECK_NEW_VERSION_DONE (1 << 5)
- enum AecMode {
- kAecOff,
- kAecOnDeviceSide,
- kAecOnServerSide,
- };
- class Application {
- public:
- static Application& GetInstance() {
- static Application instance;
- return instance;
- }
- // 删除拷贝构造函数和赋值运算符
- Application(const Application&) = delete;
- Application& operator=(const Application&) = delete;
- void Start();
- DeviceState GetDeviceState() const { return device_state_; }
- bool IsVoiceDetected() const { return audio_service_.IsVoiceDetected(); }
- void Schedule(std::function<void()> callback);
- void SetDeviceState(DeviceState state);
- void Alert(const char* status, const char* message, const char* emotion = "", const std::string_view& sound = "");
- void DismissAlert();
- void AbortSpeaking(AbortReason reason);
- void ToggleChatState();
- void StartListening();
- void StopListening();
- void Reboot();
- void WakeWordInvoke(const std::string& wake_word);
- bool CanEnterSleepMode();
- void SendMcpMessage(const std::string& payload);
- void SetAecMode(AecMode mode);
- AecMode GetAecMode() const { return aec_mode_; }
- void PlaySound(const std::string_view& sound);
- AudioService& GetAudioService() { return audio_service_;
-
- }
- private:
- Application();
- ~Application();
- std::mutex mutex_;
- std::deque<std::function<void()>> main_tasks_;
- std::unique_ptr<Protocol> protocol_;
- EventGroupHandle_t event_group_ = nullptr;
- esp_timer_handle_t clock_timer_handle_ = nullptr;
- volatile DeviceState device_state_ = kDeviceStateHome;
- ListeningMode listening_mode_ = kListeningModeAutoStop;
- AecMode aec_mode_ = kAecOff;
- std::string last_error_message_;
- AudioService audio_service_;
- bool has_server_time_ = false;
- bool aborted_ = false;
- int clock_ticks_ = 0;
- std::chrono::system_clock::time_point last_status_update_time_;
- std::chrono::system_clock::time_point last_home_update_time_; // 新增:主界面时间更新
- TaskHandle_t check_new_version_task_handle_ = nullptr;
- int countdown_seconds_; // 倒计时剩余秒数
- bool is_countdown_active_; // 倒计时是否激活
- DeviceState last_countdown_state_; // 最后设置的倒计时状态
- esp_timer_handle_t countdown_timer_handle_=nullptr; // 倒计时定时器
- // 倒计时相关方法
- void OnCountdownTimer();
- void MainEventLoop();
- void OnWakeWordDetected();
- void CheckNewVersion(Ota& ota);
- void ShowActivationCode(const std::string& code, const std::string& message);
- void OnClockTimer();
- void SetListeningMode(ListeningMode mode);
- };
- #endif // _APPLICATION_H_
|