Add firmware font size setting

This commit is contained in:
Adolfo Reyna
2026-07-24 21:28:24 -04:00
parent 56bbd6a90e
commit d8c5a55fe8
9 changed files with 200 additions and 3 deletions
+7
View File
@@ -1,5 +1,9 @@
#pragma once
namespace tt::settings::display {
enum class FontSize;
}
namespace tt::lvgl {
#ifdef ESP_PLATFORM
@@ -12,6 +16,9 @@ static constexpr auto* PATH_PREFIX = "A:/";
bool isStarted();
/** Applies the selected semantic text size to the LVGL theme and future widgets. */
void applyFontSize(settings::display::FontSize fontSize);
void start();
void stop();
@@ -22,8 +22,16 @@ enum class ScreensaverType {
Count // Sentinel for bounds checking - must be last
};
enum class FontSize {
Small,
Default,
Large,
Count
};
struct DisplaySettings {
Orientation orientation;
FontSize fontSize = FontSize::Default;
uint8_t gammaCurve;
uint8_t backlightDuty;
bool backlightTimeoutEnabled;
+33
View File
@@ -8,6 +8,7 @@
#include <Tactility/app/App.h>
#include <Tactility/hal/display/DisplayDevice.h>
#include <Tactility/lvgl/Lvgl.h>
#include <Tactility/lvgl/Toolbar.h>
#include <Tactility/settings/DisplaySettings.h>
@@ -74,6 +75,21 @@ class HalDisplayApp final : public App {
}
}
static void onFontSizeChanged(lv_event_t* event) {
auto* app = static_cast<HalDisplayApp*>(lv_event_get_user_data(event));
auto* dropdown = static_cast<lv_obj_t*>(lv_event_get_target(event));
uint32_t selected_index = lv_dropdown_get_selected(dropdown);
if (selected_index >= static_cast<uint32_t>(settings::display::FontSize::Count)) {
return;
}
auto selected_size = static_cast<settings::display::FontSize>(selected_index);
if (selected_size != app->displaySettings.fontSize) {
app->displaySettings.fontSize = selected_size;
app->displaySettingsUpdated = true;
lvgl::applyFontSize(selected_size);
}
}
static void onDisableWhenChargingChanged(lv_event_t* event) {
auto* app = static_cast<HalDisplayApp*>(lv_event_get_user_data(event));
auto* sw = static_cast<lv_obj_t*>(lv_event_get_target(event));
@@ -224,6 +240,23 @@ public:
// Set the dropdown to match current orientation enum
lv_dropdown_set_selected(orientation_dropdown, static_cast<uint16_t>(displaySettings.orientation));
// Font size
auto* font_size_wrapper = lv_obj_create(main_wrapper);
lv_obj_set_size(font_size_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(font_size_wrapper, 0, LV_STATE_DEFAULT);
lv_obj_set_style_border_width(font_size_wrapper, 0, LV_STATE_DEFAULT);
auto* font_size_label = lv_label_create(font_size_wrapper);
lv_label_set_text(font_size_label, "Font size");
lv_obj_align(font_size_label, LV_ALIGN_LEFT_MID, 0, 0);
auto* font_size_dropdown = lv_dropdown_create(font_size_wrapper);
lv_dropdown_set_options(font_size_dropdown, "Small\nDefault\nLarge");
lv_obj_align(font_size_dropdown, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_add_event_cb(font_size_dropdown, onFontSizeChanged, LV_EVENT_VALUE_CHANGED, this);
lv_dropdown_set_selected(font_size_dropdown, static_cast<uint16_t>(displaySettings.fontSize));
// Screen timeout
if (hal_display->supportsBacklightDuty()) {
@@ -11,6 +11,7 @@
#include <Tactility/service/displayidle/DisplayIdleService.h>
#endif
#include <Tactility/app/App.h>
#include <Tactility/lvgl/Lvgl.h>
#include <Tactility/lvgl/Toolbar.h>
#include <Tactility/settings/DisplaySettings.h>
@@ -69,6 +70,21 @@ class KernelDisplayApp final : public App {
}
}
static void onFontSizeChanged(lv_event_t* event) {
auto* app = static_cast<KernelDisplayApp*>(lv_event_get_user_data(event));
auto* dropdown = static_cast<lv_obj_t*>(lv_event_get_target(event));
uint32_t selected_index = lv_dropdown_get_selected(dropdown);
if (selected_index >= static_cast<uint32_t>(settings::display::FontSize::Count)) {
return;
}
auto selected_size = static_cast<settings::display::FontSize>(selected_index);
if (selected_size != app->displaySettings.fontSize) {
app->displaySettings.fontSize = selected_size;
app->displaySettingsUpdated = true;
lvgl::applyFontSize(selected_size);
}
}
static void onTimeoutSwitch(lv_event_t* event) {
auto* app = static_cast<KernelDisplayApp*>(lv_event_get_user_data(event));
auto* sw = static_cast<lv_obj_t*>(lv_event_get_target(event));
@@ -183,6 +199,23 @@ public:
// Set the dropdown to match current orientation enum
lv_dropdown_set_selected(orientation_dropdown, static_cast<uint16_t>(displaySettings.orientation));
// Font size
auto* font_size_wrapper = lv_obj_create(main_wrapper);
lv_obj_set_size(font_size_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(font_size_wrapper, 0, LV_STATE_DEFAULT);
lv_obj_set_style_border_width(font_size_wrapper, 0, LV_STATE_DEFAULT);
auto* font_size_label = lv_label_create(font_size_wrapper);
lv_label_set_text(font_size_label, "Font size");
lv_obj_align(font_size_label, LV_ALIGN_LEFT_MID, 0, 0);
auto* font_size_dropdown = lv_dropdown_create(font_size_wrapper);
lv_dropdown_set_options(font_size_dropdown, "Small\nDefault\nLarge");
lv_obj_align(font_size_dropdown, LV_ALIGN_RIGHT_MID, 0, 0);
lv_obj_add_event_cb(font_size_dropdown, onFontSizeChanged, LV_EVENT_VALUE_CHANGED, this);
lv_dropdown_set_selected(font_size_dropdown, static_cast<uint16_t>(displaySettings.fontSize));
// Screen timeout
// Note: DisplayIdleService doesn't act on these settings for kernel-driver displays yet
// (it only looks up the deprecated tt::hal::display::DisplayDevice), so these currently
+45
View File
@@ -13,6 +13,7 @@
#include <tactility/device.h>
#include <tactility/drivers/backlight.h>
#include <tactility/log.h>
#include <tactility/lvgl_fonts.h>
#include <tactility/lvgl_module.h>
#include <tactility/lvgl_pointer.h>
#include <tactility/module.h>
@@ -27,6 +28,49 @@ bool isStarted() {
return module_is_started(&lvgl_module);
}
void applyFontSize(settings::display::FontSize fontSize) {
LvglFontScale scale;
switch (fontSize) {
using enum settings::display::FontSize;
case Small:
scale = FONT_SCALE_SMALL;
break;
case Large:
scale = FONT_SCALE_LARGE;
break;
case Default:
default:
scale = FONT_SCALE_DEFAULT;
break;
}
lvgl_set_text_font_scale(scale);
const lv_font_t* font = lvgl_get_text_font(FONT_SIZE_DEFAULT);
for (lv_display_t* display = lv_display_get_next(nullptr);
display != nullptr;
display = lv_display_get_next(display)) {
#if LV_USE_THEME_DEFAULT
if (lv_display_get_theme(display) == lv_theme_default_get()) {
lv_obj_t* screen = lv_display_get_screen_active(display);
lv_theme_default_init(
display,
lv_theme_get_color_primary(screen),
lv_theme_get_color_secondary(screen),
LV_THEME_DEFAULT_DARK,
font
);
}
#endif
// The display layers are independent inheritance roots. Updating all of them makes
// the new size visible immediately in regular screens, overlays, and the status bar.
lv_obj_set_style_text_font(lv_display_get_screen_active(display), font, LV_PART_MAIN);
lv_obj_set_style_text_font(lv_display_get_layer_top(display), font, LV_PART_MAIN);
lv_obj_set_style_text_font(lv_display_get_layer_sys(display), font, LV_PART_MAIN);
lv_obj_set_style_text_font(lv_display_get_layer_bottom(display), font, LV_PART_MAIN);
}
}
void attachDevices() {
LOG_I(TAG, "Adding devices");
@@ -55,6 +99,7 @@ void attachDevices() {
if (rotation != lv_display_get_rotation(primary_lvgl_display)) {
lv_display_set_rotation(primary_lvgl_display, rotation);
}
applyFontSize(settings.fontSize);
}
// Start touch
@@ -17,6 +17,7 @@ static std::string getSettingsFilePath() {
}
constexpr auto* SETTINGS_KEY_ORIENTATION = "orientation";
constexpr auto* SETTINGS_KEY_FONT_SIZE = "fontSize";
constexpr auto* SETTINGS_KEY_GAMMA_CURVE = "gammaCurve";
constexpr auto* SETTINGS_KEY_BACKLIGHT_DUTY = "backlightDuty";
constexpr auto* SETTINGS_KEY_TIMEOUT_ENABLED = "backlightTimeoutEnabled";
@@ -71,6 +72,34 @@ static bool fromString(const std::string& str, Orientation& orientation) {
}
}
static std::string toString(FontSize font_size) {
switch (font_size) {
using enum FontSize;
case Small:
return "Small";
case Default:
return "Default";
case Large:
return "Large";
default:
std::unreachable();
}
}
static bool fromString(const std::string& str, FontSize& font_size) {
if (str == "Small") {
font_size = FontSize::Small;
return true;
} else if (str == "Default") {
font_size = FontSize::Default;
return true;
} else if (str == "Large") {
font_size = FontSize::Large;
return true;
}
return false;
}
static std::string toString(ScreensaverType type) {
switch (type) {
using enum ScreensaverType;
@@ -132,6 +161,12 @@ bool load(DisplaySettings& settings) {
orientation = getDefaultOrientation();
}
auto font_size_entry = map.find(SETTINGS_KEY_FONT_SIZE);
FontSize font_size = FontSize::Default;
if (font_size_entry != map.end()) {
fromString(font_size_entry->second, font_size);
}
auto gamma_entry = map.find(SETTINGS_KEY_GAMMA_CURVE);
int gamma_curve = 0;
if (gamma_entry != map.end()) {
@@ -172,6 +207,7 @@ bool load(DisplaySettings& settings) {
}
settings.orientation = orientation;
settings.fontSize = font_size;
settings.gammaCurve = gamma_curve;
settings.backlightDuty = backlight_duty;
settings.backlightTimeoutEnabled = timeout_enabled;
@@ -185,6 +221,7 @@ bool load(DisplaySettings& settings) {
DisplaySettings getDefault() {
return DisplaySettings {
.orientation = getDefaultOrientation(),
.fontSize = FontSize::Default,
.gammaCurve = 1,
.backlightDuty = 200,
.backlightTimeoutEnabled = false,
@@ -207,6 +244,7 @@ bool save(const DisplaySettings& settings) {
map[SETTINGS_KEY_BACKLIGHT_DUTY] = std::to_string(settings.backlightDuty);
map[SETTINGS_KEY_GAMMA_CURVE] = std::to_string(settings.gammaCurve);
map[SETTINGS_KEY_ORIENTATION] = toString(settings.orientation);
map[SETTINGS_KEY_FONT_SIZE] = toString(settings.fontSize);
map[SETTINGS_KEY_TIMEOUT_ENABLED] = settings.backlightTimeoutEnabled ? "1" : "0";
map[SETTINGS_KEY_TIMEOUT_MS] = std::to_string(settings.backlightTimeoutMs);
map[SETTINGS_KEY_SCREENSAVER_TYPE] = toString(settings.screensaverType);