diff --git a/main/application.cc b/main/application.cc index 2cb3db0b8..06016752a 100644 --- a/main/application.cc +++ b/main/application.cc @@ -612,6 +612,44 @@ void Application::MainEventLoop() { } } +void Application::RemoteWakeup(const std::string& reason){ + if (!protocol_) { + return; + } + + if (device_state_ == kDeviceStateIdle) { + audio_service_.EncodeWakeWord(); + + if (!protocol_->IsAudioChannelOpened()) { + SetDeviceState(kDeviceStateConnecting); + if (!protocol_->OpenAudioChannel()) { + audio_service_.EnableWakeWordDetection(true); + return; + } + } + + std::string wake_word = reason; +#if CONFIG_USE_AFE_WAKE_WORD || CONFIG_USE_CUSTOM_WAKE_WORD + // Encode and send the wake word data to the server + while (auto packet = audio_service_.PopWakeWordPacket()) { + protocol_->SendAudio(std::move(packet)); + } + // Set the chat state to wake word detected + protocol_->SendWakeWordDetected(wake_word); + SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime); +#else + SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime); + // Play the pop up sound to indicate the wake word is detected + audio_service_.PlaySound(Lang::Sounds::OGG_POPUP); +#endif + } else if (device_state_ == kDeviceStateSpeaking) { + AbortSpeaking(kAbortReasonWakeWordDetected); + } else if (device_state_ == kDeviceStateActivating) { + SetDeviceState(kDeviceStateIdle); + } + +} + void Application::OnWakeWordDetected() { if (!protocol_) { return; diff --git a/main/application.h b/main/application.h index bff4e9e9c..cf57d91cd 100644 --- a/main/application.h +++ b/main/application.h @@ -63,6 +63,7 @@ class Application { AecMode GetAecMode() const { return aec_mode_; } void PlaySound(const std::string_view& sound); AudioService& GetAudioService() { return audio_service_; } + void RemoteWakeup(const std::string& reason); private: Application(); diff --git a/main/mcp_server.cc b/main/mcp_server.cc index 949e5d291..70f71a6ce 100644 --- a/main/mcp_server.cc +++ b/main/mcp_server.cc @@ -300,6 +300,24 @@ void McpServer::AddUserOnlyTools() { return true; }); } + + AddUserOnlyTool("self.wakeup", "Wake up module with parameters", + PropertyList({ + Property("reason", kPropertyTypeString, "Reason for waking up"), + }), + [this](const PropertyList& properties) -> ReturnValue { + std::string reason = properties["reason"].value(); + ESP_LOGI(TAG, "Wakeup MCP: reason=%s", reason.c_str()); + + auto display = Board::GetInstance().GetDisplay(); + if (display) { + display->SetEmotion("happy"); + } + auto& app = Application::GetInstance(); + app.RemoteWakeup(reason); + return true; + } +); } void McpServer::AddTool(McpTool* tool) {