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:
Adolfo Reyna
2026-07-18 17:29:37 -04:00
parent c4adaef0a5
commit e3eb3fd415
15 changed files with 5568 additions and 26 deletions
@@ -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
File diff suppressed because it is too large Load Diff
@@ -1,4 +1,5 @@
#ifdef ESP_PLATFORM
#include <tactility/log.h>
#include <Tactility/service/displayidle/DisplayIdleService.h>
@@ -7,8 +8,8 @@
#include "MatrixRainScreensaver.h"
#include "MystifyScreensaver.h"
#include "StackChanScreensaver.h"
#include "McpScreensaver.h"
#include <tactility/log.h>
#include <Tactility/CoreDefines.h>
#include <Tactility/hal/display/DisplayDevice.h>
#include <Tactility/hal/power/PowerDevice.h>
@@ -16,6 +17,7 @@
#include <Tactility/service/ServiceContext.h>
#include <Tactility/service/ServiceManifest.h>
#include <Tactility/service/ServiceRegistration.h>
#include <Tactility/mcp/McpSystem.h>
#include <cstdlib>
#include <ctime>
@@ -33,32 +35,24 @@ static bool isDeviceCharging() {
bool charging = false;
hal::findDevices<hal::power::PowerDevice>(hal::Device::Type::Power, [&charging](const auto& power) {
if (!power->supportsMetric(hal::power::PowerDevice::MetricType::IsCharging)) {
return true;
return true; // continue
}
hal::power::PowerDevice::MetricData data;
if (power->getMetric(hal::power::PowerDevice::MetricType::IsCharging, data) && data.valueAsBool) {
charging = true;
return false;
return false; // stop iter
}
return true;
});
return charging;
}
void DisplayIdleService::stopScreensaverCb(lv_event_t* e) {
auto* self = static_cast<DisplayIdleService*>(lv_event_get_user_data(e));
lv_event_stop_bubbling(e);
self->stopScreensaverRequested.store(true, std::memory_order_release);
lv_display_trigger_activity(nullptr);
self->stopScreensaverLocked();
}
void DisplayIdleService::stopScreensaver() {
if (!lvgl::lock(100)) {
// Lock failed - keep flag set to retry on next tick
return;
}
void DisplayIdleService::stopScreensaverLocked() {
const auto restoreDuty = cachedDisplaySettings.backlightDuty;
const bool wasDimmed = displayDimmed;
@@ -70,7 +64,6 @@ void DisplayIdleService::stopScreensaver() {
lv_obj_delete(screensaverOverlay);
screensaverOverlay = nullptr;
}
lvgl::unlock();
stopScreensaverRequested.store(false, std::memory_order_relaxed);
// Reset auto-off state
@@ -85,6 +78,15 @@ void DisplayIdleService::stopScreensaver() {
displayDimmed = wasDimmed ? false : displayDimmed;
}
void DisplayIdleService::stopScreensaver() {
if (!lvgl::lock(100)) {
// Lock failed - keep flag set to retry on next tick
return;
}
stopScreensaverLocked();
lvgl::unlock();
}
void DisplayIdleService::activateScreensaver() {
lv_obj_t* top = lv_layer_top();
@@ -121,6 +123,9 @@ void DisplayIdleService::activateScreensaver() {
case settings::display::ScreensaverType::StackChan:
screensaver = std::make_unique<StackChanScreensaver>();
break;
case settings::display::ScreensaverType::McpScreen:
screensaver = std::make_unique<McpScreensaver>();
break;
case settings::display::ScreensaverType::None:
default:
// Just black screen, no animated screensaver
@@ -141,8 +146,17 @@ void DisplayIdleService::updateScreensaver() {
}
}
void DisplayIdleService::tick() {
// Check if MCP override is active — must not be auto-stopped by idle logic
// READ OUTSIDE LVGL lock to avoid lock inversion:
// MCP video task locks mutex -> LVGL, we must NOT do LVGL -> mutex
bool isMcpActive = false;
{
auto& st = mcp::getState();
std::lock_guard<std::mutex> lk(st.mutex);
isMcpActive = (st.drawArea != nullptr) || st.overrideActive;
}
if (!lvgl::lock(100)) {
return;
}
@@ -159,11 +173,13 @@ void DisplayIdleService::tick() {
uint32_t inactive_ms = 0;
inactive_ms = lv_display_get_inactive_time(nullptr);
// Only update if not stopping (prevents lag on touch)
if (displayDimmed && screensaverOverlay && !stopScreensaverRequested.load(std::memory_order_acquire)) {
// Only update if not stopping (prevents lag on touch) — skip for MCP (no animation)
if (displayDimmed && screensaverOverlay && !stopScreensaverRequested.load(std::memory_order_acquire) && !isMcpActive) {
// Check if screensaver should auto-off after 5 minutes
if (!backlightOff) {
screensaverActiveCounter++;
if (screensaverActiveCounter >= SCREENSAVER_AUTO_OFF_TICKS) {
// Stop screensaver animation and turn off backlight
if (screensaver) {
screensaver->stop();
screensaver.reset();
@@ -181,6 +197,7 @@ void DisplayIdleService::tick() {
lvgl::unlock();
// Check stop request early for faster response
if (stopScreensaverRequested.load(std::memory_order_acquire)) {
stopScreensaver();
return;
@@ -190,13 +207,15 @@ void DisplayIdleService::tick() {
bool supportsBacklight = display != nullptr && display->supportsBacklightDuty();
if (!cachedDisplaySettings.backlightTimeoutEnabled || cachedDisplaySettings.backlightTimeoutMs == 0) {
if (displayDimmed) {
// Timeout disabled (Never): ensure we restore if we were dimmed, regardless of display type
if (displayDimmed && !isMcpActive) {
if (supportsBacklight && display != nullptr) {
display->setBacklightDuty(cachedDisplaySettings.backlightDuty);
}
displayDimmed = false;
}
} else if (supportsBacklight) {
// For backlight-capable displays: full idle handling
bool charging_blocks = cachedDisplaySettings.disableScreensaverWhenCharging && isDeviceCharging();
if (!displayDimmed && inactive_ms >= cachedDisplaySettings.backlightTimeoutMs) {
@@ -204,7 +223,7 @@ void DisplayIdleService::tick() {
// Skip screensaver while charging
} else {
if (!lvgl::lock(100)) {
return;
return; // Retry on next tick
}
activateScreensaver();
lvgl::unlock();
@@ -215,23 +234,35 @@ void DisplayIdleService::tick() {
}
displayDimmed = true;
}
} else if (displayDimmed) {
} else if (displayDimmed && !isMcpActive) {
if (inactive_ms < kWakeActivityThresholdMs) {
stopScreensaver();
} else if (charging_blocks) {
stopScreensaver();
}
}
} else {
// For monochrome/RLCD (no backlight): don't auto-enter screensaver (heavy full_refresh SPI causes freeze)
// Only handle wake if we are somehow dimmed (e.g. MCP left it)
if (displayDimmed && !isMcpActive) {
if (inactive_ms < kWakeActivityThresholdMs) {
stopScreensaver();
}
}
}
}
bool DisplayIdleService::onStart(ServiceContext& service) {
// Seed random number generator for varied screensaver patterns
srand(static_cast<unsigned int>(time(nullptr)));
cachedDisplaySettings = settings::display::loadOrGetDefault();
auto display = getDisplay();
if (display != nullptr && !display->supportsBacklightDuty()) {
LOG_I(TAG, "Monochrome/RLCD display detected (no backlight control): idle timer will run but auto-backlight off is disabled");
}
timer = std::make_unique<Timer>(Timer::Type::Periodic, kernel::millisToTicks(TICK_INTERVAL_MS), [this]{ this->tick(); });
timer->setCallbackPriority(Thread::Priority::Lower);
timer->start();
@@ -288,6 +319,62 @@ void DisplayIdleService::reloadSettings() {
settingsReloadRequested.store(true, std::memory_order_release);
}
void DisplayIdleService::startMcpScreensaver() {
if (!lvgl::lock(200)) {
LOG_W(TAG, "startMcpScreensaver: failed to acquire LVGL lock");
return;
}
if (screensaverOverlay != nullptr) {
// Screensaver already active — if drawArea is registered we're done,
// otherwise stop the current one so we can replace it with McpScreensaver.
const auto& mcpState = mcp::getState();
if (mcpState.drawArea != nullptr) {
lvgl::unlock();
return; // McpScreensaver already running
}
// Wrong screensaver type active — tear it down first
if (screensaver) {
screensaver->stop();
screensaver.reset();
}
lv_obj_delete(screensaverOverlay);
screensaverOverlay = nullptr;
}
screensaverActiveCounter = 0;
backlightOff = false;
// Ensure backlight is active if the display supports it
auto display = getDisplay();
if (display != nullptr && display->supportsBacklightDuty()) {
uint8_t duty = cachedDisplaySettings.backlightDuty;
if (duty == 0) duty = 255; // ensure visible if settings not loaded / default
display->setBacklightDuty(duty);
}
lv_coord_t screenW = lv_display_get_horizontal_resolution(nullptr);
lv_coord_t screenH = lv_display_get_vertical_resolution(nullptr);
lv_obj_t* top = lv_layer_top();
screensaverOverlay = lv_obj_create(top);
lv_obj_remove_style_all(screensaverOverlay);
lv_obj_set_size(screensaverOverlay, LV_PCT(100), LV_PCT(100));
lv_obj_set_pos(screensaverOverlay, 0, 0);
lv_obj_set_style_bg_color(screensaverOverlay, lv_color_black(), 0);
lv_obj_set_style_bg_opa(screensaverOverlay, LV_OPA_COVER, 0);
lv_obj_add_flag(screensaverOverlay, LV_OBJ_FLAG_CLICKABLE);
lv_obj_add_event_cb(screensaverOverlay, stopScreensaverCb, LV_EVENT_CLICKED, this);
screensaver = std::make_unique<McpScreensaver>();
screensaver->start(screensaverOverlay, screenW, screenH);
lvgl::unlock();
displayDimmed = true;
LOG_I(TAG, "MCP screensaver activated");
}
std::shared_ptr<DisplayIdleService> findService() {
return std::static_pointer_cast<DisplayIdleService>(
findServiceById("DisplayIdle")
@@ -0,0 +1,100 @@
#ifdef ESP_PLATFORM
#include "McpScreensaver.h"
#include <Tactility/mcp/McpSystem.h>
#include <tactility/log.h>
constexpr auto* TAG = "McpScreensaver";
#include <esp_heap_caps.h>
namespace tt::service::displayidle {
void McpScreensaver::start(lv_obj_t* overlay, lv_coord_t screenW, lv_coord_t screenH) {
auto& state = mcp::getState();
// Full-screen canvas on the overlay
lv_obj_t* canvas = lv_canvas_create(overlay);
lv_obj_set_size(canvas, screenW, screenH);
lv_obj_set_pos(canvas, 0, 0);
lv_obj_set_style_radius(canvas, 0, LV_PART_MAIN);
lv_obj_set_style_border_width(canvas, 0, LV_PART_MAIN);
lv_obj_set_style_pad_all(canvas, 0, LV_PART_MAIN);
lv_obj_remove_flag(canvas, LV_OBJ_FLAG_SCROLLABLE);
// Allocate framebuffer (prefer SPIRAM)
size_t requiredSize = (size_t)screenW * screenH * sizeof(uint16_t);
framebuffer = (uint16_t*)heap_caps_malloc(requiredSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (framebuffer == nullptr) {
framebuffer = (uint16_t*)heap_caps_malloc(requiredSize, MALLOC_CAP_8BIT);
}
framebufferSize = (framebuffer != nullptr) ? requiredSize : 0;
if (framebuffer == nullptr) {
LOG_E(TAG, "Failed to allocate %uB framebuffer", (unsigned)requiredSize);
lv_obj_t* err = lv_label_create(canvas);
lv_label_set_text(err, "Framebuffer alloc failed");
lv_obj_center(err);
return;
}
// Fill with a dark slate background (inverted for display path)
size_t pixelCount = (size_t)screenW * screenH;
for (size_t i = 0; i < pixelCount; ++i) {
framebuffer[i] = ~0x18E3; // dark blue-grey
}
lv_canvas_set_buffer(canvas, framebuffer, screenW, screenH, LV_COLOR_FORMAT_RGB565);
// Waiting label (removed on first MCP draw via lv_obj_clean)
lv_obj_t* waitLabel = lv_label_create(canvas);
lv_label_set_text(waitLabel, "Waiting for LLM...");
lv_obj_set_style_text_color(waitLabel, lv_color_black(), LV_PART_MAIN); // white on screen (inverted)
lv_obj_align(waitLabel, LV_ALIGN_CENTER, 0, -20);
lv_obj_t* resLabel = lv_label_create(canvas);
lv_label_set_text_fmt(resLabel, "Display: %dx%d", (int)screenW, (int)screenH);
lv_color_t resColor = lv_palette_lighten(LV_PALETTE_BLUE, 3);
lv_obj_set_style_text_color(resLabel, lv_color_make(~resColor.red, ~resColor.green, ~resColor.blue), LV_PART_MAIN);
lv_obj_align(resLabel, LV_ALIGN_CENTER, 0, 10);
// Register with McpSystemState
std::lock_guard<std::mutex> lock(state.mutex);
state.drawArea = canvas;
state.framebuffer = framebuffer;
state.framebufferSize = framebufferSize;
state.displayWidth = (uint16_t)screenW;
state.displayHeight = (uint16_t)screenH;
state.drawWidth = (uint16_t)screenW;
state.drawHeight = (uint16_t)screenH;
// Don't reset overrideActive — if the LLM already drew, we keep the content
LOG_I(TAG, "McpScreensaver started (%dx%d)", (int)screenW, (int)screenH);
}
void McpScreensaver::stop() {
auto& state = mcp::getState();
{
std::lock_guard<std::mutex> lock(state.mutex);
state.drawArea = nullptr;
state.framebuffer = nullptr;
state.framebufferSize = 0;
state.overrideActive = false;
}
if (framebuffer != nullptr) {
heap_caps_free(framebuffer);
framebuffer = nullptr;
framebufferSize = 0;
}
LOG_I(TAG, "McpScreensaver stopped");
}
void McpScreensaver::update(lv_coord_t /*screenW*/, lv_coord_t /*screenH*/) {
// MCP draws on demand via HTTP — no per-frame animation needed
}
} // namespace tt::service::displayidle
#endif // ESP_PLATFORM
@@ -0,0 +1,30 @@
#pragma once
#ifdef ESP_PLATFORM
#include "Screensaver.h"
#include <cstdint>
namespace tt::service::displayidle {
/**
* MCP Screen screensaver.
* Creates a full-screen LVGL canvas on the overlay and registers it in
* McpSystemState so that MCP HTTP draw commands can paint to it.
* Dismissed by a touch event (handled by the parent DisplayIdle overlay).
*/
class McpScreensaver final : public Screensaver {
uint16_t* framebuffer = nullptr;
size_t framebufferSize = 0;
public:
McpScreensaver() = default;
~McpScreensaver() override = default;
void start(lv_obj_t* overlay, lv_coord_t screenW, lv_coord_t screenH) override;
void stop() override;
void update(lv_coord_t screenW, lv_coord_t screenH) override;
};
} // namespace tt::service::displayidle
#endif // ESP_PLATFORM
File diff suppressed because it is too large Load Diff
@@ -5,6 +5,8 @@
#include <Tactility/service/webserver/AssetVersion.h>
#include <Tactility/service/ServiceManifest.h>
#include <Tactility/settings/WebServerSettings.h>
#include <Tactility/settings/McpSettings.h>
#include <Tactility/mcp/McpSystem.h>
#include <Tactility/MountPoints.h>
#include <Tactility/file/File.h>
#include <Tactility/lvgl/Statusbar.h>
@@ -215,7 +217,8 @@ bool WebServerService::onStart(ServiceContext& service) {
lock.lock();
g_cachedSettings = settings::webserver::loadOrGetDefault();
g_settingsCached = true;
serverEnabled = g_cachedSettings.webServerEnabled;
auto mcpSettings = settings::mcp::loadOrGetDefault();
serverEnabled = g_cachedSettings.webServerEnabled || mcpSettings.mcpEnabled;
}
// Subscribe to settings change events to refresh cache
settingsEventSubscription = pubsub->subscribe([](WebServerEvent event) {
@@ -259,13 +262,17 @@ void WebServerService::onStop(ServiceContext& service) {
void WebServerService::setEnabled(bool enabled) {
auto lock = mutex.asScopedLock();
lock.lock();
if (enabled) {
if (!httpServer || !httpServer->isStarted()) {
startServer();
}
} else {
if (httpServer && httpServer->isStarted()) {
// Stop only if both web server and MCP are disabled
auto wsSettings = settings::webserver::loadOrGetDefault();
auto mcpSettings = settings::mcp::loadOrGetDefault();
bool anyEnabled = wsSettings.webServerEnabled || mcpSettings.mcpEnabled;
if (!anyEnabled && httpServer && httpServer->isStarted()) {
stopServer();
}
}
@@ -514,6 +521,11 @@ bool WebServerService::startServer() {
LOG_I(TAG, "HTTP server started successfully on port %u", (unsigned)settings.webServerPort);
publish_event(this, WebServerEvent::WebServerStarted);
auto mcpSettings = settings::mcp::loadOrGetDefault();
if (mcpSettings.mcpEnabled) {
mcp::startVideoStreamServer();
}
// Show statusbar icon
if (statusbarIconId >= 0) {
lvgl::statusbar_icon_set_image(statusbarIconId, LVGL_ICON_STATUSBAR_CLOUD);
@@ -533,6 +545,8 @@ void WebServerService::stopServer() {
httpServer->stop();
httpServer.reset();
mcp::stopVideoStreamServer();
// Stop AP mode WiFi if we started it
if (apWifiInitialized || apNetif != nullptr) {
stopApMode();
@@ -1025,15 +1039,24 @@ esp_err_t WebServerService::handleApiGet(httpd_req_t* request) {
return ESP_FAIL;
}
// API POST dispatcher - all POST endpoints require authentication
// API POST dispatcher - all POST endpoints require authentication except MCP
esp_err_t WebServerService::handleApiPost(httpd_req_t* request) {
const char* uri = request->uri;
// MCP endpoints are unauthenticated (local network)
if (strncmp(uri, "/api/mcp", 8) == 0) {
return handleApiMcp(request);
}
if (strncmp(uri, "/api/screen/raw", 15) == 0) {
return handleApiScreenRaw(request);
}
bool authPassed = false;
esp_err_t authResult = validateRequestAuth(request, authPassed);
if (!authPassed) {
return authResult;
}
const char* uri = request->uri;
if (strncmp(uri, "/api/apps/run", 13) == 0) {
return handleApiAppsRun(request);
}
@@ -63,6 +63,8 @@ static std::string toString(ScreensaverType type) {
case Mystify: return "Mystify";
case MatrixRain: return "MatrixRain";
case StackChan: return "StackChan";
case McpScreen: return "McpScreen";
case Count: return "None";
default: std::unreachable();
}
}
@@ -73,6 +75,7 @@ static bool fromString(const std::string& str, ScreensaverType& type) {
else if (str == "Mystify") { type = ScreensaverType::Mystify; return true; }
else if (str == "MatrixRain") { type = ScreensaverType::MatrixRain; return true; }
else if (str == "StackChan") { type = ScreensaverType::StackChan; return true; }
else if (str == "McpScreen") { type = ScreensaverType::McpScreen; return true; }
else { return false; }
}
+72
View File
@@ -0,0 +1,72 @@
#include <Tactility/settings/McpSettings.h>
#include <Tactility/file/PropertiesFile.h>
#include <Tactility/file/File.h>
#include <tactility/log.h>
constexpr auto* TAG = "McpSettings";
#include <Tactility/Paths.h>
#include <map>
#include <string>
namespace tt::settings::mcp {
static std::string getSettingsFilePath() {
return getUserDataPath() + "/settings/mcp.properties";
}
constexpr auto* KEY_MCP_ENABLED = "mcpEnabled";
bool load(McpSettings& settings) {
auto settings_path = getSettingsFilePath();
if (!file::isFile(settings_path)) {
return false;
}
std::map<std::string, std::string> map;
if (!file::loadPropertiesFile(settings_path, map)) {
return false;
}
auto mcp_enabled = map.find(KEY_MCP_ENABLED);
settings.mcpEnabled = (mcp_enabled != map.end())
? (mcp_enabled->second == "1" || mcp_enabled->second == "true")
: false;
return true;
}
McpSettings getDefault() {
return McpSettings{
.mcpEnabled = false
};
}
McpSettings loadOrGetDefault() {
McpSettings settings;
if (!load(settings)) {
settings = getDefault();
if (!save(settings)) {
LOG_W(TAG, "Failed to save default MCP settings");
}
}
return settings;
}
bool save(const McpSettings& settings) {
std::map<std::string, std::string> map;
map[KEY_MCP_ENABLED] = settings.mcpEnabled ? "true" : "false";
auto settings_path = getSettingsFilePath();
if (!file::findOrCreateParentDirectory(settings_path, 0755)) {
LOG_E(TAG, "Failed to create parent dir for %s", settings_path.c_str());
return false;
}
if (!file::savePropertiesFile(settings_path, map)) {
LOG_E(TAG, "Failed to save MCP settings to %s", settings_path.c_str());
return false;
}
return true;
}
} // namespace