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

@@ -1,5 +1,6 @@
#include "power_save_timer.h"
#include "application.h"
#include "settings.h"
#include <esp_log.h>
@@ -28,6 +29,12 @@ PowerSaveTimer::~PowerSaveTimer() {
void PowerSaveTimer::SetEnabled(bool enabled) {
if (enabled && !enabled_) {
Settings settings("wifi", false);
if (!settings.GetBool("sleep_mode", true)) {
ESP_LOGI(TAG, "Power save timer is disabled by settings");
return;
}
ticks_ = 0;
enabled_ = enabled;
ESP_ERROR_CHECK(esp_timer_start_periodic(power_save_timer_, 1000000));

View File

@@ -0,0 +1,57 @@
#include "press_to_talk_mcp_tool.h"
#include <esp_log.h>
static const char* TAG = "PressToTalkMcpTool";
PressToTalkMcpTool::PressToTalkMcpTool()
: press_to_talk_enabled_(false) {
}
void PressToTalkMcpTool::Initialize() {
// 从设置中读取当前状态
Settings settings("vendor");
press_to_talk_enabled_ = settings.GetInt("press_to_talk", 0) != 0;
// 注册MCP工具
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 {
return HandleSetPressToTalk(properties);
});
ESP_LOGI(TAG, "PressToTalkMcpTool initialized, current mode: %s",
press_to_talk_enabled_ ? "press_to_talk" : "click_to_talk");
}
bool PressToTalkMcpTool::IsPressToTalkEnabled() const {
return press_to_talk_enabled_;
}
ReturnValue PressToTalkMcpTool::HandleSetPressToTalk(const PropertyList& properties) {
auto mode = properties["mode"].value<std::string>();
if (mode == "press_to_talk") {
SetPressToTalkEnabled(true);
ESP_LOGI(TAG, "Switched to press to talk mode");
return true;
} else if (mode == "click_to_talk") {
SetPressToTalkEnabled(false);
ESP_LOGI(TAG, "Switched to click to talk mode");
return true;
}
throw std::runtime_error("Invalid mode: " + mode);
}
void PressToTalkMcpTool::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);
}

View File

@@ -0,0 +1,29 @@
#ifndef PRESS_TO_TALK_MCP_TOOL_H
#define PRESS_TO_TALK_MCP_TOOL_H
#include "mcp_server.h"
#include "settings.h"
// 可复用的按键说话模式MCP工具类
class PressToTalkMcpTool {
private:
bool press_to_talk_enabled_;
public:
PressToTalkMcpTool();
// 初始化工具注册到MCP服务器
void Initialize();
// 获取当前按键说话模式状态
bool IsPressToTalkEnabled() const;
private:
// MCP工具的回调函数
ReturnValue HandleSetPressToTalk(const PropertyList& properties);
// 内部方法设置press to talk状态并保存到设置
void SetPressToTalkEnabled(bool enabled);
};
#endif // PRESS_TO_TALK_MCP_TOOL_H

View File

@@ -2,6 +2,7 @@
#include "application.h"
#include "board.h"
#include "display.h"
#include "settings.h"
#include <esp_log.h>
#include <esp_sleep.h>
@@ -32,6 +33,12 @@ SleepTimer::~SleepTimer() {
void SleepTimer::SetEnabled(bool enabled) {
if (enabled && !enabled_) {
Settings settings("wifi", false);
if (!settings.GetBool("sleep_mode", true)) {
ESP_LOGI(TAG, "Power save timer is disabled by settings");
return;
}
ticks_ = 0;
enabled_ = enabled;
ESP_ERROR_CHECK(esp_timer_start_periodic(sleep_timer_, 1000000));