Files
frame/poc/apps/m1_hardware/main/hardware_status.hh
T
Lainandlain f5f944bef0 feat(m1): local time display and three-button monitoring (#6)
## Summary

Two M1 firmware enhancements on the Waveshare ESP32-S3-RLCD-4.2:

**Local-time display (UTC+8, fixed offset — China has no DST).**
Internal time stays strictly
UTC (RTC chip, JSONL `utc`, console `--json` keep the `Z`-suffixed
machine format). Only
human-facing surfaces convert via the new
`board::kLocalUtcOffsetSeconds`:

- display RTC line renders `unix + 8*3600` labeled `UTC+8` (split after
the date: measured
montserrat16 widths 157px + 119px fit the 179px status column; the old
one-line layout was
  already 223px and overflowed)
- text `status` and `rtc status` show the labeled UTC+8 line and keep
both the raw unix and a
  UTC line
- `rtc set` now takes UTC+8 local time (validates the local calendar
round-trip, converts
  -8h before `set_rtc_time`); help/usage/README updated

**Three buttons.**

- KEY (GPIO18) unchanged (short <1s / long 1-3s / >=3s portal).
- BOOT (GPIO0, strapping pin — input + pull-up, runtime read only):
mirrors the KEY 30ms
debounce and release bands (short <1s, long >=1s), new
`boot_short_presses` /
  `boot_long_presses`, no portal binding.
- RST is hardwired to chip reset, so it is tracked as a persistent boot
counter: NVS
namespace `frame`, key `rst_cnt`, incremented once per boot after
`nvs_flash_init`, exposed
  as `reset_count`.
- Surfaces: display `KEY:a/b BOOT:c/d` + `RST:e` lines, console text +
`--json`
(`boot_short`, `boot_long`, `reset_count`), JSONL
`boot_short`/`boot_long` only — measured
594B worst observed line vs the 640B buffer (46B headroom, gate was
>=40B); `reset_count`
  is static per boot so it is `--json`-only.

## On-device verification (34/34 deterministic asserts PASS + user
assist)

Evidence:
`.omo/evidence/m1wp/tz-buttons-finish-m1-wifi-provisioning.log`

- Build 0 warnings; clang-format 18.1.8 clean on whole `main/`.
- `rtc status`/text status UTC+8 lines match host `date` +8h (<=3s
skew); `rtc set` with a
  UTC+8 local time round-trips (`1788157541 = 14:25:41 local - 8h`).
- JSONL `utc` unchanged UTC-with-Z, crc32c intact, max line 594B < 640.
- KEY short press: +1 with BOOT unchanged (user-assisted). BOOT short:
+2 (user-assisted).
BOOT long: not physically exercised this session — code path is the
device-proven KEY band
logic (same constants); noted in `.omo/notepads` for the next bench
session.
- RST/reset_count: host RTS-pulse reboot 3->4 with boot banner; `--json`
agrees.
- Regressions: wifi auto-connect CONNECTED, tf_logging true, selftest
`wifi_config: ok` +
  `bad ignored`, `prov status` three-state.

Co-authored-by: ania-lain <[email protected]>
2026-08-31 14:39:04 +08:00

121 lines
2.9 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
namespace frame::m1 {
enum class health_level : uint8_t {
ok,
degraded,
failed,
recovery,
};
enum class sd_state : uint8_t {
absent,
health_checking,
ready,
failed,
recovering,
};
enum class wifi_state : uint8_t {
unconfigured,
disconnected,
connecting,
connected,
};
struct sensor_sample {
int16_t temperature_tenths_celsius{};
uint16_t humidity_tenths_percent{};
bool valid{};
int64_t sampled_at_us{};
};
struct rtc_time {
int64_t unix_seconds{};
bool valid{};
int64_t read_at_us{};
};
struct status_snapshot {
char device_id[13]{};
char elf_sha256[65]{};
char image_sha256[65]{};
char app_version[32]{};
char idf_version[32]{};
char toolchain_version[48]{};
char ip_address[16]{};
std::size_t flash_bytes{};
std::size_t psram_bytes{};
std::size_t internal_free_bytes{};
std::size_t internal_largest_block{};
std::size_t psram_free_bytes{};
std::size_t psram_largest_block{};
uint32_t chip_revision{};
uint32_t cpu_cores{};
uint32_t reset_reason{};
uint64_t key_short_presses{};
uint64_t key_long_presses{};
uint64_t boot_short_presses{};
uint64_t boot_long_presses{};
uint32_t reset_count{};
bool target_ok{};
bool flash_ok{};
bool psram_ok{};
bool partitions_ok{};
bool rtc_present{};
bool sensor_present{};
bool ssid_configured{};
bool ap_active{};
bool ap_mode_requested{};
bool key_pressed{};
bool nvs_encrypted{};
bool tf_logging_ok{};
sd_state storage{sd_state::absent};
wifi_state wifi{wifi_state::unconfigured};
sensor_sample sensor{};
rtc_time rtc{};
esp_err_t display_error{ESP_ERR_INVALID_STATE};
esp_err_t storage_error{ESP_ERR_INVALID_STATE};
esp_err_t board_error{ESP_ERR_INVALID_STATE};
esp_err_t wifi_error{ESP_ERR_INVALID_STATE};
esp_err_t logging_error{ESP_ERR_INVALID_STATE};
};
class hardware_status {
public:
hardware_status();
hardware_status(const hardware_status&) = delete;
hardware_status& operator=(const hardware_status&) = delete;
void initialize_platform();
void refresh_memory_metrics();
[[nodiscard]] status_snapshot snapshot() const;
template <typename Function> void update(Function&& function) {
if (xSemaphoreTake(mutex_, portMAX_DELAY) == pdTRUE) {
function(value_);
xSemaphoreGive(mutex_);
}
}
[[nodiscard]] static health_level overall_health(const status_snapshot& value);
[[nodiscard]] static const char* health_name(health_level value);
[[nodiscard]] static const char* sd_state_name(sd_state value);
[[nodiscard]] static const char* wifi_state_name(wifi_state value);
private:
mutable SemaphoreHandle_t mutex_{};
status_snapshot value_{};
};
} // namespace frame::m1