From 2aacecadff0694af58ae6994ccf0056c89f5232f Mon Sep 17 00:00:00 2001 From: taco Date: Mon, 26 Jan 2026 16:41:08 +1100 Subject: [PATCH] refactor ui with ring buffer and display most recent --- examples/companion_radio/ui-new/UITask.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/companion_radio/ui-new/UITask.cpp b/examples/companion_radio/ui-new/UITask.cpp index 8077627f8..b639f7e7d 100644 --- a/examples/companion_radio/ui-new/UITask.cpp +++ b/examples/companion_radio/ui-new/UITask.cpp @@ -452,15 +452,17 @@ class MsgPreviewScreen : public UIScreen { }; #define MAX_UNREAD_MSGS 32 int num_unread; + int head = MAX_UNREAD_MSGS - 1; // index of latest unread message MsgEntry unread[MAX_UNREAD_MSGS]; public: MsgPreviewScreen(UITask* task, mesh::RTCClock* rtc) : _task(task), _rtc(rtc) { num_unread = 0; } void addPreview(uint8_t path_len, const char* from_name, const char* msg) { - if (num_unread >= MAX_UNREAD_MSGS) return; // full + head = (head + 1) % MAX_UNREAD_MSGS; + if (num_unread < MAX_UNREAD_MSGS) num_unread++; - auto p = &unread[num_unread++]; + auto p = &unread[head]; p->timestamp = _rtc->getCurrentTime(); if (path_len == 0xFF) { sprintf(p->origin, "(D) %s:", from_name); @@ -478,7 +480,7 @@ class MsgPreviewScreen : public UIScreen { sprintf(tmp, "Unread: %d", num_unread); display.print(tmp); - auto p = &unread[0]; + auto p = &unread[head]; int secs = _rtc->getCurrentTime() - p->timestamp; if (secs < 60) { @@ -514,14 +516,10 @@ class MsgPreviewScreen : public UIScreen { bool handleInput(char c) override { if (c == KEY_NEXT || c == KEY_RIGHT) { + head = (head + MAX_UNREAD_MSGS - 1) % MAX_UNREAD_MSGS; num_unread--; if (num_unread == 0) { _task->gotoHomeScreen(); - } else { - // delete first/curr item from unread queue - for (int i = 0; i < num_unread; i++) { - unread[i] = unread[i + 1]; - } } return true; }