forked from xiaozhi/xiaozhi-esp32
* Upgrade component version * update fonts component version * Handle OTA error code * Update project version to 2.1.0 and add device state machine implementation - Upgrade esp-wifi-connect to 3.0.0, allowing reconfiguring wifi without rebooting - Introduce device state machine with state change notification in new files - Remove obsolete device state event files - Update application logic to utilize new state machine - Minor adjustments in various board implementations for state handling * fix compile errors * Refactor power saving mode implementation to use PowerSaveLevel enumeration - Updated Application class to replace SetPowerSaveMode with SetPowerSaveLevel, allowing for LOW_POWER and PERFORMANCE settings. - Modified various board implementations to align with the new power save level structure. - Ensured consistent handling of power save levels across different board files, enhancing code maintainability and clarity. * Refactor power save level checks across multiple board implementations - Updated the condition for power save level checks in various board files to ensure that the power save timer only wakes up when the level is not set to LOW_POWER. - Improved consistency in handling power save levels, enhancing code clarity and maintainability. * Refactor EnterWifiConfigMode calls in board implementations - Updated calls to EnterWifiConfigMode to use the appropriate instance reference (self or board) across multiple board files. - Improved code consistency and clarity in handling device state during WiFi configuration mode entry. * Add cellular modem event handling and improve network status updates - Introduced new network events for cellular modem operations, including detecting, registration errors, and timeouts. - Enhanced the Application class to handle different network states and update the display status accordingly. - Refactored Ml307Board to implement a callback mechanism for network events, improving modularity and responsiveness. - Updated dual_network_board and board headers to support new network event callbacks, ensuring consistent handling across board implementations. * update esp-wifi-connect version * Update WiFi configuration tool messages across multiple board implementations to clarify user actions
186 lines
5.3 KiB
C++
186 lines
5.3 KiB
C++
#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 <memory>
|
|
|
|
#include "protocol.h"
|
|
#include "ota.h"
|
|
#include "audio_service.h"
|
|
#include "device_state.h"
|
|
#include "device_state_machine.h"
|
|
|
|
// Main event bits
|
|
#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_ACTIVATION_DONE (1 << 5)
|
|
#define MAIN_EVENT_CLOCK_TICK (1 << 6)
|
|
#define MAIN_EVENT_NETWORK_CONNECTED (1 << 7)
|
|
#define MAIN_EVENT_NETWORK_DISCONNECTED (1 << 8)
|
|
#define MAIN_EVENT_TOGGLE_CHAT (1 << 9)
|
|
#define MAIN_EVENT_START_LISTENING (1 << 10)
|
|
#define MAIN_EVENT_STOP_LISTENING (1 << 11)
|
|
#define MAIN_EVENT_STATE_CHANGED (1 << 12)
|
|
|
|
|
|
enum AecMode {
|
|
kAecOff,
|
|
kAecOnDeviceSide,
|
|
kAecOnServerSide,
|
|
};
|
|
|
|
class Application {
|
|
public:
|
|
static Application& GetInstance() {
|
|
static Application instance;
|
|
return instance;
|
|
}
|
|
// Delete copy constructor and assignment operator
|
|
Application(const Application&) = delete;
|
|
Application& operator=(const Application&) = delete;
|
|
|
|
/**
|
|
* Initialize the application
|
|
* This sets up display, audio, network callbacks, etc.
|
|
* Network connection starts asynchronously.
|
|
*/
|
|
void Initialize();
|
|
|
|
/**
|
|
* Run the main event loop
|
|
* This function runs in the main task and never returns.
|
|
* It handles all events including network, state changes, and user interactions.
|
|
*/
|
|
void Run();
|
|
|
|
DeviceState GetDeviceState() const { return state_machine_.GetState(); }
|
|
bool IsVoiceDetected() const { return audio_service_.IsVoiceDetected(); }
|
|
|
|
/**
|
|
* Request state transition
|
|
* Returns true if transition was successful
|
|
*/
|
|
bool SetDeviceState(DeviceState state);
|
|
|
|
/**
|
|
* Schedule a callback to be executed in the main task
|
|
*/
|
|
void Schedule(std::function<void()> callback);
|
|
|
|
/**
|
|
* Alert with status, message, emotion and optional sound
|
|
*/
|
|
void Alert(const char* status, const char* message, const char* emotion = "", const std::string_view& sound = "");
|
|
void DismissAlert();
|
|
|
|
void AbortSpeaking(AbortReason reason);
|
|
|
|
/**
|
|
* Toggle chat state (event-based, thread-safe)
|
|
* Sends MAIN_EVENT_TOGGLE_CHAT to be handled in Run()
|
|
*/
|
|
void ToggleChatState();
|
|
|
|
/**
|
|
* Start listening (event-based, thread-safe)
|
|
* Sends MAIN_EVENT_START_LISTENING to be handled in Run()
|
|
*/
|
|
void StartListening();
|
|
|
|
/**
|
|
* Stop listening (event-based, thread-safe)
|
|
* Sends MAIN_EVENT_STOP_LISTENING to be handled in Run()
|
|
*/
|
|
void StopListening();
|
|
|
|
void Reboot();
|
|
void WakeWordInvoke(const std::string& wake_word);
|
|
bool UpgradeFirmware(const std::string& url, const std::string& version = "");
|
|
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_; }
|
|
|
|
/**
|
|
* Reset protocol resources (thread-safe)
|
|
* Can be called from any task to release resources allocated after network connected
|
|
* This includes closing audio channel, resetting protocol and ota objects
|
|
*/
|
|
void ResetProtocol();
|
|
|
|
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;
|
|
DeviceStateMachine state_machine_;
|
|
ListeningMode listening_mode_ = kListeningModeAutoStop;
|
|
AecMode aec_mode_ = kAecOff;
|
|
std::string last_error_message_;
|
|
AudioService audio_service_;
|
|
std::unique_ptr<Ota> ota_;
|
|
|
|
bool has_server_time_ = false;
|
|
bool aborted_ = false;
|
|
bool assets_version_checked_ = false;
|
|
int clock_ticks_ = 0;
|
|
TaskHandle_t activation_task_handle_ = nullptr;
|
|
|
|
|
|
// Event handlers
|
|
void HandleStateChangedEvent();
|
|
void HandleToggleChatEvent();
|
|
void HandleStartListeningEvent();
|
|
void HandleStopListeningEvent();
|
|
void HandleNetworkConnectedEvent();
|
|
void HandleNetworkDisconnectedEvent();
|
|
void HandleActivationDoneEvent();
|
|
void HandleWakeWordDetectedEvent();
|
|
|
|
// Activation task (runs in background)
|
|
void ActivationTask();
|
|
|
|
// Helper methods
|
|
void CheckAssetsVersion();
|
|
void CheckNewVersion();
|
|
void InitializeProtocol();
|
|
void ShowActivationCode(const std::string& code, const std::string& message);
|
|
void SetListeningMode(ListeningMode mode);
|
|
|
|
// State change handler called by state machine
|
|
void OnStateChanged(DeviceState old_state, DeviceState new_state);
|
|
};
|
|
|
|
|
|
class TaskPriorityReset {
|
|
public:
|
|
TaskPriorityReset(BaseType_t priority) {
|
|
original_priority_ = uxTaskPriorityGet(NULL);
|
|
vTaskPrioritySet(NULL, priority);
|
|
}
|
|
~TaskPriorityReset() {
|
|
vTaskPrioritySet(NULL, original_priority_);
|
|
}
|
|
|
|
private:
|
|
BaseType_t original_priority_;
|
|
};
|
|
|
|
#endif // _APPLICATION_H_
|