Files
thermostat-hw/main/ui/ui_manager.h
mattandClaude 3aabcac90a Add thermostat control for heat and fan with cycle protection
Turns the weather dashboard into an actual thermostat. Relays are not
fitted yet -- the GPIOs are driven and can be metered at the UART1-OUT
plug (idle 3.3V, energized 0V, active-low module assumed).

Control loop (main/control/thermostat.c) is pure logic: no ESP-IDF, no
network, no clock of its own. The caller passes a temperature and a
millisecond timestamp, which is what lets the simulator and the test
harness run the identical decision code.

Equipment protections, all verified by tools/thermostat_test.c:

  deadband      1.0F    stops the burner chattering at the setpoint
  minimum on    3 min   a shorter call is all ignition wear, no heat
  minimum off   5 min   back-to-back ignitions are what kill igniters
  cycles/hour   5       standard gas default; catches a flapping sensor
  boot lockout  60 s    a boot loop must not machine-gun the gas valve

That test drives the state machine with synthetic time, so two hours of
furnace behaviour runs in milliseconds. It exits non-zero on failure and
is worth re-running after any edit to thermostat.c -- this switches a gas
valve.

Two deliberate decisions:

  - G is never energized during a heat call. The furnace runs its own
    blower with a fan-on delay that waits for the heat exchanger and a
    fan-off delay that scavenges residual heat; driving G ourselves would
    blow cold air at people and throw that heat away. G is manual
    circulation only.
  - The control loop starts before every early return in app_main. Those
    returns are all "no WiFi" or "no HA token", and none of them is a
    reason to leave a house unheated.

Relay pins are IO20 heat / IO19 fan on the UART1-OUT plug, measured on
this board since Elecrow documents the GPIO numbers but not the connector
pin order. Heat is on IO20 because it is USB D+, the line carrying the
hardware pull-up -- with an active-low module it fails toward "no heat"
rather than "heat stuck on" if the USB PHY is ever re-enabled. Both pins
were confirmed free of the onboard mic before use.
CONFIG_USJ_ENABLE_USB_SERIAL_JTAG and CONFIG_ESP_PHY_ENABLE_USB are now
off to release them; the console is on UART0 via the CH340 regardless.

UI: the indoor card gains a status dot, mode and fan buttons, and a
setpoint column with finger-sized up/down targets. The reading grid drops
from six cells to four to make room -- wind was the one to cut, since the
station anemometer has been dead since 2026-07-27 and that cell has never
shown a number. Wind now rides on the outdoor cell's sub-line and will
reappear on its own if the sensor is fixed. Sunrise and sunset share a
cell, showing whichever comes next.

Known weakness, documented in README and top of TODO: indoor temperature
comes from Home Assistant over the network, so an HA outage ages the
reading until the 15-minute failsafe forces heat off. A local I2C sensor
removes the failure mode entirely and is the next thing to add. The
setpoint also resets to 68F on reboot, because persisting it means an NVS
write and a flash write while the panel scans faults the bounce-buffer
ISR.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-05 20:09:36 -07:00

72 lines
2.2 KiB
C

#ifndef UI_MANAGER_H
#define UI_MANAGER_H
#include <stdbool.h>
#include <stdint.h>
#include "ha_data.h"
#include "lvgl.h"
#include "thermostat.h"
/*
* The screen layout.
*
* This file is compiled into the desktop simulator as well as the firmware
* (see sim/README.md), so it must never touch a driver, a socket or ESP-IDF
* directly. Data arrives as ha_data_t; commands leave through the callback
* below. Everything in between is drawing.
*/
void ui_init(void);
/* Safe to call from any task -- these take the LVGL lock internally. */
/* Redraw every panel from `data`. Cheap to call when nothing changed: every
* label goes through a comparison first, so an unchanged value costs no
* invalidation and therefore no flush. */
void ui_update(const ha_data_t *data);
void ui_set_status(const char *status);
/*
* Mark the displayed readings as no longer trustworthy. Dims the temperature
* so a value that stopped updating is visibly distinct from a live one,
* rather than sitting there at full styling looking current.
*/
void ui_set_stale(bool stale);
/*
* Called when the user operates a light tile.
*
* `index` indexes HA_LIGHTS. `brightness` is 0-255, or -1 for a plain on/off
* with no brightness change (a tile tap, or any switch-domain entity).
*
* Runs on the LVGL task with the LVGL lock held, so the implementation must
* not block: hand the command to another task rather than doing HTTP here.
*/
typedef void (*ui_light_cmd_cb_t)(int index, bool on, int16_t brightness);
void ui_set_light_cb(ui_light_cmd_cb_t cb);
/* Redraw the thermostat card from `st`. Same cheapness rules as ui_update. */
void ui_update_thermostat(const therm_status_t *st);
typedef enum {
UI_THERM_SETPOINT_UP = 0,
UI_THERM_SETPOINT_DOWN,
UI_THERM_MODE_TOGGLE,
UI_THERM_FAN_TOGGLE,
} ui_therm_cmd_t;
/*
* Called when the user operates a thermostat control.
*
* Runs on the LVGL task with the lock held, so it must not block -- but
* unlike the light commands there is no network here, so the handler can
* apply the change and call ui_update_thermostat() straight back. The LVGL
* lock is recursive, so that re-entry is fine.
*/
typedef void (*ui_therm_cmd_cb_t)(ui_therm_cmd_t cmd);
void ui_set_therm_cb(ui_therm_cmd_cb_t cb);
#endif