feat(mcp): restore MCP system with native log.h
- MCP 3.3K LOC: McpSystem 1899 + McpHandler 1029 + McpScreensaver + 2 apps
- Uses native tactility/log.h TAG macros, no old Logger.h
- DisplaySettings McpScreen enum, WebServer coexistence (MCP enabled starts HTTP)
- DisplayIdle: startMcpScreensaver + isDeviceCharging + lock inversion fix 3629ffef
- LVGL 512K PSRAM cache retained from previous perf patch
- Audio kept upstream es8311-module per note we might not need custom
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <Tactility/Tactility.h>
|
||||
#include <Tactility/mcp/McpSystem.h>
|
||||
#include <Tactility/lvgl/Toolbar.h>
|
||||
#include <Tactility/lvgl/LvglSync.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
constexpr auto* TAG = "McpOverrideApp";
|
||||
|
||||
#include <lvgl.h>
|
||||
#include <tactility/lvgl_icon_shared.h>
|
||||
#include <esp_heap_caps.h>
|
||||
|
||||
namespace tt::app::mcpoverride {
|
||||
|
||||
|
||||
class McpOverrideApp final : public App {
|
||||
|
||||
public:
|
||||
void onCreate(AppContext& app) override {
|
||||
// Prepare global state
|
||||
auto& state = mcp::getState();
|
||||
state.overrideActive = false;
|
||||
}
|
||||
|
||||
void onShow(AppContext& app, lv_obj_t* parent) override {
|
||||
LOG_I(TAG, "onShow: Starting MCP Override display canvas");
|
||||
auto& state = mcp::getState();
|
||||
|
||||
lv_obj_remove_flag(parent, LV_OBJ_FLAG_SCROLLABLE);
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_pad_all(parent, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_row(parent, 0, LV_PART_MAIN);
|
||||
|
||||
// Standard toolbar so the user can navigate back
|
||||
lv_obj_t* toolbar = lvgl::toolbar_create(parent, app);
|
||||
lv_obj_align(toolbar, LV_ALIGN_TOP_MID, 0, 0);
|
||||
|
||||
lv_obj_t* title_label = lv_label_create(toolbar);
|
||||
lv_label_set_text(title_label, "MCP Override Screen");
|
||||
|
||||
// Create drawing canvas
|
||||
state.drawArea = lv_canvas_create(parent);
|
||||
lv_obj_set_width(state.drawArea, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(state.drawArea, 1);
|
||||
lv_obj_set_style_radius(state.drawArea, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_border_width(state.drawArea, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_all(state.drawArea, 0, LV_PART_MAIN);
|
||||
lv_obj_remove_flag(state.drawArea, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
// Get display metrics
|
||||
lv_display_t* display = lv_obj_get_display(parent);
|
||||
state.displayWidth = lv_display_get_horizontal_resolution(display);
|
||||
state.displayHeight = lv_display_get_vertical_resolution(display);
|
||||
|
||||
lv_obj_update_layout(parent);
|
||||
state.drawWidth = lv_obj_get_content_width(state.drawArea);
|
||||
state.drawHeight = lv_obj_get_content_height(state.drawArea);
|
||||
|
||||
// Allocate framebuffer
|
||||
size_t required_size = (size_t)state.drawWidth * state.drawHeight * sizeof(uint16_t);
|
||||
if (state.framebuffer == nullptr || state.framebufferSize != required_size) {
|
||||
if (state.framebuffer != nullptr) {
|
||||
heap_caps_free(state.framebuffer);
|
||||
state.framebuffer = nullptr;
|
||||
}
|
||||
state.framebuffer = (uint16_t*)heap_caps_malloc(required_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (state.framebuffer == nullptr) {
|
||||
state.framebuffer = (uint16_t*)heap_caps_malloc(required_size, MALLOC_CAP_8BIT);
|
||||
}
|
||||
state.framebufferSize = state.framebuffer == nullptr ? 0 : required_size;
|
||||
}
|
||||
|
||||
if (state.framebuffer == nullptr) {
|
||||
LOG_E(TAG, "Failed to allocate %u bytes framebuffer", (unsigned)required_size);
|
||||
lv_obj_t* error = lv_label_create(state.drawArea);
|
||||
lv_label_set_text(error, "Framebuffer allocation failed");
|
||||
lv_obj_center(error);
|
||||
return;
|
||||
}
|
||||
|
||||
lv_canvas_set_buffer(
|
||||
state.drawArea,
|
||||
state.framebuffer,
|
||||
state.drawWidth,
|
||||
state.drawHeight,
|
||||
LV_COLOR_FORMAT_RGB565
|
||||
);
|
||||
|
||||
// Initialize welcome/waiting screen if LLM hasn't written anything yet
|
||||
if (!state.overrideActive) {
|
||||
// Fill with a nice dark blue/slate color
|
||||
for (size_t i = 0; i < (size_t)state.drawWidth * state.drawHeight; ++i) {
|
||||
state.framebuffer[i] = 0x18E3;
|
||||
}
|
||||
state.drawColor = 1;
|
||||
|
||||
lv_obj_t* welcome_label = lv_label_create(state.drawArea);
|
||||
lv_label_set_text(welcome_label, "Waiting for LLM...");
|
||||
lv_obj_set_style_text_color(welcome_label, lv_color_white(), LV_PART_MAIN);
|
||||
lv_obj_align(welcome_label, LV_ALIGN_CENTER, 0, -20);
|
||||
|
||||
lv_obj_t* desc_label = lv_label_create(state.drawArea);
|
||||
lv_label_set_text_fmt(desc_label, "Display Resolution: %ux%u", state.drawWidth, state.drawHeight);
|
||||
lv_obj_set_style_text_color(desc_label, lv_palette_lighten(LV_PALETTE_BLUE, 3), LV_PART_MAIN);
|
||||
lv_obj_align(desc_label, LV_ALIGN_CENTER, 0, 10);
|
||||
}
|
||||
}
|
||||
|
||||
void onHide(AppContext& app) override {
|
||||
LOG_I(TAG, "onHide: Tearing down MCP Override canvas");
|
||||
auto& state = mcp::getState();
|
||||
state.drawArea = nullptr;
|
||||
if (state.framebuffer != nullptr) {
|
||||
heap_caps_free(state.framebuffer);
|
||||
state.framebuffer = nullptr;
|
||||
state.framebufferSize = 0;
|
||||
}
|
||||
state.overrideActive = false;
|
||||
|
||||
// Stop any running tone or recording to prevent stuck state
|
||||
state.audioRunning = false;
|
||||
}
|
||||
|
||||
void onDestroy(AppContext& app) override {
|
||||
onHide(app);
|
||||
}
|
||||
};
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.appId = "one.tactility.mcpscreen", // Keep the original appId for compatibility
|
||||
.appName = "MCP Override Screen",
|
||||
.appIcon = LVGL_ICON_SHARED_TOOLBAR,
|
||||
.appCategory = Category::System,
|
||||
.createApp = create<McpOverrideApp>
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
@@ -0,0 +1,196 @@
|
||||
#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/log.h>
|
||||
|
||||
constexpr auto* TAG = "McpSettingsApp";
|
||||
|
||||
#include <lvgl.h>
|
||||
#include <tactility/lvgl_icon_shared.h>
|
||||
|
||||
#include <esp_netif.h>
|
||||
#include <esp_wifi.h>
|
||||
|
||||
namespace tt::app::mcpsettings {
|
||||
|
||||
|
||||
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://";
|
||||
bool ip_added = false;
|
||||
|
||||
// Try getting station IP first (we are connected to home Wi-Fi)
|
||||
esp_netif_t* sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
if (sta_netif != nullptr) {
|
||||
esp_netif_ip_info_t ip_info;
|
||||
if (esp_netif_get_ip_info(sta_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;
|
||||
ip_added = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If no station IP, check if the AP interface has a valid IP address
|
||||
if (!ip_added) {
|
||||
esp_netif_t* ap_netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
|
||||
if (ap_netif != nullptr) {
|
||||
esp_netif_ip_info_t ip_info;
|
||||
if (esp_netif_get_ip_info(ap_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;
|
||||
ip_added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback if no active IP address is detected on either interface
|
||||
if (!ip_added) {
|
||||
if (wsSettings.wifiMode == settings::webserver::WiFiMode::AccessPoint) {
|
||||
url += "192.168.4.1";
|
||||
} else {
|
||||
url = "Connecting...";
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
LOG_W(TAG, "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) {
|
||||
LOG_I(TAG, "MCP server state changed to %s", 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
|
||||
Reference in New Issue
Block a user