cd921e1aa9
- Add ScreensaverType::McpScreen to DisplaySettings enum, toString/fromString - Add 'MCP Screen' option to the Display settings screensaver dropdown - New McpScreensaver (Screensaver subclass): creates full-screen lv_canvas on the overlay, allocates framebuffer in SPIRAM, registers canvas pointer in McpSystemState, shows 'Waiting for LLM...' until first draw command arrives - Wire McpScreensaver into DisplayIdle::activateScreensaver() switch - McpSystem::ensureOverrideScreen() now calls displayidle::findService()-> startScreensaver() instead of launching McpOverrideApp — canvas appears automatically on any LLM draw command - Remove McpOverrideApp (replaced by screensaver), deregister from Tactility.cpp - Remove mcpOverrideEnabled from McpSettings — no longer needed - Simplify McpSettingsApp: remove Screen Override toggle, update info text to direct users to Settings -> Display -> Screensaver -> MCP Screen
178 lines
6.5 KiB
C++
178 lines
6.5 KiB
C++
#ifdef ESP_PLATFORM
|
|
|
|
#include <Tactility/Tactility.h>
|
|
#include <Tactility/settings/McpSettings.h>
|
|
#include <Tactility/settings/WebServerSettings.h>
|
|
#include <Tactility/service/webserver/WebServerService.h>
|
|
#include <Tactility/lvgl/Toolbar.h>
|
|
#include <Tactility/lvgl/LvglSync.h>
|
|
#include <Tactility/Logger.h>
|
|
|
|
#include <lvgl.h>
|
|
#include <tactility/lvgl_icon_shared.h>
|
|
|
|
#include <esp_netif.h>
|
|
#include <esp_wifi.h>
|
|
|
|
namespace tt::app::mcpsettings {
|
|
|
|
static const auto LOGGER = tt::Logger("McpSettingsApp");
|
|
|
|
class McpSettingsApp final : public App {
|
|
|
|
settings::mcp::McpSettings mcpSettings;
|
|
settings::mcp::McpSettings originalSettings;
|
|
settings::webserver::WebServerSettings wsSettings;
|
|
bool updated = false;
|
|
|
|
lv_obj_t* switchMcpEnabled = nullptr;
|
|
lv_obj_t* labelUrlValue = nullptr;
|
|
|
|
static void onMcpEnabledSwitch(lv_event_t* e) {
|
|
auto* app = static_cast<McpSettingsApp*>(lv_event_get_user_data(e));
|
|
bool enabled = lv_obj_has_state(app->switchMcpEnabled, LV_STATE_CHECKED);
|
|
getMainDispatcher().dispatch([app, enabled] {
|
|
app->mcpSettings.mcpEnabled = enabled;
|
|
app->updated = true;
|
|
if (lvgl::lock(100)) {
|
|
app->updateUrlDisplay();
|
|
lvgl::unlock();
|
|
}
|
|
});
|
|
}
|
|
|
|
void updateUrlDisplay() {
|
|
if (!labelUrlValue) return;
|
|
|
|
if (!mcpSettings.mcpEnabled) {
|
|
lv_label_set_text(labelUrlValue, "Disabled");
|
|
return;
|
|
}
|
|
|
|
std::string url = "http://";
|
|
|
|
if (wsSettings.wifiMode == settings::webserver::WiFiMode::AccessPoint) {
|
|
url += "192.168.4.1";
|
|
} else {
|
|
esp_netif_t* netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
|
if (netif != nullptr) {
|
|
esp_netif_ip_info_t ip_info;
|
|
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
|
|
char ip_str[16];
|
|
snprintf(ip_str, sizeof(ip_str), IPSTR, IP2STR(&ip_info.ip));
|
|
url += ip_str;
|
|
} else {
|
|
url = "Connecting...";
|
|
}
|
|
} else {
|
|
url = "Not connected";
|
|
}
|
|
}
|
|
|
|
if (url.starts_with("http://")) {
|
|
if (wsSettings.webServerPort != 80) {
|
|
url += ":" + std::to_string(wsSettings.webServerPort);
|
|
}
|
|
url += "/api/mcp";
|
|
}
|
|
|
|
lv_label_set_text(labelUrlValue, url.c_str());
|
|
}
|
|
|
|
public:
|
|
void onCreate(AppContext& app) override {
|
|
mcpSettings = settings::mcp::loadOrGetDefault();
|
|
originalSettings = mcpSettings;
|
|
wsSettings = settings::webserver::loadOrGetDefault();
|
|
}
|
|
|
|
void onShow(AppContext& app, lv_obj_t* parent) override {
|
|
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT);
|
|
|
|
lv_obj_t* toolbar = lvgl::toolbar_create(parent, app);
|
|
|
|
// MCP Enable toggle on toolbar
|
|
switchMcpEnabled = lvgl::toolbar_add_switch_action(toolbar);
|
|
if (mcpSettings.mcpEnabled) {
|
|
lv_obj_add_state(switchMcpEnabled, LV_STATE_CHECKED);
|
|
}
|
|
lv_obj_add_event_cb(switchMcpEnabled, onMcpEnabledSwitch, LV_EVENT_VALUE_CHANGED, this);
|
|
|
|
auto* main_wrapper = lv_obj_create(parent);
|
|
lv_obj_set_flex_flow(main_wrapper, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_width(main_wrapper, LV_PCT(100));
|
|
lv_obj_set_flex_grow(main_wrapper, 1);
|
|
|
|
// URL Display
|
|
auto* url_wrapper = lv_obj_create(main_wrapper);
|
|
lv_obj_set_size(url_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
|
lv_obj_set_style_pad_all(url_wrapper, 10, LV_STATE_DEFAULT);
|
|
lv_obj_set_style_border_width(url_wrapper, 1, LV_STATE_DEFAULT);
|
|
lv_obj_set_flex_flow(url_wrapper, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_style_flex_cross_place(url_wrapper, LV_FLEX_ALIGN_START, 0);
|
|
|
|
auto* url_title = lv_label_create(url_wrapper);
|
|
lv_label_set_text(url_title, "MCP Endpoint URL:");
|
|
|
|
labelUrlValue = lv_label_create(url_wrapper);
|
|
if (lv_display_get_color_format(lv_obj_get_display(parent)) == LV_COLOR_FORMAT_L8) {
|
|
lv_obj_set_style_text_color(labelUrlValue, lv_theme_get_color_secondary(labelUrlValue), LV_PART_MAIN);
|
|
} else {
|
|
lv_obj_set_style_text_color(labelUrlValue, lv_palette_main(LV_PALETTE_BLUE), 0);
|
|
}
|
|
|
|
updateUrlDisplay();
|
|
|
|
// Info / Documentation text
|
|
auto* info_label = lv_label_create(main_wrapper);
|
|
lv_label_set_long_mode(info_label, LV_LABEL_LONG_WRAP);
|
|
lv_obj_set_width(info_label, LV_PCT(95));
|
|
if (lv_display_get_color_format(lv_obj_get_display(parent)) != LV_COLOR_FORMAT_L8) {
|
|
lv_obj_set_style_text_color(info_label, lv_palette_main(LV_PALETTE_GREY), 0);
|
|
}
|
|
lv_label_set_text(info_label,
|
|
"MCP (Model Context Protocol) Screen service allows LLMs to interact with the device "
|
|
"screen, audio, and tools directly.\n\n"
|
|
"Endpoints:\n"
|
|
"- POST /api/mcp (JSON-RPC tools)\n"
|
|
"- POST /api/screen/raw (big-endian RGB565 writes)\n\n"
|
|
"To show the LLM canvas, select 'MCP Screen' in Settings -> Display -> Screensaver. "
|
|
"The canvas also pops up automatically when an LLM sends a draw command.");
|
|
}
|
|
|
|
void onHide(AppContext& app) override {
|
|
if (updated) {
|
|
const auto copy = mcpSettings;
|
|
const bool mcpStateChanged = (copy.mcpEnabled != originalSettings.mcpEnabled);
|
|
|
|
getMainDispatcher().dispatch([copy, mcpStateChanged]{
|
|
// Save to properties file
|
|
if (!settings::mcp::save(copy)) {
|
|
LOGGER.warn("Failed to persist MCP settings");
|
|
}
|
|
|
|
// Publish WebServerSettingsChanged event so the HTTP server restarts/refreshes if needed
|
|
service::webserver::getPubsub()->publish(service::webserver::WebServerEvent::WebServerSettingsChanged);
|
|
|
|
if (mcpStateChanged) {
|
|
LOGGER.info("MCP server state changed to {}", copy.mcpEnabled ? "enabled" : "disabled");
|
|
service::webserver::setWebServerEnabled(copy.mcpEnabled);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
extern const AppManifest manifest = {
|
|
.appId = "McpSettings",
|
|
.appName = "MCP Screen",
|
|
.appIcon = LVGL_ICON_SHARED_SETTINGS,
|
|
.appCategory = Category::Settings,
|
|
.createApp = create<McpSettingsApp>
|
|
};
|
|
|
|
} // namespace
|
|
|
|
#endif // ESP_PLATFORM
|