Add Dark mode for a screen beside the bed, with every colour in one table (0.2.63)

Dark mode is a new switch under Brightness on the screen's settings page, in
the Screen settings of ESP Screens and as switch.<screen>_dark_mode in Home
Assistant, so an automation can turn it on at bedtime. The screen changes
look at once, open cards and an alert included, and keeps it after a
restart. The design stays the same: a black page, graphite cards with a
hairline, soft white text, Home Assistant's state colours where they tell
something, and pastel card colours as deeper versions of themselves.

Every colour the firmware chooses now lives in components/smart_display/
theme.h: roles with a light and a dark value, the named card colours and
Home Assistant's state colours. The board profiles give widgets colourless
shared styles that theme.h fills, so a change of look refills those styles
and draws again what code painted by state. The light look is unchanged
apart from merges below one panel step. Dark mode costs 440 bytes of static
RAM on the CYD, and the LVGL tree of the host build is 416 bytes smaller; tests
keep colours out of every other file.

Comparing a screen that switched looks with one that booted in it also
found two older bugs, now fixed: a settings switch that was on showed its
knob at the left whenever its page was drawn, and the sun card's sunlit
area turned orange after a palette change. Firmware 0.2.54.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Max Gramser
2026-09-17 12:07:03 +02:00
parent 8971ab07e1
commit bc2a754706
40 changed files with 1885 additions and 1363 deletions
+22 -1
View File
@@ -43,6 +43,11 @@ int main() {
assert(moment_text(1320, false) == "10:00 PM" && moment_text(0, false) == "12:00 AM");
assert(moment_text(750, false) == "12:30 PM" && moment_text(60, false) == "1:00 AM");
// ---- a switch's knob: inset at the left when off, flush with the same inset at the right when on ----
// The Guition switch is 62 by 34 (a 28 px knob), the CYD one 40 by 22 (an 18 px knob).
assert(knob_x(62, 34, false) == 3 && knob_x(62, 34, true) == 31 && 31 + 28 + 3 == 62);
assert(knob_x(40, 22, false) == 2 && knob_x(40, 22, true) == 20 && 20 + 18 + 2 == 40);
// ---- rows read and write the real settings ----
screen_settings::current = screen_settings::Settings{};
const Page &light = pages[1];
@@ -58,6 +63,14 @@ int main() {
standby.write(90);
assert(screen_settings::current.standby_brightness == 30); // never above the normal brightness
assert(screen_settings::current.valid());
// Dark mode (firmware 0.2.54+) sits right under Brightness, a switch of its own outside the frozen block.
assert(light.count == 5 && light.rows[1].kind == Kind::toggle && std::string(light.rows[1].label) == "Dark mode");
const Row &dark = row_named(light, "Dark mode");
assert(value_text(dark) == "Off");
dark.write(1);
assert(dark_mode == 1 && value_text(dark) == "On");
dark.write(0);
assert(dark_mode == 0 && value_text(dark) == "Off");
const Page &screen = pages[3];
const Row &clock = row_named(screen, "Clock");
@@ -138,6 +151,7 @@ int main() {
{"night_start", -5, 0}, {"night_end", 2000, 1439}, {"night_brightness", 7, 7}, {"clock_24h", 0, 0},
{"home_on_standby", 1, 1}, {"swipe_pages", 2, 1}, {"rotation", 100, 90}, {"rotation", 400, 270},
{"auto_home", 0, 0}, {"auto_home_seconds", 5, 30}, {"auto_home_seconds", 99999, 3600},
{"dark_mode", 7, 1},
};
for (const auto &c : cases) {
result = set(c.key, c.value);
@@ -146,8 +160,15 @@ int main() {
}
assert(screen_settings::current.standby_seconds == 86400 && screen_settings::current.brightness == 100);
assert(screen_settings::current.night_start == 0 && screen_settings::current.night_end == 1439);
assert(swipe_pages == 1 && rotation == 270 && auto_home == 0 && auto_home_seconds == 3600);
assert(swipe_pages == 1 && rotation == 270 && auto_home == 0 && auto_home_seconds == 3600 && dark_mode == 1);
assert(screen_settings::current.valid());
// Dark mode again is the same look: nothing stored, applied or reported; off is a change.
{
const int stored_before = stored, applied_before = applied;
assert(set("dark_mode", 1) == SetResult::same && stored == stored_before && applied == applied_before);
assert(set("dark_mode", 0) == SetResult::changed && last_key == "dark_mode" && last_value == 0 && dark_mode == 0);
set("dark_mode", 1);
}
// A lower brightness pulls both dim levels down, and reports the brightness itself.
set("standby_brightness", 60);
set("night_brightness", 50);