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]>
This commit is contained in:
@@ -19,6 +19,21 @@ uint8_t from_bcd(uint8_t value) {
|
||||
return static_cast<uint8_t>((value >> 4U) * 10U + (value & 0x0FU));
|
||||
}
|
||||
|
||||
// Renders unix seconds (already offset by the caller) as "YYYY-MM-DD HH:MM:SS".
|
||||
void format_unix_datetime(int64_t unix_seconds, char* output, std::size_t capacity) {
|
||||
output[0] = '\0';
|
||||
const time_t seconds = static_cast<time_t>(unix_seconds);
|
||||
struct tm utc_time {};
|
||||
if (gmtime_r(&seconds, &utc_time) != nullptr) {
|
||||
const unsigned year = static_cast<unsigned>(utc_time.tm_year + 1900);
|
||||
std::snprintf(
|
||||
output, capacity, "%04u-%02u-%02u %02u:%02u:%02u", year,
|
||||
static_cast<unsigned>(utc_time.tm_mon + 1), static_cast<unsigned>(utc_time.tm_mday),
|
||||
static_cast<unsigned>(utc_time.tm_hour), static_cast<unsigned>(utc_time.tm_min),
|
||||
static_cast<unsigned>(utc_time.tm_sec));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
esp_err_t console_service::start() {
|
||||
@@ -75,7 +90,7 @@ esp_err_t console_service::start() {
|
||||
};
|
||||
const esp_console_cmd_t rtc_cmd{
|
||||
.command = "rtc",
|
||||
.help = "rtc status | rtc raw | rtc set YYYY-MM-DD HH:MM:SS (UTC)",
|
||||
.help = "rtc status | rtc raw | rtc set YYYY-MM-DD HH:MM:SS (UTC+8 local)",
|
||||
.hint = nullptr,
|
||||
.func = &console_service::rtc_command,
|
||||
.argtable = nullptr,
|
||||
@@ -244,7 +259,7 @@ int console_service::rtc_command(int argc, char** argv) {
|
||||
if (argc == 4 && std::strcmp(argv[1], "set") == 0) {
|
||||
return instance_->run_rtc_set(argv[2], argv[3]);
|
||||
}
|
||||
std::printf("usage: rtc status | rtc raw | rtc set YYYY-MM-DD HH:MM:SS\n");
|
||||
std::printf("usage: rtc status | rtc raw | rtc set YYYY-MM-DD HH:MM:SS (UTC+8 local)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -262,19 +277,14 @@ void console_service::print_rtc_status() const {
|
||||
value.rtc.unix_seconds);
|
||||
|
||||
char utc[32]{};
|
||||
char local[32]{};
|
||||
if (value.rtc.valid) {
|
||||
const time_t seconds = static_cast<time_t>(value.rtc.unix_seconds);
|
||||
struct tm utc_time {};
|
||||
if (gmtime_r(&seconds, &utc_time) != nullptr) {
|
||||
const unsigned year = static_cast<unsigned>(utc_time.tm_year + 1900);
|
||||
std::snprintf(
|
||||
utc, sizeof(utc), "%04u-%02u-%02u %02u:%02u:%02u", year,
|
||||
static_cast<unsigned>(utc_time.tm_mon + 1), static_cast<unsigned>(utc_time.tm_mday),
|
||||
static_cast<unsigned>(utc_time.tm_hour), static_cast<unsigned>(utc_time.tm_min),
|
||||
static_cast<unsigned>(utc_time.tm_sec));
|
||||
}
|
||||
format_unix_datetime(value.rtc.unix_seconds, utc, sizeof(utc));
|
||||
format_unix_datetime(value.rtc.unix_seconds + board::kLocalUtcOffsetSeconds, local,
|
||||
sizeof(local));
|
||||
}
|
||||
std::printf("utc: %s\n", utc[0] == '\0' ? "-" : utc);
|
||||
std::printf("local: %s UTC+8\n", local[0] == '\0' ? "-" : local);
|
||||
std::printf("OS bit: %s\n", os_state);
|
||||
}
|
||||
|
||||
@@ -309,7 +319,7 @@ int console_service::run_rtc_set(const char* date_text, const char* time_text) {
|
||||
}
|
||||
struct tm parsed {};
|
||||
if (strptime(text, "%Y-%m-%d %H:%M:%S", &parsed) == nullptr) {
|
||||
std::printf("rtc set: unparseable, expected YYYY-MM-DD HH:MM:SS (UTC)\n");
|
||||
std::printf("rtc set: unparseable, expected YYYY-MM-DD HH:MM:SS (UTC+8 local)\n");
|
||||
return 1;
|
||||
}
|
||||
const int year = parsed.tm_year + 1900;
|
||||
@@ -320,10 +330,11 @@ int console_service::run_rtc_set(const char* date_text, const char* time_text) {
|
||||
std::printf("rtc set: value out of range\n");
|
||||
return 1;
|
||||
}
|
||||
const int64_t unix_seconds = board_service::unix_from_utc(
|
||||
// Input is UTC+8 local time; validate the local calendar, then shift to UTC for the chip.
|
||||
const int64_t local_seconds = board_service::unix_from_utc(
|
||||
year, month, static_cast<unsigned>(parsed.tm_mday), static_cast<unsigned>(parsed.tm_hour),
|
||||
static_cast<unsigned>(parsed.tm_min), static_cast<unsigned>(parsed.tm_sec));
|
||||
time_t check_seconds = static_cast<time_t>(unix_seconds);
|
||||
time_t check_seconds = static_cast<time_t>(local_seconds);
|
||||
struct tm check {};
|
||||
if (gmtime_r(&check_seconds, &check) == nullptr || check.tm_year != parsed.tm_year ||
|
||||
check.tm_mon != parsed.tm_mon || check.tm_mday != parsed.tm_mday ||
|
||||
@@ -333,8 +344,10 @@ int console_service::run_rtc_set(const char* date_text, const char* time_text) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const int64_t unix_seconds = local_seconds - board::kLocalUtcOffsetSeconds;
|
||||
const esp_err_t result = board_.set_rtc_time(unix_seconds);
|
||||
std::printf("rtc set: %s (unix %" PRId64 ")\n", esp_err_to_name(result), unix_seconds);
|
||||
std::printf("rtc set: %s (local %s %s UTC+8 -> unix %" PRId64 ")\n", esp_err_to_name(result),
|
||||
date_text, time_text, unix_seconds);
|
||||
return result == ESP_OK ? 0 : 1;
|
||||
}
|
||||
|
||||
@@ -398,6 +411,7 @@ void console_service::print_status(bool json) const {
|
||||
"\"wifi\":\"%s\",\"ap_active\":%s,\"credentials_persisted\":%s,"
|
||||
"\"tf_logging\":%s,\"logging_error\":%d,\"temperature_tenths_c\":%d,"
|
||||
"\"humidity_tenths_percent\":%u,\"key_short\":%" PRIu64 ",\"key_long\":%" PRIu64
|
||||
",\"boot_short\":%" PRIu64 ",\"boot_long\":%" PRIu64 ",\"reset_count\":%" PRIu32
|
||||
",\"elf_sha256\":\"%s\",\"image_sha256\":\"%s\"}\n",
|
||||
board::kFirmwareVersion, board::kIdfTag, value.toolchain_version, value.device_id,
|
||||
health, value.target_ok ? "true" : "false", value.cpu_cores,
|
||||
@@ -411,22 +425,17 @@ void console_service::print_status(bool json) const {
|
||||
value.tf_logging_ok ? "true" : "false", static_cast<int>(value.logging_error),
|
||||
value.sensor.valid ? static_cast<int>(value.sensor.temperature_tenths_celsius) : 0,
|
||||
value.sensor.valid ? static_cast<unsigned>(value.sensor.humidity_tenths_percent) : 0U,
|
||||
value.key_short_presses, value.key_long_presses, value.elf_sha256, value.image_sha256);
|
||||
value.key_short_presses, value.key_long_presses, value.boot_short_presses,
|
||||
value.boot_long_presses, value.reset_count, value.elf_sha256, value.image_sha256);
|
||||
return;
|
||||
}
|
||||
|
||||
char rtc_text[32]{};
|
||||
char local_text[32]{};
|
||||
char utc_text[32]{};
|
||||
if (value.rtc.valid) {
|
||||
const time_t seconds = static_cast<time_t>(value.rtc.unix_seconds);
|
||||
struct tm utc_time {};
|
||||
if (gmtime_r(&seconds, &utc_time) != nullptr) {
|
||||
const unsigned year = static_cast<unsigned>(utc_time.tm_year + 1900);
|
||||
std::snprintf(
|
||||
rtc_text, sizeof(rtc_text), "%04u-%02u-%02u %02u:%02u:%02u", year,
|
||||
static_cast<unsigned>(utc_time.tm_mon + 1), static_cast<unsigned>(utc_time.tm_mday),
|
||||
static_cast<unsigned>(utc_time.tm_hour), static_cast<unsigned>(utc_time.tm_min),
|
||||
static_cast<unsigned>(utc_time.tm_sec));
|
||||
}
|
||||
format_unix_datetime(value.rtc.unix_seconds + board::kLocalUtcOffsetSeconds, local_text,
|
||||
sizeof(local_text));
|
||||
format_unix_datetime(value.rtc.unix_seconds, utc_text, sizeof(utc_text));
|
||||
}
|
||||
std::printf("Frame M1 %s (%s)\n", board::kFirmwareVersion, health);
|
||||
std::printf(" device: %s image: %.16s\n IDF: %s toolchain: %s\n", value.device_id,
|
||||
@@ -439,10 +448,18 @@ void console_service::print_status(bool json) const {
|
||||
value.display_error == ESP_OK ? "OK" : "FAILED",
|
||||
hardware_status::sd_state_name(value.storage), value.tf_logging_ok ? "OK" : "WAIT",
|
||||
value.reset_reason);
|
||||
std::printf(" RTC: %s %s SHTC3: %s KEY: %" PRIu64 "/%" PRIu64 "\n",
|
||||
value.rtc_present ? "OK" : "MISS", rtc_text[0] == '\0' ? "-" : rtc_text,
|
||||
std::printf(" RTC: %s %s UTC+8\n", value.rtc_present ? "OK" : "MISS",
|
||||
local_text[0] == '\0' ? "-" : local_text);
|
||||
if (utc_text[0] == '\0') {
|
||||
std::printf(" utc: - unix: %" PRId64 "\n", value.rtc.unix_seconds);
|
||||
} else {
|
||||
std::printf(" utc: %sZ unix: %" PRId64 "\n", utc_text, value.rtc.unix_seconds);
|
||||
}
|
||||
std::printf(" SHTC3: %s KEY: %" PRIu64 "/%" PRIu64 " BOOT: %" PRIu64 "/%" PRIu64
|
||||
" RST: %" PRIu32 "\n",
|
||||
value.sensor_present ? "OK" : "MISS", value.key_short_presses,
|
||||
value.key_long_presses);
|
||||
value.key_long_presses, value.boot_short_presses, value.boot_long_presses,
|
||||
value.reset_count);
|
||||
std::printf(" WiFi: %s SSID: %s IPv4: %s credentials: system_fs (dev)\n",
|
||||
hardware_status::wifi_state_name(value.wifi), value.ssid_configured ? "set" : "-",
|
||||
value.ip_address[0] == '\0' ? "-" : value.ip_address);
|
||||
|
||||
Reference in New Issue
Block a user