xmin-c3 share MCP tool and sleep mode can be disabled

This commit is contained in:
Terrence
2025-08-09 03:07:09 +08:00
parent 2a02dd65be
commit 48a1c5da29
9 changed files with 149 additions and 109 deletions

View File

@@ -10,6 +10,7 @@
#include "power_save_timer.h"
#include "font_awesome_symbols.h"
#include "adc_battery_monitor.h"
#include "press_to_talk_mcp_tool.h"
#include <wifi_station.h>
#include <esp_log.h>
@@ -31,9 +32,9 @@ private:
esp_lcd_panel_handle_t panel_ = nullptr;
Display* display_ = nullptr;
Button boot_button_;
bool press_to_talk_enabled_ = false;
PowerSaveTimer* power_save_timer_ = nullptr;
AdcBatteryMonitor* adc_battery_monitor_ = nullptr;
PressToTalkMcpTool* press_to_talk_tool_ = nullptr;
void InitializePowerManager() {
adc_battery_monitor_ = new AdcBatteryMonitor(ADC_UNIT_1, ADC_CHANNEL_3, 100000, 100000, GPIO_NUM_12);
@@ -140,7 +141,7 @@ private:
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
ResetWifiConfiguration();
}
if (!press_to_talk_enabled_) {
if (!press_to_talk_tool_ || !press_to_talk_tool_->IsPressToTalkEnabled()) {
app.ToggleChatState();
}
});
@@ -148,39 +149,20 @@ private:
if (power_save_timer_) {
power_save_timer_->WakeUp();
}
if (press_to_talk_enabled_) {
if (press_to_talk_tool_ && press_to_talk_tool_->IsPressToTalkEnabled()) {
Application::GetInstance().StartListening();
}
});
boot_button_.OnPressUp([this]() {
if (press_to_talk_enabled_) {
if (press_to_talk_tool_ && press_to_talk_tool_->IsPressToTalkEnabled()) {
Application::GetInstance().StopListening();
}
});
}
void InitializeTools() {
Settings settings("vendor");
press_to_talk_enabled_ = settings.GetInt("press_to_talk", 0) != 0;
auto& mcp_server = McpServer::GetInstance();
mcp_server.AddTool("self.set_press_to_talk",
"Switch between press to talk mode (长按说话) and click to talk mode (单击说话).\n"
"The mode can be `press_to_talk` or `click_to_talk`.",
PropertyList({
Property("mode", kPropertyTypeString)
}),
[this](const PropertyList& properties) -> ReturnValue {
auto mode = properties["mode"].value<std::string>();
if (mode == "press_to_talk") {
SetPressToTalkEnabled(true);
return true;
} else if (mode == "click_to_talk") {
SetPressToTalkEnabled(false);
return true;
}
throw std::runtime_error("Invalid mode: " + mode);
});
press_to_talk_tool_ = new PressToTalkMcpTool();
press_to_talk_tool_->Initialize();
}
public:
@@ -216,18 +198,6 @@ public:
return true;
}
void SetPressToTalkEnabled(bool enabled) {
press_to_talk_enabled_ = enabled;
Settings settings("vendor", true);
settings.SetInt("press_to_talk", enabled ? 1 : 0);
ESP_LOGI(TAG, "Press to talk enabled: %d", enabled);
}
bool IsPressToTalkEnabled() {
return press_to_talk_enabled_;
}
virtual void SetPowerSaveMode(bool enabled) override {
if (!enabled) {
power_save_timer_->WakeUp();