#include #include #ifdef ESP_PLATFORM #include #endif #include #include #include #include #include #include #include #include namespace tt::app::display { static const auto LOGGER = Logger("Display"); static std::shared_ptr getHalDisplay() { return hal::findFirstDevice(hal::Device::Type::Display); } static bool hasCalibratableTouchDevice() { auto touch_devices = hal::findDevices(hal::Device::Type::Touch); for (const auto& touch_device : touch_devices) { if (touch_device != nullptr && touch_device->supportsCalibration()) { return true; } } return false; } class DisplayApp final : public App { settings::display::DisplaySettings displaySettings; bool displaySettingsUpdated = false; lv_obj_t* timeoutSwitch = nullptr; lv_obj_t* timeoutDropdown = nullptr; lv_obj_t* screensaverDropdown = nullptr; lv_obj_t* disableWhenChargingWrapper = nullptr; lv_obj_t* disableWhenChargingSwitch = nullptr; static void onBacklightSliderEvent(lv_event_t* event) { auto* slider = static_cast(lv_event_get_target(event)); auto* app = static_cast(lv_event_get_user_data(event)); auto hal_display = getHalDisplay(); assert(hal_display != nullptr); if (hal_display->supportsBacklightDuty()) { int32_t slider_value = lv_slider_get_value(slider); app->displaySettings.backlightDuty = static_cast(slider_value); app->displaySettingsUpdated = true; hal_display->setBacklightDuty(app->displaySettings.backlightDuty); } } static void onGammaSliderEvent(lv_event_t* event) { auto* slider = static_cast(lv_event_get_target(event)); auto hal_display = hal::findFirstDevice(hal::Device::Type::Display); auto* app = static_cast(lv_event_get_user_data(event)); assert(hal_display != nullptr); if (hal_display->getGammaCurveCount() > 0) { int32_t slider_value = lv_slider_get_value(slider); app->displaySettings.gammaCurve = static_cast(slider_value); app->displaySettingsUpdated = true; hal_display->setGammaCurve(app->displaySettings.gammaCurve); } } static void onOrientationSet(lv_event_t* event) { auto* app = static_cast(lv_event_get_user_data(event)); auto* dropdown = static_cast(lv_event_get_target(event)); uint32_t selected_index = lv_dropdown_get_selected(dropdown); LOGGER.info("Selected {}", selected_index); auto selected_orientation = static_cast(selected_index); if (selected_orientation != app->displaySettings.orientation) { app->displaySettings.orientation = selected_orientation; app->displaySettingsUpdated = true; lv_display_set_rotation(lv_display_get_default(), settings::display::toLvglDisplayRotation(selected_orientation)); } } static void onTimeoutSwitch(lv_event_t* event) { auto* app = static_cast(lv_event_get_user_data(event)); auto* sw = static_cast(lv_event_get_target(event)); bool enabled = lv_obj_has_state(sw, LV_STATE_CHECKED); app->displaySettings.backlightTimeoutEnabled = enabled; app->displaySettingsUpdated = true; if (app->timeoutDropdown) { if (enabled) { lv_obj_clear_state(app->timeoutDropdown, LV_STATE_DISABLED); if (app->screensaverDropdown) { lv_obj_clear_state(app->screensaverDropdown, LV_STATE_DISABLED); } if (app->disableWhenChargingWrapper) { lv_obj_clear_state(app->disableWhenChargingWrapper, LV_STATE_DISABLED); } } else { lv_obj_add_state(app->timeoutDropdown, LV_STATE_DISABLED); if (app->screensaverDropdown) { lv_obj_add_state(app->screensaverDropdown, LV_STATE_DISABLED); } if (app->disableWhenChargingWrapper) { lv_obj_add_state(app->disableWhenChargingWrapper, LV_STATE_DISABLED); } } } } static void onDisableWhenChargingChanged(lv_event_t* event) { auto* app = static_cast(lv_event_get_user_data(event)); auto* sw = static_cast(lv_event_get_target(event)); bool enabled = lv_obj_has_state(sw, LV_STATE_CHECKED); app->displaySettings.disableScreensaverWhenCharging = enabled; app->displaySettingsUpdated = true; } static void onInvertColorChanged(lv_event_t* event) { auto* app = static_cast(lv_event_get_user_data(event)); auto* sw = static_cast(lv_event_get_target(event)); bool enabled = lv_obj_has_state(sw, LV_STATE_CHECKED); app->displaySettings.invertColor = enabled; app->displaySettingsUpdated = true; // Apply immediately auto hal_display = getHalDisplay(); if (hal_display && hal_display->supportsInvertColor()) { hal_display->setInvertColor(enabled); } } static void onMonoThresholdChanged(lv_event_t* event) { auto* slider = static_cast(lv_event_get_target(event)); auto* app = static_cast(lv_event_get_user_data(event)); int32_t val = lv_slider_get_value(slider); if (val < 1) val = 1; if (val > 255) val = 255; app->displaySettings.monochromeThreshold = static_cast(val); app->displaySettingsUpdated = true; auto hal_display = getHalDisplay(); if (hal_display && hal_display->supportsMonochromeThreshold()) { hal_display->setMonochromeThreshold(static_cast(val)); } } static void onMonoThresholdChanging(lv_event_t* event) { auto* slider = static_cast(lv_event_get_target(event)); auto* value_label = static_cast(lv_event_get_user_data(event)); if (value_label) { int32_t v = lv_slider_get_value(slider); lv_label_set_text_fmt(value_label, "%d", (int)v); } } static void onTimeoutChanged(lv_event_t* event) { auto* app = static_cast(lv_event_get_user_data(event)); auto* dropdown = static_cast(lv_event_get_target(event)); uint32_t idx = lv_dropdown_get_selected(dropdown); // Map dropdown index to ms: 0=15s,1=30s,2=1m,3=2m,4=5m,5=Never static const uint32_t values_ms[] = {15000, 30000, 60000, 120000, 300000, 0}; if (idx < (sizeof(values_ms)/sizeof(values_ms[0]))) { app->displaySettings.backlightTimeoutMs = values_ms[idx]; app->displaySettingsUpdated = true; } } static void onScreensaverChanged(lv_event_t* event) { auto* app = static_cast(lv_event_get_user_data(event)); auto* dropdown = static_cast(lv_event_get_target(event)); uint32_t idx = lv_dropdown_get_selected(dropdown); // Validate index bounds before casting to enum if (idx >= static_cast(settings::display::ScreensaverType::Count)) { return; } auto selected_type = static_cast(idx); if (selected_type != app->displaySettings.screensaverType) { app->displaySettings.screensaverType = selected_type; app->displaySettingsUpdated = true; } } static void onCalibrateTouchClicked(lv_event_t*) { app::start("TouchCalibration"); } public: void onShow(AppContext& app, lv_obj_t* parent) override { displaySettings = settings::display::loadOrGetDefault(); auto ui_density = lvgl_get_ui_density(); lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN); lv_obj_set_style_pad_row(parent, 0, LV_STATE_DEFAULT); auto hal_display = getHalDisplay(); assert(hal_display != nullptr); lvgl::toolbar_create(parent, app); 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); // Backlight slider if (hal_display->supportsBacklightDuty()) { auto* brightness_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(brightness_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_hor(brightness_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(brightness_wrapper, 0, LV_STATE_DEFAULT); if (ui_density != LVGL_UI_DENSITY_COMPACT) { lv_obj_set_style_pad_ver(brightness_wrapper, 4, LV_STATE_DEFAULT); } auto* brightness_label = lv_label_create(brightness_wrapper); lv_label_set_text(brightness_label, "Brightness"); lv_obj_align(brightness_label, LV_ALIGN_LEFT_MID, 0, 0); auto* brightness_slider = lv_slider_create(brightness_wrapper); lv_obj_set_width(brightness_slider, LV_PCT(50)); lv_obj_align(brightness_slider, LV_ALIGN_RIGHT_MID, 0, 0); lv_slider_set_range(brightness_slider, 0, 255); lv_obj_add_event_cb(brightness_slider, onBacklightSliderEvent, LV_EVENT_VALUE_CHANGED, this); lv_slider_set_value(brightness_slider, displaySettings.backlightDuty, LV_ANIM_OFF); } // Gamma slider if (hal_display->getGammaCurveCount() > 0) { auto* gamma_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(gamma_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_hor(gamma_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(gamma_wrapper, 0, LV_STATE_DEFAULT); if (ui_density != LVGL_UI_DENSITY_COMPACT) { lv_obj_set_style_pad_ver(gamma_wrapper, 4, LV_STATE_DEFAULT); } auto* gamma_label = lv_label_create(gamma_wrapper); lv_label_set_text(gamma_label, "Gamma"); lv_obj_align(gamma_label, LV_ALIGN_LEFT_MID, 0, 0); lv_obj_set_y(gamma_label, 0); auto* gamma_slider = lv_slider_create(gamma_wrapper); lv_obj_set_width(gamma_slider, LV_PCT(50)); lv_obj_align(gamma_slider, LV_ALIGN_RIGHT_MID, 0, 0); lv_slider_set_range(gamma_slider, 0, hal_display->getGammaCurveCount()); lv_obj_add_event_cb(gamma_slider, onGammaSliderEvent, LV_EVENT_VALUE_CHANGED, this); uint8_t curve_index = displaySettings.gammaCurve; lv_slider_set_value(gamma_slider, curve_index, LV_ANIM_OFF); } // Orientation auto* orientation_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(orientation_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(orientation_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(orientation_wrapper, 0, LV_STATE_DEFAULT); auto* orientation_label = lv_label_create(orientation_wrapper); lv_label_set_text(orientation_label, "Orientation"); lv_obj_align(orientation_label, LV_ALIGN_LEFT_MID, 0, 0); auto* orientation_dropdown = lv_dropdown_create(orientation_wrapper); // Note: order correlates with settings::display::Orientation item order lv_dropdown_set_options(orientation_dropdown, "Landscape\nPortrait Right\nLandscape Flipped\nPortrait Left"); lv_obj_align(orientation_dropdown, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(orientation_dropdown, onOrientationSet, LV_EVENT_VALUE_CHANGED, this); // Set the dropdown to match current orientation enum lv_dropdown_set_selected(orientation_dropdown, static_cast(displaySettings.orientation)); // Screen timeout if (hal_display->supportsBacklightDuty()) { auto* timeout_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(timeout_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(timeout_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(timeout_wrapper, 0, LV_STATE_DEFAULT); auto* timeout_label = lv_label_create(timeout_wrapper); lv_label_set_text(timeout_label, "Auto screen off"); lv_obj_align(timeout_label, LV_ALIGN_LEFT_MID, 0, 0); timeoutSwitch = lv_switch_create(timeout_wrapper); if (displaySettings.backlightTimeoutEnabled) { lv_obj_add_state(timeoutSwitch, LV_STATE_CHECKED); } lv_obj_align(timeoutSwitch, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(timeoutSwitch, onTimeoutSwitch, LV_EVENT_VALUE_CHANGED, this); auto* timeout_select_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(timeout_select_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(timeout_select_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(timeout_select_wrapper, 0, LV_STATE_DEFAULT); auto* timeout_value_label = lv_label_create(timeout_select_wrapper); lv_label_set_text(timeout_value_label, "Timeout"); lv_obj_align(timeout_value_label, LV_ALIGN_LEFT_MID, 0, 0); timeoutDropdown = lv_dropdown_create(timeout_select_wrapper); lv_dropdown_set_options(timeoutDropdown, "15 seconds\n30 seconds\n1 minute\n2 minutes\n5 minutes\nNever"); lv_obj_align(timeoutDropdown, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(timeoutDropdown, onTimeoutChanged, LV_EVENT_VALUE_CHANGED, this); // Initialize dropdown selection from settings uint32_t ms = displaySettings.backlightTimeoutMs; uint32_t idx = 2; // default 1 minute if (ms == 15000) idx = 0; else if (ms == 30000) idx = 1; else if (ms == 60000) idx = 2; else if (ms == 120000) idx = 3; else if (ms == 300000) idx = 4; else if (ms == 0) idx = 5; lv_dropdown_set_selected(timeoutDropdown, idx); if (!displaySettings.backlightTimeoutEnabled) { lv_obj_add_state(timeoutDropdown, LV_STATE_DISABLED); } // Screensaver type auto* screensaver_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(screensaver_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(screensaver_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(screensaver_wrapper, 0, LV_STATE_DEFAULT); auto* screensaver_label = lv_label_create(screensaver_wrapper); lv_label_set_text(screensaver_label, "Screensaver"); lv_obj_align(screensaver_label, LV_ALIGN_LEFT_MID, 0, 0); screensaverDropdown = lv_dropdown_create(screensaver_wrapper); // Note: order correlates with settings::display::ScreensaverType enum order lv_dropdown_set_options(screensaverDropdown, "None\nBouncing Balls\nMystify\nMatrix Rain\nStackChan\nMCP Screen"); lv_obj_align(screensaverDropdown, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(screensaverDropdown, onScreensaverChanged, LV_EVENT_VALUE_CHANGED, this); lv_dropdown_set_selected(screensaverDropdown, static_cast(displaySettings.screensaverType)); if (!displaySettings.backlightTimeoutEnabled) { lv_obj_add_state(screensaverDropdown, LV_STATE_DISABLED); } // Disable screensaver when charging toggle disableWhenChargingWrapper = lv_obj_create(main_wrapper); lv_obj_set_size(disableWhenChargingWrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(disableWhenChargingWrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(disableWhenChargingWrapper, 0, LV_STATE_DEFAULT); auto* charging_label = lv_label_create(disableWhenChargingWrapper); lv_label_set_text(charging_label, "Disable on charging"); lv_obj_align(charging_label, LV_ALIGN_LEFT_MID, 0, 0); disableWhenChargingSwitch = lv_switch_create(disableWhenChargingWrapper); if (displaySettings.disableScreensaverWhenCharging) { lv_obj_add_state(disableWhenChargingSwitch, LV_STATE_CHECKED); } lv_obj_align(disableWhenChargingSwitch, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(disableWhenChargingSwitch, onDisableWhenChargingChanged, LV_EVENT_VALUE_CHANGED, this); if (!displaySettings.backlightTimeoutEnabled) { lv_obj_add_state(disableWhenChargingWrapper, LV_STATE_DISABLED); } } // Invert color toggle (if display supports it - e.g. RLCD ST7305) { auto hal_disp = getHalDisplay(); if (hal_disp && hal_disp->supportsInvertColor()) { auto* invert_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(invert_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(invert_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(invert_wrapper, 0, LV_STATE_DEFAULT); auto* invert_label = lv_label_create(invert_wrapper); lv_label_set_text(invert_label, "Invert colors"); lv_obj_align(invert_label, LV_ALIGN_LEFT_MID, 0, 0); auto* invert_switch = lv_switch_create(invert_wrapper); if (displaySettings.invertColor) { lv_obj_add_state(invert_switch, LV_STATE_CHECKED); } lv_obj_align(invert_switch, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(invert_switch, onInvertColorChanged, LV_EVENT_VALUE_CHANGED, this); } } // Monochrome threshold slider (ST7305 reflective) - adjustable B/W cut { auto hal_disp = getHalDisplay(); if (hal_disp && hal_disp->supportsMonochromeThreshold()) { auto* thresh_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(thresh_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(thresh_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(thresh_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_flex_flow(thresh_wrapper, LV_FLEX_FLOW_COLUMN); lv_obj_set_style_pad_row(thresh_wrapper, 2, LV_STATE_DEFAULT); auto* header_row = lv_obj_create(thresh_wrapper); lv_obj_set_size(header_row, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(header_row, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(header_row, 0, LV_STATE_DEFAULT); auto* thresh_label = lv_label_create(header_row); lv_label_set_text(thresh_label, "Mono threshold"); lv_obj_align(thresh_label, LV_ALIGN_LEFT_MID, 0, 0); auto* value_label = lv_label_create(header_row); lv_label_set_text_fmt(value_label, "%d", displaySettings.monochromeThreshold); lv_obj_align(value_label, LV_ALIGN_RIGHT_MID, 0, 0); auto* thresh_slider = lv_slider_create(thresh_wrapper); lv_obj_set_width(thresh_slider, LV_PCT(100)); lv_slider_set_range(thresh_slider, 20, 230); // avoid extremes that make screen all black/white lv_slider_set_value(thresh_slider, displaySettings.monochromeThreshold, LV_ANIM_OFF); // Live label update: pass value_label as user_data lv_obj_add_event_cb(thresh_slider, onMonoThresholdChanging, LV_EVENT_VALUE_CHANGED, value_label); // Persist + apply: pass app as user_data lv_obj_add_event_cb(thresh_slider, onMonoThresholdChanged, LV_EVENT_VALUE_CHANGED, this); // Apply initial threshold live hal_disp->setMonochromeThreshold(displaySettings.monochromeThreshold); } } if (hasCalibratableTouchDevice()) { auto* calibrate_wrapper = lv_obj_create(main_wrapper); lv_obj_set_size(calibrate_wrapper, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_all(calibrate_wrapper, 0, LV_STATE_DEFAULT); lv_obj_set_style_border_width(calibrate_wrapper, 0, LV_STATE_DEFAULT); auto* calibrate_label = lv_label_create(calibrate_wrapper); lv_label_set_text(calibrate_label, "Touch calibration"); lv_obj_align(calibrate_label, LV_ALIGN_LEFT_MID, 0, 0); auto* calibrate_button = lv_button_create(calibrate_wrapper); lv_obj_align(calibrate_button, LV_ALIGN_RIGHT_MID, 0, 0); lv_obj_add_event_cb(calibrate_button, onCalibrateTouchClicked, LV_EVENT_SHORT_CLICKED, this); auto* calibrate_button_label = lv_label_create(calibrate_button); lv_label_set_text(calibrate_button_label, "Calibrate"); lv_obj_center(calibrate_button_label); } } void onHide(AppContext& app) override { if (displaySettingsUpdated) { // Dispatch it, so file IO doesn't block the UI const settings::display::DisplaySettings settings_to_save = displaySettings; getMainDispatcher().dispatch([settings_to_save] { settings::display::save(settings_to_save); #ifdef ESP_PLATFORM // Notify DisplayIdle service to reload settings auto displayIdle = service::displayidle::findService(); if (displayIdle) { displayIdle->reloadSettings(); } #endif }); } } }; extern const AppManifest manifest = { .appId = "Display", .appName = "Display", .appIcon = LVGL_ICON_SHARED_DISPLAY_SETTINGS, .appCategory = Category::Settings, .createApp = create }; } // namespace