/* * settings — NVS persistence for everything that must survive a reboot * (PRD 4.5): setpoint, mode, selected room, schedule. * * Writes are debounced. Turning the knob generates a setpoint change every * detent, and committing each one would put thousands of writes a year onto a * flash sector for no benefit. settings_mark_dirty() records intent and a * background task commits once the value has been still for a few seconds. * * Runtime flash writes are safe here only because the RGB panel is configured * with no bounce buffers (see drivers/display/display.c). If that ever * changes, this module becomes a source of "Cache disabled but cached memory * region accessed" panics and the panel must be paused around the commit. */ #pragma once #include #include #include "control.h" #include "esp_err.h" /* Schedule: time-of-day rules picking a governing room (PRD 4.2 mode 2). */ #define SCHEDULE_MAX_RULES 6 typedef struct { uint8_t start_hour; /* rule applies from this hour... */ uint8_t start_minute; uint8_t room_index; /* ...until the next rule's start */ bool enabled; } schedule_rule_t; typedef struct { float setpoint_c; uint8_t mode; /* room_mode_t */ uint8_t selected_room; schedule_rule_t schedule[SCHEDULE_MAX_RULES]; } settings_t; /* Load from NVS, falling back to defaults for anything absent or invalid. */ esp_err_t settings_init(void); /* Current settings. Never fails; returns defaults before settings_init(). */ void settings_get(settings_t *out); void settings_set_setpoint(float celsius); void settings_set_mode(uint8_t mode); void settings_set_selected_room(uint8_t room); void settings_set_schedule(const schedule_rule_t *rules, int count); /* * Which room the schedule says should govern at the given local time, or -1 * if no rule matches. */ int settings_schedule_room_for_time(int hour, int minute); /* Force an immediate commit, bypassing the debounce. */ esp_err_t settings_commit_now(void);