application.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef _APPLICATION_H_
  2. #define _APPLICATION_H_
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/event_groups.h>
  5. #include <freertos/task.h>
  6. #include <esp_timer.h>
  7. #include <string>
  8. #include <mutex>
  9. #include <deque>
  10. #include <vector>
  11. #include <memory>
  12. #include "protocol.h"
  13. #include "ota.h"
  14. #include "audio_service.h"
  15. #include "device_state_event.h"
  16. #define MAIN_EVENT_SCHEDULE (1 << 0)
  17. #define MAIN_EVENT_SEND_AUDIO (1 << 1)
  18. #define MAIN_EVENT_WAKE_WORD_DETECTED (1 << 2)
  19. #define MAIN_EVENT_VAD_CHANGE (1 << 3)
  20. #define MAIN_EVENT_ERROR (1 << 4)
  21. #define MAIN_EVENT_CHECK_NEW_VERSION_DONE (1 << 5)
  22. enum AecMode {
  23. kAecOff,
  24. kAecOnDeviceSide,
  25. kAecOnServerSide,
  26. };
  27. class Application {
  28. public:
  29. static Application& GetInstance() {
  30. static Application instance;
  31. return instance;
  32. }
  33. // 删除拷贝构造函数和赋值运算符
  34. Application(const Application&) = delete;
  35. Application& operator=(const Application&) = delete;
  36. void Start();
  37. DeviceState GetDeviceState() const { return device_state_; }
  38. bool IsVoiceDetected() const { return audio_service_.IsVoiceDetected(); }
  39. void Schedule(std::function<void()> callback);
  40. void SetDeviceState(DeviceState state);
  41. void Alert(const char* status, const char* message, const char* emotion = "", const std::string_view& sound = "");
  42. void DismissAlert();
  43. void AbortSpeaking(AbortReason reason);
  44. void ToggleChatState();
  45. void StartListening();
  46. void StopListening();
  47. void Reboot();
  48. void WakeWordInvoke(const std::string& wake_word);
  49. bool CanEnterSleepMode();
  50. void SendMcpMessage(const std::string& payload);
  51. void SetAecMode(AecMode mode);
  52. AecMode GetAecMode() const { return aec_mode_; }
  53. void PlaySound(const std::string_view& sound);
  54. AudioService& GetAudioService() { return audio_service_;
  55. }
  56. private:
  57. Application();
  58. ~Application();
  59. std::mutex mutex_;
  60. std::deque<std::function<void()>> main_tasks_;
  61. std::unique_ptr<Protocol> protocol_;
  62. EventGroupHandle_t event_group_ = nullptr;
  63. esp_timer_handle_t clock_timer_handle_ = nullptr;
  64. volatile DeviceState device_state_ = kDeviceStateHome;
  65. ListeningMode listening_mode_ = kListeningModeAutoStop;
  66. AecMode aec_mode_ = kAecOff;
  67. std::string last_error_message_;
  68. AudioService audio_service_;
  69. bool has_server_time_ = false;
  70. bool aborted_ = false;
  71. int clock_ticks_ = 0;
  72. std::chrono::system_clock::time_point last_status_update_time_;
  73. std::chrono::system_clock::time_point last_home_update_time_; // 新增:主界面时间更新
  74. TaskHandle_t check_new_version_task_handle_ = nullptr;
  75. int countdown_seconds_; // 倒计时剩余秒数
  76. bool is_countdown_active_; // 倒计时是否激活
  77. DeviceState last_countdown_state_; // 最后设置的倒计时状态
  78. esp_timer_handle_t countdown_timer_handle_=nullptr; // 倒计时定时器
  79. // 倒计时相关方法
  80. void OnCountdownTimer();
  81. void MainEventLoop();
  82. void OnWakeWordDetected();
  83. void CheckNewVersion(Ota& ota);
  84. void ShowActivationCode(const std::string& code, const std::string& message);
  85. void OnClockTimer();
  86. void SetListeningMode(ListeningMode mode);
  87. };
  88. #endif // _APPLICATION_H_