application.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. private:
  56. Application();
  57. ~Application();
  58. std::mutex mutex_;
  59. std::deque<std::function<void()>> main_tasks_;
  60. std::unique_ptr<Protocol> protocol_;
  61. EventGroupHandle_t event_group_ = nullptr;
  62. esp_timer_handle_t clock_timer_handle_ = nullptr;
  63. volatile DeviceState device_state_ = kDeviceStateUnknown;
  64. ListeningMode listening_mode_ = kListeningModeAutoStop;
  65. AecMode aec_mode_ = kAecOff;
  66. std::string last_error_message_;
  67. AudioService audio_service_;
  68. bool has_server_time_ = false;
  69. bool aborted_ = false;
  70. int clock_ticks_ = 0;
  71. TaskHandle_t check_new_version_task_handle_ = nullptr;
  72. void MainEventLoop();
  73. void OnWakeWordDetected();
  74. void CheckNewVersion(Ota& ota);
  75. void ShowActivationCode(const std::string& code, const std::string& message);
  76. void OnClockTimer();
  77. void SetListeningMode(ListeningMode mode);
  78. };
  79. #endif // _APPLICATION_H_