From 19ba4b1cb2cd2200b9b2e0b33b07199b6662a972 Mon Sep 17 00:00:00 2001 From: "Rich O." Date: Sun, 8 Nov 2026 18:53:49 -0500 Subject: [PATCH] palm-client: the screens' run-time lines are built in screen_fmt.c, and tests/test_screens.c checks them with their longest values in the large font. That found the Node clock lines running off the edge even in the small font (now 'Visor 11/8 14:02 (UTC-5:00)' and 'Radio 11/8 19:02 UTC, sys 300', a font row apart), Radio Stats lines too long for big counts (they wrap now, and the test checks they still fit above Done; errors get their own line, in hex that shows), and the contact summary and a Trace message too wide ('Room server' is 'Room'). Rows that could still be long are cut instead of running off --- palm-client/Makefile | 4 +- palm-client/src/contact_form.c | 30 ++-- palm-client/src/contacts_form.c | 2 +- palm-client/src/node_form.c | 39 +++--- palm-client/src/screen_fmt.c | 231 +++++++++++++++++++++++++++++++ palm-client/src/screen_fmt.h | 39 ++++++ palm-client/src/stats_form.c | 70 ++-------- palm-client/src/trace_form.c | 8 +- palm-client/tests/test_screens.c | 180 ++++++++++++++++++++++++ 9 files changed, 507 insertions(+), 96 deletions(-) create mode 100644 palm-client/src/screen_fmt.c create mode 100644 palm-client/src/screen_fmt.h create mode 100644 palm-client/tests/test_screens.c diff --git a/palm-client/Makefile b/palm-client/Makefile index 060b39f..69055cf 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -11,7 +11,7 @@ APP = visormesh NAME = VisorMesh CREATOR = VMsh -SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/title.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c src/db.c src/reaction_fmt.c src/reactions.c src/rxpath.c src/msg_menu.c src/protocol2.c src/info_fmt.c src/stats_form.c src/sb_test.c src/packets_form.c src/trace_form.c src/blocks.c src/beam.c src/beam_rx.c src/stale.c src/convo_extra.c src/neigh.c +SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/title.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c src/db.c src/reaction_fmt.c src/reactions.c src/rxpath.c src/msg_menu.c src/protocol2.c src/info_fmt.c src/screen_fmt.c src/stats_form.c src/sb_test.c src/packets_form.c src/trace_form.c src/blocks.c src/beam.c src/beam_rx.c src/stale.c src/convo_extra.c src/neigh.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -79,6 +79,8 @@ test: | build build/test_text gcc -std=c99 -Wall -Wextra -o build/test_sblora tests/test_sblora.c src/sb_spi.c build/test_sblora + gcc -std=c99 -Wall -Wextra -Isrc -o build/test_screens tests/test_screens.c src/screen_fmt.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c + build/test_screens gcc -std=c99 -Wall -Wextra -o build/test_scopes tests/test_scopes.c src/scope_proto.c src/protocol.c src/sha256.c build/test_scopes gcc -std=c99 -Wall -Wextra -D_DEFAULT_SOURCE -o build/test_map tests/test_map.c src/map_geo.c -lm diff --git a/palm-client/src/contact_form.c b/palm-client/src/contact_form.c index 96b9f28..e55ccb0 100644 --- a/palm-client/src/contact_form.c +++ b/palm-client/src/contact_form.c @@ -20,6 +20,7 @@ #include "ui/draw.h" #include "ui/text.h" #include "ui/rowlist.h" +#include "screen_fmt.h" #include "contact_form.h" #include "remote.h" #include "trace_form.h" @@ -31,6 +32,7 @@ static void *GetObject(FormType *form, UInt16 id) VM_SECT3; static void Error(const Char *msg) VM_SECT3; static void ErrorFor(Err err) VM_SECT3; static void FormatWhen(Char *out, UInt32 utc) VM_SECT3; +static void LocalTime(ScrTime *t, UInt32 utc) VM_SECT3; static void DrawHex(const UInt8 *bytes, UInt16 n, Coord x, Coord y) VM_SECT3; static void DrawRow(UInt16 row, const RectangleType *b, void *ctx) VM_SECT3; static void Load(void) VM_SECT3; @@ -85,6 +87,17 @@ static void FormatWhen(Char *out, UInt32 utc) StrPrintF(out, "%02u-%02u %02u:%02u", dt.month, dt.day, dt.hour, dt.minute); } +static void LocalTime(ScrTime *t, UInt32 utc) +{ + DateTimeType dt; + + TimSecondsToDateTime(SessionLocalPalm(utc), &dt); + t->month = (UInt8)dt.month; + t->day = (UInt8)dt.day; + t->hour = (UInt8)dt.hour; + t->minute = (UInt8)dt.minute; +} + static void DrawHex(const UInt8 *bytes, UInt16 n, Coord x, Coord y) { Char hex[2 * 16 + 1]; @@ -99,26 +112,27 @@ static void DrawRow(UInt16 row, const RectangleType *b, void *ctx) { FontID old = FntSetFont(vmSmallFont); Coord x = b->topLeft.x + 1, y = b->topLeft.y, right = b->topLeft.x + b->extent.x - 1; - Char line[48], route[12], when[16], snr[8]; + Char line[SCR_LINE], route[12], when[16], snr[8]; + ScrTime t; UInt32 now = SessionNow(); switch (row) { case 0: - line[0] = ContactTypeGlyph(gContact.type); + /* tests/test_screens.c checks these two rows fit in the large font */ ContactFormatRoute(route, gContact.outPathLen); - StrPrintF(line + 1, " %s, %s%s", ContactTypeName(gContact.type), route, - SessionIsLoggedIn(gContact.pubKey) ? ", logged in" : ""); - WinDrawChars(line, StrLen(line), x, y); + ScrContactSummary(line, ContactTypeGlyph(gContact.type), ContactTypeName(gContact.type), route, + SessionIsLoggedIn(gContact.pubKey)); + DrawCut(line, x, y, right - x); break; case 1: if (gContact.lastAdvert) { - FormatWhen(when, gContact.lastAdvert); + LocalTime(&t, gContact.lastAdvert); TxtFormatAge(route, now > gContact.lastAdvert ? now - gContact.lastAdvert : 0); - StrPrintF(line, "Advert %s (%s ago)", when, route); + ScrContactAdvert(line, &t, route); } else { StrCopy(line, "No advert heard yet"); } - WinDrawChars(line, StrLen(line), x, y); + DrawCut(line, x, y, right - x); break; case 2: WinDrawChars("Key", 3, x, y); diff --git a/palm-client/src/contacts_form.c b/palm-client/src/contacts_form.c index 14e3587..cbe0648 100644 --- a/palm-client/src/contacts_form.c +++ b/palm-client/src/contacts_form.c @@ -46,7 +46,7 @@ const Char *ContactTypeName(UInt8 type) switch (type) { case VM_ADV_TYPE_CHAT: return "Companion"; case VM_ADV_TYPE_REPEATER: return "Repeater"; - case VM_ADV_TYPE_ROOM: return "Room server"; + case VM_ADV_TYPE_ROOM: return "Room"; case VM_ADV_TYPE_SENSOR: return "Sensor"; default: return "Unknown"; } diff --git a/palm-client/src/node_form.c b/palm-client/src/node_form.c index 78fc4c0..87fff79 100644 --- a/palm-client/src/node_form.c +++ b/palm-client/src/node_form.c @@ -8,6 +8,7 @@ #include "transport.h" #include "session.h" #include "app.h" +#include "screen_fmt.h" #include "node_form.h" #include "ui/draw.h" @@ -82,40 +83,38 @@ static void ShowRadioInfo(FormType *form) } /* - * Clock check, small font, under the radio details: - * Visor 11/1 17:02 UTC 11/1 22:02 (-5:00) - * radio 11/1 11:57 UTC sys 300 + * Clock check, in the UI font, under the radio details: + * Visor 11/1 17:02 (UTC-5:00) + * Radio 11/1 22:02 UTC, sys 300 * "sys" is Palm OS's own minutes-west-of-GMT preference (the offset's default). */ static void DrawClockLine(void) { DateTimeType dt; RectangleType r; - Char line[56], utc[16], radio[16]; - UInt32 now = TimGetSeconds(); - Int16 off = SessionUtcOffset(); + ScrTime t; + Char line[SCR_LINE]; UInt32 rt = SessionRadioTime(); FontID old = FntSetFont(vmSmallFont); RctSetRectangle(&r, 0, 104, 160, 20); WinEraseRectangle(&r, 0); - TimSecondsToDateTime(SessionNow() + 2082844800UL, &dt); - StrPrintF(utc, "%u/%u %02u:%02u", dt.month, dt.day, dt.hour, dt.minute); - TimSecondsToDateTime(now, &dt); - StrPrintF(line, "Visor %u/%u %02u:%02u UTC %s (%s%d:%02d)", dt.month, dt.day, dt.hour, dt.minute, utc, - off < 0 ? "-" : "+", (off < 0 ? -off : off) / 60, (off < 0 ? -off : off) % 60); - WinDrawChars(line, StrLen(line), 5, 105); + TimSecondsToDateTime(TimGetSeconds(), &dt); + t.month = (UInt8)dt.month; + t.day = (UInt8)dt.day; + t.hour = (UInt8)dt.hour; + t.minute = (UInt8)dt.minute; + ScrVisorClock(line, &t, SessionUtcOffset()); + DrawCut(line, 5, 105, 155); if (rt) { TimSecondsToDateTime(rt + 2082844800UL, &dt); - StrPrintF(radio, "%u/%u %02u:%02u", dt.month, dt.day, dt.hour, dt.minute); - } else { - StrCopy(radio, "--"); + t.month = (UInt8)dt.month; + t.day = (UInt8)dt.day; + t.hour = (UInt8)dt.hour; + t.minute = (UInt8)dt.minute; } - if (SessionClockMismatch()) - StrPrintF(line, "radio %s UTC MISMATCH: check date", radio); - else - StrPrintF(line, "radio %s UTC sys %ld", radio, (Int32)PrefGetPreference(prefMinutesWestOfGMT)); - WinDrawChars(line, StrLen(line), 5, 113); + ScrRadioClock(line, rt ? &t : NULL, SessionClockMismatch(), (Int32)PrefGetPreference(prefMinutesWestOfGMT)); + DrawCut(line, 5, 105 + VM_ROW_H, 155); FntSetFont(old); } diff --git a/palm-client/src/screen_fmt.c b/palm-client/src/screen_fmt.c new file mode 100644 index 0000000..7c198e0 --- /dev/null +++ b/palm-client/src/screen_fmt.c @@ -0,0 +1,231 @@ +#include "screen_fmt.h" +#include "ui/text.h" + +/* In the fifth code segment: see sections.h for the rules. */ +static void Put(Char *out, UInt16 *i, const Char *s) VM_SECT5; +static void PutNum(Char *out, UInt16 *i, UInt32 v, UInt16 minDigits) VM_SECT5; +static void PutInt(Char *out, UInt16 *i, Int32 v) VM_SECT5; +static void PutDate(Char *out, UInt16 *i, const ScrTime *t, Char sep, Boolean pad) VM_SECT5; + +/* Every writer stops at SCR_LINE - 1 and keeps out[] terminated. */ +static void Put(Char *out, UInt16 *i, const Char *s) +{ + while (*s && *i < SCR_LINE - 1) + out[(*i)++] = *s++; + out[*i] = 0; +} + +static void PutNum(Char *out, UInt16 *i, UInt32 v, UInt16 minDigits) +{ + Char d[11]; + UInt16 n = 0; + + do { + d[n++] = (Char)('0' + v % 10); + v /= 10; + } while (v || n < minDigits); + while (n && *i < SCR_LINE - 1) + out[(*i)++] = d[--n]; + out[*i] = 0; +} + +static void PutInt(Char *out, UInt16 *i, Int32 v) +{ + if (v < 0) { + Put(out, i, "-"); + v = -v; + } + PutNum(out, i, (UInt32)v, 1); +} + +/* "11/8 14:02", or with pad "11-08 14:02" */ +static void PutDate(Char *out, UInt16 *i, const ScrTime *t, Char sep, Boolean pad) +{ + Char s[2]; + + s[0] = sep; + s[1] = 0; + PutNum(out, i, t->month, pad ? 2 : 1); + Put(out, i, s); + PutNum(out, i, t->day, pad ? 2 : 1); + Put(out, i, " "); + PutNum(out, i, t->hour, 2); + Put(out, i, ":"); + PutNum(out, i, t->minute, 2); +} + +void ScrSecs(Char *out, UInt32 s) +{ + UInt16 i = 0; + + out[0] = 0; + if (s < 60) { + PutNum(out, &i, s, 1); + Put(out, &i, "s"); + } else if (s < 3600) { + PutNum(out, &i, s / 60, 1); + Put(out, &i, "m"); + } else if (s < 86400UL) { + PutNum(out, &i, s / 3600, 1); + Put(out, &i, "h "); + PutNum(out, &i, s / 60 % 60, 1); + Put(out, &i, "m"); + } else { + PutNum(out, &i, s / 86400UL, 1); + Put(out, &i, "d "); + PutNum(out, &i, s / 3600 % 24, 1); + Put(out, &i, "h"); + } +} + +UInt16 ScrStatsLines(const VmStats *st, const VmBattery *b, Boolean hasBatt, Char lines[][SCR_LINE]) +{ + UInt16 n = 0, i; + Char a[16], c[16]; + UInt8 hex[2]; + + if (hasBatt) { + i = 0; + Put(lines[n], &i, "Battery "); + PutNum(lines[n], &i, b->battMv / 1000, 1); + Put(lines[n], &i, "."); + PutNum(lines[n], &i, b->battMv % 1000 / 10, 2); + Put(lines[n++], &i, " V"); + if (b->hasStorage) { + i = 0; + Put(lines[n], &i, "Storage "); + PutNum(lines[n], &i, b->usedKb, 1); + Put(lines[n], &i, " of "); + PutNum(lines[n], &i, b->totalKb, 1); + Put(lines[n++], &i, " KB used"); + } + } + if (st->hasCore) { + i = 0; + ScrSecs(a, st->uptimeSecs); + Put(lines[n], &i, "Up "); + Put(lines[n], &i, a); + Put(lines[n], &i, ", "); + PutNum(lines[n], &i, st->queueLen, 1); + Put(lines[n++], &i, " queued"); + if (st->errFlags) { + i = 0; + hex[0] = (UInt8)(st->errFlags >> 8); + hex[1] = (UInt8)st->errFlags; + TxtHex(hex, 2, c); + Put(lines[n], &i, "Errors "); + Put(lines[n++], &i, c); + } + } + if (st->hasRadio) { + i = 0; + Put(lines[n], &i, "Noise floor "); + PutInt(lines[n], &i, st->noiseFloor); + Put(lines[n++], &i, " dBm"); + i = 0; + TxtFormatSnr(c, st->lastSnrQ); + Put(lines[n], &i, "Last heard "); + PutInt(lines[n], &i, st->lastRssi); + Put(lines[n], &i, " dBm, "); + Put(lines[n++], &i, c); + i = 0; + ScrSecs(a, st->txAirSecs); + ScrSecs(c, st->rxAirSecs); + Put(lines[n], &i, "Airtime: sent "); + Put(lines[n], &i, a); + Put(lines[n], &i, ", heard "); + Put(lines[n++], &i, c); + } + if (st->hasPackets) { + i = 0; + Put(lines[n], &i, "Heard "); + PutNum(lines[n], &i, st->recv, 1); + Put(lines[n], &i, ": "); + PutNum(lines[n], &i, st->recvFlood, 1); + Put(lines[n], &i, " flood, "); + PutNum(lines[n], &i, st->recvDirect, 1); + Put(lines[n++], &i, " direct"); + i = 0; + Put(lines[n], &i, "Sent "); + PutNum(lines[n], &i, st->sent, 1); + Put(lines[n], &i, ": "); + PutNum(lines[n], &i, st->sentFlood, 1); + Put(lines[n], &i, " flood, "); + PutNum(lines[n], &i, st->sentDirect, 1); + Put(lines[n++], &i, " direct"); + } + return n; +} + +void ScrVisorClock(Char *out, const ScrTime *local, Int16 off) +{ + UInt16 i = 0; + Int16 a = off < 0 ? -off : off; + + out[0] = 0; + Put(out, &i, "Visor "); + PutDate(out, &i, local, '/', 0); + Put(out, &i, off < 0 ? " (UTC-" : " (UTC+"); + PutNum(out, &i, a / 60, 1); + Put(out, &i, ":"); + PutNum(out, &i, a % 60, 2); + Put(out, &i, ")"); +} + +void ScrRadioClock(Char *out, const ScrTime *radio, Boolean mismatch, Int32 sys) +{ + UInt16 i = 0; + + out[0] = 0; + Put(out, &i, "Radio "); + if (radio) + PutDate(out, &i, radio, '/', 0); + else + Put(out, &i, "--"); + if (mismatch) { + Put(out, &i, " UTC: MISMATCH"); + return; + } + Put(out, &i, " UTC, sys "); + PutInt(out, &i, sys); +} + +void ScrContactSummary(Char *out, Char glyph, const Char *type, const Char *route, Boolean loggedIn) +{ + UInt16 i = 1; + + out[0] = glyph; + out[1] = 0; + Put(out, &i, " "); + Put(out, &i, type); + Put(out, &i, ", "); + Put(out, &i, route); + if (loggedIn) + Put(out, &i, ", logged in"); +} + +void ScrContactAdvert(Char *out, const ScrTime *when, const Char *ago) +{ + UInt16 i = 0; + + out[0] = 0; + Put(out, &i, "Advert "); + PutDate(out, &i, when, '-', 1); + Put(out, &i, " ("); + Put(out, &i, ago); + Put(out, &i, " ago)"); +} + +void ScrTraceBack(Char *out, UInt32 ms, UInt16 hops) +{ + UInt16 i = 0; + + out[0] = 0; + Put(out, &i, "Back in "); + PutNum(out, &i, ms / 1000, 1); + Put(out, &i, "."); + PutNum(out, &i, ms % 1000 / 100, 1); + Put(out, &i, " s, "); + PutNum(out, &i, hops, 1); + Put(out, &i, hops == 1 ? " hop" : " hops"); +} diff --git a/palm-client/src/screen_fmt.h b/palm-client/src/screen_fmt.h new file mode 100644 index 0000000..268de2b --- /dev/null +++ b/palm-client/src/screen_fmt.h @@ -0,0 +1,39 @@ +/* + * Lines of text the screens build at run time, kept out of the forms so the + * host tests (tests/test_screens.c) can feed them their longest values and + * measure the result in the large font. Pure C, no Palm OS calls. + * Each out[] needs SCR_LINE bytes. + */ +#ifndef VM_SCREEN_FMT_H +#define VM_SCREEN_FMT_H + +#include "protocol2.h" + +#define SCR_LINE 48 + +/* A local or UTC time as the screens show it. */ +typedef struct { + UInt8 month, day, hour, minute; +} ScrTime; + +/* "45s", "12m", "3h 20m", "2d 4h" */ +void ScrSecs(Char *out, UInt32 secs) VM_SECT5; + +/* Radio Stats: the report's lines into lines[], at most SCR_STATS_LINES; returns how many. */ +#define SCR_STATS_LINES 9 +UInt16 ScrStatsLines(const VmStats *st, const VmBattery *batt, Boolean hasBatt, Char lines[][SCR_LINE]) VM_SECT5; + +/* Node: "Visor 11/8 14:02 (UTC-5:00)" */ +void ScrVisorClock(Char *out, const ScrTime *local, Int16 offsetMin) VM_SECT5; +/* Node: "Radio 11/8 19:02 UTC, sys 300", or "... UTC: MISMATCH"; radio NULL while it's unknown. */ +void ScrRadioClock(Char *out, const ScrTime *radio, Boolean mismatch, Int32 sysMinutesWest) VM_SECT5; + +/* Contact: " Room server, 3 hops, logged in" */ +void ScrContactSummary(Char *out, Char glyph, const Char *type, const Char *route, Boolean loggedIn) VM_SECT5; +/* Contact: "Advert 11-08 14:02 (3h ago)" */ +void ScrContactAdvert(Char *out, const ScrTime *when, const Char *ago) VM_SECT5; + +/* Trace: "Back in 1.2 s, 3 hops" */ +void ScrTraceBack(Char *out, UInt32 ms, UInt16 hops) VM_SECT5; + +#endif diff --git a/palm-client/src/stats_form.c b/palm-client/src/stats_form.c index e5489cf..fb81ede 100644 --- a/palm-client/src/stats_form.c +++ b/palm-client/src/stats_form.c @@ -11,11 +11,10 @@ #include "protocol2.h" #include "ui/draw.h" #include "ui/text.h" +#include "screen_fmt.h" #include "stats_form.h" static void Read(void) VM_SECT5; -static void FormatSecs(Char *out, UInt32 s) VM_SECT5; -static void DrawText(Coord y, const Char *s) VM_SECT5; static void DrawGraph(const RectangleType *g) VM_SECT5; static void Draw(void) VM_SECT5; @@ -31,6 +30,7 @@ static Err gErr; static UInt32 gReadAt; static Int16 gNoise[NOISE_MAX]; static UInt16 gNoiseCount = 0, gNoiseNext = 0; +static Char gLines[SCR_STATS_LINES][SCR_LINE]; static void Read(void) { @@ -64,24 +64,6 @@ static void Read(void) } } -/* "45s", "12m", "3h 20m", "2d 4h" */ -static void FormatSecs(Char *out, UInt32 s) -{ - if (s < 60) - StrPrintF(out, "%lus", s); - else if (s < 3600) - StrPrintF(out, "%lum", s / 60); - else if (s < 86400UL) - StrPrintF(out, "%luh %lum", s / 3600, s / 60 % 60); - else - StrPrintF(out, "%lud %luh", s / 86400UL, s / 3600 % 24); -} - -static void DrawText(Coord y, const Char *s) -{ - DrawCut(s, 3, y, 154); /* never past the edge, whatever the font */ -} - /* The noise floor samples, oldest at the left; the scale fits what's there, at least 10 dB. */ static void DrawGraph(const RectangleType *g) { @@ -120,8 +102,8 @@ static void Draw(void) { FontID old = FntSetFont(vmSmallFont); RectangleType r; - Char line[48], a[16], b[16], snr[8]; - Coord y = 18, step = VM_ROW_H; + Coord y = 18; + UInt16 n, i; RctSetRectangle(&r, 0, 16, 160, 127); WinEraseRectangle(&r, 0); @@ -130,51 +112,15 @@ static void Draw(void) FntSetFont(old); return; } - if (gHasBatt) { - StrPrintF(line, "Battery %u.%02u V", gBatt.battMv / 1000, (gBatt.battMv % 1000) / 10); - DrawText(y, line); - y += step; - if (gBatt.hasStorage) { - StrPrintF(line, "Storage %lu of %lu KB used", gBatt.usedKb, gBatt.totalKb); - DrawText(y, line); - y += step; - } - } - if (gStats.hasCore) { - FormatSecs(a, gStats.uptimeSecs); - StrPrintF(line, "Up %s, %u queued", a, gStats.queueLen); - if (gStats.errFlags) - StrPrintF(line + StrLen(line), ", errors %x", gStats.errFlags); - DrawText(y, line); - y += step; - } - if (gStats.hasRadio) { - StrPrintF(line, "Noise floor %d dBm", gStats.noiseFloor); - DrawText(y, line); - y += step; - TxtFormatSnr(snr, gStats.lastSnrQ); - StrPrintF(line, "Last heard %d dBm, %s", (Int16)gStats.lastRssi, snr); - DrawText(y, line); - y += step; - FormatSecs(a, gStats.txAirSecs); - FormatSecs(b, gStats.rxAirSecs); - StrPrintF(line, "Airtime: sent %s, heard %s", a, b); - DrawText(y, line); - y += step; - } - if (gStats.hasPackets) { - StrPrintF(line, "Heard %lu: %lu flood, %lu direct", gStats.recv, gStats.recvFlood, gStats.recvDirect); - DrawText(y, line); - y += step; - StrPrintF(line, "Sent %lu: %lu flood, %lu direct", gStats.sent, gStats.sentFlood, gStats.sentDirect); - DrawText(y, line); - y += step; - } if (!gStats.hasCore && !gStats.hasRadio && !gStats.hasPackets) { DrawWrap(gErr == vmErrRadio ? "This firmware doesn't report stats." : "The radio didn't answer.", 3, y, 154); FntSetFont(old); return; } + /* tests/test_screens.c checks the longest values still fit above Done */ + n = ScrStatsLines(&gStats, &gBatt, gHasBatt, gLines); + for (i = 0; i < n; i++) + y = DrawWrap(gLines[i], 3, y, 154); /* the graph takes what the lines leave, which depends on the font */ y += 2; if (GRAPH_BOTTOM - (y + 12) < GRAPH_MIN_H) { diff --git a/palm-client/src/trace_form.c b/palm-client/src/trace_form.c index 2af2284..29b6000 100644 --- a/palm-client/src/trace_form.c +++ b/palm-client/src/trace_form.c @@ -19,6 +19,7 @@ #include "ui/rowlist.h" #include "ui/glyphs.h" #include "ui/title.h" +#include "screen_fmt.h" #include "trace_form.h" static void *GetObject(FormType *form, UInt16 id) VM_SECT5; @@ -124,14 +125,13 @@ static void DrawRow(UInt16 row, const RectangleType *b, void *ctx) if (row == 0) { if (gErr == vmErrNoReply) - StrCopy(line, "No answer: a hop didn't hear it."); + StrCopy(line, "No answer: a hop missed it."); else if (gErr != errNone) { AppErrorText(gErr, msg); StrCopy(line, msg); } else - StrPrintF(line, "Back in %lu.%lu s, %u hop%s", gTripMs / 1000, gTripMs % 1000 / 100, - gTrace.hops, gTrace.hops == 1 ? "" : "s"); - WinDrawChars(line, StrLen(line), x, y); + ScrTraceBack(line, gTripMs, gTrace.hops); + DrawCut(line, x, y, b->extent.x - 2); } else { line[0] = GLYPH_FORWARD; line[1] = ' '; diff --git a/palm-client/tests/test_screens.c b/palm-client/tests/test_screens.c new file mode 100644 index 0000000..930a6bf --- /dev/null +++ b/palm-client/tests/test_screens.c @@ -0,0 +1,180 @@ +/* + * Host tests for the screens' run-time lines (src/screen_fmt.c): each fed its + * longest realistic values must fit its screen's width in the large font, + * which is what the user sees with Text size Large. Run with `make test`. + */ +#include +#include +#include "../src/screen_fmt.h" +#include "../src/ui/text.h" +#include "../src/ui/glyphs.h" + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \ +} while (0) + +/* Widths available: x to the right edge of each screen (see the Draw code). */ +#define W_STATS 154 /* x 3, cut at 154 */ +#define W_NODE 155 /* x 5 */ +#define W_CONTACT 151 /* the 153-wide gadget, x 1, right x - 1 */ +#define W_TRACE 151 +#define STATS_ROWS ((147 - 18) / (VM_LARGE_FONT_HEIGHT + 1)) /* from y 18 to the Done button */ + +static void fits(const char *what, const Char *s, int maxW) +{ + int w = TxtWidth(s, (UInt16)strlen(s), TxtSmallWidth); + + if (w > maxW) { + printf("FAIL %s: \"%s\" is %d px, room for %d\n", what, s, w, maxW); + failures++; + } +} + +/* Rows s takes when word-wrapped to maxW. */ +static int wrapped(const Char *s, int maxW) +{ + UInt16 len = (UInt16)strlen(s), used; + int rows = 0; + + while (len) { + TxtWrap(s, len, maxW, TxtSmallWidth, &used); + if (used == 0) + break; + s += used; + len -= used; + rows++; + } + return rows; +} + +static void test_secs(void) +{ + Char s[SCR_LINE]; + + ScrSecs(s, 45); CHECK(strcmp(s, "45s") == 0); + ScrSecs(s, 720); CHECK(strcmp(s, "12m") == 0); + ScrSecs(s, 12000); CHECK(strcmp(s, "3h 20m") == 0); + ScrSecs(s, 187200UL); CHECK(strcmp(s, "2d 4h") == 0); +} + +static void test_stats(void) +{ + VmStats st; + VmBattery b; + Char lines[SCR_STATS_LINES][SCR_LINE]; + UInt16 n, i, rows; + + memset(&st, 0, sizeof(st)); + memset(&b, 0, sizeof(b)); + b.battMv = 4205; + b.hasStorage = 1; + b.usedKb = 312; + b.totalKb = 4096; + st.hasCore = st.hasRadio = st.hasPackets = 1; + st.uptimeSecs = 9; + st.queueLen = 2; + st.noiseFloor = -112; + st.lastRssi = -87; + st.lastSnrQ = 25; + st.recv = 5; + st.recvFlood = 3; + st.recvDirect = 2; + n = ScrStatsLines(&st, &b, 1, lines); + CHECK(n == 8); + for (i = 0; i < n; i++) + fits("Radio Stats (typical)", lines[i], W_STATS); + CHECK(strcmp(lines[0], "Battery 4.20 V") == 0); + CHECK(strcmp(lines[1], "Storage 312 of 4096 KB used") == 0); + CHECK(strcmp(lines[2], "Up 9s, 2 queued") == 0); + CHECK(strcmp(lines[3], "Noise floor -112 dBm") == 0); + CHECK(strcmp(lines[4], "Last heard -87 dBm, +6dB") == 0); + CHECK(strcmp(lines[6], "Heard 5: 3 flood, 2 direct") == 0); + st.errFlags = 0x0104; + ScrStatsLines(&st, &b, 1, lines); + CHECK(strcmp(lines[3], "Errors 0104") == 0); + + /* a radio up for a year that hears a busy mesh */ + b.battMv = 65535; + b.usedKb = 99999; + b.totalKb = 99999; + st.uptimeSecs = 365UL * 86400 + 23 * 3600; + st.queueLen = 255; + st.errFlags = 0xFFFF; + st.noiseFloor = -128; + st.lastRssi = -128; + st.lastSnrQ = -128; + st.txAirSecs = st.rxAirSecs = 365UL * 86400 + 23 * 3600; + st.recv = st.recvFlood = st.recvDirect = 9999999UL; + st.sent = st.sentFlood = st.sentDirect = 9999999UL; + n = ScrStatsLines(&st, &b, 1, lines); + for (i = rows = 0; i < n; i++) + rows += wrapped(lines[i], W_STATS); + /* the lines wrap (DrawWrap) and the graph gives way; they must still fit above Done */ + if (rows > STATS_ROWS) { + printf("FAIL Radio Stats: %d rows wrapped, room for %d\n", rows, STATS_ROWS); + failures++; + } +} + +static void test_node(void) +{ + ScrTime t = { 12, 31, 23, 59 }; + Char s[SCR_LINE]; + + ScrVisorClock(s, &t, -300); + CHECK(strcmp(s, "Visor 12/31 23:59 (UTC-5:00)") == 0); + ScrVisorClock(s, &t, 765); /* +12:45, the widest zone */ + fits("Node clock", s, W_NODE); + ScrRadioClock(s, &t, 0, -765); + CHECK(strcmp(s, "Radio 12/31 23:59 UTC, sys -765") == 0); + fits("Node radio clock", s, W_NODE); + ScrRadioClock(s, &t, 1, 0); + fits("Node radio clock", s, W_NODE); + ScrRadioClock(s, NULL, 0, 300); + CHECK(strcmp(s, "Radio -- UTC, sys 300") == 0); +} + +static void test_contact(void) +{ + ScrTime t = { 12, 31, 23, 59 }; + Char s[SCR_LINE]; + + /* only repeaters and rooms are logged into; 32 hops is the most a path holds */ + ScrContactSummary(s, GLYPH_REPEATER, "Repeater", "32 hops", 1); + fits("Contact summary", s, W_CONTACT); + ScrContactSummary(s, GLYPH_ROOM, "Room", "32 hops", 1); + fits("Contact summary", s, W_CONTACT); + ScrContactSummary(s, GLYPH_COMPANION, "Companion", "32 hops", 0); + fits("Contact summary", s, W_CONTACT); + ScrContactAdvert(s, &t, "364d"); + CHECK(strcmp(s, "Advert 12-31 23:59 (364d ago)") == 0); + fits("Contact advert", s, W_CONTACT); +} + +static void test_trace(void) +{ + Char s[SCR_LINE]; + + ScrTraceBack(s, 1250, 1); + CHECK(strcmp(s, "Back in 1.2 s, 1 hop") == 0); + ScrTraceBack(s, 99999UL, 64); + fits("Trace", s, W_TRACE); +} + +int main(void) +{ + gTxtLarge = 1; + test_secs(); + test_stats(); + test_node(); + test_contact(); + test_trace(); + if (failures) { + printf("%d failure(s)\n", failures); + return 1; + } + printf("screens: all tests passed\n"); + return 0; +}