Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(dial): compact elapsed time on the round (#1338)

0xdiego25500cf6205bcf
diff --git a/devices/harness-device/firmware/main/CMakeLists.txt b/devices/harness-device/firmware/main/CMakeLists.txt
index 554dad958..044187a11 100644
--- a/devices/harness-device/firmware/main/CMakeLists.txt
+++ b/devices/harness-device/firmware/main/CMakeLists.txt
@@ -30,6 +30,7 @@ set(BOARD_SRCS
"last_words.c"
"fw_update.c"
"pet_store.c"
+ "elapsed.c"
"audio_capture.c"
"audio_client_habitat.c"
"audio_probe.c"
diff --git a/devices/harness-device/firmware/main/elapsed.c b/devices/harness-device/firmware/main/elapsed.c
new file mode 100644
index 000000000..3b9908463
--- /dev/null
+++ b/devices/harness-device/firmware/main/elapsed.c
@@ -0,0 +1,35 @@
+#include "elapsed.h"
+#include <stdio.h>
+
+#define MIN 60LL
+#define HOUR 3600LL
+#define DAY 86400LL
+
+size_t fmt_elapsed(char *out, size_t n, int64_t secs)
+{
+ if (!out || !n) return 0;
+ char tmp[FMT_ELAPSED_MAX + 8];
+ int len = 0;
+ if (secs < 0) tmp[0] = 0;
+ else if (secs < HOUR) len = snprintf(tmp, sizeof tmp, "%d:%02d", (int)(secs / MIN), (int)(secs % MIN));
+ else if (secs < DAY) {
+ int h = (int)(secs / HOUR), m = (int)(secs % HOUR / MIN);
+ len = m ? snprintf(tmp, sizeof tmp, "%dh %dm", h, m) : snprintf(tmp, sizeof tmp, "%dh", h);
+ } else if (secs < 7 * DAY) {
+ int d = (int)(secs / DAY), h = (int)(secs % DAY / HOUR);
+ len = h ? snprintf(tmp, sizeof tmp, "%dd %dh", d, h) : snprintf(tmp, sizeof tmp, "%dd", d);
+ } else if (secs < 100 * DAY) len = snprintf(tmp, sizeof tmp, "%dd", (int)(secs / DAY));
+ else len = snprintf(tmp, sizeof tmp, "99d+");
+ size_t k = 0;
+ for (; k + 1 < n && k < (size_t)len; k++) out[k] = tmp[k];
+ out[k] = 0;
+ return k;
+}
+
+uint32_t fmt_elapsed_next_ms(int64_t secs)
+{
+ if (secs < 0 || secs >= 100 * DAY) return 0;
+ if (secs < HOUR) return 1000;
+ int64_t unit = secs < DAY ? MIN : HOUR;
+ return (uint32_t)((unit - secs % unit) * 1000);
+}
diff --git a/devices/harness-device/firmware/main/elapsed.h b/devices/harness-device/firmware/main/elapsed.h
new file mode 100644
index 000000000..72988a5be
--- /dev/null
+++ b/devices/harness-device/firmware/main/elapsed.h
@@ -0,0 +1,25 @@
+// The one elapsed-time format for every timer the dial shows (working line, "finished in", tab rows).
+// Pure C, no dependencies, so the host tests compile it as is.
+//
+// < 1 h M:SS 0:07 2:15 59:59
+// 1 h .. 24 h Hh Mm 1h 1h 5m 23h 59m
+// 1 d .. 7 d Dd Hh 1d 1d 1h 6d 23h
+// 7 d ..100 d Dd 7d 10d 99d
+// >= 100 d 99d+
+//
+// Always floored, never rounded up. At most two units, largest first, a zero second unit left out, no
+// leading zero on it. The longest string is "23h 59m" (7 characters).
+#pragma once
+#include <stddef.h>
+#include <stdint.h>
+
+#define FMT_ELAPSED_MAX 8 // longest string plus its NUL
+
+// Writes the text for `secs` into out (at most n bytes, always NUL-terminated when n > 0) and returns its
+// length. secs < 0 means unknown or clock-skewed: writes "" and returns 0, so nothing is drawn.
+size_t fmt_elapsed(char *out, size_t n, int64_t secs);
+
+// Milliseconds until the text for `secs` can next change, measured from the start of that second:
+// 1000 under 1 h, to the next minute boundary under 24 h, to the next hour boundary after that.
+// Returns 0 when it never changes again (>= 100 d, or secs < 0).
+uint32_t fmt_elapsed_next_ms(int64_t secs);
diff --git a/devices/harness-device/firmware/main/ui/habitat/character_types.h b/devices/harness-device/firmware/main/ui/habitat/character_types.h
index 3487bbaf5..84415b765 100644
--- a/devices/harness-device/firmware/main/ui/habitat/character_types.h
+++ b/devices/harness-device/firmware/main/ui/habitat/character_types.h
@@ -30,14 +30,15 @@ typedef struct {
* which does not exist anywhere in the cable vocabulary. `engine` is on the wire and stored
* (agent_t.engine) and until now was drawn nowhere. `elapsed` is SECONDS SINCE THIS DIAL HEARD
* ABOUT THE TURN, not since the turn began: `turn.started` carries no timestamp, so the device
- * stamps its own clock. A turn that predates the attach reads from zero. 0 = do not draw it.
+ * stamps its own clock. A turn that predates the attach reads from zero. 0 = unknown, do not draw it (formatted by
+ * fmt_elapsed, main/elapsed.h: "0:34", "1h 5m", ...).
*
* `activity` is what the daemon scraped off the engine's spinner footer — a gerund like
* "Coalescing", never a tool call. The creature skins fold it into `recipient` on a rotation,
* because they have one text seat; a skin with two reads it here instead.
*/
const char *tab, *engine, *activity;
- uint16_t elapsed;
+ uint32_t elapsed;
ht_character_mood_t mood;
ht_character_pose_t pose;
bool focus, carrying, footer_action, straight_title, unread, primary_title, roomy_reading, single_label;
diff --git a/devices/harness-device/firmware/main/ui/habitat/focus.c b/devices/harness-device/firmware/main/ui/habitat/focus.c
index 14cbc03b3..14a982eb9 100644
--- a/devices/harness-device/firmware/main/ui/habitat/focus.c
+++ b/devices/harness-device/firmware/main/ui/habitat/focus.c
@@ -3,6 +3,7 @@
#include "focus_faces.h"
#include "theme.h"
#include "../../pet_store.h"
+#include "../../elapsed.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
@@ -740,11 +741,10 @@ static void label_runs(ht_scene_t *s, const ht_lv_label_t *l, int runs, int x, i
}
}
-// "34s", then "1m 05s" — the old firmware's fmt_elapsed.
-static void elapsed_text(char *out, size_t cap, unsigned sec)
+// The shared elapsed format (main/elapsed.c): "0:34", "1:05", "1h 5m", "2d 3h". 0 = unknown, not drawn.
+static void elapsed_text(char *out, size_t cap, uint32_t sec)
{
- if (sec < 60) snprintf(out, cap, "%us", sec);
- else snprintf(out, cap, "%um %02us", sec / 60, sec % 60);
+ fmt_elapsed(out, cap, sec);
}
/*
diff --git a/devices/harness-device/firmware/main/ui/habitat/ui_habitat.c b/devices/harness-device/firmware/main/ui/habitat/ui_habitat.c
index a8d9e8d64..4c3bfcdca 100644
--- a/devices/harness-device/firmware/main/ui/habitat/ui_habitat.c
+++ b/devices/harness-device/firmware/main/ui/habitat/ui_habitat.c
@@ -26,6 +26,7 @@ static ht_gallery_t gallery;
#include "config_store.h"
#include "cable_client.h"
#include "pet_store.h"
+#include "../../elapsed.h"
#include "esp_attr.h"
#include "esp_timer.h"
#include "esp_log.h"
@@ -242,6 +243,7 @@ static EXT_RAM_BSS_ATTR struct {
uint8_t status_phase;
unsigned bell_unread; // the bell's count at the last home render, to see a notice arrive
uint32_t notice_ms; // the face clock when it last went up, 0 = none since boot (the pet's alert bubble)
+ uint32_t timer_at; // when the working line's elapsed text next changes (ms() clock), 0 = never (elapsed.h)
uint32_t pet_next_ms; // when the pet's (or the voice screen's listening scene's) drawn frame next changes (clock_ms), 0 = never
int start_x, start_y, last_x, last_y;
uint32_t touch_started;
@@ -1002,6 +1004,7 @@ static void surface_tick(uint32_t now)
// The voice screen's listening scene runs while the finger is down (hold-to-talk); the pets do not.
if (s.pet_next_ms && (((s.view == HOME || s.view == AGENT) && !s.touch_down) || s.view == VOICE) &&
(int32_t)(now - s.pet_next_ms) >= 0) { s.pet_next_ms = 0; change(); }
+ if (s.timer_at && (s.view == HOME || s.view == AGENT) && (int32_t)(now - s.timer_at) >= 0) { s.timer_at = 0; change(); }
bool main = s.view == HOME || s.view == AGENT;
bool visible = !display_is_asleep() &&
((main && s.connected && !s.loading) || s.view == VOICE);
@@ -1111,7 +1114,7 @@ static void focus_bell(ht_scene_t *f, unsigned count, int bell_y)
static void render_home(ht_scene_t *f)
{
s.caption_arc = (ht_rect_t){0};
- s.pet_next_ms = 0;
+ s.pet_next_ms = 0; s.timer_at = 0;
if (!s.connected || s.loading) { render_brand(f); return; }
if (workspace.touching && workspace.moved && !workspace.cancelled) { render_workspace_preview(f); return; }
agent_t *a = active();
@@ -1152,7 +1155,15 @@ static void render_home(ht_scene_t *f)
// Seconds since THIS DIAL heard about the turn, not since it began: turn.started carries no
// timestamp. The 25 s staleness fuse (ui_prune_stale_busy) is what keeps this from counting a
// turn nobody is running any more.
- uint32_t since = a && a->busy && a->busy_ms && !is_question(a->id) ? (ms() - a->busy_ms) / 1000 : 0;
+ // 0 = unknown (not busy, no start stamp, or under a second): nothing is drawn. The same stamp sets the
+ // redraw deadline: the text only changes when fmt_elapsed's displayed unit does (elapsed.h), not every tick.
+ uint32_t since_ms = a && a->busy && a->busy_ms && !is_question(a->id) ? ms() - a->busy_ms : 0;
+ uint32_t since = since_ms / 1000;
+ s.timer_at = 0;
+ if (since) {
+ uint32_t next = fmt_elapsed_next_ms(since), frac = since_ms % 1000;
+ if (next > frac) { s.timer_at = ms() + next - frac; if (!s.timer_at) s.timer_at = 1; }
+ }
int tab_index = workspace_index(s.selected_tab);
uint32_t clock = ms() | 1;
// A notice arrives: the count went up. Its time drives the pet's alert bubble (focus.c THE ALERT); one that is
@@ -1167,7 +1178,7 @@ static void render_home(ht_scene_t *f)
.tab = tab_index >= 0 ? s.tabs[tab_index].name : "",
.engine = a ? a->engine : "",
.activity = activity,
- .elapsed = since > 65535 ? 65535 : (uint16_t)since,
+ .elapsed = since,
.detail = "",
.mood = companion_celebrating ? HT_CHARACTER_DONE : character_mood(), .pose = character.motion.reaction.pose,
.asking = a && is_question(a->id),
@@ -3276,6 +3287,11 @@ uint32_t habitat_next_wake_ms(void)
uint32_t due = status_wake_ms(now);
if (due < delay) delay = due;
}
+ if (s.timer_at && (s.view == HOME || s.view == AGENT) && !display_is_asleep()) {
+ int32_t left = (int32_t)(s.timer_at - now);
+ uint32_t due = left < 1 ? 1 : (uint32_t)left;
+ if (due < delay) delay = due;
+ }
if (s.pet_next_ms && (((s.view == HOME || s.view == AGENT) && !s.touch_down) || s.view == VOICE)) {
int32_t left = (int32_t)(s.pet_next_ms - now);
uint32_t due = left < 1 ? 1 : (uint32_t)left;
diff --git a/devices/harness-device/firmware/test/run.sh b/devices/harness-device/firmware/test/run.sh
index aa64194ed..28003c37e 100644
--- a/devices/harness-device/firmware/test/run.sh
+++ b/devices/harness-device/firmware/test/run.sh
@@ -20,7 +20,7 @@ python3 "$here/../scripts/gen_pets.py" --check
python3 "$here/../scripts/gen_arc_geometry.py" --check
cc -std=c11 -Wall -Wextra -Werror -O1 -g -fsanitize="${SANITIZERS:-undefined,bounds}" \
- -I "$here/../main" -o "$out/test_character" "$here/test_character.c" "$here/../main/pet_store.c" \
+ -I "$here/../main" -o "$out/test_character" "$here/test_character.c" "$here/../main/pet_store.c" "$here/../main/elapsed.c" \
"$here/../main/ui/habitat/character.c" "$here/../main/ui/habitat/illustrated.c" "$here/../main/ui/habitat/character_motion.c" \
"$here/../main/ui/habitat/character_layout.c" "$here/../main/ui/habitat/tux.c" \
"$here/../main/ui/habitat/focus.c" "$here/../main/ui/habitat/lvgl_fonts.c" "$here/../main/ui/habitat/lvgl_icons.c" "$here/../main/ui/habitat/focus_marks.c" "$here/../main/ui/habitat/focus_faces.c" "$here/../main/ui/habitat/pets.c" \
@@ -30,7 +30,7 @@ cc -std=c11 -Wall -Wextra -Werror -O1 -g -fsanitize="${SANITIZERS:-undefined,bou
"$out/test_character" "$here/vectors/pet_min.hpet"
# The same source again, only the custom-pet cases, under the address sanitizer: a pack read after it is freed.
cc -std=c11 -Wall -Wextra -Werror -O1 -g -fsanitize="${PET_SANITIZERS:-address,undefined}" \
- -I "$here/../main" -o "$out/test_character_asan" "$here/test_character.c" "$here/../main/pet_store.c" \
+ -I "$here/../main" -o "$out/test_character_asan" "$here/test_character.c" "$here/../main/pet_store.c" "$here/../main/elapsed.c" \
"$here/../main/ui/habitat/character.c" "$here/../main/ui/habitat/illustrated.c" "$here/../main/ui/habitat/character_motion.c" \
"$here/../main/ui/habitat/character_layout.c" "$here/../main/ui/habitat/tux.c" \
"$here/../main/ui/habitat/focus.c" "$here/../main/ui/habitat/lvgl_fonts.c" "$here/../main/ui/habitat/lvgl_icons.c" "$here/../main/ui/habitat/focus_marks.c" "$here/../main/ui/habitat/focus_faces.c" "$here/../main/ui/habitat/pets.c" \
@@ -40,6 +40,10 @@ cc -std=c11 -Wall -Wextra -Werror -O1 -g -fsanitize="${PET_SANITIZERS:-address,u
"$out/test_character_asan" "$here/vectors/pet_min.hpet" --custom-only
python3 "$here/test_character_preferences.py"
+cc -std=c11 -Wall -Wextra -Werror -O1 -g -fsanitize="${SANITIZERS:-undefined,bounds}" \
+ -I "$here/../main" -o "$out/test_elapsed" "$here/test_elapsed.c" "$here/../main/elapsed.c"
+"$out/test_elapsed"
+
cc -std=c11 -Wall -Wextra -Werror -O1 \
-o "$out/test_cable_frame" "$here/test_cable_frame.c" "$here/../main/cable_frame.c"
"$out/test_cable_frame" "$here/vectors/cable_frame.txt"
diff --git a/devices/harness-device/firmware/test/test_character.c b/devices/harness-device/firmware/test/test_character.c
index b51d155bd..816871b80 100644
--- a/devices/harness-device/firmware/test/test_character.c
+++ b/devices/harness-device/firmware/test/test_character.c
@@ -601,7 +601,7 @@ static void focus_face(void)
ht_character_face(&scene, &c, &f, 0xffff, "");
only_inter(&scene);
assert(scene.runs[7].y == 233 - ht_lv_inter_30.base.height / 2 && scene.runs[7].font == &ht_lv_inter_30.base &&
- !strcmp(scene.runs[7].text, "Simmering\xe2\x80\xa6 34s")); // the gerund for 30..35 s
+ !strcmp(scene.runs[7].text, "Simmering\xe2\x80\xa6 0:34")); // the gerund for 30..35 s
{
int d = pet_gaps(&scene, &scene.runs[1]);
assert(d >= -3 && d <= 3);
@@ -801,7 +801,7 @@ static void focus_face(void)
assert(mark->y == 233 + 4 - ws->h / 2 + ws->dy + bob);
assert(!scene.runs[7].text[0]); // the centred status is empty
const ht_run_t *lower = &scene.runs[10];
- assert(lower->arc == 2 && !strcmp(lower->text, "Working\xe2\x80\xa6 5s")); // Inter has the real ellipsis
+ assert(lower->arc == 2 && !strcmp(lower->text, "Working\xe2\x80\xa6 0:05")); // Inter has the real ellipsis
assert(lower->fg == ht_rgb(0x00ff2f));
frame_at[state][step] = frame;
} else {
@@ -972,7 +972,7 @@ static void focus_face(void)
assert(scene_frame(ws, scene.runs[1].sprite.cells, 1) == ws->loop[step]);
assert(!scene.runs[7].text[0] && scene.runs[3].sprite.cells); // the props (pan, food) in run 3
assert(scene.runs[10].arc == 2 && scene.runs[10].fg == ht_rgb(0x00ff2f) &&
- !strcmp(scene.runs[10].text, "Coalescing\xe2\x80\xa6 34s") &&
+ !strcmp(scene.runs[10].text, "Coalescing\xe2\x80\xa6 0:34") &&
scene.runs[10].font == &ht_lv_inter_med_26.base);
bool fresh = true;
for (int k = 0; k < distinct; k++) fresh &= frames_seen[k] != ws->loop[step];
@@ -1066,7 +1066,7 @@ static void focus_face(void)
assert(scene.count == 11 && scene.runs[1].sprite.width == 56);
assert(scene.runs[7].font == &ht_lv_inter_30.base && scene.runs[7].fg == ht_rgb(0x00ff2f) &&
scene.runs[7].y == 233 - ht_lv_inter_30.base.height / 2 &&
- !strcmp(scene.runs[7].text, "Coalescing\xe2\x80\xa6 34s"));
+ !strcmp(scene.runs[7].text, "Coalescing\xe2\x80\xa6 0:34"));
assert(!scene.runs[10].text[0] && !scene.runs[10].arc);
// Listening: Claude draws a scene frame, no bars; an engine without one draws its seven bars.
@@ -1216,7 +1216,7 @@ static void focus_face(void)
}
if (kind == 0) { // status on the lower arc, nothing centred
assert(!cs_.runs[7].text[0] && cs_.runs[10].arc == 2 &&
- !strcmp(cs_.runs[10].text, "Coalescing\xe2\x80\xa6 34s") && !cs_.runs[10].gained);
+ !strcmp(cs_.runs[10].text, "Coalescing\xe2\x80\xa6 0:34") && !cs_.runs[10].gained);
assert(!cs_.runs[4].text[0] && !cs_.runs[5].text[0] && !cs_.runs[6].text[0]); // the other recap lines stay empty
}
if (kind == 1) {
@@ -1587,7 +1587,7 @@ static void focus_face(void)
assert(scene.count == 11 && scene.runs[1].sprite.width == cp->working_scene->w);
const ht_run_t *line = &scene.runs[7];
assert(!line->arc && line->font == &ht_lv_inter_30.base && line->y == 334 &&
- line->fg == ht_rgb(0x00ff2f) && !strcmp(line->text, "Coalescing\xe2\x80\xa6 34s"));
+ line->fg == ht_rgb(0x00ff2f) && !strcmp(line->text, "Coalescing\xe2\x80\xa6 0:34"));
assert(line->x + line->w / 2 >= 232 && line->x + line->w / 2 <= 234);
assert(!scene.runs[10].text[0] && !scene.runs[10].arc);
for (int i = 0; i < scene.count; i++) assert(scene.runs[i].arc != 2);
@@ -1596,16 +1596,20 @@ static void focus_face(void)
f.footer_action = false; f.activity = "Running firmware checks";
ht_scene_clear(&scene, 0); ht_character_face(&scene, &c, &f, 0xffff, "");
size_t n = strlen(scene.runs[10].text);
- assert(scene.runs[10].arc == 2 && n >= 3 && !strcmp(scene.runs[10].text + n - 3, "34s") && ht_arc_measure(&ht_arc_inter_lower, scene.runs[10].text) <= HT_ARC_SPAN);
+ assert(scene.runs[10].arc == 2 && n >= 3 && !strcmp(scene.runs[10].text + n - 4, "0:34") && ht_arc_measure(&ht_arc_inter_lower, scene.runs[10].text) <= HT_ARC_SPAN);
assert(strstr(scene.runs[10].text, "Running firmw") && !scene.runs[7].text[0]);
f.elapsed = 65;
ht_scene_clear(&scene, 0); ht_character_face(&scene, &c, &f, 0xffff, "");
n = strlen(scene.runs[10].text);
- assert(!strcmp(scene.runs[10].text + n - 6, "1m 05s") && ht_arc_measure(&ht_arc_inter_lower, scene.runs[10].text) <= HT_ARC_SPAN);
+ assert(!strcmp(scene.runs[10].text + n - 4, "1:05") && ht_arc_measure(&ht_arc_inter_lower, scene.runs[10].text) <= HT_ARC_SPAN);
+ f.elapsed = 86399; // the widest string the format makes, "23h 59m": the verb gives way, the time stays whole
+ ht_scene_clear(&scene, 0); ht_character_face(&scene, &c, &f, 0xffff, "");
+ n = strlen(scene.runs[10].text);
+ assert(n >= 7 && !strcmp(scene.runs[10].text + n - 7, "23h 59m") && ht_arc_measure(&ht_arc_inter_lower, scene.runs[10].text) <= HT_ARC_SPAN);
f.elapsed = 34; f.activity = "Reconciling the payment ledger migration plan"; f.footer_action = true;
ht_scene_clear(&scene, 0); ht_character_face(&scene, &c, &f, 0xffff, "");
n = strlen(scene.runs[7].text);
- assert(!strcmp(scene.runs[7].text + n - 3, "34s") && scene.runs[7].w <= 384 && scene.runs[7].w > 300);
+ assert(!strcmp(scene.runs[7].text + n - 4, "0:34") && scene.runs[7].w <= 384 && scene.runs[7].w > 300);
assert(scene.runs[7].x >= 41 && scene.runs[7].x + scene.runs[7].w <= 41 + 384);
}
diff --git a/devices/harness-device/firmware/test/test_character_preferences.py b/devices/harness-device/firmware/test/test_character_preferences.py
index d97e42aff..de8cf2e02 100644
--- a/devices/harness-device/firmware/test/test_character_preferences.py
+++ b/devices/harness-device/firmware/test/test_character_preferences.py
@@ -117,7 +117,7 @@ with tempfile.TemporaryDirectory(prefix='harness-character-pref-') as directory:
out = Path(directory)
(out / 'test.c').write_text(code)
sources = ['character.c', 'illustrated.c', 'character_motion.c', 'character_layout.c', 'tux.c', 'focus.c', 'lvgl_fonts.c', 'lvgl_icons.c', 'focus_marks.c', 'focus_faces.c', 'pets.c',
- 'octopus.c', 'octopus_font.c', 'ascii_clip.c', 'terminal.c', 'fonts.c', '../../pet_store.c']
+ 'octopus.c', 'octopus_font.c', 'ascii_clip.c', 'terminal.c', 'fonts.c', '../../pet_store.c', '../../elapsed.c']
for flags in ([], ['-DDEVICE_DEFAULT_CHARACTER_TUX=1']):
subprocess.run(['cc', '-std=c11', '-D_POSIX_C_SOURCE=200809L', '-Wall', '-Wextra', '-Werror', '-O1', '-g',
'-fsanitize=' + os.environ.get('SANITIZERS', 'undefined,bounds'), *flags,
diff --git a/devices/harness-device/firmware/test/test_companions.py b/devices/harness-device/firmware/test/test_companions.py
index 03bdcf5aa..685e0f320 100644
--- a/devices/harness-device/firmware/test/test_companions.py
+++ b/devices/harness-device/firmware/test/test_companions.py
@@ -20,7 +20,7 @@ _binary_companion_art_pack_end:
''')
sources = ['character.c', 'illustrated.c', 'illustrated_cache.c', 'character_motion.c',
'character_layout.c', 'octopus.c', 'octopus_font.c', 'ascii_clip.c', 'tux.c',
- 'focus.c', 'lvgl_fonts.c', 'lvgl_icons.c', 'focus_marks.c', 'focus_faces.c', 'pets.c', 'terminal.c', 'fonts.c', '../../pet_store.c']
+ 'focus.c', 'lvgl_fonts.c', 'lvgl_icons.c', 'focus_marks.c', 'focus_faces.c', 'pets.c', 'terminal.c', 'fonts.c', '../../pet_store.c', '../../elapsed.c']
subprocess.run(['cc', '-std=c11', '-D_POSIX_C_SOURCE=200809L', '-Wall', '-Wextra', '-Werror', '-O1', '-g',
'-fsanitize=' + os.environ.get('SANITIZERS', 'undefined,bounds'), '-I', str(NATIVE), '-I', str(NATIVE.parent.parent),
str(HERE / 'test_companions.c'), *(str(NATIVE / name) for name in sources),
diff --git a/devices/harness-device/firmware/test/test_device_settings.py b/devices/harness-device/firmware/test/test_device_settings.py
index c19c1e801..5d381f274 100644
--- a/devices/harness-device/firmware/test/test_device_settings.py
+++ b/devices/harness-device/firmware/test/test_device_settings.py
@@ -244,7 +244,7 @@ with tempfile.TemporaryDirectory(prefix='harness-device-settings-') as directory
out = Path(directory)
(out / 'test.c').write_text(code)
sources = ['character.c', 'illustrated.c', 'character_motion.c', 'character_layout.c', 'tux.c', 'focus.c', 'lvgl_fonts.c', 'lvgl_icons.c', 'focus_marks.c', 'focus_faces.c', 'pets.c',
- 'octopus.c', 'octopus_font.c', 'ascii_clip.c', 'terminal.c', 'fonts.c', '../../pet_store.c']
+ 'octopus.c', 'octopus_font.c', 'ascii_clip.c', 'terminal.c', 'fonts.c', '../../pet_store.c', '../../elapsed.c']
subprocess.run(['cc', '-std=c11', '-D_POSIX_C_SOURCE=200809L', '-Wall', '-Wextra', '-Werror', '-O1', '-g',
'-fsanitize=' + os.environ.get('SANITIZERS', 'undefined,bounds'),
'-I', str(native), '-I', str(native.parent.parent), str(out / 'test.c'), *(str(native / f) for f in sources),
diff --git a/devices/harness-device/firmware/test/test_elapsed.c b/devices/harness-device/firmware/test/test_elapsed.c
new file mode 100644
index 000000000..6cda3b1e3
--- /dev/null
+++ b/devices/harness-device/firmware/test/test_elapsed.c
@@ -0,0 +1,56 @@
+// fmt_elapsed / fmt_elapsed_next_ms: the one elapsed-time format (main/elapsed.c).
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "elapsed.h"
+
+static void is(int64_t secs, const char *want)
+{
+ char b[FMT_ELAPSED_MAX];
+ size_t n = fmt_elapsed(b, sizeof b, secs);
+ if (strcmp(b, want) || n != strlen(want)) {
+ fprintf(stderr, "fmt_elapsed(%lld) = \"%s\" (%zu), want \"%s\"\n", (long long)secs, b, n, want);
+ assert(0);
+ }
+}
+static void next(int64_t secs, uint32_t want)
+{
+ if (fmt_elapsed_next_ms(secs) != want) {
+ fprintf(stderr, "next_ms(%lld) = %u, want %u\n", (long long)secs, fmt_elapsed_next_ms(secs), want);
+ assert(0);
+ }
+}
+
+int main(void)
+{
+ is(0, "0:00"); is(7, "0:07"); is(59, "0:59"); is(60, "1:00"); is(135, "2:15"); is(3599, "59:59");
+ is(3600, "1h"); is(3659, "1h"); is(3660, "1h 1m"); is(3900, "1h 5m"); is(86399, "23h 59m");
+ is(86400, "1d"); is(86400 + 3599, "1d"); is(90000, "1d 1h"); is(604799, "6d 23h");
+ is(604800, "7d"); is(934740, "10d"); is(99 * 86400 + 86399, "99d");
+ is(100 * 86400 - 1, "99d"); is(100 * 86400, "99d+"); is(8640000, "99d+"); is((int64_t)1 << 40, "99d+");
+ is(-1, ""); is(-5, ""); is(INT64_MIN, "");
+ // longest string fits 7 characters
+ char b[FMT_ELAPSED_MAX];
+ assert(fmt_elapsed(b, sizeof b, 86399) == 7);
+ // truncation keeps the NUL
+ char t[4];
+ assert(fmt_elapsed(t, sizeof t, 86399) == 3 && !strcmp(t, "23h"));
+ assert(fmt_elapsed(t, 1, 5) == 0 && !t[0]);
+
+ next(0, 1000); next(3599, 1000);
+ next(3600, 60000); next(3659, 1000); next(3660, 60000); next(86399, 1000); next(86340, 60000);
+ next(86400, 3600000); next(86400 + 3599, 1000); next(604799, 1000);
+ next(7 * 86400, 3600000); next(100 * 86400 - 1, 1000);
+ next(100 * 86400, 0); next(-1, 0);
+ // The string must not change before next_ms and must change at it.
+ for (int64_t s = 0; s < 3 * 86400; s += (s < 4000 ? 1 : 37)) {
+ char a[FMT_ELAPSED_MAX], c[FMT_ELAPSED_MAX], d[FMT_ELAPSED_MAX];
+ fmt_elapsed(a, sizeof a, s);
+ uint32_t ms = fmt_elapsed_next_ms(s);
+ fmt_elapsed(c, sizeof c, s + ms / 1000 - 1);
+ fmt_elapsed(d, sizeof d, s + ms / 1000);
+ assert(!strcmp(a, c) && strcmp(a, d));
+ }
+ puts("test_elapsed OK");
+ return 0;
+}
diff --git a/devices/harness-device/firmware/test/test_touch_ui.py b/devices/harness-device/firmware/test/test_touch_ui.py
index 5bc9699c6..2171de976 100644
--- a/devices/harness-device/firmware/test/test_touch_ui.py
+++ b/devices/harness-device/firmware/test/test_touch_ui.py
@@ -20,6 +20,7 @@ def function(name):
code = r'''
#include "runtime.h"
#include "../../cable_features.h"
+#include "../../elapsed.h"
#include "gestures.h"
#include "scroll.h"
#include "selection.h"
@@ -66,6 +67,7 @@ static struct {
unsigned bell_unread;
uint32_t notice_ms;
uint32_t pet_next_ms;
+ uint32_t timer_at;
cable_swarm_t tabs[SWARMS_MAX];
struct { char id[64], name[96], state[16]; bool local; } machines[2];
char selected_tab[ID_MAX], pending_focus[ID_MAX], opening_notice[ID_MAX], title[80], message[256];
@@ -2008,6 +2010,20 @@ int main(int argc, char **argv) {
s.touch_down = false;
s.view = HOME;
}
+ // THE WORKING TIMER: the elapsed text redraws when its displayed unit changes (elapsed.h) — each second
+ // under an hour, each minute under a day — and not before. 0 = unknown (no start stamp): no deadline.
+ {
+ reset(); ht_character_select(&character, HT_CHARACTER_FOCUS);
+ s.agents[0].busy = true; strcpy(s.agents[0].tool, "Running firmware checks");
+ fake_ms = 1200; scene_take(); assert(!s.timer_at); // busy_ms = 0: unknown, nothing drawn
+ s.agents[0].busy_ms = 1000; fake_ms = 1000 + 7500; scene_take(); // 7.5 s in: "0:07" until 8.0 s
+ assert(s.timer_at == 1000 + 8000);
+ surface_tick(1000 + 7999); assert(s.timer_at == 1000 + 8000); // not due: still waiting
+ changes = 0; surface_tick(1000 + 8000); assert(changes >= 1 && !s.timer_at);
+ s.agents[0].busy_ms = 1000; fake_ms = 1000 + 3600000 + 90000 + 400; // 1 h 1 m 30.4 s: "1h 1m" until 1 h 2 m
+ scene_take(); assert(s.timer_at == 1000 + 3600000 + 120000);
+ s.agents[0].busy = false; scene_take(); assert(!s.timer_at);
+ }
// THE WORKING SCENE (claude): a busy agent's face is the large cooking Clawd, which changes on its step
// clock, so s.pet_next_ms is its next change and surface_tick redraws then and not before.
{
@@ -2781,7 +2797,7 @@ with tempfile.TemporaryDirectory(prefix='harness-touch-ui-') as d:
subprocess.run(['cc','-std=c11', '-D_POSIX_C_SOURCE=200809L','-Wall','-Wextra','-Werror','-O1','-g',
'-fsanitize='+os.environ.get('SANITIZERS','undefined,bounds'),
*extra_includes, '-I',str(native),str(out/'touch_ui.c'), *extra_sources, str(native/'gestures.c'),
- str(native/'form.c'),str(native/'visit.c'),str(native/'draft.c'), str(native/'scroll.c'),str(native/'selection.c'),str(native/'carry.c'),str(native/'tim.c'),str(native/'character_motion.c'),str(native/'character_layout.c'),str(native/'character.c'),str(native/'illustrated.c'),str(native/'tux.c'),str(native/'focus.c'),str(native/'lvgl_fonts.c'),str(native/'lvgl_icons.c'),str(native/'focus_marks.c'),str(native/'focus_faces.c'),str(native/'pets.c'),str(native/'../../pet_store.c'),str(native/'terminal.c'),
+ str(native/'form.c'),str(native/'visit.c'),str(native/'draft.c'), str(native/'scroll.c'),str(native/'selection.c'),str(native/'carry.c'),str(native/'tim.c'),str(native/'character_motion.c'),str(native/'character_layout.c'),str(native/'character.c'),str(native/'illustrated.c'),str(native/'tux.c'),str(native/'focus.c'),str(native/'lvgl_fonts.c'),str(native/'lvgl_icons.c'),str(native/'focus_marks.c'),str(native/'focus_faces.c'),str(native/'pets.c'),str(native/'../../pet_store.c'),str(native/'../../elapsed.c'),str(native/'terminal.c'),
str(native/'fonts.c'),str(native/'octopus.c'),str(native/'ascii_clip.c'),str(native/'octopus_font.c'),str(native/'workspace.c'),str(native/'command_face.c'),'-o',str(out/'touch_ui'),'-lm'],check=True)
args=[str(out/'touch_ui')]
if os.environ.get('HABITAT_PREVIEW_DIR'):