From 9da4396c6f600a00e9519dbcbc0698205fa50fdb Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Wed, 24 Dec 2025 17:13:31 -0500 Subject: [PATCH] Multi message storage (#8182) * First try at multimessage storage and display * Nrf built issue fix * Message view mode * Add channel name instead of channel slot * trunk fix * Fix for DM threading * fix for message time * rename of view mode to Conversations * Reply in thread feature * rename Select View Mode to Select Conversation * dismiss all live fix * Messages from phone show on screen * Decoupled message packets from screen.cpp and cleaned up * Cannedmessage cleanup and emotes fixed * Ack on messages sent * Ack message cleanup * Dismiss feature fixed * removed legacy temporary messages * Emote picker fix * Memory size debug * Build error fix * Sanity checks are okay sometimes * Lengthen channel name and finalize cleanup removal of Broadcast * Change DM to @ in order to unify on a single method * Continue unifying display, also show message status on the "isMine" lines * Add context for incoming messages * Better to say "in" vs "on" * crash fix for confirmation nodes * Fix outbound labels based to avoid creating delays * Eink autoscroll dissabled * gating for message storage when not using a screen * revert * Build fail fix * Don't error out with unset MAC address in unit tests * Provide some extra spacing for low hanging characters in messages * Reorder menu options and reword Respond * Reword menus to better reflect actions * Go to thread from favorite screen * Reorder Favorite Action Menu with simple word modifications * Consolidate wording on "Chats" * Mute channel fix * trunk fix * Clean up how muting works along with when we wake the screen * Fix builds for HELTEC_MESH_SOLAR * Signal bars for message ack * fix for notification renderer * Remove duplicate code, fix more Chats, and fix C6L MessageRenderer * Fix to many warnings related to BaseUI * preset aware signal strength display * More C6L fixes and clean up header lines * Use text aligns for message layout where necessary * Attempt to fix memory usage of invalidLifetime * Update channel mute for adjusted protobuf * Missed a comma in merge conflicts * cleanup to get more space * Trunk fixes * Optimize Hi Rez Chirpy to save space * more fixes * More cleanup * Remove used getConversationWith * Remove unused dismissNewestMessage * Fix another build error on occassion * Dimiss key combo function deprecated * More cleanup * Fn symbol code removed * Waypoint cleanup * Trunk fix * Fixup Waypoint screen with BaseUI code * Implement Haruki's ClockRenderer and broadcast decomposeTime across various files. * Revert "Implement Haruki's ClockRenderer and broadcast decomposeTime across various files." This reverts commit 2f6572177482fe059d37253117fa8f47abdb6310. * Implement Haruki's ClockRenderer and broadcast decomposeTime across various files. Attempt 2! * remove memory usage debug * Revert only RangeTestModule.cpp change * Switch from dynamic std::string storage to fixed-size char[] * Removing old left over code * More optimization * Free Heap when not on Message screen * build error fixes * Restore ellipsis to end of long names * Remove legacy function renderMessageContent * improved destination filtering * force PKI * cleanup * Shorten longNames to not exceed message popups * log messages sent from apps * Trunk fix * Improve layout of messages screen * Fix potential crash for undefined variable * Revert changes to RedirectablePrint.cpp * Apply shortening to longNames in Select Destination * Fix short name displays * Fix sprintfOverlappingData issue * Fix nullPointerRedundantCheck warning on ESP32 * Add "Delete All Chats" to all chat views * Improve getSafeNodeName / sanitizeString code. * Improve getSafeNodeName further * Restore auto favorite; but only if not CLIENT_BASE * Don't favorite if WE are CLIENT_BASE role * Don't run message persistent in MUI * Fix broken endifs * Unkwnown nodes no longer show as ??? on message thread * More delete options and cleanup of code * fix for delete this chat * Message menu cleanup * trunk fix * Clean up some menu options and remove some Unit C6L ifdefines * Rework Delete flow * Desperate times call for desperate measures * Create a background on the connected icon to reduce overlap impact * Optimize code for background image * Fix for Muzi_Base * Trunk Fixes * Remove the up/down shortcut to launch canned messages (#8370) * Remove the up/down shortcut to launch canned messages * Enabled MQTT and WEBSERVER by default (#8679) Signed-off-by: kur1k0 Co-authored-by: Ben Meadors Co-authored-by: Jonathan Bennett --------- Signed-off-by: kur1k0 Co-authored-by: Riker Co-authored-by: Ben Meadors * Correct string length calculation for signal bars * Manual message scrolling * Fix * Restore CannedMessages on Home Frame * UpDown situational destination for textMessage * Correct up/down destinations on textMessage frame * Update Screen.h for handleTextMessage * Update Screen.cpp to repair a merge issue * Add nudge scroll on UpDownEncoder devices. * Set nodeName to maximum size * Revert "Set nodeName to maximum size" This reverts commit e254f399256c746b0c82d62475b580d27d4fbbb9. * Reflow Node Lists and TLora Pager Views (#8942) * Add files via upload * Move files into the right place * Short or Long Names for everyone! * Add scrolling to Node list * Pagination fix for Latest to oldest per page * Page counters * Dynamic scaling of column counts based upon screen size, clean up box drawing * Reflow Node Lists and TLora Pager Views (#8942) * Add files via upload * Move files into the right place * Short or Long Names for everyone! * Add scrolling to Node list * Pagination fix for Latest to oldest per page * Page counters * Dynamic scaling of column counts based upon screen size, clean up box drawing * Update exempt labels for stale bot workflow Adds triaged and backlog to the list of exempt labels. * Update naming of Frame Visibility toggles * Fix to scrolling * Fix for content cutting off when from us * Fix for "delete this chat" now it does delete the current one * Rework isHighResolution to be an enum called ScreenResolution * Migrate Unit C6L macro guards into currentResolution UltraLow checks * Mistakes happen - restoring NodeList Renderer line --------- Signed-off-by: kur1k0 Co-authored-by: Jason P Co-authored-by: Jonathan Bennett Co-authored-by: Riker Co-authored-by: Ben Meadors Co-authored-by: whywilson Co-authored-by: Tom Fifield --- src/MessageStore.cpp | 426 +++++++ src/MessageStore.h | 131 +++ src/Power.cpp | 5 +- src/RedirectablePrint.cpp | 1 + src/graphics/Screen.cpp | 322 ++--- src/graphics/Screen.h | 31 +- src/graphics/SharedUIDisplay.cpp | 66 +- src/graphics/SharedUIDisplay.h | 7 +- src/graphics/VirtualKeyboard.cpp | 2 - src/graphics/draw/ClockRenderer.cpp | 205 ++-- src/graphics/draw/CompassRenderer.cpp | 4 +- src/graphics/draw/DebugRenderer.cpp | 147 ++- src/graphics/draw/DrawRenderers.h | 3 - src/graphics/draw/MenuHandler.cpp | 699 ++++++++--- src/graphics/draw/MenuHandler.h | 10 +- src/graphics/draw/MessageRenderer.cpp | 1038 ++++++++++++----- src/graphics/draw/MessageRenderer.h | 49 +- src/graphics/draw/NodeListRenderer.cpp | 379 ++++-- src/graphics/draw/NodeListRenderer.h | 21 +- src/graphics/draw/NotificationRenderer.cpp | 215 ++-- src/graphics/draw/UIRenderer.cpp | 125 +- src/graphics/draw/UIRenderer.h | 7 +- src/graphics/images.h | 52 - src/input/kbI2cBase.cpp | 2 - src/mesh/MeshModule.h | 1 + src/mesh/MeshService.cpp | 12 +- src/modules/CannedMessageModule.cpp | 835 +++++++------ src/modules/CannedMessageModule.h | 17 +- src/modules/DropzoneModule.cpp | 4 +- src/modules/SystemCommandsModule.cpp | 9 +- .../Telemetry/EnvironmentTelemetry.cpp | 2 +- src/modules/Telemetry/PowerTelemetry.cpp | 2 +- src/modules/TextMessageModule.cpp | 18 +- src/modules/TextMessageModule.h | 16 +- src/modules/WaypointModule.cpp | 120 +- src/platform/portduino/PortduinoGlue.cpp | 2 + .../nrf52840/heltec_mesh_solar/platformio.ini | 8 + 37 files changed, 3337 insertions(+), 1656 deletions(-) create mode 100644 src/MessageStore.cpp create mode 100644 src/MessageStore.h diff --git a/src/MessageStore.cpp b/src/MessageStore.cpp new file mode 100644 index 000000000..c96645b1c --- /dev/null +++ b/src/MessageStore.cpp @@ -0,0 +1,426 @@ +#include "configuration.h" +#if HAS_SCREEN +#include "FSCommon.h" +#include "MessageStore.h" +#include "NodeDB.h" +#include "SPILock.h" +#include "SafeFile.h" +#include "gps/RTC.h" +#include "graphics/draw/MessageRenderer.h" +#include // memcpy + +#ifndef MESSAGE_TEXT_POOL_SIZE +#define MESSAGE_TEXT_POOL_SIZE (MAX_MESSAGES_SAVED * MAX_MESSAGE_SIZE) +#endif + +// Global message text pool and state +static char *g_messagePool = nullptr; +static size_t g_poolWritePos = 0; + +// Reset pool (called on boot or clear) +static inline void resetMessagePool() +{ + if (!g_messagePool) { + g_messagePool = static_cast(malloc(MESSAGE_TEXT_POOL_SIZE)); + if (!g_messagePool) { + LOG_ERROR("MessageStore: Failed to allocate %d bytes for message pool", MESSAGE_TEXT_POOL_SIZE); + return; + } + } + g_poolWritePos = 0; + memset(g_messagePool, 0, MESSAGE_TEXT_POOL_SIZE); +} + +// Allocate text in pool and return offset +// If not enough space remains, wrap around (ring buffer style) +static inline uint16_t storeTextInPool(const char *src, size_t len) +{ + if (len >= MAX_MESSAGE_SIZE) + len = MAX_MESSAGE_SIZE - 1; + + // Wrap pool if out of space + if (g_poolWritePos + len + 1 >= MESSAGE_TEXT_POOL_SIZE) { + g_poolWritePos = 0; + } + + uint16_t offset = g_poolWritePos; + memcpy(&g_messagePool[g_poolWritePos], src, len); + g_messagePool[g_poolWritePos + len] = '\0'; + g_poolWritePos += (len + 1); + return offset; +} + +// Retrieve a const pointer to message text by offset +static inline const char *getTextFromPool(uint16_t offset) +{ + if (!g_messagePool || offset >= MESSAGE_TEXT_POOL_SIZE) + return ""; + return &g_messagePool[offset]; +} + +// Helper: assign a timestamp (RTC if available, else boot-relative) +static inline void assignTimestamp(StoredMessage &sm) +{ + uint32_t nowSecs = getValidTime(RTCQuality::RTCQualityDevice, true); + if (nowSecs) { + sm.timestamp = nowSecs; + sm.isBootRelative = false; + } else { + sm.timestamp = millis() / 1000; + sm.isBootRelative = true; + } +} + +// Generic push with cap (used by live + persisted queues) +template static inline void pushWithLimit(std::deque &queue, const T &msg) +{ + if (queue.size() >= MAX_MESSAGES_SAVED) + queue.pop_front(); + queue.push_back(msg); +} + +template static inline void pushWithLimit(std::deque &queue, T &&msg) +{ + if (queue.size() >= MAX_MESSAGES_SAVED) + queue.pop_front(); + queue.emplace_back(std::move(msg)); +} + +MessageStore::MessageStore(const std::string &label) +{ + filename = "/Messages_" + label + ".msgs"; + resetMessagePool(); // initialize text pool on boot +} + +// Live message handling (RAM only) +void MessageStore::addLiveMessage(StoredMessage &&msg) +{ + pushWithLimit(liveMessages, std::move(msg)); +} +void MessageStore::addLiveMessage(const StoredMessage &msg) +{ + pushWithLimit(liveMessages, msg); +} + +// Add from incoming/outgoing packet +const StoredMessage &MessageStore::addFromPacket(const meshtastic_MeshPacket &packet) +{ + StoredMessage sm; + assignTimestamp(sm); + sm.channelIndex = packet.channel; + + const char *payload = reinterpret_cast(packet.decoded.payload.bytes); + size_t len = strnlen(payload, MAX_MESSAGE_SIZE - 1); + sm.textOffset = storeTextInPool(payload, len); + sm.textLength = len; + + // Determine sender + uint32_t localNode = nodeDB->getNodeNum(); + sm.sender = (packet.from == 0) ? localNode : packet.from; + + sm.dest = packet.to; + + bool isDM = (sm.dest != 0 && sm.dest != NODENUM_BROADCAST); + + if (packet.from == 0) { + sm.type = isDM ? MessageType::DM_TO_US : MessageType::BROADCAST; + sm.ackStatus = AckStatus::NONE; + } else { + sm.type = isDM ? MessageType::DM_TO_US : MessageType::BROADCAST; + sm.ackStatus = AckStatus::ACKED; + } + + addLiveMessage(sm); + return liveMessages.back(); +} + +// Outgoing/manual message +void MessageStore::addFromString(uint32_t sender, uint8_t channelIndex, const std::string &text) +{ + StoredMessage sm; + + // Always use our local time (helper handles RTC vs boot time) + assignTimestamp(sm); + + sm.sender = sender; + sm.channelIndex = channelIndex; + sm.textOffset = storeTextInPool(text.c_str(), text.size()); + sm.textLength = text.size(); + + // Use the provided destination + sm.dest = sender; + sm.type = MessageType::DM_TO_US; + + // Outgoing messages always start with unknown ack status + sm.ackStatus = AckStatus::NONE; + + addLiveMessage(sm); +} + +#if ENABLE_MESSAGE_PERSISTENCE + +// Compact, fixed-size on-flash representation using offset + length +struct __attribute__((packed)) StoredMessageRecord { + uint32_t timestamp; + uint32_t sender; + uint8_t channelIndex; + uint32_t dest; + uint8_t isBootRelative; + uint8_t ackStatus; // static_cast(AckStatus) + uint8_t type; // static_cast(MessageType) + uint16_t textLength; // message length + char text[MAX_MESSAGE_SIZE]; // store actual text here +}; + +// Serialize one StoredMessage to flash +static inline void writeMessageRecord(SafeFile &f, const StoredMessage &m) +{ + StoredMessageRecord rec = {}; + rec.timestamp = m.timestamp; + rec.sender = m.sender; + rec.channelIndex = m.channelIndex; + rec.dest = m.dest; + rec.isBootRelative = m.isBootRelative; + rec.ackStatus = static_cast(m.ackStatus); + rec.type = static_cast(m.type); + rec.textLength = m.textLength; + + // Copy the actual text into the record from RAM pool + const char *txt = getTextFromPool(m.textOffset); + strncpy(rec.text, txt, MAX_MESSAGE_SIZE - 1); + rec.text[MAX_MESSAGE_SIZE - 1] = '\0'; + + f.write(reinterpret_cast(&rec), sizeof(rec)); +} + +// Deserialize one StoredMessage from flash; returns false on short read +static inline bool readMessageRecord(File &f, StoredMessage &m) +{ + StoredMessageRecord rec = {}; + if (f.readBytes(reinterpret_cast(&rec), sizeof(rec)) != sizeof(rec)) + return false; + + m.timestamp = rec.timestamp; + m.sender = rec.sender; + m.channelIndex = rec.channelIndex; + m.dest = rec.dest; + m.isBootRelative = rec.isBootRelative; + m.ackStatus = static_cast(rec.ackStatus); + m.type = static_cast(rec.type); + m.textLength = rec.textLength; + + // 💡 Re-store text into pool and update offset + m.textLength = strnlen(rec.text, MAX_MESSAGE_SIZE - 1); + m.textOffset = storeTextInPool(rec.text, m.textLength); + + return true; +} + +void MessageStore::saveToFlash() +{ +#ifdef FSCom + // Ensure root exists + spiLock->lock(); + FSCom.mkdir("/"); + spiLock->unlock(); + + SafeFile f(filename.c_str(), false); + + spiLock->lock(); + uint8_t count = static_cast(liveMessages.size()); + if (count > MAX_MESSAGES_SAVED) + count = MAX_MESSAGES_SAVED; + f.write(&count, 1); + + for (uint8_t i = 0; i < count; ++i) { + writeMessageRecord(f, liveMessages[i]); + } + spiLock->unlock(); + + f.close(); +#endif +} + +void MessageStore::loadFromFlash() +{ + std::deque().swap(liveMessages); + resetMessagePool(); // reset pool when loading + +#ifdef FSCom + concurrency::LockGuard guard(spiLock); + + if (!FSCom.exists(filename.c_str())) + return; + + auto f = FSCom.open(filename.c_str(), FILE_O_READ); + if (!f) + return; + + uint8_t count = 0; + f.readBytes(reinterpret_cast(&count), 1); + if (count > MAX_MESSAGES_SAVED) + count = MAX_MESSAGES_SAVED; + + for (uint8_t i = 0; i < count; ++i) { + StoredMessage m; + if (!readMessageRecord(f, m)) + break; + liveMessages.push_back(m); + } + + f.close(); +#endif +} + +#else +// If persistence is disabled, these functions become no-ops +void MessageStore::saveToFlash() {} +void MessageStore::loadFromFlash() {} +#endif + +// Clear all messages (RAM + persisted queue) +void MessageStore::clearAllMessages() +{ + std::deque().swap(liveMessages); + resetMessagePool(); + +#ifdef FSCom + SafeFile f(filename.c_str(), false); + uint8_t count = 0; + f.write(&count, 1); // write "0 messages" + f.close(); +#endif +} + +// Internal helper: erase first or last message matching a predicate +template static void eraseIf(std::deque &deque, Predicate pred, bool fromBack = false) +{ + if (fromBack) { + // Iterate from the back and erase all matches from the end + for (auto it = deque.rbegin(); it != deque.rend();) { + if (pred(*it)) { + it = std::deque::reverse_iterator(deque.erase(std::next(it).base())); + } else { + ++it; + } + } + } else { + // Manual forward search to erase all matches + for (auto it = deque.begin(); it != deque.end();) { + if (pred(*it)) { + it = deque.erase(it); + } else { + ++it; + } + } + } +} + +// Delete oldest message (RAM + persisted queue) +void MessageStore::deleteOldestMessage() +{ + eraseIf(liveMessages, [](StoredMessage &) { return true; }); + saveToFlash(); +} + +// Delete oldest message in a specific channel +void MessageStore::deleteOldestMessageInChannel(uint8_t channel) +{ + auto pred = [channel](const StoredMessage &m) { return m.type == MessageType::BROADCAST && m.channelIndex == channel; }; + eraseIf(liveMessages, pred); + saveToFlash(); +} + +void MessageStore::deleteAllMessagesInChannel(uint8_t channel) +{ + auto pred = [channel](const StoredMessage &m) { return m.type == MessageType::BROADCAST && m.channelIndex == channel; }; + eraseIf(liveMessages, pred, false /* delete ALL, not just first */); + saveToFlash(); +} + +void MessageStore::deleteAllMessagesWithPeer(uint32_t peer) +{ + uint32_t local = nodeDB->getNodeNum(); + auto pred = [&](const StoredMessage &m) { + if (m.type != MessageType::DM_TO_US) + return false; + uint32_t other = (m.sender == local) ? m.dest : m.sender; + return other == peer; + }; + eraseIf(liveMessages, pred, false); + saveToFlash(); +} + +// Delete oldest message in a direct chat with a node +void MessageStore::deleteOldestMessageWithPeer(uint32_t peer) +{ + auto pred = [peer](const StoredMessage &m) { + if (m.type != MessageType::DM_TO_US) + return false; + uint32_t other = (m.sender == nodeDB->getNodeNum()) ? m.dest : m.sender; + return other == peer; + }; + eraseIf(liveMessages, pred); + saveToFlash(); +} + +std::deque MessageStore::getChannelMessages(uint8_t channel) const +{ + std::deque result; + for (const auto &m : liveMessages) { + if (m.type == MessageType::BROADCAST && m.channelIndex == channel) { + result.push_back(m); + } + } + return result; +} + +std::deque MessageStore::getDirectMessages() const +{ + std::deque result; + for (const auto &m : liveMessages) { + if (m.type == MessageType::DM_TO_US) { + result.push_back(m); + } + } + return result; +} + +// Upgrade boot-relative timestamps once RTC is valid +// Only same-boot boot-relative messages are healed. +// Persisted boot-relative messages from old boots stay ??? forever. +void MessageStore::upgradeBootRelativeTimestamps() +{ + uint32_t nowSecs = getValidTime(RTCQuality::RTCQualityDevice, true); + if (nowSecs == 0) + return; // Still no valid RTC + + uint32_t bootNow = millis() / 1000; + + auto fix = [&](std::deque &dq) { + for (auto &m : dq) { + if (m.isBootRelative && m.timestamp <= bootNow) { + uint32_t bootOffset = nowSecs - bootNow; + m.timestamp += bootOffset; + m.isBootRelative = false; + } + } + }; + fix(liveMessages); +} + +const char *MessageStore::getText(const StoredMessage &msg) +{ + // Wrapper around the internal helper + return getTextFromPool(msg.textOffset); +} + +uint16_t MessageStore::storeText(const char *src, size_t len) +{ + // Wrapper around the internal helper + return storeTextInPool(src, len); +} + +// Global definition +MessageStore messageStore("default"); +#endif \ No newline at end of file diff --git a/src/MessageStore.h b/src/MessageStore.h new file mode 100644 index 000000000..41eb56b66 --- /dev/null +++ b/src/MessageStore.h @@ -0,0 +1,131 @@ +#pragma once + +#if HAS_SCREEN + +// Disable debug logging entirely on release builds of HELTEC_MESH_SOLAR for space constraints +#if defined(HELTEC_MESH_SOLAR) +#define LOG_DEBUG(...) +#endif + +// Enable or disable message persistence (flash storage) +// Define -DENABLE_MESSAGE_PERSISTENCE=0 in build_flags to disable it entirely +#ifndef ENABLE_MESSAGE_PERSISTENCE +#define ENABLE_MESSAGE_PERSISTENCE 1 +#endif + +#include "mesh/generated/meshtastic/mesh.pb.h" +#include +#include +#include + +// How many messages are stored (RAM + flash). +// Define -DMESSAGE_HISTORY_LIMIT=N in build_flags to control memory usage. +#ifndef MESSAGE_HISTORY_LIMIT +#define MESSAGE_HISTORY_LIMIT 20 +#endif + +// Internal alias used everywhere in code – do NOT redefine elsewhere. +#define MAX_MESSAGES_SAVED MESSAGE_HISTORY_LIMIT + +// Maximum text payload size per message in bytes. +// This still defines the max message length, but we no longer reserve this space per message. +#define MAX_MESSAGE_SIZE 220 + +// Total shared text pool size for all messages combined. +// The text pool is RAM-only. Text is re-stored from flash into the pool on boot. +#ifndef MESSAGE_TEXT_POOL_SIZE +#define MESSAGE_TEXT_POOL_SIZE (MAX_MESSAGES_SAVED * MAX_MESSAGE_SIZE) +#endif + +// Explicit message classification +enum class MessageType : uint8_t { + BROADCAST = 0, // broadcast message + DM_TO_US = 1 // direct message addressed to this node +}; + +// Delivery status for messages we sent +enum class AckStatus : uint8_t { + NONE = 0, // just sent, waiting (no symbol shown) + ACKED = 1, // got a valid ACK from destination + NACKED = 2, // explicitly failed + TIMEOUT = 3, // no ACK after retry window + RELAYED = 4 // got an ACK from relay, not destination +}; + +struct StoredMessage { + uint32_t timestamp; // When message was created (secs since boot or RTC) + uint32_t sender; // NodeNum of sender + uint8_t channelIndex; // Channel index used + uint32_t dest; // Destination node (broadcast or direct) + MessageType type; // Derived from dest (explicit classification) + bool isBootRelative; // true = millis()/1000 fallback; false = epoch/RTC absolute + AckStatus ackStatus; // Delivery status (only meaningful for our own sent messages) + + // Text storage metadata — rebuilt from flash at boot + uint16_t textOffset; // Offset into global text pool (valid only after loadFromFlash()) + uint16_t textLength; // Length of text in bytes + + // Default constructor initializes all fields safely + StoredMessage() + : timestamp(0), sender(0), channelIndex(0), dest(0xffffffff), type(MessageType::BROADCAST), isBootRelative(false), + ackStatus(AckStatus::NONE), textOffset(0), textLength(0) + { + } +}; + +class MessageStore +{ + public: + explicit MessageStore(const std::string &label); + + // Live RAM methods (always current, used by UI and runtime) + void addLiveMessage(StoredMessage &&msg); + void addLiveMessage(const StoredMessage &msg); // convenience overload + const std::deque &getLiveMessages() const { return liveMessages; } + + // Add new messages from packets or manual input + const StoredMessage &addFromPacket(const meshtastic_MeshPacket &mp); // Incoming/outgoing → RAM only + void addFromString(uint32_t sender, uint8_t channelIndex, const std::string &text); // Manual add + + // Persistence methods (used only on boot/shutdown) + void saveToFlash(); // Save messages to flash + void loadFromFlash(); // Load messages from flash + + // Clear all messages (RAM + persisted queue + text pool) + void clearAllMessages(); + + // Delete helpers + void deleteOldestMessage(); // remove oldest from RAM (and flash on save) + void deleteOldestMessageInChannel(uint8_t channel); + void deleteOldestMessageWithPeer(uint32_t peer); + void deleteAllMessagesInChannel(uint8_t channel); + void deleteAllMessagesWithPeer(uint32_t peer); + + // Unified accessor (for UI code, defaults to RAM buffer) + const std::deque &getMessages() const { return liveMessages; } + + // Helper filters for future use + std::deque getChannelMessages(uint8_t channel) const; // Only broadcast messages on a channel + std::deque getDirectMessages() const; // Only direct messages + + // Upgrade boot-relative timestamps once RTC is valid + void upgradeBootRelativeTimestamps(); + + // Retrieve the C-string text for a stored message + static const char *getText(const StoredMessage &msg); + + // Allocate text into pool (used by sender-side code) + static uint16_t storeText(const char *src, size_t len); + + // Used when loading from flash to rebuild the text pool + static uint16_t rebuildTextFromFlash(const char *src, size_t len); + + private: + std::deque liveMessages; // Single in-RAM message buffer (also used for persistence) + std::string filename; // Flash filename for persistence +}; + +// Global instance (defined in MessageStore.cpp) +extern MessageStore messageStore; + +#endif diff --git a/src/Power.cpp b/src/Power.cpp index 7bb8896ce..33dda8e11 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -11,6 +11,7 @@ * For more information, see: https://meshtastic.org/ */ #include "power.h" +#include "MessageStore.h" #include "NodeDB.h" #include "PowerFSM.h" #include "Throttle.h" @@ -786,7 +787,9 @@ void Power::shutdown() playShutdownMelody(); #endif nodeDB->saveToDisk(); - +#if HAS_SCREEN + messageStore.saveToFlash(); +#endif #if defined(ARCH_NRF52) || defined(ARCH_ESP32) || defined(ARCH_RP2040) #ifdef PIN_LED1 ledOff(PIN_LED1); diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index 9624a4593..895dcb147 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -131,6 +131,7 @@ void RedirectablePrint::log_to_serial(const char *logLevel, const char *format, int hour = hms / SEC_PER_HOUR; int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN; int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN + #ifdef ARCH_PORTDUINO ::printf("%s ", logLevel); if (color) { diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 8bac6936a..0012aeb5d 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -46,6 +46,7 @@ along with this program. If not, see . #endif #include "FSCommon.h" #include "MeshService.h" +#include "MessageStore.h" #include "RadioLibInterface.h" #include "error.h" #include "gps/GeoCoord.h" @@ -64,10 +65,7 @@ along with this program. If not, see . #include "modules/WaypointModule.h" #include "sleep.h" #include "target_specific.h" - -using graphics::Emote; -using graphics::emotes; -using graphics::numEmotes; +extern MessageStore messageStore; #if USE_TFTDISPLAY extern uint16_t TFT_MESH; @@ -119,10 +117,6 @@ uint32_t dopThresholds[5] = {2000, 1000, 500, 200, 100}; // we'll need to hold onto pointers for the modules that can draw a frame. std::vector moduleFrames; -// Global variables for screen function overlay symbols -std::vector functionSymbol; -std::string functionSymbolString; - #if HAS_GPS // GeoCoord object for the screen GeoCoord geoCoord; @@ -263,19 +257,11 @@ static void drawModuleFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int } else { // otherwise, just display the module frame that's aligned with the current frame module_frame = state->currentFrame; - // LOG_DEBUG("Screen is not in transition. Frame: %d", module_frame); } - // LOG_DEBUG("Draw Module Frame %d", module_frame); MeshModule &pi = *moduleFrames.at(module_frame); pi.drawFrame(display, state, x, y); } -// Ignore messages originating from phone (from the current node 0x0) unless range test or store and forward module are enabled -static bool shouldDrawMessage(const meshtastic_MeshPacket *packet) -{ - return packet->from != 0 && !moduleConfig.store_forward.enabled; -} - /** * Given a recent lat/lon return a guess of the heading the user is walking on. * @@ -322,16 +308,16 @@ Screen::Screen(ScanI2C::DeviceAddress address, meshtastic_Config_DisplayConfig_O { graphics::normalFrames = new FrameCallback[MAX_NUM_NODES + NUM_EXTRA_FRAMES]; - LOG_INFO("Protobuf Value uiconfig.screen_rgb_color: %d", uiconfig.screen_rgb_color); int32_t rawRGB = uiconfig.screen_rgb_color; - if (rawRGB > 0 && rawRGB <= 255255255) { - uint8_t TFT_MESH_r = (rawRGB >> 16) & 0xFF; - uint8_t TFT_MESH_g = (rawRGB >> 8) & 0xFF; - uint8_t TFT_MESH_b = rawRGB & 0xFF; - LOG_INFO("Values of r,g,b: %d, %d, %d", TFT_MESH_r, TFT_MESH_g, TFT_MESH_b); - if (TFT_MESH_r <= 255 && TFT_MESH_g <= 255 && TFT_MESH_b <= 255) { - TFT_MESH = COLOR565(TFT_MESH_r, TFT_MESH_g, TFT_MESH_b); + // Only validate the combined value once + if (rawRGB > 0 && rawRGB <= 255255255) { + // Extract each component as a normal int first + int r = (rawRGB >> 16) & 0xFF; + int g = (rawRGB >> 8) & 0xFF; + int b = rawRGB & 0xFF; + if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) { + TFT_MESH = COLOR565(static_cast(r), static_cast(g), static_cast(b)); } } @@ -550,10 +536,10 @@ void Screen::handleSetOn(bool on, FrameCallback einkScreensaver) void Screen::setup() { - // === Enable display rendering === + // Enable display rendering useDisplay = true; - // === Load saved brightness from UI config === + // Load saved brightness from UI config // For OLED displays (SSD1306), default brightness is 255 if not set if (uiconfig.screen_brightness == 0) { #if defined(USE_OLED) || defined(USE_SSD1306) || defined(USE_SH1106) || defined(USE_SH1107) @@ -565,7 +551,7 @@ void Screen::setup() brightness = uiconfig.screen_brightness; } - // === Detect OLED subtype (if supported by board variant) === + // Detect OLED subtype (if supported by board variant) #ifdef AutoOLEDWire_h if (isAUTOOled) static_cast(dispdev)->setDetected(model); @@ -587,7 +573,7 @@ void Screen::setup() static_cast(dispdev)->setRGB(TFT_MESH); #endif - // === Initialize display and UI system === + // Initialize display and UI system ui->init(); displayWidth = dispdev->width(); displayHeight = dispdev->height(); @@ -599,7 +585,7 @@ void Screen::setup() ui->disableAllIndicators(); // Disable page indicator dots ui->getUiState()->userData = this; // Allow static callbacks to access Screen instance - // === Apply loaded brightness === + // Apply loaded brightness #if defined(ST7789_CS) static_cast(dispdev)->setDisplayBrightness(brightness); #elif defined(USE_OLED) || defined(USE_SSD1306) || defined(USE_SH1106) || defined(USE_SH1107) || defined(USE_SPISSD1306) @@ -607,20 +593,20 @@ void Screen::setup() #endif LOG_INFO("Applied screen brightness: %d", brightness); - // === Set custom overlay callbacks === + // Set custom overlay callbacks static OverlayCallback overlays[] = { graphics::UIRenderer::drawNavigationBar // Custom indicator icons for each frame }; ui->setOverlays(overlays, sizeof(overlays) / sizeof(overlays[0])); - // === Enable UTF-8 to display mapping === + // Enable UTF-8 to display mapping dispdev->setFontTableLookupFunction(customFontTableLookup); #ifdef USERPREFS_OEM_TEXT logo_timeout *= 2; // Give more time for branded boot logos #endif - // === Configure alert frames (e.g., "Resuming..." or region name) === + // Configure alert frames (e.g., "Resuming..." or region name) EINK_ADD_FRAMEFLAG(dispdev, DEMAND_FAST); // Skip slow refresh alertFrames[0] = [this](OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { #ifdef ARCH_ESP32 @@ -636,10 +622,10 @@ void Screen::setup() ui->setFrames(alertFrames, 1); ui->disableAutoTransition(); // Require manual navigation between frames - // === Log buffer for on-screen logs (3 lines max) === + // Log buffer for on-screen logs (3 lines max) dispdev->setLogBuffer(3, 32); - // === Optional screen mirroring or flipping (e.g. for T-Beam orientation) === + // Optional screen mirroring or flipping (e.g. for T-Beam orientation) #ifdef SCREEN_MIRROR dispdev->mirrorScreen(); #else @@ -657,7 +643,7 @@ void Screen::setup() } #endif - // === Generate device ID from MAC address === + // Generate device ID from MAC address uint8_t dmac[6]; getMacAddr(dmac); snprintf(screen->ourId, sizeof(screen->ourId), "%02x%02x", dmac[4], dmac[5]); @@ -666,9 +652,9 @@ void Screen::setup() handleSetOn(false); // Ensure proper init for Arduino targets #endif - // === Turn on display and trigger first draw === + // Turn on display and trigger first draw handleSetOn(true); - determineResolution(dispdev->height(), dispdev->width()); + graphics::currentResolution = graphics::determineScreenResolution(dispdev->height(), dispdev->width()); ui->update(); #ifndef USE_EINK ui->update(); // Some SSD1306 clones drop the first draw, so run twice @@ -689,7 +675,7 @@ void Screen::setup() touchScreenImpl1->init(); #endif - // === Subscribe to device status updates === + // Subscribe to device status updates powerStatusObserver.observe(&powerStatus->onNewStatus); gpsStatusObserver.observe(&gpsStatus->onNewStatus); nodeStatusObserver.observe(&nodeStatus->onNewStatus); @@ -697,12 +683,14 @@ void Screen::setup() #if !MESHTASTIC_EXCLUDE_ADMIN adminMessageObserver.observe(adminModule); #endif - if (textMessageModule) - textMessageObserver.observe(textMessageModule); if (inputBroker) inputObserver.observe(inputBroker); - // === Notify modules that support UI events === + // Load persisted messages into RAM + messageStore.loadFromFlash(); + LOG_INFO("MessageStore loaded from flash"); + + // Notify modules that support UI events MeshModule::observeUIEvents(&uiFrameEventObserver); } @@ -773,6 +761,23 @@ int32_t Screen::runOnce() if (displayHeight == 0) { displayHeight = dispdev->getHeight(); } + + // Detect frame transitions and clear message cache when leaving text message screen + { + static int8_t lastFrameIndex = -1; + int8_t currentFrameIndex = ui->getUiState()->currentFrame; + int8_t textMsgIndex = framesetInfo.positions.textMessage; + + if (lastFrameIndex != -1 && currentFrameIndex != lastFrameIndex) { + + if (lastFrameIndex == textMsgIndex && currentFrameIndex != textMsgIndex) { + graphics::MessageRenderer::clearMessageCache(); + } + } + + lastFrameIndex = currentFrameIndex; + } + menuHandler::handleMenuSwitch(dispdev); // Show boot screen for first logo_timeout seconds, then switch to normal operation. @@ -828,17 +833,17 @@ int32_t Screen::runOnce() break; case Cmd::ON_PRESS: if (NotificationRenderer::current_notification_type != notificationTypeEnum::text_input) { - handleOnPress(); + showFrame(FrameDirection::NEXT); } break; case Cmd::SHOW_PREV_FRAME: if (NotificationRenderer::current_notification_type != notificationTypeEnum::text_input) { - handleShowPrevFrame(); + showFrame(FrameDirection::PREVIOUS); } break; case Cmd::SHOW_NEXT_FRAME: if (NotificationRenderer::current_notification_type != notificationTypeEnum::text_input) { - handleShowNextFrame(); + showFrame(FrameDirection::NEXT); } break; case Cmd::START_ALERT_FRAME: { @@ -859,6 +864,7 @@ int32_t Screen::runOnce() break; case Cmd::STOP_ALERT_FRAME: NotificationRenderer::pauseBanner = false; + break; case Cmd::STOP_BOOT_SCREEN: EINK_ADD_FRAMEFLAG(dispdev, COSMETIC); // E-Ink: Explicitly use full-refresh for next frame if (NotificationRenderer::current_notification_type != notificationTypeEnum::text_input) { @@ -1029,9 +1035,6 @@ void Screen::setFrames(FrameFocus focus) } #endif - // Declare this early so it’s available in FOCUS_PRESERVE block - bool willInsertTextMessage = shouldDrawMessage(&devicestate.rx_text_message); - if (!hiddenFrames.home) { fsi.positions.home = numframes; normalFrames[numframes++] = graphics::UIRenderer::drawDeviceFocused; @@ -1043,11 +1046,16 @@ void Screen::setFrames(FrameFocus focus) indicatorIcons.push_back(icon_mail); #ifndef USE_EINK - if (!hiddenFrames.nodelist) { - fsi.positions.nodelist = numframes; - normalFrames[numframes++] = graphics::NodeListRenderer::drawDynamicNodeListScreen; + if (!hiddenFrames.nodelist_nodes) { + fsi.positions.nodelist_nodes = numframes; + normalFrames[numframes++] = graphics::NodeListRenderer::drawDynamicListScreen_Nodes; indicatorIcons.push_back(icon_nodes); } + if (!hiddenFrames.nodelist_location) { + fsi.positions.nodelist_location = numframes; + normalFrames[numframes++] = graphics::NodeListRenderer::drawDynamicListScreen_Location; + indicatorIcons.push_back(icon_list); + } #endif // Show detailed node views only on E-Ink builds @@ -1069,11 +1077,13 @@ void Screen::setFrames(FrameFocus focus) } #endif #if HAS_GPS +#ifdef USE_EINK if (!hiddenFrames.nodelist_bearings) { fsi.positions.nodelist_bearings = numframes; normalFrames[numframes++] = graphics::NodeListRenderer::drawNodeListWithCompasses; indicatorIcons.push_back(icon_list); } +#endif if (!hiddenFrames.gps) { fsi.positions.gps = numframes; normalFrames[numframes++] = graphics::UIRenderer::drawCompassAndLocationScreen; @@ -1173,7 +1183,7 @@ void Screen::setFrames(FrameFocus focus) } fsi.frameCount = numframes; // Total framecount is used to apply FOCUS_PRESERVE - this->frameCount = numframes; // ✅ Save frame count for use in custom overlay + this->frameCount = numframes; // Save frame count for use in custom overlay LOG_DEBUG("Finished build frames. numframes: %d", numframes); ui->setFrames(normalFrames, numframes); @@ -1193,10 +1203,6 @@ void Screen::setFrames(FrameFocus focus) case FOCUS_FAULT: ui->switchToFrame(fsi.positions.fault); break; - case FOCUS_TEXTMESSAGE: - hasUnreadMessage = false; // ✅ Clear when message is *viewed* - ui->switchToFrame(fsi.positions.textMessage); - break; case FOCUS_MODULE: // Whichever frame was marked by MeshModule::requestFocus(), if any // If no module requested focus, will show the first frame instead @@ -1239,8 +1245,11 @@ void Screen::setFrameImmediateDraw(FrameCallback *drawFrames) void Screen::toggleFrameVisibility(const std::string &frameName) { #ifndef USE_EINK - if (frameName == "nodelist") { - hiddenFrames.nodelist = !hiddenFrames.nodelist; + if (frameName == "nodelist_nodes") { + hiddenFrames.nodelist_nodes = !hiddenFrames.nodelist_nodes; + } + if (frameName == "nodelist_location") { + hiddenFrames.nodelist_location = !hiddenFrames.nodelist_location; } #endif #ifdef USE_EINK @@ -1255,9 +1264,11 @@ void Screen::toggleFrameVisibility(const std::string &frameName) } #endif #if HAS_GPS +#ifdef USE_EINK if (frameName == "nodelist_bearings") { hiddenFrames.nodelist_bearings = !hiddenFrames.nodelist_bearings; } +#endif if (frameName == "gps") { hiddenFrames.gps = !hiddenFrames.gps; } @@ -1279,8 +1290,10 @@ void Screen::toggleFrameVisibility(const std::string &frameName) bool Screen::isFrameHidden(const std::string &frameName) const { #ifndef USE_EINK - if (frameName == "nodelist") - return hiddenFrames.nodelist; + if (frameName == "nodelist_nodes") + return hiddenFrames.nodelist_nodes; + if (frameName == "nodelist_location") + return hiddenFrames.nodelist_location; #endif #ifdef USE_EINK if (frameName == "nodelist_lastheard") @@ -1291,8 +1304,10 @@ bool Screen::isFrameHidden(const std::string &frameName) const return hiddenFrames.nodelist_distance; #endif #if HAS_GPS +#ifdef USE_EINK if (frameName == "nodelist_bearings") return hiddenFrames.nodelist_bearings; +#endif if (frameName == "gps") return hiddenFrames.gps; #endif @@ -1308,37 +1323,6 @@ bool Screen::isFrameHidden(const std::string &frameName) const return false; } -// Dismisses the currently displayed screen frame, if possible -// Relevant for text message, waypoint, others in future? -// Triggered with a CardKB keycombo -void Screen::hideCurrentFrame() -{ - uint8_t currentFrame = ui->getUiState()->currentFrame; - bool dismissed = false; - if (currentFrame == framesetInfo.positions.textMessage && devicestate.has_rx_text_message) { - LOG_INFO("Hide Text Message"); - devicestate.has_rx_text_message = false; - memset(&devicestate.rx_text_message, 0, sizeof(devicestate.rx_text_message)); - } else if (currentFrame == framesetInfo.positions.waypoint && devicestate.has_rx_waypoint) { - LOG_DEBUG("Hide Waypoint"); - devicestate.has_rx_waypoint = false; - hiddenFrames.waypoint = true; - dismissed = true; - } else if (currentFrame == framesetInfo.positions.wifi) { - LOG_DEBUG("Hide WiFi Screen"); - hiddenFrames.wifi = true; - dismissed = true; - } else if (currentFrame == framesetInfo.positions.lora) { - LOG_INFO("Hide LoRa"); - hiddenFrames.lora = true; - dismissed = true; - } - - if (dismissed) { - setFrames(FOCUS_DEFAULT); // You could also use FOCUS_PRESERVE - } -} - void Screen::handleStartFirmwareUpdateScreen() { LOG_DEBUG("Show firmware screen"); @@ -1391,28 +1375,6 @@ void Screen::decreaseBrightness() /* TO DO: add little popup in center of screen saying what brightness level it is set to*/ } -void Screen::setFunctionSymbol(std::string sym) -{ - if (std::find(functionSymbol.begin(), functionSymbol.end(), sym) == functionSymbol.end()) { - functionSymbol.push_back(sym); - functionSymbolString = ""; - for (auto symbol : functionSymbol) { - functionSymbolString = symbol + " " + functionSymbolString; - } - setFastFramerate(); - } -} - -void Screen::removeFunctionSymbol(std::string sym) -{ - functionSymbol.erase(std::remove(functionSymbol.begin(), functionSymbol.end(), sym), functionSymbol.end()); - functionSymbolString = ""; - for (auto symbol : functionSymbol) { - functionSymbolString = symbol + " " + functionSymbolString; - } - setFastFramerate(); -} - void Screen::handleOnPress() { // If screen was off, just wake it, otherwise advance to next frame @@ -1424,23 +1386,17 @@ void Screen::handleOnPress() } } -void Screen::handleShowPrevFrame() +void Screen::showFrame(FrameDirection direction) { - // If screen was off, just wake it, otherwise go back to previous frame - // If we are in a transition, the press must have bounced, drop it. + // Only advance frames when UI is stable if (ui->getUiState()->frameState == FIXED) { - ui->previousFrame(); - lastScreenTransition = millis(); - setFastFramerate(); - } -} -void Screen::handleShowNextFrame() -{ - // If screen was off, just wake it, otherwise advance to next frame - // If we are in a transition, the press must have bounced, drop it. - if (ui->getUiState()->frameState == FIXED) { - ui->nextFrame(); + if (direction == FrameDirection::NEXT) { + ui->nextFrame(); + } else { + ui->previousFrame(); + } + lastScreenTransition = millis(); setFastFramerate(); } @@ -1466,7 +1422,6 @@ void Screen::setFastFramerate() int Screen::handleStatusUpdate(const meshtastic::Status *arg) { - // LOG_DEBUG("Screen got status update %d", arg->getStatusType()); switch (arg->getStatusType()) { case STATUS_TYPE_NODE: if (showingNormalScreen && nodeStatus->getLastNumTotal() != nodeStatus->getNumTotal()) { @@ -1584,11 +1539,11 @@ int Screen::handleTextMessage(const meshtastic_MeshPacket *packet) screen->showSimpleBanner(banner, 3000); } else if (!channel.settings.has_module_settings || !channel.settings.module_settings.is_muted) { if (longName && longName[0]) { -#if defined(M5STACK_UNITC6L) - strcpy(banner, "New Message"); -#else - snprintf(banner, sizeof(banner), "New Message from\n%s", longName); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + strcpy(banner, "New Message"); + } else { + snprintf(banner, sizeof(banner), "New Message from\n%s", longName); + } } else { strcpy(banner, "New Message"); } @@ -1624,16 +1579,26 @@ int Screen::handleUIFrameEvent(const UIFrameEvent *event) if (showingNormalScreen) { // Regenerate the frameset, potentially honoring a module's internal requestFocus() call - if (event->action == UIFrameEvent::Action::REGENERATE_FRAMESET) + if (event->action == UIFrameEvent::Action::REGENERATE_FRAMESET) { setFrames(FOCUS_MODULE); + } - // Regenerate the frameset, while Attempt to maintain focus on the current frame - else if (event->action == UIFrameEvent::Action::REGENERATE_FRAMESET_BACKGROUND) + // Regenerate the frameset, while attempting to maintain focus on the current frame + else if (event->action == UIFrameEvent::Action::REGENERATE_FRAMESET_BACKGROUND) { setFrames(FOCUS_PRESERVE); + } // Don't regenerate the frameset, just re-draw whatever is on screen ASAP - else if (event->action == UIFrameEvent::Action::REDRAW_ONLY) + else if (event->action == UIFrameEvent::Action::REDRAW_ONLY) { setFastFramerate(); + } + + // Jump directly to the Text Message screen + else if (event->action == UIFrameEvent::Action::SWITCH_TO_TEXTMESSAGE) { + setFrames(FOCUS_PRESERVE); // preserve current frame ordering + ui->switchToFrame(framesetInfo.positions.textMessage); + setFastFramerate(); // force redraw ASAP + } } return 0; @@ -1671,7 +1636,48 @@ int Screen::handleInputEvent(const InputEvent *event) menuHandler::handleMenuSwitch(dispdev); return 0; } + // UP/DOWN in message screen scrolls through message threads + if (ui->getUiState()->currentFrame == framesetInfo.positions.textMessage) { + if (event->inputEvent == INPUT_BROKER_UP) { + if (messageStore.getMessages().empty()) { + cannedMessageModule->LaunchWithDestination(NODENUM_BROADCAST); + } else { + graphics::MessageRenderer::scrollUp(); + setFastFramerate(); // match existing behavior + return 0; + } + } + + if (event->inputEvent == INPUT_BROKER_DOWN) { + if (messageStore.getMessages().empty()) { + cannedMessageModule->LaunchWithDestination(NODENUM_BROADCAST); + } else { + graphics::MessageRenderer::scrollDown(); + setFastFramerate(); + return 0; + } + } + } + // UP/DOWN in node list screens scrolls through node pages + if (ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_nodes || + ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_location || + ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_lastheard || + ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_hopsignal || + ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_distance || + ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_bearings) { + if (event->inputEvent == INPUT_BROKER_UP) { + graphics::NodeListRenderer::scrollUp(); + setFastFramerate(); + return 0; + } + + if (event->inputEvent == INPUT_BROKER_DOWN) { + graphics::NodeListRenderer::scrollDown(); + setFastFramerate(); + return 0; + } + } // Use left or right input from a keyboard to move between frames, // so long as a mesh module isn't using these events for some other purpose if (showingNormalScreen) { @@ -1685,16 +1691,39 @@ int Screen::handleInputEvent(const InputEvent *event) // If no modules are using the input, move between frames if (!inputIntercepted) { +#if defined(INPUTDRIVER_ENCODER_TYPE) && INPUTDRIVER_ENCODER_TYPE == 2 + bool handledEncoderScroll = false; + const bool isTextMessageFrame = (framesetInfo.positions.textMessage != 255 && + this->ui->getUiState()->currentFrame == framesetInfo.positions.textMessage && + !messageStore.getMessages().empty()); + if (isTextMessageFrame) { + if (event->inputEvent == INPUT_BROKER_UP_LONG) { + graphics::MessageRenderer::nudgeScroll(-1); + handledEncoderScroll = true; + } else if (event->inputEvent == INPUT_BROKER_DOWN_LONG) { + graphics::MessageRenderer::nudgeScroll(1); + handledEncoderScroll = true; + } + } + + if (handledEncoderScroll) { + setFastFramerate(); + return 0; + } +#endif if (event->inputEvent == INPUT_BROKER_LEFT || event->inputEvent == INPUT_BROKER_ALT_PRESS) { - showPrevFrame(); + showFrame(FrameDirection::PREVIOUS); } else if (event->inputEvent == INPUT_BROKER_RIGHT || event->inputEvent == INPUT_BROKER_USER_PRESS) { - showNextFrame(); + showFrame(FrameDirection::NEXT); } else if (event->inputEvent == INPUT_BROKER_UP_LONG) { // Long press up button for fast frame switching showPrevFrame(); } else if (event->inputEvent == INPUT_BROKER_DOWN_LONG) { // Long press down button for fast frame switching showNextFrame(); + } else if ((event->inputEvent == INPUT_BROKER_UP || event->inputEvent == INPUT_BROKER_DOWN) && + this->ui->getUiState()->currentFrame == framesetInfo.positions.home) { + cannedMessageModule->LaunchWithDestination(NODENUM_BROADCAST); } else if (event->inputEvent == INPUT_BROKER_SELECT) { if (this->ui->getUiState()->currentFrame == framesetInfo.positions.home) { menuHandler::homeBaseMenu(); @@ -1709,20 +1738,21 @@ int Screen::handleInputEvent(const InputEvent *event) } else if (this->ui->getUiState()->currentFrame == framesetInfo.positions.lora) { menuHandler::loraMenu(); } else if (this->ui->getUiState()->currentFrame == framesetInfo.positions.textMessage) { - if (devicestate.rx_text_message.from) { + if (!messageStore.getMessages().empty()) { menuHandler::messageResponseMenu(); } else { -#if defined(M5STACK_UNITC6L) - menuHandler::textMessageMenu(); -#else - menuHandler::textMessageBaseMenu(); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + menuHandler::textMessageMenu(); + } else { + menuHandler::textMessageBaseMenu(); + } } } else if (framesetInfo.positions.firstFavorite != 255 && this->ui->getUiState()->currentFrame >= framesetInfo.positions.firstFavorite && this->ui->getUiState()->currentFrame <= framesetInfo.positions.lastFavorite) { menuHandler::favoriteBaseMenu(); - } else if (this->ui->getUiState()->currentFrame == framesetInfo.positions.nodelist || + } else if (this->ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_nodes || + this->ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_location || this->ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_lastheard || this->ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_hopsignal || this->ui->getUiState()->currentFrame == framesetInfo.positions.nodelist_distance || @@ -1733,7 +1763,7 @@ int Screen::handleInputEvent(const InputEvent *event) menuHandler::wifiBaseMenu(); } } else if (event->inputEvent == INPUT_BROKER_BACK) { - showPrevFrame(); + showFrame(FrameDirection::PREVIOUS); } else if (event->inputEvent == INPUT_BROKER_CANCEL) { setOn(false); } diff --git a/src/graphics/Screen.h b/src/graphics/Screen.h index a40579ff5..4bb808970 100644 --- a/src/graphics/Screen.h +++ b/src/graphics/Screen.h @@ -40,7 +40,6 @@ class Screen FOCUS_DEFAULT, // No specific frame FOCUS_PRESERVE, // Return to the previous frame FOCUS_FAULT, - FOCUS_TEXTMESSAGE, FOCUS_MODULE, // Note: target module should call requestFocus(), otherwise no info about which module to focus FOCUS_CLOCK, FOCUS_SYSTEM, @@ -55,8 +54,6 @@ class Screen void startFirmwareUpdateScreen() {} void increaseBrightness() {} void decreaseBrightness() {} - void setFunctionSymbol(std::string) {} - void removeFunctionSymbol(std::string) {} void startAlert(const char *) {} void showSimpleBanner(const char *message, uint32_t durationMs = 0) {} void showOverlayBanner(BannerOverlayOptions) {} @@ -172,6 +169,8 @@ class Point namespace graphics { +enum class FrameDirection { NEXT, PREVIOUS }; + // Forward declarations class Screen; @@ -211,8 +210,6 @@ class Screen : public concurrency::OSThread CallbackObserver(this, &Screen::handleStatusUpdate); CallbackObserver nodeStatusObserver = CallbackObserver(this, &Screen::handleStatusUpdate); - CallbackObserver textMessageObserver = - CallbackObserver(this, &Screen::handleTextMessage); CallbackObserver uiFrameEventObserver = CallbackObserver(this, &Screen::handleUIFrameEvent); // Sent by Mesh Modules CallbackObserver inputObserver = @@ -223,6 +220,10 @@ class Screen : public concurrency::OSThread public: OLEDDisplay *getDisplayDevice() { return dispdev; } explicit Screen(ScanI2C::DeviceAddress, meshtastic_Config_DisplayConfig_OledType, OLEDDISPLAY_GEOMETRY); + + // Screen dimension accessors + inline int getHeight() const { return displayHeight; } + inline int getWidth() const { return displayWidth; } size_t frameCount = 0; // Total number of active frames ~Screen(); @@ -231,7 +232,6 @@ class Screen : public concurrency::OSThread FOCUS_DEFAULT, // No specific frame FOCUS_PRESERVE, // Return to the previous frame FOCUS_FAULT, - FOCUS_TEXTMESSAGE, FOCUS_MODULE, // Note: target module should call requestFocus(), otherwise no info about which module to focus FOCUS_CLOCK, FOCUS_SYSTEM, @@ -279,6 +279,7 @@ class Screen : public concurrency::OSThread void onPress() { enqueueCmd(ScreenCmd{.cmd = Cmd::ON_PRESS}); } void showPrevFrame() { enqueueCmd(ScreenCmd{.cmd = Cmd::SHOW_PREV_FRAME}); } void showNextFrame() { enqueueCmd(ScreenCmd{.cmd = Cmd::SHOW_NEXT_FRAME}); } + void showFrame(FrameDirection direction); // generic alert start void startAlert(FrameCallback _alertFrame) @@ -346,9 +347,6 @@ class Screen : public concurrency::OSThread void increaseBrightness(); void decreaseBrightness(); - void setFunctionSymbol(std::string sym); - void removeFunctionSymbol(std::string sym); - /// Stops showing the boot screen. void stopBootScreen() { enqueueCmd(ScreenCmd{.cmd = Cmd::STOP_BOOT_SCREEN}); } @@ -579,7 +577,7 @@ class Screen : public concurrency::OSThread // Handle observer events int handleStatusUpdate(const meshtastic::Status *arg); - int handleTextMessage(const meshtastic_MeshPacket *arg); + int handleTextMessage(const meshtastic_MeshPacket *packet); int handleUIFrameEvent(const UIFrameEvent *arg); int handleInputEvent(const InputEvent *arg); int handleAdminMessage(AdminModule_ObserverData *arg); @@ -590,9 +588,6 @@ class Screen : public concurrency::OSThread /// Draws our SSL cert screen during boot (called from WebServer) void setSSLFrames(); - // Dismiss the currently focussed frame, if possible (e.g. text message, waypoint) - void hideCurrentFrame(); - // Menu-driven Show / Hide Toggle void toggleFrameVisibility(const std::string &frameName); bool isFrameHidden(const std::string &frameName) const; @@ -640,8 +635,6 @@ class Screen : public concurrency::OSThread // Implementations of various commands, called from doTask(). void handleSetOn(bool on, FrameCallback einkScreensaver = NULL); void handleOnPress(); - void handleShowNextFrame(); - void handleShowPrevFrame(); void handleStartFirmwareUpdateScreen(); // Info collected by setFrames method. @@ -661,7 +654,8 @@ class Screen : public concurrency::OSThread uint8_t gps = 255; uint8_t home = 255; uint8_t textMessage = 255; - uint8_t nodelist = 255; + uint8_t nodelist_nodes = 255; + uint8_t nodelist_location = 255; uint8_t nodelist_lastheard = 255; uint8_t nodelist_hopsignal = 255; uint8_t nodelist_distance = 255; @@ -684,7 +678,8 @@ class Screen : public concurrency::OSThread bool home = false; bool clock = false; #ifndef USE_EINK - bool nodelist = false; + bool nodelist_nodes = false; + bool nodelist_location = false; #endif #ifdef USE_EINK bool nodelist_lastheard = false; @@ -692,7 +687,9 @@ class Screen : public concurrency::OSThread bool nodelist_distance = false; #endif #if HAS_GPS +#ifdef USE_EINK bool nodelist_bearings = false; +#endif bool gps = false; #endif bool lora = false; diff --git a/src/graphics/SharedUIDisplay.cpp b/src/graphics/SharedUIDisplay.cpp index 892285dcb..5660810e6 100644 --- a/src/graphics/SharedUIDisplay.cpp +++ b/src/graphics/SharedUIDisplay.cpp @@ -15,27 +15,49 @@ namespace graphics { -void determineResolution(int16_t screenheight, int16_t screenwidth) +ScreenResolution determineScreenResolution(int16_t screenheight, int16_t screenwidth) { #ifdef FORCE_LOW_RES - isHighResolution = false; - return; -#endif - - if (screenwidth > 128) { - isHighResolution = true; + return ScreenResolution::Low; +#else + // Unit C6L and other ultra low res screens + if (screenwidth <= 64 || screenheight <= 48) { + return ScreenResolution::UltraLow; } + // Standard OLED screens if (screenwidth > 128 && screenheight <= 64) { - isHighResolution = false; + return ScreenResolution::Low; } + + // High Resolutions screens like T114, TDeck, TLora Pager, etc + if (screenwidth > 128) { + return ScreenResolution::High; + } + + // Default to low resolution + return ScreenResolution::Low; +#endif +} + +void decomposeTime(uint32_t rtc_sec, int &hour, int &minute, int &second) +{ + hour = 0; + minute = 0; + second = 0; + if (rtc_sec == 0) + return; + uint32_t hms = (rtc_sec % SEC_PER_DAY + SEC_PER_DAY) % SEC_PER_DAY; + hour = hms / SEC_PER_HOUR; + minute = (hms % SEC_PER_HOUR) / SEC_PER_MIN; + second = hms % SEC_PER_MIN; } // === Shared External State === bool hasUnreadMessage = false; bool isMuted = false; -bool isHighResolution = false; +ScreenResolution currentResolution = ScreenResolution::Low; // === Internal State === bool isBoltVisibleShared = true; @@ -91,7 +113,7 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti display->setColor(BLACK); display->fillRect(0, 0, screenW, highlightHeight + 2); display->setColor(WHITE); - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { display->drawLine(0, 20, screenW, 20); } else { display->drawLine(0, 14, screenW, 14); @@ -129,7 +151,7 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti } #endif - bool useHorizontalBattery = (isHighResolution && screenW >= screenH); + bool useHorizontalBattery = (currentResolution == ScreenResolution::High && screenW >= screenH); const int textY = y + (highlightHeight - FONT_HEIGHT_SMALL) / 2; int batteryX = 1; @@ -139,7 +161,7 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti if (usbPowered && !isCharging) { // This is a basic check to determine USB Powered is flagged but not charging batteryX += 1; batteryY += 2; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { display->drawXbm(batteryX, batteryY, 19, 12, imgUSB_HighResolution); batteryX += 20; // Icon + 1 pixel } else { @@ -200,8 +222,8 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti if (rtc_sec > 0) { // === Build Time String === long hms = (rtc_sec % SEC_PER_DAY + SEC_PER_DAY) % SEC_PER_DAY; - int hour = hms / SEC_PER_HOUR; - int minute = (hms % SEC_PER_HOUR) / SEC_PER_MIN; + int hour, minute, second; + graphics::decomposeTime(rtc_sec, hour, minute, second); snprintf(timeStr, sizeof(timeStr), "%d:%02d", hour, minute); // === Build Date String === @@ -209,7 +231,7 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti UIRenderer::formatDateTime(datetimeStr, sizeof(datetimeStr), rtc_sec, display, false); char dateLine[40]; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { snprintf(dateLine, sizeof(dateLine), "%s", datetimeStr); } else { if (hasUnreadMessage) { @@ -285,7 +307,7 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti display->drawXbm(iconX, iconY, mail_width, mail_height, mail); } } else if (isMuted) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { int iconX = iconRightEdge - mute_symbol_big_width; int iconY = textY + (FONT_HEIGHT_SMALL - mute_symbol_big_height) / 2; @@ -362,7 +384,7 @@ void drawCommonHeader(OLEDDisplay *display, int16_t x, int16_t y, const char *ti display->drawXbm(iconX, iconY, mail_width, mail_height, mail); } } else if (isMuted) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { int iconX = iconRightEdge - mute_symbol_big_width; int iconY = textY + (FONT_HEIGHT_SMALL - mute_symbol_big_height) / 2; display->drawXbm(iconX, iconY, mute_symbol_big_width, mute_symbol_big_height, mute_symbol_big); @@ -381,7 +403,7 @@ const int *getTextPositions(OLEDDisplay *display) { static int textPositions[7]; // Static array that persists beyond function scope - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { textPositions[0] = textZeroLine; textPositions[1] = textFirstLine_medium; textPositions[2] = textSecondLine_medium; @@ -414,8 +436,12 @@ void drawCommonFooter(OLEDDisplay *display, int16_t x, int16_t y) } if (drawConnectionState) { - if (isHighResolution) { - const int scale = 2; + const int scale = (currentResolution == ScreenResolution::High) ? 2 : 1; + display->setColor(BLACK); + display->fillRect(0, SCREEN_HEIGHT - (1 * scale) - (connection_icon_height * scale), (connection_icon_width * scale), + (connection_icon_height * scale) + (2 * scale)); + display->setColor(WHITE); + if (currentResolution == ScreenResolution::High) { const int bytesPerRow = (connection_icon_width + 7) / 8; int iconX = 0; int iconY = SCREEN_HEIGHT - (connection_icon_height * 2); diff --git a/src/graphics/SharedUIDisplay.h b/src/graphics/SharedUIDisplay.h index b51dfea36..af0d8dac1 100644 --- a/src/graphics/SharedUIDisplay.h +++ b/src/graphics/SharedUIDisplay.h @@ -42,8 +42,11 @@ namespace graphics // Shared state (declare inside namespace) extern bool hasUnreadMessage; extern bool isMuted; -extern bool isHighResolution; -void determineResolution(int16_t screenheight, int16_t screenwidth); +enum class ScreenResolution : uint8_t { UltraLow = 0, Low = 1, High = 2 }; +extern ScreenResolution currentResolution; +ScreenResolution determineScreenResolution(int16_t screenheight, int16_t screenwidth); + +void decomposeTime(uint32_t rtc_sec, int &hour, int &minute, int &second); // Rounded highlight (used for inverted headers) void drawRoundedHighlight(OLEDDisplay *display, int16_t x, int16_t y, int16_t w, int16_t h, int16_t r); diff --git a/src/graphics/VirtualKeyboard.cpp b/src/graphics/VirtualKeyboard.cpp index a332aad9a..a24f5b15c 100644 --- a/src/graphics/VirtualKeyboard.cpp +++ b/src/graphics/VirtualKeyboard.cpp @@ -354,8 +354,6 @@ void VirtualKeyboard::drawInputArea(OLEDDisplay *display, int16_t offsetX, int16 if (screenHeight <= 64) { textY = boxY + (boxHeight - inputLineH) / 2; } else { - const int innerLeft = boxX + 1; - const int innerRight = boxX + boxWidth - 2; const int innerTop = boxY + 1; const int innerBottom = boxY + boxHeight - 2; diff --git a/src/graphics/draw/ClockRenderer.cpp b/src/graphics/draw/ClockRenderer.cpp index cc6a70957..66bbe1bfe 100644 --- a/src/graphics/draw/ClockRenderer.cpp +++ b/src/graphics/draw/ClockRenderer.cpp @@ -1,15 +1,10 @@ #include "configuration.h" #if HAS_SCREEN #include "ClockRenderer.h" -#include "NodeDB.h" -#include "UIRenderer.h" -#include "configuration.h" -#include "gps/GeoCoord.h" #include "gps/RTC.h" #include "graphics/ScreenFonts.h" #include "graphics/SharedUIDisplay.h" #include "graphics/draw/UIRenderer.h" -#include "graphics/emotes.h" #include "graphics/images.h" #include "main.h" @@ -23,6 +18,31 @@ namespace graphics namespace ClockRenderer { +// Segment bitmaps for numerals 0-9 stored in flash to save RAM. +// Each row is a digit, each column is a segment state (1 = on, 0 = off). +// Segment layout reference: +// +// ___1___ +// 6 | | 2 +// |_7___| +// 5 | | 3 +// |___4_| +// +// Segment order: [1, 2, 3, 4, 5, 6, 7] +// +static const uint8_t PROGMEM digitSegments[10][7] = { + {1, 1, 1, 1, 1, 1, 0}, // 0 + {0, 1, 1, 0, 0, 0, 0}, // 1 + {1, 1, 0, 1, 1, 0, 1}, // 2 + {1, 1, 1, 1, 0, 0, 1}, // 3 + {0, 1, 1, 0, 0, 1, 1}, // 4 + {1, 0, 1, 1, 0, 1, 1}, // 5 + {1, 0, 1, 1, 1, 1, 1}, // 6 + {1, 1, 1, 0, 0, 1, 0}, // 7 + {1, 1, 1, 1, 1, 1, 1}, // 8 + {1, 1, 1, 1, 0, 1, 1} // 9 +}; + void drawSegmentedDisplayColon(OLEDDisplay *display, int x, int y, float scale) { uint16_t segmentWidth = SEGMENT_WIDTH * scale; @@ -30,7 +50,7 @@ void drawSegmentedDisplayColon(OLEDDisplay *display, int x, int y, float scale) uint16_t cellHeight = (segmentWidth * 2) + (segmentHeight * 3) + 8; - uint16_t topAndBottomX = x + (4 * scale); + uint16_t topAndBottomX = x + static_cast(4 * scale); uint16_t quarterCellHeight = cellHeight / 4; @@ -43,34 +63,16 @@ void drawSegmentedDisplayColon(OLEDDisplay *display, int x, int y, float scale) void drawSegmentedDisplayCharacter(OLEDDisplay *display, int x, int y, uint8_t number, float scale) { - // the numbers 0-9, each expressed as an array of seven boolean (0|1) values encoding the on/off state of - // segment {innerIndex + 1} - // e.g., to display the numeral '0', segments 1-6 are on, and segment 7 is off. - uint8_t numbers[10][7] = { - {1, 1, 1, 1, 1, 1, 0}, // 0 Display segment key - {0, 1, 1, 0, 0, 0, 0}, // 1 1 - {1, 1, 0, 1, 1, 0, 1}, // 2 ___ - {1, 1, 1, 1, 0, 0, 1}, // 3 6 | | 2 - {0, 1, 1, 0, 0, 1, 1}, // 4 |_7̲_| - {1, 0, 1, 1, 0, 1, 1}, // 5 5 | | 3 - {1, 0, 1, 1, 1, 1, 1}, // 6 |___| - {1, 1, 1, 0, 0, 1, 0}, // 7 - {1, 1, 1, 1, 1, 1, 1}, // 8 4 - {1, 1, 1, 1, 0, 1, 1}, // 9 - }; - - // the width and height of each segment's central rectangle: - // _____________________ - // ⋰| (only this part, |⋱ - // ⋰ | not including | ⋱ - // ⋱ | the triangles | ⋰ - // ⋱| on the ends) |⋰ - // ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + // Read 7-segment pattern for the digit from flash + uint8_t seg[7]; + for (uint8_t i = 0; i < 7; i++) { + seg[i] = pgm_read_byte(&digitSegments[number][i]); + } uint16_t segmentWidth = SEGMENT_WIDTH * scale; uint16_t segmentHeight = SEGMENT_HEIGHT * scale; - // segment x and y coordinates + // Precompute segment positions uint16_t segmentOneX = x + segmentHeight + 2; uint16_t segmentOneY = y; @@ -92,33 +94,21 @@ void drawSegmentedDisplayCharacter(OLEDDisplay *display, int x, int y, uint8_t n uint16_t segmentSevenX = segmentOneX; uint16_t segmentSevenY = segmentTwoY + segmentWidth + 2; - if (numbers[number][0]) { - graphics::ClockRenderer::drawHorizontalSegment(display, segmentOneX, segmentOneY, segmentWidth, segmentHeight); - } - - if (numbers[number][1]) { - graphics::ClockRenderer::drawVerticalSegment(display, segmentTwoX, segmentTwoY, segmentWidth, segmentHeight); - } - - if (numbers[number][2]) { - graphics::ClockRenderer::drawVerticalSegment(display, segmentThreeX, segmentThreeY, segmentWidth, segmentHeight); - } - - if (numbers[number][3]) { - graphics::ClockRenderer::drawHorizontalSegment(display, segmentFourX, segmentFourY, segmentWidth, segmentHeight); - } - - if (numbers[number][4]) { - graphics::ClockRenderer::drawVerticalSegment(display, segmentFiveX, segmentFiveY, segmentWidth, segmentHeight); - } - - if (numbers[number][5]) { - graphics::ClockRenderer::drawVerticalSegment(display, segmentSixX, segmentSixY, segmentWidth, segmentHeight); - } - - if (numbers[number][6]) { - graphics::ClockRenderer::drawHorizontalSegment(display, segmentSevenX, segmentSevenY, segmentWidth, segmentHeight); - } + // Draw only the active segments + if (seg[0]) + drawHorizontalSegment(display, segmentOneX, segmentOneY, segmentWidth, segmentHeight); + if (seg[1]) + drawVerticalSegment(display, segmentTwoX, segmentTwoY, segmentWidth, segmentHeight); + if (seg[2]) + drawVerticalSegment(display, segmentThreeX, segmentThreeY, segmentWidth, segmentHeight); + if (seg[3]) + drawHorizontalSegment(display, segmentFourX, segmentFourY, segmentWidth, segmentHeight); + if (seg[4]) + drawVerticalSegment(display, segmentFiveX, segmentFiveY, segmentWidth, segmentHeight); + if (seg[5]) + drawVerticalSegment(display, segmentSixX, segmentSixY, segmentWidth, segmentHeight); + if (seg[6]) + drawHorizontalSegment(display, segmentSevenX, segmentSevenY, segmentWidth, segmentHeight); } void drawHorizontalSegment(OLEDDisplay *display, int x, int y, int width, int height) @@ -147,42 +137,6 @@ void drawVerticalSegment(OLEDDisplay *display, int x, int y, int width, int heig display->fillTriangle(x, y + width, x + height - 1, y + width, x + halfHeight, y + width + halfHeight); } -/* -void drawWatchFaceToggleButton(OLEDDisplay *display, int16_t x, int16_t y, bool digitalMode, float scale) -{ - uint16_t segmentWidth = SEGMENT_WIDTH * scale; - uint16_t segmentHeight = SEGMENT_HEIGHT * scale; - - if (digitalMode) { - uint16_t radius = (segmentWidth + (segmentHeight * 2) + 4) / 2; - uint16_t centerX = (x + segmentHeight + 2) + (radius / 2); - uint16_t centerY = (y + segmentHeight + 2) + (radius / 2); - - display->drawCircle(centerX, centerY, radius); - display->drawCircle(centerX, centerY, radius + 1); - display->drawLine(centerX, centerY, centerX, centerY - radius + 3); - display->drawLine(centerX, centerY, centerX + radius - 3, centerY); - } else { - uint16_t segmentOneX = x + segmentHeight + 2; - uint16_t segmentOneY = y; - - uint16_t segmentTwoX = segmentOneX + segmentWidth + 2; - uint16_t segmentTwoY = segmentOneY + segmentHeight + 2; - - uint16_t segmentThreeX = segmentOneX; - uint16_t segmentThreeY = segmentTwoY + segmentWidth + 2; - - uint16_t segmentFourX = x; - uint16_t segmentFourY = y + segmentHeight + 2; - - drawHorizontalSegment(display, segmentOneX, segmentOneY, segmentWidth, segmentHeight); - drawVerticalSegment(display, segmentTwoX, segmentTwoY, segmentWidth, segmentHeight); - drawHorizontalSegment(display, segmentThreeX, segmentThreeY, segmentWidth, segmentHeight); - drawVerticalSegment(display, segmentFourX, segmentFourY, segmentWidth, segmentHeight); - } -} -*/ -// Draw a digital clock void drawDigitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { display->clear(); @@ -192,7 +146,6 @@ void drawDigitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int1 const char *titleStr = ""; // === Header === graphics::drawCommonHeader(display, x, y, titleStr, true, true); - int line = 0; uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice, true); // Display local timezone char timeString[16]; @@ -237,7 +190,7 @@ void drawDigitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int1 float target_width = display->getWidth() * screenwidth_target_ratio; float target_height = display->getHeight() - - (isHighResolution + ((currentResolution == ScreenResolution::High) ? 46 : 33); // Be careful adjusting this number, we have to account for header and the text under the time @@ -268,10 +221,9 @@ void drawDigitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int1 scaleInitialized = true; } - size_t len = strlen(timeString); - // calculate hours:minutes string width - uint16_t timeStringWidth = len * 5; // base spacing between characters + size_t len = strlen(timeString); + uint16_t timeStringWidth = len * 5; for (size_t i = 0; i < len; i++) { char character = timeString[i]; @@ -310,9 +262,16 @@ void drawDigitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int1 // draw seconds string + AM/PM display->setFont(FONT_SMALL); - int xOffset = (isHighResolution) ? 0 : -1; + int xOffset = -1; + if (currentResolution == ScreenResolution::High) { + xOffset = 0; + } if (hour >= 10) { - xOffset += (isHighResolution) ? 32 : 18; + if (currentResolution == ScreenResolution::High) { + xOffset += 32; + } else { + xOffset += 18; + } } if (config.display.use_12h_clock) { @@ -320,7 +279,7 @@ void drawDigitalClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int1 } #ifndef USE_EINK - xOffset = (isHighResolution) ? 18 : 10; + xOffset = (currentResolution == ScreenResolution::High) ? 18 : 10; if (scale >= 2.0f) { xOffset -= (int)(4.5f * scale); } @@ -339,19 +298,13 @@ void drawAnalogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 const char *titleStr = ""; // === Header === graphics::drawCommonHeader(display, x, y, titleStr, true, true); - int line = 0; // clock face center coordinates int16_t centerX = display->getWidth() / 2; int16_t centerY = display->getHeight() / 2; // clock face radius - int16_t radius = 0; - if (display->getHeight() < display->getWidth()) { - radius = (display->getHeight() / 2) * 0.9; - } else { - radius = (display->getWidth() / 2) * 0.9; - } + int16_t radius = (std::min(display->getWidth(), display->getHeight()) / 2) * 0.9; #ifdef T_WATCH_S3 radius = (display->getWidth() / 2) * 0.8; #endif @@ -366,17 +319,8 @@ void drawAnalogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 // tick mark outer y coordinate; (first nested circle) int16_t tickMarkOuterNoonY = secondHandNoonY; - // seconds tick mark inner y coordinate; (second nested circle) - double secondsTickMarkInnerNoonY = (double)noonY + 4; - if (isHighResolution) { - secondsTickMarkInnerNoonY = (double)noonY + 8; - } - - // hours tick mark inner y coordinate; (third nested circle) - double hoursTickMarkInnerNoonY = (double)noonY + 6; - if (isHighResolution) { - hoursTickMarkInnerNoonY = (double)noonY + 16; - } + double secondsTickMarkInnerNoonY = noonY + ((currentResolution == ScreenResolution::High) ? 8 : 4); + double hoursTickMarkInnerNoonY = noonY + ((currentResolution == ScreenResolution::High) ? 16 : 6); // minute hand y coordinate int16_t minuteHandNoonY = secondsTickMarkInnerNoonY + 4; @@ -386,7 +330,7 @@ void drawAnalogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 // hour hand radius and y coordinate int16_t hourHandRadius = radius * 0.35; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { hourHandRadius = radius * 0.55; } int16_t hourHandNoonY = centerY - hourHandRadius; @@ -396,19 +340,13 @@ void drawAnalogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice, true); // Display local timezone if (rtc_sec > 0) { - long hms = rtc_sec % SEC_PER_DAY; - hms = (hms + SEC_PER_DAY) % SEC_PER_DAY; + int hour, minute, second; + decomposeTime(rtc_sec, hour, minute, second); - // Tear apart hms into h:m:s - int hour = hms / SEC_PER_HOUR; - int minute = (hms % SEC_PER_HOUR) / SEC_PER_MIN; - int second = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN - - bool isPM = hour >= 12; if (config.display.use_12h_clock) { - isPM = hour >= 12; + bool isPM = hour >= 12; display->setFont(FONT_SMALL); - int yOffset = isHighResolution ? 1 : 0; + int yOffset = (currentResolution == ScreenResolution::High) ? 1 : 0; #ifdef USE_EINK yOffset += 3; #endif @@ -499,12 +437,13 @@ void drawAnalogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 display->drawStringf(hourStringX, hourStringY, buffer, "%d", hourInt); #else #ifdef USE_EINK - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { // draw hour number display->drawStringf(hourStringX, hourStringY, buffer, "%d", hourInt); } #else - if (isHighResolution && (hourInt == 3 || hourInt == 6 || hourInt == 9 || hourInt == 12)) { + if (currentResolution == ScreenResolution::High && + (hourInt == 3 || hourInt == 6 || hourInt == 9 || hourInt == 12)) { // draw hour number display->drawStringf(hourStringX, hourStringY, buffer, "%d", hourInt); } @@ -516,7 +455,7 @@ void drawAnalogClockFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 double startX = sineAngleInRadians * (secondsTickMarkInnerNoonY - centerY) + noonX; double startY = cosineAngleInRadians * (secondsTickMarkInnerNoonY - centerY) + centerY; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { // draw minute tick mark display->drawLine(startX, startY, endX, endY); } diff --git a/src/graphics/draw/CompassRenderer.cpp b/src/graphics/draw/CompassRenderer.cpp index 629949ffd..42600ce96 100644 --- a/src/graphics/draw/CompassRenderer.cpp +++ b/src/graphics/draw/CompassRenderer.cpp @@ -48,7 +48,7 @@ void drawCompassNorth(OLEDDisplay *display, int16_t compassX, int16_t compassY, // This could draw a "N" indicator or north arrow // For now, we'll draw a simple north indicator // const float radius = 17.0f; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { radius += 4; } Point north(0, -radius); @@ -59,7 +59,7 @@ void drawCompassNorth(OLEDDisplay *display, int16_t compassX, int16_t compassY, display->setFont(FONT_SMALL); display->setTextAlignment(TEXT_ALIGN_CENTER); display->setColor(BLACK); - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { display->fillRect(north.x - 8, north.y - 1, display->getStringWidth("N") + 3, FONT_HEIGHT_SMALL - 6); } else { display->fillRect(north.x - 4, north.y - 1, display->getStringWidth("N") + 2, FONT_HEIGHT_SMALL - 6); diff --git a/src/graphics/draw/DebugRenderer.cpp b/src/graphics/draw/DebugRenderer.cpp index ceb3b83f5..75b65c65f 100644 --- a/src/graphics/draw/DebugRenderer.cpp +++ b/src/graphics/draw/DebugRenderer.cpp @@ -282,13 +282,13 @@ void drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t std::string uptime = UIRenderer::drawTimeDelta(days, hours, minutes, seconds); // Line 1 (Still) -#if !defined(M5STACK_UNITC6L) - display->drawString(x + SCREEN_WIDTH - display->getStringWidth(uptime.c_str()), y, uptime.c_str()); - if (config.display.heading_bold) - display->drawString(x - 1 + SCREEN_WIDTH - display->getStringWidth(uptime.c_str()), y, uptime.c_str()); + if (currentResolution != graphics::ScreenResolution::UltraLow) { + display->drawString(x + SCREEN_WIDTH - display->getStringWidth(uptime.c_str()), y, uptime.c_str()); + if (config.display.heading_bold) + display->drawString(x - 1 + SCREEN_WIDTH - display->getStringWidth(uptime.c_str()), y, uptime.c_str()); - display->setColor(WHITE); -#endif + display->setColor(WHITE); + } // Setup string to assemble analogClock string std::string analogClock = ""; @@ -301,9 +301,8 @@ void drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t hms = (hms + SEC_PER_DAY) % SEC_PER_DAY; // Tear apart hms into h:m:s - int hour = hms / SEC_PER_HOUR; - int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN; - int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN + int hour, min, sec; + graphics::decomposeTime(rtc_sec, hour, min, sec); char timebuf[12]; @@ -379,7 +378,7 @@ void drawLoRaFocused(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int line = 1; // === Set Title - const char *titleStr = (isHighResolution) ? "LoRa Info" : "LoRa"; + const char *titleStr = (currentResolution == ScreenResolution::High) ? "LoRa Info" : "LoRa"; // === Header === graphics::drawCommonHeader(display, x, y, titleStr); @@ -391,11 +390,11 @@ void drawLoRaFocused(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, char shortnameble[35]; getMacAddr(dmac); snprintf(screen->ourId, sizeof(screen->ourId), "%02x%02x", dmac[4], dmac[5]); -#if defined(M5STACK_UNITC6L) - snprintf(shortnameble, sizeof(shortnameble), "%s", screen->ourId); -#else - snprintf(shortnameble, sizeof(shortnameble), "BLE: %s", screen->ourId); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + snprintf(shortnameble, sizeof(shortnameble), "%s", screen->ourId); + } else { + snprintf(shortnameble, sizeof(shortnameble), "BLE: %s", screen->ourId); + } int textWidth = display->getStringWidth(shortnameble); int nameX = (SCREEN_WIDTH - textWidth); display->drawString(nameX, getTextPositions(display)[line++], shortnameble); @@ -414,11 +413,11 @@ void drawLoRaFocused(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, char regionradiopreset[25]; const char *region = myRegion ? myRegion->name : NULL; if (region != nullptr) { -#if defined(M5STACK_UNITC6L) - snprintf(regionradiopreset, sizeof(regionradiopreset), "%s", region); -#else - snprintf(regionradiopreset, sizeof(regionradiopreset), "%s/%s", region, mode); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + snprintf(regionradiopreset, sizeof(regionradiopreset), "%s", region); + } else { + snprintf(regionradiopreset, sizeof(regionradiopreset), "%s/%s", region, mode); + } } textWidth = display->getStringWidth(regionradiopreset); nameX = (SCREEN_WIDTH - textWidth) / 2; @@ -430,17 +429,17 @@ void drawLoRaFocused(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, float freq = RadioLibInterface::instance->getFreq(); snprintf(freqStr, sizeof(freqStr), "%.3f", freq); if (config.lora.channel_num == 0) { -#if defined(M5STACK_UNITC6L) - snprintf(frequencyslot, sizeof(frequencyslot), "%sMHz", freqStr); -#else - snprintf(frequencyslot, sizeof(frequencyslot), "Freq: %sMHz", freqStr); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + snprintf(frequencyslot, sizeof(frequencyslot), "%sMHz", freqStr); + } else { + snprintf(frequencyslot, sizeof(frequencyslot), "Freq: %sMHz", freqStr); + } } else { -#if defined(M5STACK_UNITC6L) - snprintf(frequencyslot, sizeof(frequencyslot), "%sMHz (%d)", freqStr, config.lora.channel_num); -#else - snprintf(frequencyslot, sizeof(frequencyslot), "Freq/Ch: %sMHz (%d)", freqStr, config.lora.channel_num); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + snprintf(frequencyslot, sizeof(frequencyslot), "%sMHz (%d)", freqStr, config.lora.channel_num); + } else { + snprintf(frequencyslot, sizeof(frequencyslot), "Freq/Ch: %sMHz (%d)", freqStr, config.lora.channel_num); + } } size_t len = strlen(frequencyslot); if (len >= 4 && strcmp(frequencyslot + len - 4, " (0)") == 0) { @@ -456,12 +455,13 @@ void drawLoRaFocused(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, char chUtilPercentage[10]; snprintf(chUtilPercentage, sizeof(chUtilPercentage), "%2.0f%%", airTime->channelUtilizationPercent()); - int chUtil_x = (isHighResolution) ? display->getStringWidth(chUtil) + 10 : display->getStringWidth(chUtil) + 5; + int chUtil_x = (currentResolution == ScreenResolution::High) ? display->getStringWidth(chUtil) + 10 + : display->getStringWidth(chUtil) + 5; int chUtil_y = getTextPositions(display)[line] + 3; - int chutil_bar_width = (isHighResolution) ? 100 : 50; - int chutil_bar_height = (isHighResolution) ? 12 : 7; - int extraoffset = (isHighResolution) ? 6 : 3; + int chutil_bar_width = (currentResolution == ScreenResolution::High) ? 100 : 50; + int chutil_bar_height = (currentResolution == ScreenResolution::High) ? 12 : 7; + int extraoffset = (currentResolution == ScreenResolution::High) ? 6 : 3; int chutil_percent = airTime->channelUtilizationPercent(); int centerofscreen = SCREEN_WIDTH / 2; @@ -530,17 +530,18 @@ void drawSystemScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x int line = 1; const int barHeight = 6; const int labelX = x; - int barsOffset = (isHighResolution) ? 24 : 0; + int barsOffset = (currentResolution == ScreenResolution::High) ? 24 : 0; #ifdef USE_EINK #ifndef T_DECK_PRO barsOffset -= 12; #endif #endif -#if defined(M5STACK_UNITC6L) - const int barX = x + 45 + barsOffset; -#else - const int barX = x + 40 + barsOffset; -#endif + int barX = x + barsOffset; + if (currentResolution == ScreenResolution::UltraLow) { + barX += 45; + } else { + barX += 40; + } auto drawUsageRow = [&](const char *label, uint32_t used, uint32_t total, bool isHeap = false) { if (total == 0) return; @@ -548,7 +549,7 @@ void drawSystemScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x int percent = (used * 100) / total; char combinedStr[24]; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { snprintf(combinedStr, sizeof(combinedStr), "%s%3d%% %u/%uKB", (percent > 80) ? "! " : "", percent, used / 1024, total / 1024); } else { @@ -628,25 +629,33 @@ void drawSystemScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x line += 1; char appversionstr[35]; - snprintf(appversionstr, sizeof(appversionstr), "Ver: %s", optstr(APP_VERSION)); char appversionstr_formatted[40]; - char *lastDot = strrchr(appversionstr, '.'); -#if defined(M5STACK_UNITC6L) - if (lastDot != nullptr) { - *lastDot = '\0'; // truncate string + + const char *ver = optstr(APP_VERSION); + char verbuf[32]; + strncpy(verbuf, ver, sizeof(verbuf) - 1); + verbuf[sizeof(verbuf) - 1] = '\0'; + + char *lastDot = strrchr(verbuf, '.'); + + if (currentResolution == ScreenResolution::UltraLow) { + if (lastDot != nullptr) { + *lastDot = '\0'; + } + snprintf(appversionstr, sizeof(appversionstr), "Ver: %s", verbuf); + } else { + if (lastDot) { + size_t prefixLen = (size_t)(lastDot - verbuf); + snprintf(appversionstr_formatted, sizeof(appversionstr_formatted), "Ver: %.*s", (int)prefixLen, verbuf); + strncat(appversionstr_formatted, " (", sizeof(appversionstr_formatted) - strlen(appversionstr_formatted) - 1); + strncat(appversionstr_formatted, lastDot + 1, sizeof(appversionstr_formatted) - strlen(appversionstr_formatted) - 1); + strncat(appversionstr_formatted, ")", sizeof(appversionstr_formatted) - strlen(appversionstr_formatted) - 1); + strncpy(appversionstr, appversionstr_formatted, sizeof(appversionstr) - 1); + appversionstr[sizeof(appversionstr) - 1] = '\0'; + } else { + snprintf(appversionstr, sizeof(appversionstr), "Ver: %s", verbuf); + } } -#else - if (lastDot) { - size_t prefixLen = lastDot - appversionstr; - strncpy(appversionstr_formatted, appversionstr, prefixLen); - appversionstr_formatted[prefixLen] = '\0'; - strncat(appversionstr_formatted, " (", sizeof(appversionstr_formatted) - strlen(appversionstr_formatted) - 1); - strncat(appversionstr_formatted, lastDot + 1, sizeof(appversionstr_formatted) - strlen(appversionstr_formatted) - 1); - strncat(appversionstr_formatted, ")", sizeof(appversionstr_formatted) - strlen(appversionstr_formatted) - 1); - strncpy(appversionstr, appversionstr_formatted, sizeof(appversionstr) - 1); - appversionstr[sizeof(appversionstr) - 1] = '\0'; - } -#endif int textWidth = display->getStringWidth(appversionstr); int nameX = (SCREEN_WIDTH - textWidth) / 2; @@ -665,7 +674,7 @@ void drawSystemScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x const char *clientWord = nullptr; // Determine if narrow or wide screen - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { clientWord = "Client"; } else { clientWord = "App"; @@ -706,11 +715,23 @@ void drawChirpy(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int1 int iconX = SCREEN_WIDTH - chirpy_width - (chirpy_width / 3); int iconY = (SCREEN_HEIGHT - chirpy_height) / 2; int textX_offset = 10; - if (isHighResolution) { - iconX = SCREEN_WIDTH - chirpy_width_hirez - (chirpy_width_hirez / 3); - iconY = (SCREEN_HEIGHT - chirpy_height_hirez) / 2; + if (currentResolution == ScreenResolution::High) { textX_offset = textX_offset * 4; - display->drawXbm(iconX, iconY, chirpy_width_hirez, chirpy_height_hirez, chirpy_hirez); + const int scale = 2; + const int bytesPerRow = (chirpy_width + 7) / 8; + + for (int yy = 0; yy < chirpy_height; ++yy) { + iconX = SCREEN_WIDTH - (chirpy_width * 2) - ((chirpy_width * 2) / 3); + iconY = (SCREEN_HEIGHT - (chirpy_height * 2)) / 2; + const uint8_t *rowPtr = chirpy + yy * bytesPerRow; + for (int xx = 0; xx < chirpy_width; ++xx) { + const uint8_t byteVal = pgm_read_byte(rowPtr + (xx >> 3)); + const uint8_t bitMask = 1U << (xx & 7); // XBM is LSB-first + if (byteVal & bitMask) { + display->fillRect(iconX + xx * scale, iconY + yy * scale, scale, scale); + } + } + } } else { display->drawXbm(iconX, iconY, chirpy_width, chirpy_height, chirpy); } diff --git a/src/graphics/draw/DrawRenderers.h b/src/graphics/draw/DrawRenderers.h index 6f1929ebd..c55e66ede 100644 --- a/src/graphics/draw/DrawRenderers.h +++ b/src/graphics/draw/DrawRenderers.h @@ -11,7 +11,6 @@ #include "graphics/draw/CompassRenderer.h" #include "graphics/draw/DebugRenderer.h" #include "graphics/draw/NodeListRenderer.h" -#include "graphics/draw/ScreenRenderer.h" #include "graphics/draw/UIRenderer.h" namespace graphics @@ -30,8 +29,6 @@ using namespace ClockRenderer; using namespace CompassRenderer; using namespace DebugRenderer; using namespace NodeListRenderer; -using namespace ScreenRenderer; -using namespace UIRenderer; } // namespace DrawRenderers diff --git a/src/graphics/draw/MenuHandler.cpp b/src/graphics/draw/MenuHandler.cpp index 586bdd4a6..ac877e150 100644 --- a/src/graphics/draw/MenuHandler.cpp +++ b/src/graphics/draw/MenuHandler.cpp @@ -1,14 +1,17 @@ #include "configuration.h" #if HAS_SCREEN #include "ClockRenderer.h" +#include "Default.h" #include "GPS.h" #include "MenuHandler.h" #include "MeshRadio.h" #include "MeshService.h" +#include "MessageStore.h" #include "NodeDB.h" #include "buzz.h" #include "graphics/Screen.h" #include "graphics/SharedUIDisplay.h" +#include "graphics/draw/MessageRenderer.h" #include "graphics/draw/UIRenderer.h" #include "input/RotaryEncoderInterruptImpl1.h" #include "input/UpDownInterruptImpl1.h" @@ -134,11 +137,10 @@ void menuHandler::LoraRegionPicker(uint32_t duration) "NP_865", "BR_902"}; BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "LoRa Region"; -#else bannerOptions.message = "Set the LoRa region"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "LoRa Region"; + } bannerOptions.durationMs = duration; bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsCount = 27; @@ -426,60 +428,415 @@ void menuHandler::clockMenu() }; screen->showOverlayBanner(bannerOptions); } - void menuHandler::messageResponseMenu() { - enum optionsNumbers { Back = 0, Dismiss = 1, Preset = 2, Freetext = 3, Aloud = 4, enumEnd = 5 }; -#if defined(M5STACK_UNITC6L) - static const char *optionsArray[enumEnd] = {"Back", "Dismiss", "Reply Preset"}; -#else - static const char *optionsArray[enumEnd] = {"Back", "Dismiss", "Reply via Preset"}; -#endif - static int optionsEnumArray[enumEnd] = {Back, Dismiss, Preset}; - int options = 3; + enum optionsNumbers { Back = 0, ViewMode, DeleteAll, DeleteOldest, ReplyMenu, Aloud, enumEnd }; - if (kb_found) { - optionsArray[options] = "Reply via Freetext"; - optionsEnumArray[options++] = Freetext; - } + static const char *optionsArray[enumEnd]; + static int optionsEnumArray[enumEnd]; + int options = 0; + + auto mode = graphics::MessageRenderer::getThreadMode(); + + optionsArray[options] = "Back"; + optionsEnumArray[options++] = Back; + + // New Reply submenu (replaces Preset and Freetext directly in this menu) + optionsArray[options] = "Reply"; + optionsEnumArray[options++] = ReplyMenu; + + optionsArray[options] = "View Chats"; + optionsEnumArray[options++] = ViewMode; + + // Delete submenu + optionsArray[options] = "Delete"; + optionsEnumArray[options++] = 900; #ifdef HAS_I2S optionsArray[options] = "Read Aloud"; optionsEnumArray[options++] = Aloud; #endif + BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Message"; -#else - bannerOptions.message = "Message Action"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Message"; + } else { + bannerOptions.message = "Message Action"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsEnumPtr = optionsEnumArray; bannerOptions.optionsCount = options; bannerOptions.bannerCallback = [](int selected) -> void { - if (selected == Dismiss) { - screen->hideCurrentFrame(); - } else if (selected == Preset) { - if (devicestate.rx_text_message.to == NODENUM_BROADCAST) { - cannedMessageModule->LaunchWithDestination(NODENUM_BROADCAST, devicestate.rx_text_message.channel); - } else { - cannedMessageModule->LaunchWithDestination(devicestate.rx_text_message.from); + LOG_DEBUG("messageResponseMenu: selected %d", selected); + + auto mode = graphics::MessageRenderer::getThreadMode(); + int ch = graphics::MessageRenderer::getThreadChannel(); + uint32_t peer = graphics::MessageRenderer::getThreadPeer(); + + LOG_DEBUG("[ReplyCtx] mode=%d ch=%d peer=0x%08x", (int)mode, ch, (unsigned int)peer); + + if (selected == ViewMode) { + menuHandler::menuQueue = menuHandler::message_viewmode_menu; + screen->runNow(); + + // Reply submenu + } else if (selected == ReplyMenu) { + menuHandler::menuQueue = menuHandler::reply_menu; + screen->runNow(); + + // Delete submenu + } else if (selected == 900) { + menuHandler::menuQueue = menuHandler::delete_messages_menu; + screen->runNow(); + + // Delete oldest FIRST (only change) + } else if (selected == DeleteOldest) { + auto mode = graphics::MessageRenderer::getThreadMode(); + int ch = graphics::MessageRenderer::getThreadChannel(); + uint32_t peer = graphics::MessageRenderer::getThreadPeer(); + + if (mode == graphics::MessageRenderer::ThreadMode::ALL) { + // Global oldest + messageStore.deleteOldestMessage(); + } else if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) { + // Oldest in current channel + messageStore.deleteOldestMessageInChannel(ch); + } else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + // Oldest in current DM + messageStore.deleteOldestMessageWithPeer(peer); } - } else if (selected == Freetext) { - if (devicestate.rx_text_message.to == NODENUM_BROADCAST) { - cannedMessageModule->LaunchFreetextWithDestination(NODENUM_BROADCAST, devicestate.rx_text_message.channel); - } else { - cannedMessageModule->LaunchFreetextWithDestination(devicestate.rx_text_message.from); - } - } + + // Delete all messages + } else if (selected == DeleteAll) { + messageStore.clearAllMessages(); + graphics::MessageRenderer::clearThreadRegistries(); + graphics::MessageRenderer::clearMessageCache(); + #ifdef HAS_I2S - else if (selected == Aloud) { + } else if (selected == Aloud) { const meshtastic_MeshPacket &mp = devicestate.rx_text_message; const char *msg = reinterpret_cast(mp.decoded.payload.bytes); - audioThread->readAloud(msg); - } #endif + } + }; + screen->showOverlayBanner(bannerOptions); +} + +void menuHandler::replyMenu() +{ + enum replyOptions { Back = 0, ReplyPreset, ReplyFreetext, enumEnd }; + + static const char *optionsArray[enumEnd]; + static int optionsEnumArray[enumEnd]; + int options = 0; + + // Back + optionsArray[options] = "Back"; + optionsEnumArray[options++] = Back; + + // Preset reply + optionsArray[options] = "With Preset"; + optionsEnumArray[options++] = ReplyPreset; + + // Freetext reply (only when keyboard exists) + if (kb_found) { + optionsArray[options] = "With Freetext"; + optionsEnumArray[options++] = ReplyFreetext; + } + + BannerOverlayOptions bannerOptions; + + // Dynamic title based on thread mode + auto mode = graphics::MessageRenderer::getThreadMode(); + if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) { + bannerOptions.message = "Reply to Channel"; + } else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + bannerOptions.message = "Reply to DM"; + } else { + // View All + bannerOptions.message = "Reply to Last Msg"; + } + + bannerOptions.optionsArrayPtr = optionsArray; + bannerOptions.optionsEnumPtr = optionsEnumArray; + bannerOptions.optionsCount = options; + bannerOptions.InitialSelected = 1; + + bannerOptions.bannerCallback = [](int selected) -> void { + auto mode = graphics::MessageRenderer::getThreadMode(); + int ch = graphics::MessageRenderer::getThreadChannel(); + uint32_t peer = graphics::MessageRenderer::getThreadPeer(); + + if (selected == Back) { + menuHandler::menuQueue = menuHandler::message_response_menu; + screen->runNow(); + return; + } + + // Preset reply + if (selected == ReplyPreset) { + + if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) { + cannedMessageModule->LaunchWithDestination(NODENUM_BROADCAST, ch); + + } else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + cannedMessageModule->LaunchWithDestination(peer); + + } else { + // Fallback for last received message + if (devicestate.rx_text_message.to == NODENUM_BROADCAST) { + cannedMessageModule->LaunchWithDestination(NODENUM_BROADCAST, devicestate.rx_text_message.channel); + } else { + cannedMessageModule->LaunchWithDestination(devicestate.rx_text_message.from); + } + } + + return; + } + + // Freetext reply + if (selected == ReplyFreetext) { + + if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) { + cannedMessageModule->LaunchFreetextWithDestination(NODENUM_BROADCAST, ch); + + } else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + cannedMessageModule->LaunchFreetextWithDestination(peer); + + } else { + // Fallback for last received message + if (devicestate.rx_text_message.to == NODENUM_BROADCAST) { + cannedMessageModule->LaunchFreetextWithDestination(NODENUM_BROADCAST, devicestate.rx_text_message.channel); + } else { + cannedMessageModule->LaunchFreetextWithDestination(devicestate.rx_text_message.from); + } + } + + return; + } + }; + screen->showOverlayBanner(bannerOptions); +} +void menuHandler::deleteMessagesMenu() +{ + enum optionsNumbers { Back = 0, DeleteOldest, DeleteThis, DeleteAll, enumEnd }; + + static const char *optionsArray[enumEnd]; + static int optionsEnumArray[enumEnd]; + int options = 0; + + auto mode = graphics::MessageRenderer::getThreadMode(); + + optionsArray[options] = "Back"; + optionsEnumArray[options++] = Back; + + optionsArray[options] = "Delete Oldest"; + optionsEnumArray[options++] = DeleteOldest; + + // If viewing ALL chats → hide “Delete This Chat” + if (mode != graphics::MessageRenderer::ThreadMode::ALL) { + optionsArray[options] = "Delete This Chat"; + optionsEnumArray[options++] = DeleteThis; + } + if (currentResolution == ScreenResolution::UltraLow) { + optionsArray[options] = "Delete All"; + } else { + optionsArray[options] = "Delete All Chats"; + } + optionsEnumArray[options++] = DeleteAll; + + BannerOverlayOptions bannerOptions; + bannerOptions.message = "Delete Messages"; + + bannerOptions.optionsArrayPtr = optionsArray; + bannerOptions.optionsEnumPtr = optionsEnumArray; + bannerOptions.optionsCount = options; + bannerOptions.bannerCallback = [mode](int selected) -> void { + int ch = graphics::MessageRenderer::getThreadChannel(); + uint32_t peer = graphics::MessageRenderer::getThreadPeer(); + + if (selected == Back) { + menuHandler::menuQueue = menuHandler::message_response_menu; + screen->runNow(); + return; + } + + if (selected == DeleteAll) { + LOG_INFO("Deleting all messages"); + messageStore.clearAllMessages(); + graphics::MessageRenderer::clearThreadRegistries(); + graphics::MessageRenderer::clearMessageCache(); + return; + } + + if (selected == DeleteOldest) { + LOG_INFO("Deleting oldest message"); + + if (mode == graphics::MessageRenderer::ThreadMode::ALL) { + messageStore.deleteOldestMessage(); + } else if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) { + messageStore.deleteOldestMessageInChannel(ch); + } else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + messageStore.deleteOldestMessageWithPeer(peer); + } + + return; + } + + // This only appears in non-ALL modes + if (selected == DeleteThis) { + LOG_INFO("Deleting all messages in this thread"); + + if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) { + messageStore.deleteAllMessagesInChannel(ch); + } else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + messageStore.deleteAllMessagesWithPeer(peer); + } + + return; + } + }; + + screen->showOverlayBanner(bannerOptions); +} + +void menuHandler::messageViewModeMenu() +{ + auto encodeChannelId = [](int ch) -> int { return 100 + ch; }; + auto isChannelSel = [](int id) -> bool { return id >= 100 && id < 200; }; + + static std::vector labels; + static std::vector ids; + static std::vector idToPeer; // DM lookup + + labels.clear(); + ids.clear(); + idToPeer.clear(); + + labels.push_back("Back"); + ids.push_back(-1); + labels.push_back("View All Chats"); + ids.push_back(-2); + + // Channels with messages + for (int ch = 0; ch < 8; ++ch) { + auto msgs = messageStore.getChannelMessages((uint8_t)ch); + if (!msgs.empty()) { + char buf[40]; + const char *cname = channels.getName(ch); + snprintf(buf, sizeof(buf), cname && cname[0] ? "#%s" : "#Ch%d", cname ? cname : "", ch); + labels.push_back(buf); + ids.push_back(encodeChannelId(ch)); + LOG_DEBUG("messageViewModeMenu: Added live channel %s (id=%d)", buf, encodeChannelId(ch)); + } + } + + // Registry channels + for (int ch : graphics::MessageRenderer::getSeenChannels()) { + if (ch < 0 || ch >= 8) + continue; + auto msgs = messageStore.getChannelMessages((uint8_t)ch); + if (msgs.empty()) + continue; + int enc = encodeChannelId(ch); + if (std::find(ids.begin(), ids.end(), enc) == ids.end()) { + char buf[40]; + const char *cname = channels.getName(ch); + snprintf(buf, sizeof(buf), cname && cname[0] ? "#%s" : "#Ch%d", cname ? cname : "", ch); + labels.push_back(buf); + ids.push_back(enc); + LOG_DEBUG("messageViewModeMenu: Added registry channel %s (id=%d)", buf, enc); + } + } + + // Gather unique peers + auto dms = messageStore.getDirectMessages(); + std::vector uniquePeers; + for (auto &m : dms) { + uint32_t peer = (m.sender == nodeDB->getNodeNum()) ? m.dest : m.sender; + if (peer != nodeDB->getNodeNum() && std::find(uniquePeers.begin(), uniquePeers.end(), peer) == uniquePeers.end()) + uniquePeers.push_back(peer); + } + for (uint32_t peer : graphics::MessageRenderer::getSeenPeers()) { + if (peer != nodeDB->getNodeNum() && std::find(uniquePeers.begin(), uniquePeers.end(), peer) == uniquePeers.end()) + uniquePeers.push_back(peer); + } + std::sort(uniquePeers.begin(), uniquePeers.end()); + + // Encode peers + for (size_t i = 0; i < uniquePeers.size(); ++i) { + uint32_t peer = uniquePeers[i]; + auto node = nodeDB->getMeshNode(peer); + std::string name; + if (node && node->has_user) + name = sanitizeString(node->user.long_name).substr(0, 15); + else { + char buf[20]; + snprintf(buf, sizeof(buf), "Node %08X", peer); + name = buf; + } + labels.push_back("@" + name); + int encPeer = 1000 + (int)idToPeer.size(); + ids.push_back(encPeer); + idToPeer.push_back(peer); + LOG_DEBUG("messageViewModeMenu: Added DM %s peer=0x%08x id=%d", name.c_str(), (unsigned int)peer, encPeer); + } + + // Active ID + int activeId = -2; + auto mode = graphics::MessageRenderer::getThreadMode(); + if (mode == graphics::MessageRenderer::ThreadMode::CHANNEL) + activeId = encodeChannelId(graphics::MessageRenderer::getThreadChannel()); + else if (mode == graphics::MessageRenderer::ThreadMode::DIRECT) { + uint32_t cur = graphics::MessageRenderer::getThreadPeer(); + for (size_t i = 0; i < idToPeer.size(); ++i) + if (idToPeer[i] == cur) { + activeId = 1000 + (int)i; + break; + } + } + + LOG_DEBUG("messageViewModeMenu: Active thread id=%d", activeId); + + // Build banner + static std::vector options; + static std::vector optionIds; + options.clear(); + optionIds.clear(); + + int initialIndex = 0; + for (size_t i = 0; i < labels.size(); i++) { + options.push_back(labels[i].c_str()); + optionIds.push_back(ids[i]); + if (ids[i] == activeId) + initialIndex = (int)i; + } + + BannerOverlayOptions bannerOptions; + bannerOptions.message = "Select Conversation"; + bannerOptions.optionsArrayPtr = options.data(); + bannerOptions.optionsEnumPtr = optionIds.data(); + bannerOptions.optionsCount = options.size(); + bannerOptions.InitialSelected = initialIndex; + + bannerOptions.bannerCallback = [=](int selected) -> void { + LOG_DEBUG("messageViewModeMenu: selected=%d", selected); + if (selected == -1) { + menuHandler::menuQueue = menuHandler::message_response_menu; + screen->runNow(); + } else if (selected == -2) { + graphics::MessageRenderer::setThreadMode(graphics::MessageRenderer::ThreadMode::ALL); + } else if (isChannelSel(selected)) { + int ch = selected - 100; + graphics::MessageRenderer::setThreadMode(graphics::MessageRenderer::ThreadMode::CHANNEL, ch); + } else if (selected >= 1000) { + int idx = selected - 1000; + if (idx >= 0 && (size_t)idx < idToPeer.size()) { + uint32_t peer = idToPeer[idx]; + graphics::MessageRenderer::setThreadMode(graphics::MessageRenderer::ThreadMode::DIRECT, -1, peer); + } + } }; screen->showOverlayBanner(bannerOptions); } @@ -505,23 +862,12 @@ void menuHandler::homeBaseMenu() optionsArray[options] = "Send Node Info"; } optionsEnumArray[options++] = Position; -#if defined(M5STACK_UNITC6L) - optionsArray[options] = "New Preset"; -#else - optionsArray[options] = "New Preset Msg"; -#endif - optionsEnumArray[options++] = Preset; - if (kb_found) { - optionsArray[options] = "New Freetext Msg"; - optionsEnumArray[options++] = Freetext; - } BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Home"; -#else bannerOptions.message = "Home Action"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Home"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsEnumPtr = optionsEnumArray; bannerOptions.optionsCount = options; @@ -606,21 +952,22 @@ void menuHandler::systemBaseMenu() optionsArray[options] = "Display Options"; optionsEnumArray[options++] = ScreenOptions; -#if defined(M5STACK_UNITC6L) - optionsArray[options] = "Bluetooth"; -#else - optionsArray[options] = "Bluetooth Toggle"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + optionsArray[options] = "Bluetooth"; + } else { + optionsArray[options] = "Bluetooth Toggle"; + } optionsEnumArray[options++] = Bluetooth; #if HAS_WIFI && !defined(ARCH_PORTDUINO) optionsArray[options] = "WiFi Toggle"; optionsEnumArray[options++] = WiFiToggle; #endif -#if defined(M5STACK_UNITC6L) - optionsArray[options] = "Power"; -#else - optionsArray[options] = "Reboot/Shutdown"; -#endif + + if (currentResolution == ScreenResolution::UltraLow) { + optionsArray[options] = "Power"; + } else { + optionsArray[options] = "Reboot/Shutdown"; + } optionsEnumArray[options++] = PowerMenu; if (test_enabled) { @@ -629,11 +976,10 @@ void menuHandler::systemBaseMenu() } BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "System"; -#else bannerOptions.message = "System Action"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "System"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsCount = options; bannerOptions.optionsEnumPtr = optionsEnumArray; @@ -670,32 +1016,49 @@ void menuHandler::systemBaseMenu() void menuHandler::favoriteBaseMenu() { - enum optionsNumbers { Back, Preset, Freetext, Remove, TraceRoute, enumEnd }; -#if defined(M5STACK_UNITC6L) - static const char *optionsArray[enumEnd] = {"Back", "New Preset"}; -#else - static const char *optionsArray[enumEnd] = {"Back", "New Preset Msg"}; -#endif - static int optionsEnumArray[enumEnd] = {Back, Preset}; - int options = 2; + enum optionsNumbers { Back, Preset, Freetext, GoToChat, Remove, TraceRoute, enumEnd }; + + static const char *optionsArray[enumEnd] = {"Back"}; + static int optionsEnumArray[enumEnd] = {Back}; + int options = 1; + + // Only show "View Conversation" if a message exists with this node + uint32_t peer = graphics::UIRenderer::currentFavoriteNodeNum; + bool hasConversation = false; + for (const auto &m : messageStore.getMessages()) { + if ((m.sender == peer || m.dest == peer)) { + hasConversation = true; + break; + } + } + if (hasConversation) { + optionsArray[options] = "Go To Chat"; + optionsEnumArray[options++] = GoToChat; + } + if (currentResolution == ScreenResolution::UltraLow) { + optionsArray[options] = "New Preset"; + } else { + optionsArray[options] = "New Preset Msg"; + } + optionsEnumArray[options++] = Preset; if (kb_found) { optionsArray[options] = "New Freetext Msg"; optionsEnumArray[options++] = Freetext; } -#if !defined(M5STACK_UNITC6L) - optionsArray[options] = "Trace Route"; - optionsEnumArray[options++] = TraceRoute; -#endif + + if (currentResolution != ScreenResolution::UltraLow) { + optionsArray[options] = "Trace Route"; + optionsEnumArray[options++] = TraceRoute; + } optionsArray[options] = "Remove Favorite"; optionsEnumArray[options++] = Remove; BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Favorites"; -#else bannerOptions.message = "Favorites Action"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Favorites"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsEnumPtr = optionsEnumArray; bannerOptions.optionsCount = options; @@ -704,6 +1067,17 @@ void menuHandler::favoriteBaseMenu() cannedMessageModule->LaunchWithDestination(graphics::UIRenderer::currentFavoriteNodeNum); } else if (selected == Freetext) { cannedMessageModule->LaunchFreetextWithDestination(graphics::UIRenderer::currentFavoriteNodeNum); + } + // Handle new Go To Thread action + else if (selected == GoToChat) { + // Switch thread to direct conversation with this node + graphics::MessageRenderer::setThreadMode(graphics::MessageRenderer::ThreadMode::DIRECT, -1, + graphics::UIRenderer::currentFavoriteNodeNum); + + // Manually create and send a UIFrameEvent to trigger the jump + UIFrameEvent evt; + evt.action = UIFrameEvent::Action::SWITCH_TO_TEXTMESSAGE; + screen->handleUIFrameEvent(&evt); } else if (selected == Remove) { menuHandler::menuQueue = menuHandler::remove_favorite; screen->runNow(); @@ -753,20 +1127,33 @@ void menuHandler::positionBaseMenu() void menuHandler::nodeListMenu() { - enum optionsNumbers { Back, Favorite, TraceRoute, Verify, Reset, enumEnd }; -#if defined(M5STACK_UNITC6L) - static const char *optionsArray[] = {"Back", "Add Favorite", "Reset Node"}; -#else - static const char *optionsArray[] = {"Back", "Add Favorite", "Trace Route", "Key Verification", "Reset NodeDB"}; -#endif + enum optionsNumbers { Back, Favorite, TraceRoute, Verify, Reset, NodeNameLength, enumEnd }; + static const char *optionsArray[enumEnd] = {"Back"}; + static int optionsEnumArray[enumEnd] = {Back}; + int options = 1; + + optionsArray[options] = "Add Favorite"; + optionsEnumArray[options++] = Favorite; + optionsArray[options] = "Trace Route"; + optionsEnumArray[options++] = TraceRoute; + + if (currentResolution != ScreenResolution::UltraLow) { + optionsArray[options] = "Key Verification"; + optionsEnumArray[options++] = Verify; + } + + if (currentResolution != ScreenResolution::UltraLow) { + optionsArray[options] = "Show Long/Short Name"; + optionsEnumArray[options++] = NodeNameLength; + } + optionsArray[options] = "Reset NodeDB"; + optionsEnumArray[options++] = Reset; + BannerOverlayOptions bannerOptions; bannerOptions.message = "Node Action"; bannerOptions.optionsArrayPtr = optionsArray; -#if defined(M5STACK_UNITC6L) - bannerOptions.optionsCount = 3; -#else - bannerOptions.optionsCount = 5; -#endif + bannerOptions.optionsCount = options; + bannerOptions.optionsEnumPtr = optionsEnumArray; bannerOptions.bannerCallback = [](int selected) -> void { if (selected == Favorite) { menuQueue = add_favorite; @@ -780,6 +1167,9 @@ void menuHandler::nodeListMenu() } else if (selected == TraceRoute) { menuQueue = trace_route_menu; screen->runNow(); + } else if (selected == NodeNameLength) { + menuHandler::menuQueue = menuHandler::node_name_length_menu; + screen->runNow(); } }; screen->showOverlayBanner(bannerOptions); @@ -803,7 +1193,7 @@ void menuHandler::nodeNameLengthMenu() LOG_INFO("Setting names to short"); config.display.use_long_node_name = false; } else if (selected == Back) { - menuQueue = screen_options_menu; + menuQueue = node_base_menu; screen->runNow(); } }; @@ -831,6 +1221,9 @@ void menuHandler::resetNodeDBMenu() LOG_INFO("Initiate node-db reset but keeping favorites"); nodeDB->resetNodes(1); rebootAtMsec = (millis() + DEFAULT_REBOOT_SECONDS * 1000); + } else if (selected == 0) { + menuQueue = node_base_menu; + screen->runNow(); } }; screen->showOverlayBanner(bannerOptions); @@ -904,13 +1297,14 @@ void menuHandler::GPSFormatMenu() { static const char *optionsArray[] = {"Back", - isHighResolution ? "Decimal Degrees" : "DEC", - isHighResolution ? "Degrees Minutes Seconds" : "DMS", - isHighResolution ? "Universal Transverse Mercator" : "UTM", - isHighResolution ? "Military Grid Reference System" : "MGRS", - isHighResolution ? "Open Location Code" : "OLC", - isHighResolution ? "Ordnance Survey Grid Ref" : "OSGR", - isHighResolution ? "Maidenhead Locator" : "MLS"}; + (currentResolution == ScreenResolution::High) ? "Decimal Degrees" : "DEC", + (currentResolution == ScreenResolution::High) ? "Degrees Minutes Seconds" : "DMS", + (currentResolution == ScreenResolution::High) ? "Universal Transverse Mercator" : "UTM", + (currentResolution == ScreenResolution::High) ? "Military Grid Reference System" + : "MGRS", + (currentResolution == ScreenResolution::High) ? "Open Location Code" : "OLC", + (currentResolution == ScreenResolution::High) ? "Ordnance Survey Grid Ref" : "OSGR", + (currentResolution == ScreenResolution::High) ? "Maidenhead Locator" : "MLS"}; BannerOverlayOptions bannerOptions; bannerOptions.message = "GPS Format"; bannerOptions.optionsArrayPtr = optionsArray; @@ -958,11 +1352,10 @@ void menuHandler::BluetoothToggleMenu() { static const char *optionsArray[] = {"Back", "Enabled", "Disabled"}; BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Bluetooth"; -#else bannerOptions.message = "Toggle Bluetooth"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Bluetooth"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsCount = 3; bannerOptions.bannerCallback = [](int selected) -> void { @@ -1178,17 +1571,17 @@ void menuHandler::rebootMenu() { static const char *optionsArray[] = {"Back", "Confirm"}; BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Reboot"; -#else bannerOptions.message = "Reboot Device?"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Reboot"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsCount = 2; bannerOptions.bannerCallback = [](int selected) -> void { if (selected == 1) { IF_SCREEN(screen->showSimpleBanner("Rebooting...", 0)); nodeDB->saveToDisk(); + messageStore.saveToFlash(); rebootAtMsec = millis() + DEFAULT_REBOOT_SECONDS * 1000; } else { menuQueue = power_menu; @@ -1202,11 +1595,10 @@ void menuHandler::shutdownMenu() { static const char *optionsArray[] = {"Back", "Confirm"}; BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Shutdown"; -#else bannerOptions.message = "Shutdown Device?"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Shutdown"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsCount = 2; bannerOptions.bannerCallback = [](int selected) -> void { @@ -1223,12 +1615,13 @@ void menuHandler::shutdownMenu() void menuHandler::addFavoriteMenu() { -#if defined(M5STACK_UNITC6L) - screen->showNodePicker("Node Favorite", 30000, [](uint32_t nodenum) -> void { -#else - screen->showNodePicker("Node To Favorite", 30000, [](uint32_t nodenum) -> void { - -#endif + const char *NODE_PICKER_TITLE; + if (currentResolution == ScreenResolution::UltraLow) { + NODE_PICKER_TITLE = "Node Favorite"; + } else { + NODE_PICKER_TITLE = "Node To Favorite"; + } + screen->showNodePicker(NODE_PICKER_TITLE, 30000, [](uint32_t nodenum) -> void { LOG_WARN("Nodenum: %u", nodenum); nodeDB->set_favorite(true, nodenum); screen->setFrames(graphics::Screen::FOCUS_PRESERVE); @@ -1393,16 +1786,11 @@ void menuHandler::screenOptionsMenu() hasSupportBrightness = false; #endif - enum optionsNumbers { Back, NodeNameLength, Brightness, ScreenColor, FrameToggles, DisplayUnits }; + enum optionsNumbers { Back, Brightness, ScreenColor, FrameToggles, DisplayUnits }; static const char *optionsArray[5] = {"Back"}; static int optionsEnumArray[5] = {Back}; int options = 1; -#if defined(T_DECK) || defined(T_LORA_PAGER) || defined(HACKADAY_COMMUNICATOR) - optionsArray[options] = "Show Long/Short Name"; - optionsEnumArray[options++] = NodeNameLength; -#endif - // Only show brightness for B&W displays if (hasSupportBrightness) { optionsArray[options] = "Brightness"; @@ -1416,7 +1804,7 @@ void menuHandler::screenOptionsMenu() optionsEnumArray[options++] = ScreenColor; #endif - optionsArray[options] = "Frame Visibility Toggle"; + optionsArray[options] = "Frame Visibility"; optionsEnumArray[options++] = FrameToggles; optionsArray[options] = "Display Units"; @@ -1434,9 +1822,6 @@ void menuHandler::screenOptionsMenu() } else if (selected == ScreenColor) { menuHandler::menuQueue = menuHandler::tftcolormenupicker; screen->runNow(); - } else if (selected == NodeNameLength) { - menuHandler::menuQueue = menuHandler::node_name_length_menu; - screen->runNow(); } else if (selected == FrameToggles) { menuHandler::menuQueue = menuHandler::FrameToggles; screen->runNow(); @@ -1471,11 +1856,10 @@ void menuHandler::powerMenu() #endif BannerOverlayOptions bannerOptions; -#if defined(M5STACK_UNITC6L) - bannerOptions.message = "Power"; -#else bannerOptions.message = "Reboot / Shutdown"; -#endif + if (currentResolution == ScreenResolution::UltraLow) { + bannerOptions.message = "Power"; + } bannerOptions.optionsArrayPtr = optionsArray; bannerOptions.optionsCount = options; bannerOptions.optionsEnumPtr = optionsEnumArray; @@ -1532,7 +1916,8 @@ void menuHandler::FrameToggles_menu() { enum optionsNumbers { Finish, - nodelist, + nodelist_nodes, + nodelist_location, nodelist_lastheard, nodelist_hopsignal, nodelist_distance, @@ -1553,20 +1938,25 @@ void menuHandler::FrameToggles_menu() static int lastSelectedIndex = 0; #ifndef USE_EINK - optionsArray[options] = screen->isFrameHidden("nodelist") ? "Show Node List" : "Hide Node List"; - optionsEnumArray[options++] = nodelist; -#endif -#ifdef USE_EINK + optionsArray[options] = screen->isFrameHidden("nodelist_nodes") ? "Show Node Lists" : "Hide Node Lists"; + optionsEnumArray[options++] = nodelist_nodes; +#else optionsArray[options] = screen->isFrameHidden("nodelist_lastheard") ? "Show NL - Last Heard" : "Hide NL - Last Heard"; optionsEnumArray[options++] = nodelist_lastheard; optionsArray[options] = screen->isFrameHidden("nodelist_hopsignal") ? "Show NL - Hops/Signal" : "Hide NL - Hops/Signal"; optionsEnumArray[options++] = nodelist_hopsignal; +#endif + +#if HAS_GPS +#ifndef USE_EINK + optionsArray[options] = screen->isFrameHidden("nodelist_location") ? "Show Position Lists" : "Hide Position Lists"; + optionsEnumArray[options++] = nodelist_location; +#else optionsArray[options] = screen->isFrameHidden("nodelist_distance") ? "Show NL - Distance" : "Hide NL - Distance"; optionsEnumArray[options++] = nodelist_distance; -#endif -#if HAS_GPS - optionsArray[options] = screen->isFrameHidden("nodelist_bearings") ? "Show Bearings" : "Hide Bearings"; + optionsArray[options] = screen->isFrameHidden("nodelist_bearings") ? "Show NL - Bearings" : "Hide NL - Bearings"; optionsEnumArray[options++] = nodelist_bearings; +#endif optionsArray[options] = screen->isFrameHidden("gps") ? "Show Position" : "Hide Position"; optionsEnumArray[options++] = gps; @@ -1605,8 +1995,12 @@ void menuHandler::FrameToggles_menu() if (selected == Finish) { screen->setFrames(Screen::FOCUS_DEFAULT); - } else if (selected == nodelist) { - screen->toggleFrameVisibility("nodelist"); + } else if (selected == nodelist_nodes) { + screen->toggleFrameVisibility("nodelist_nodes"); + menuHandler::menuQueue = menuHandler::FrameToggles; + screen->runNow(); + } else if (selected == nodelist_location) { + screen->toggleFrameVisibility("nodelist_location"); menuHandler::menuQueue = menuHandler::FrameToggles; screen->runNow(); } else if (selected == nodelist_lastheard) { @@ -1722,6 +2116,9 @@ void menuHandler::handleMenuSwitch(OLEDDisplay *display) case position_base_menu: positionBaseMenu(); break; + case node_base_menu: + nodeListMenu(); + break; #if !MESHTASTIC_EXCLUDE_GPS case gps_toggle_menu: GPSToggleMenu(); @@ -1802,6 +2199,18 @@ void menuHandler::handleMenuSwitch(OLEDDisplay *display) case throttle_message: screen->showSimpleBanner("Too Many Attempts\nTry again in 60 seconds.", 5000); break; + case message_response_menu: + messageResponseMenu(); + break; + case reply_menu: + replyMenu(); + break; + case delete_messages_menu: + deleteMessagesMenu(); + break; + case message_viewmode_menu: + messageViewModeMenu(); + break; } menuQueue = menu_none; } @@ -1813,4 +2222,4 @@ void menuHandler::saveUIConfig() } // namespace graphics -#endif +#endif \ No newline at end of file diff --git a/src/graphics/draw/MenuHandler.h b/src/graphics/draw/MenuHandler.h index df7c2739b..e53b4baf7 100644 --- a/src/graphics/draw/MenuHandler.h +++ b/src/graphics/draw/MenuHandler.h @@ -19,6 +19,7 @@ class menuHandler clock_face_picker, clock_menu, position_base_menu, + node_base_menu, gps_toggle_menu, gps_format_menu, compass_point_north_menu, @@ -43,6 +44,10 @@ class menuHandler key_verification_final_prompt, trace_route_menu, throttle_message, + message_response_menu, + message_viewmode_menu, + reply_menu, + delete_messages_menu, node_name_length_menu, FrameToggles, DisplayUnits @@ -61,6 +66,9 @@ class menuHandler static void TwelveHourPicker(); static void ClockFacePicker(); static void messageResponseMenu(); + static void messageViewModeMenu(); + static void replyMenu(); + static void deleteMessagesMenu(); static void homeBaseMenu(); static void textMessageBaseMenu(); static void systemBaseMenu(); @@ -119,4 +127,4 @@ template struct MenuOption { using RadioPresetOption = MenuOption; } // namespace graphics -#endif \ No newline at end of file +#endif diff --git a/src/graphics/draw/MessageRenderer.cpp b/src/graphics/draw/MessageRenderer.cpp index da6ec7abc..09b798e06 100644 --- a/src/graphics/draw/MessageRenderer.cpp +++ b/src/graphics/draw/MessageRenderer.cpp @@ -1,51 +1,27 @@ -/* -BaseUI - -Developed and Maintained By: -- Ronald Garcia (HarukiToreda) – Lead development and implementation. -- JasonP (Xaositek) – Screen layout and icon design, UI improvements and testing. -- TonyG (Tropho) – Project management, structural planning, and testing - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -*/ - #include "configuration.h" #if HAS_SCREEN #include "MessageRenderer.h" // Core includes +#include "MessageStore.h" #include "NodeDB.h" +#include "UIRenderer.h" #include "configuration.h" #include "gps/RTC.h" +#include "graphics/Screen.h" #include "graphics/ScreenFonts.h" #include "graphics/SharedUIDisplay.h" +#include "graphics/TimeFormatters.h" #include "graphics/emotes.h" #include "main.h" #include "meshUtils.h" - -// Additional includes for UI rendering -#include "UIRenderer.h" -#include "graphics/TimeFormatters.h" - -// Additional includes for dependencies #include #include // External declarations extern bool hasUnreadMessage; extern meshtastic_DeviceState devicestate; +extern graphics::Screen *screen; using graphics::Emote; using graphics::emotes; @@ -56,17 +32,105 @@ namespace graphics namespace MessageRenderer { -// Simple cache based on text hash -static size_t cachedKey = 0; static std::vector cachedLines; static std::vector cachedHeights; +static bool manualScrolling = false; + +// UTF-8 skip helper +static inline size_t utf8CharLen(uint8_t c) +{ + if ((c & 0xE0) == 0xC0) + return 2; + if ((c & 0xF0) == 0xE0) + return 3; + if ((c & 0xF8) == 0xF0) + return 4; + return 1; +} + +// Remove variation selectors (FE0F) and skin tone modifiers from emoji so they match your labels +std::string normalizeEmoji(const std::string &s) +{ + std::string out; + for (size_t i = 0; i < s.size();) { + uint8_t c = static_cast(s[i]); + size_t len = utf8CharLen(c); + + if (c == 0xEF && i + 2 < s.size() && (uint8_t)s[i + 1] == 0xB8 && (uint8_t)s[i + 2] == 0x8F) { + i += 3; + continue; + } + + // Skip skin tone modifiers + if (c == 0xF0 && i + 3 < s.size() && (uint8_t)s[i + 1] == 0x9F && (uint8_t)s[i + 2] == 0x8F && + ((uint8_t)s[i + 3] >= 0xBB && (uint8_t)s[i + 3] <= 0xBF)) { + i += 4; + continue; + } + + out.append(s, i, len); + i += len; + } + return out; +} + +// Scroll state (file scope so we can reset on new message) +float scrollY = 0.0f; +uint32_t lastTime = 0; +uint32_t scrollStartDelay = 0; +uint32_t pauseStart = 0; +bool waitingToReset = false; +bool scrollStarted = false; +static bool didReset = false; + +void scrollUp() +{ + manualScrolling = true; + scrollY -= 12; + if (scrollY < 0) + scrollY = 0; +} + +void scrollDown() +{ + manualScrolling = true; + + int totalHeight = 0; + for (int h : cachedHeights) + totalHeight += h; + + int visibleHeight = screen->getHeight() - (FONT_HEIGHT_SMALL * 2); + int maxScroll = totalHeight - visibleHeight; + if (maxScroll < 0) + maxScroll = 0; + + scrollY += 12; + if (scrollY > maxScroll) + scrollY = maxScroll; +} void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string &line, const Emote *emotes, int emoteCount) { + std::string renderLine; + for (size_t i = 0; i < line.size();) { + uint8_t c = (uint8_t)line[i]; + size_t len = utf8CharLen(c); + if (c == 0xEF && i + 2 < line.size() && (uint8_t)line[i + 1] == 0xB8 && (uint8_t)line[i + 2] == 0x8F) { + i += 3; + continue; + } + if (c == 0xF0 && i + 3 < line.size() && (uint8_t)line[i + 1] == 0x9F && (uint8_t)line[i + 2] == 0x8F && + ((uint8_t)line[i + 3] >= 0xBB && (uint8_t)line[i + 3] <= 0xBF)) { + i += 4; + continue; + } + renderLine.append(line, i, len); + i += len; + } int cursorX = x; const int fontHeight = FONT_HEIGHT_SMALL; - // === Step 1: Find tallest emote in the line === + // Step 1: Find tallest emote in the line int maxIconHeight = fontHeight; for (size_t i = 0; i < line.length();) { bool matched = false; @@ -81,25 +145,16 @@ void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string } } if (!matched) { - uint8_t c = static_cast(line[i]); - if ((c & 0xE0) == 0xC0) - i += 2; - else if ((c & 0xF0) == 0xE0) - i += 3; - else if ((c & 0xF8) == 0xF0) - i += 4; - else - i += 1; + i += utf8CharLen(static_cast(line[i])); } } - // === Step 2: Baseline alignment === + // Step 2: Baseline alignment int lineHeight = std::max(fontHeight, maxIconHeight); int baselineOffset = (lineHeight - fontHeight) / 2; int fontY = y + baselineOffset; - int fontMidline = fontY + fontHeight / 2; - // === Step 3: Render line in segments === + // Step 3: Render line in segments size_t i = 0; bool inBold = false; @@ -148,10 +203,12 @@ void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string // Render the emote (if found) if (matchedEmote && i == nextEmotePos) { - int iconY = fontMidline - matchedEmote->height / 2 - 1; + // Vertically center emote relative to font baseline (not just midline) + int iconY = fontY + (fontHeight - matchedEmote->height) / 2; display->drawXbm(cursorX, iconY, matchedEmote->width, matchedEmote->height, matchedEmote->bitmap); cursorX += matchedEmote->width + 1; i += emojiLen; + continue; } else { // No more emotes — render the rest of the line std::string remaining = line.substr(i); @@ -164,235 +221,471 @@ void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string #else cursorX += display->getStringWidth(remaining.c_str()); #endif - break; } } } +// Reset scroll state when new messages arrive +void resetScrollState() +{ + scrollY = 0.0f; + scrollStarted = false; + waitingToReset = false; + scrollStartDelay = millis(); + lastTime = millis(); + manualScrolling = false; + didReset = false; +} + +void nudgeScroll(int8_t direction) +{ + if (direction == 0) + return; + + if (cachedHeights.empty()) { + scrollY = 0.0f; + return; + } + + OLEDDisplay *display = (screen != nullptr) ? screen->getDisplayDevice() : nullptr; + const int displayHeight = display ? display->getHeight() : 64; + const int navHeight = FONT_HEIGHT_SMALL; + const int usableHeight = std::max(0, displayHeight - navHeight); + + int totalHeight = 0; + for (int h : cachedHeights) + totalHeight += h; + + if (totalHeight <= usableHeight) { + scrollY = 0.0f; + return; + } + + const int scrollStop = std::max(0, totalHeight - usableHeight + cachedHeights.back()); + const int step = std::max(FONT_HEIGHT_SMALL, usableHeight / 3); + + float newScroll = scrollY + static_cast(direction) * static_cast(step); + if (newScroll < 0.0f) + newScroll = 0.0f; + if (newScroll > scrollStop) + newScroll = static_cast(scrollStop); + + if (newScroll != scrollY) { + scrollY = newScroll; + waitingToReset = false; + scrollStarted = false; + scrollStartDelay = millis(); + lastTime = millis(); + } +} + +// Fully free cached message data from heap +void clearMessageCache() +{ + std::vector().swap(cachedLines); + std::vector().swap(cachedHeights); + + // Reset scroll so we rebuild cleanly next time we enter the screen + resetScrollState(); +} + +// Current thread state +static ThreadMode currentMode = ThreadMode::ALL; +static int currentChannel = -1; +static uint32_t currentPeer = 0; + +// Registry of seen threads for manual toggle +static std::vector seenChannels; +static std::vector seenPeers; + +// Public helper so menus / store can clear stale registries +void clearThreadRegistries() +{ + seenChannels.clear(); + seenPeers.clear(); +} + +// Setter so other code can switch threads +void setThreadMode(ThreadMode mode, int channel /* = -1 */, uint32_t peer /* = 0 */) +{ + currentMode = mode; + currentChannel = channel; + currentPeer = peer; + didReset = false; // force reset when mode changes + + // Track channels we’ve seen + if (mode == ThreadMode::CHANNEL && channel >= 0) { + if (std::find(seenChannels.begin(), seenChannels.end(), channel) == seenChannels.end()) { + seenChannels.push_back(channel); + } + } + + // Track DMs we’ve seen + if (mode == ThreadMode::DIRECT && peer != 0) { + if (std::find(seenPeers.begin(), seenPeers.end(), peer) == seenPeers.end()) { + seenPeers.push_back(peer); + } + } +} + +ThreadMode getThreadMode() +{ + return currentMode; +} + +int getThreadChannel() +{ + return currentChannel; +} + +uint32_t getThreadPeer() +{ + return currentPeer; +} + +// Accessors for menuHandler +const std::vector &getSeenChannels() +{ + return seenChannels; +} +const std::vector &getSeenPeers() +{ + return seenPeers; +} + +static int centerYForRow(int y, int size) +{ + int midY = y + (FONT_HEIGHT_SMALL / 2); + return midY - (size / 2); +} + +// Helpers for drawing status marks (thickened strokes) +static void drawCheckMark(OLEDDisplay *display, int x, int y, int size) +{ + int topY = centerYForRow(y, size); + display->setColor(WHITE); + display->drawLine(x, topY + size / 2, x + size / 3, topY + size); + display->drawLine(x, topY + size / 2 + 1, x + size / 3, topY + size + 1); + display->drawLine(x + size / 3, topY + size, x + size, topY); + display->drawLine(x + size / 3, topY + size + 1, x + size, topY + 1); +} + +static void drawXMark(OLEDDisplay *display, int x, int y, int size = 8) +{ + int topY = centerYForRow(y, size); + display->setColor(WHITE); + display->drawLine(x, topY, x + size, topY + size); + display->drawLine(x, topY + 1, x + size, topY + size + 1); + display->drawLine(x + size, topY, x, topY + size); + display->drawLine(x + size, topY + 1, x, topY + size + 1); +} + +static void drawRelayMark(OLEDDisplay *display, int x, int y, int size = 8) +{ + int r = size / 2; + int centerY = centerYForRow(y, size) + r; + int centerX = x + r; + display->setColor(WHITE); + display->drawCircle(centerX, centerY, r); + display->drawLine(centerX, centerY - 2, centerX, centerY); + display->setPixel(centerX, centerY + 2); + display->drawLine(centerX - 1, centerY - 4, centerX + 1, centerY - 4); +} + +static inline int getRenderedLineWidth(OLEDDisplay *display, const std::string &line, const Emote *emotes, int emoteCount) +{ + std::string normalized = normalizeEmoji(line); + int totalWidth = 0; + + size_t i = 0; + while (i < normalized.length()) { + bool matched = false; + for (int e = 0; e < emoteCount; ++e) { + size_t emojiLen = strlen(emotes[e].label); + if (normalized.compare(i, emojiLen, emotes[e].label) == 0) { + totalWidth += emotes[e].width + 1; // +1 spacing + i += emojiLen; + matched = true; + break; + } + } + if (!matched) { + size_t charLen = utf8CharLen(static_cast(normalized[i])); +#if defined(OLED_UA) || defined(OLED_RU) + totalWidth += display->getStringWidth(normalized.substr(i, charLen).c_str(), charLen, true); +#else + totalWidth += display->getStringWidth(normalized.substr(i, charLen).c_str()); +#endif + i += charLen; + } + } + return totalWidth; +} + +static void drawMessageScrollbar(OLEDDisplay *display, int visibleHeight, int totalHeight, int scrollOffset, int startY) +{ + if (totalHeight <= visibleHeight) + return; // no scrollbar needed + + int scrollbarX = display->getWidth() - 2; + int scrollbarHeight = visibleHeight; + int thumbHeight = std::max(6, (scrollbarHeight * visibleHeight) / totalHeight); + int maxScroll = std::max(1, totalHeight - visibleHeight); + int thumbY = startY + (scrollbarHeight - thumbHeight) * scrollOffset / maxScroll; + + for (int i = 0; i < thumbHeight; i++) { + display->setPixel(scrollbarX, thumbY + i); + } +} + void drawTextMessageFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { + // Ensure any boot-relative timestamps are upgraded if RTC is valid + messageStore.upgradeBootRelativeTimestamps(); + + if (!didReset) { + resetScrollState(); + didReset = true; + } + // Clear the unread message indicator when viewing the message hasUnreadMessage = false; - const meshtastic_MeshPacket &mp = devicestate.rx_text_message; - const char *msg = reinterpret_cast(mp.decoded.payload.bytes); + // Filter messages based on thread mode + std::deque filtered; + for (const auto &m : messageStore.getLiveMessages()) { + bool include = false; + switch (currentMode) { + case ThreadMode::ALL: + include = true; + break; + case ThreadMode::CHANNEL: + if (m.type == MessageType::BROADCAST && (int)m.channelIndex == currentChannel) + include = true; + break; + case ThreadMode::DIRECT: + if (m.dest != NODENUM_BROADCAST && (m.sender == currentPeer || m.dest == currentPeer)) + include = true; + break; + } + if (include) + filtered.push_back(m); + } display->clear(); display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); -#if defined(M5STACK_UNITC6L) - const int fixedTopHeight = 24; - const int windowX = 0; - const int windowY = fixedTopHeight; - const int windowWidth = 64; - const int windowHeight = SCREEN_HEIGHT - fixedTopHeight; -#else const int navHeight = FONT_HEIGHT_SMALL; const int scrollBottom = SCREEN_HEIGHT - navHeight; const int usableHeight = scrollBottom; - const int textWidth = SCREEN_WIDTH; + constexpr int LEFT_MARGIN = 2; + constexpr int RIGHT_MARGIN = 2; + constexpr int SCROLLBAR_WIDTH = 3; -#endif - bool isInverted = (config.display.displaymode != meshtastic_Config_DisplayConfig_DisplayMode_INVERTED); - bool isBold = config.display.heading_bold; + const int leftTextWidth = SCREEN_WIDTH - LEFT_MARGIN - RIGHT_MARGIN; - // === Set Title + const int rightTextWidth = SCREEN_WIDTH - LEFT_MARGIN - RIGHT_MARGIN - SCROLLBAR_WIDTH; + + // Title string depending on mode + static char titleBuf[32]; const char *titleStr = "Messages"; + switch (currentMode) { + case ThreadMode::ALL: + titleStr = "Messages"; + break; + case ThreadMode::CHANNEL: { + const char *cname = channels.getName(currentChannel); + if (cname && cname[0]) { + snprintf(titleBuf, sizeof(titleBuf), "#%s", cname); + } else { + snprintf(titleBuf, sizeof(titleBuf), "Ch%d", currentChannel); + } + titleStr = titleBuf; + break; + } + case ThreadMode::DIRECT: { + meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(currentPeer); + if (node && node->has_user) { + snprintf(titleBuf, sizeof(titleBuf), "@%s", node->user.short_name); + } else { + snprintf(titleBuf, sizeof(titleBuf), "@%08x", currentPeer); + } + titleStr = titleBuf; + break; + } + } - // Check if we have more than an empty message to show - char messageBuf[237]; - snprintf(messageBuf, sizeof(messageBuf), "%s", msg); - if (strlen(messageBuf) == 0) { - // === Header === + if (filtered.empty()) { + // If current conversation is empty go back to ALL view + if (currentMode != ThreadMode::ALL) { + setThreadMode(ThreadMode::ALL); + resetScrollState(); + return; // Next draw will rerun in ALL mode + } + + // Still in ALL mode and no messages at all → show placeholder graphics::drawCommonHeader(display, x, y, titleStr); + didReset = false; const char *messageString = "No messages"; int center_text = (SCREEN_WIDTH / 2) - (display->getStringWidth(messageString) / 2); -#if defined(M5STACK_UNITC6L) - display->drawString(center_text, windowY + (windowHeight / 2) - (FONT_HEIGHT_SMALL / 2) - 5, messageString); -#else display->drawString(center_text, getTextPositions(display)[2], messageString); -#endif graphics::drawCommonFooter(display, x, y); return; } - // === Header Construction === - meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(getFrom(&mp)); - char headerStr[80]; - const char *sender = "???"; -#if defined(M5STACK_UNITC6L) - if (node && node->has_user) - sender = node->user.short_name; -#else - if (node && node->has_user) { - if (SCREEN_WIDTH >= 200 && strlen(node->user.long_name) > 0) { - sender = node->user.long_name; - } else { - sender = node->user.short_name; - } - } -#endif - uint32_t seconds = sinceReceived(&mp), minutes = seconds / 60, hours = minutes / 60, days = hours / 24; - uint8_t timestampHours, timestampMinutes; - int32_t daysAgo; - bool useTimestamp = deltaToTimestamp(seconds, ×tampHours, ×tampMinutes, &daysAgo); + // Build lines for filtered messages (newest first) + std::vector allLines; + std::vector isMine; // track alignment + std::vector isHeader; // track header lines + std::vector ackForLine; - if (useTimestamp && minutes >= 15 && daysAgo == 0) { - std::string prefix = (daysAgo == 1 && SCREEN_WIDTH >= 200) ? "Yesterday" : "At"; - if (config.display.use_12h_clock) { - bool isPM = timestampHours >= 12; - timestampHours = timestampHours % 12; - if (timestampHours == 0) - timestampHours = 12; - snprintf(headerStr, sizeof(headerStr), "%s %d:%02d%s from %s", prefix.c_str(), timestampHours, timestampMinutes, - isPM ? "p" : "a", sender); - } else { - snprintf(headerStr, sizeof(headerStr), "%s %d:%02d from %s", prefix.c_str(), timestampHours, timestampMinutes, - sender); - } - } else { -#if defined(M5STACK_UNITC6L) - snprintf(headerStr, sizeof(headerStr), "%s from %s", UIRenderer::drawTimeDelta(days, hours, minutes, seconds).c_str(), - sender); -#else - snprintf(headerStr, sizeof(headerStr), "%s ago from %s", UIRenderer::drawTimeDelta(days, hours, minutes, seconds).c_str(), - sender); -#endif - } -#if defined(M5STACK_UNITC6L) - graphics::drawCommonHeader(display, x, y, titleStr); - int headerY = getTextPositions(display)[1]; - display->drawString(x, headerY, headerStr); - for (int separatorX = 0; separatorX < SCREEN_WIDTH; separatorX += 2) { - display->setPixel(separatorX, fixedTopHeight - 1); - } - cachedLines.clear(); - std::string fullMsg(messageBuf); - std::string currentLine; - for (size_t i = 0; i < fullMsg.size();) { - unsigned char c = fullMsg[i]; - size_t charLen = 1; - if ((c & 0xE0) == 0xC0) - charLen = 2; - else if ((c & 0xF0) == 0xE0) - charLen = 3; - else if ((c & 0xF8) == 0xF0) - charLen = 4; - std::string nextChar = fullMsg.substr(i, charLen); - std::string testLine = currentLine + nextChar; - if (display->getStringWidth(testLine.c_str()) > windowWidth) { - cachedLines.push_back(currentLine); - currentLine = nextChar; - } else { - currentLine = testLine; - } + for (auto it = filtered.rbegin(); it != filtered.rend(); ++it) { + const auto &m = *it; - i += charLen; - } - if (!currentLine.empty()) - cachedLines.push_back(currentLine); - cachedHeights = calculateLineHeights(cachedLines, emotes); - int yOffset = windowY; - int linesDrawn = 0; - for (size_t i = 0; i < cachedLines.size(); ++i) { - if (linesDrawn >= 2) - break; - int lineHeight = cachedHeights[i]; - if (yOffset + lineHeight > windowY + windowHeight) - break; - drawStringWithEmotes(display, windowX, yOffset, cachedLines[i], emotes, numEmotes); - yOffset += lineHeight; - linesDrawn++; - } - screen->forceDisplay(); -#else - uint32_t now = millis(); -#ifndef EXCLUDE_EMOJI - // === Bounce animation setup === - static uint32_t lastBounceTime = 0; - static int bounceY = 0; - const int bounceRange = 2; // Max pixels to bounce up/down - const int bounceInterval = 10; // How quickly to change bounce direction (ms) - - if (now - lastBounceTime >= bounceInterval) { - lastBounceTime = now; - bounceY = (bounceY + 1) % (bounceRange * 2); - } - for (int i = 0; i < numEmotes; ++i) { - const Emote &e = emotes[i]; - if (strcmp(msg, e.label) == 0) { - int headerY = getTextPositions(display)[1]; // same as scrolling header line - display->drawString(x + 3, headerY, headerStr); - if (isInverted && isBold) - display->drawString(x + 4, headerY, headerStr); - - // Draw separator (same as scroll version) - for (int separatorX = 1; separatorX <= (display->getStringWidth(headerStr) + 2); separatorX += 2) { - display->setPixel(separatorX, headerY + ((isHighResolution) ? 19 : 13)); + // Channel / destination labeling + char chanType[32] = ""; + if (currentMode == ThreadMode::ALL) { + if (m.dest == NODENUM_BROADCAST) { + snprintf(chanType, sizeof(chanType), "#%s", channels.getName(m.channelIndex)); + } else { + snprintf(chanType, sizeof(chanType), "(DM)"); } + } - // Center the emote below the header line + separator + nav - int remainingHeight = SCREEN_HEIGHT - (headerY + FONT_HEIGHT_SMALL) - navHeight; - int emoteY = headerY + 6 + FONT_HEIGHT_SMALL + (remainingHeight - e.height) / 2 + bounceY - bounceRange; - display->drawXbm((SCREEN_WIDTH - e.width) / 2, emoteY, e.width, e.height, e.bitmap); + // Calculate how long ago + uint32_t nowSecs = getValidTime(RTCQuality::RTCQualityDevice, true); + uint32_t seconds = 0; + bool invalidTime = true; - // Draw header at the end to sort out overlapping elements - graphics::drawCommonHeader(display, x, y, titleStr); - return; + if (m.timestamp > 0 && nowSecs > 0) { + if (nowSecs >= m.timestamp) { + seconds = nowSecs - m.timestamp; + invalidTime = (seconds > 315360000); // >10 years + } else { + uint32_t ahead = m.timestamp - nowSecs; + if (ahead <= 600) { // allow small skew + seconds = 0; + invalidTime = false; + } + } + } else if (m.timestamp > 0 && nowSecs == 0) { + // RTC not valid: only trust boot-relative if same boot + uint32_t bootNow = millis() / 1000; + if (m.isBootRelative && m.timestamp <= bootNow) { + seconds = bootNow - m.timestamp; + invalidTime = false; + } else { + invalidTime = true; // old persisted boot-relative, ignore until healed + } + } + + char timeBuf[16]; + if (invalidTime) { + snprintf(timeBuf, sizeof(timeBuf), "???"); + } else if (seconds < 60) { + snprintf(timeBuf, sizeof(timeBuf), "%us", seconds); + } else if (seconds < 3600) { + snprintf(timeBuf, sizeof(timeBuf), "%um", seconds / 60); + } else if (seconds < 86400) { + snprintf(timeBuf, sizeof(timeBuf), "%uh", seconds / 3600); + } else { + snprintf(timeBuf, sizeof(timeBuf), "%ud", seconds / 86400); + } + + // Build header line for this message + meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(m.sender); + meshtastic_NodeInfoLite *node_recipient = nodeDB->getMeshNode(m.dest); + + char senderBuf[48] = ""; + if (node && node->has_user) { + // Use long name if present + strncpy(senderBuf, node->user.long_name, sizeof(senderBuf) - 1); + senderBuf[sizeof(senderBuf) - 1] = '\0'; + } else { + // No long/short name → show NodeID in parentheses + snprintf(senderBuf, sizeof(senderBuf), "(%08x)", m.sender); + } + + // If this is *our own* message, override senderBuf to who the recipient was + bool mine = (m.sender == nodeDB->getNodeNum()); + if (mine && node_recipient && node_recipient->has_user) { + strcpy(senderBuf, node_recipient->user.long_name); + } + + // Shrink Sender name if needed + int availWidth = SCREEN_WIDTH - display->getStringWidth(timeBuf) - display->getStringWidth(chanType) - + display->getStringWidth(" @...") - 10; + if (availWidth < 0) + availWidth = 0; + + size_t origLen = strlen(senderBuf); + while (senderBuf[0] && display->getStringWidth(senderBuf) > availWidth) { + senderBuf[strlen(senderBuf) - 1] = '\0'; + } + + // If we actually truncated, append "..." + if (strlen(senderBuf) < origLen) { + strcat(senderBuf, "..."); + } + + // Final header line + char headerStr[96]; + if (mine) { + if (currentMode == ThreadMode::ALL) { + if (strcmp(chanType, "(DM)") == 0) { + snprintf(headerStr, sizeof(headerStr), "%s to %s", timeBuf, senderBuf); + } else { + snprintf(headerStr, sizeof(headerStr), "%s to %s", timeBuf, chanType); + } + } else { + snprintf(headerStr, sizeof(headerStr), "%s", timeBuf); + } + } else { + snprintf(headerStr, sizeof(headerStr), "%s @%s %s", timeBuf, senderBuf, chanType); + } + + // Push header line + allLines.push_back(std::string(headerStr)); + isMine.push_back(mine); + isHeader.push_back(true); + ackForLine.push_back(m.ackStatus); + + const char *msgText = MessageStore::getText(m); + + int wrapWidth = mine ? rightTextWidth : leftTextWidth; + std::vector wrapped = generateLines(display, "", msgText, wrapWidth); + for (auto &ln : wrapped) { + allLines.push_back(ln); + isMine.push_back(mine); + isHeader.push_back(false); + ackForLine.push_back(AckStatus::NONE); } } -#endif - // === Generate the cache key === - size_t currentKey = (size_t)mp.from; - currentKey ^= ((size_t)mp.to << 8); - currentKey ^= ((size_t)mp.rx_time << 16); - currentKey ^= ((size_t)mp.id << 24); - if (cachedKey != currentKey) { - LOG_INFO("Onscreen message scroll cache key needs updating: cachedKey=0x%0x, currentKey=0x%x", cachedKey, currentKey); + // Cache lines and heights + cachedLines = allLines; + cachedHeights = calculateLineHeights(cachedLines, emotes, isHeader); - // Cache miss - regenerate lines and heights - cachedLines = generateLines(display, headerStr, messageBuf, textWidth); - cachedHeights = calculateLineHeights(cachedLines, emotes); - cachedKey = currentKey; - } else { - // Cache hit but update the header line with current time information - cachedLines[0] = std::string(headerStr); - // The header always has a fixed height since it doesn't contain emotes - // As per calculateLineHeights logic for lines without emotes: - cachedHeights[0] = FONT_HEIGHT_SMALL - 2; - if (cachedHeights[0] < 8) - cachedHeights[0] = 8; // minimum safety - } - - // === Scrolling logic === + // Scrolling logic (unchanged) int totalHeight = 0; - for (size_t i = 1; i < cachedHeights.size(); ++i) { + for (size_t i = 0; i < cachedHeights.size(); ++i) totalHeight += cachedHeights[i]; - } - int usableScrollHeight = usableHeight - cachedHeights[0]; // remove header height + int usableScrollHeight = usableHeight; int scrollStop = std::max(0, totalHeight - usableScrollHeight + cachedHeights.back()); - static float scrollY = 0.0f; - static uint32_t lastTime = 0, scrollStartDelay = 0, pauseStart = 0; - static bool waitingToReset = false, scrollStarted = false; - - // === Smooth scrolling adjustment === - // You can tweak this divisor to change how smooth it scrolls. - // Lower = smoother, but can feel slow. +#ifndef USE_EINK + uint32_t now = millis(); float delta = (now - lastTime) / 400.0f; lastTime = now; + const float scrollSpeed = 2.0f; - const float scrollSpeed = 2.0f; // pixels per second - - // Delay scrolling start by 2 seconds if (scrollStartDelay == 0) scrollStartDelay = now; if (!scrollStarted && now - scrollStartDelay > 2000) scrollStarted = true; - if (totalHeight > usableScrollHeight) { + if (!manualScrolling && totalHeight > usableScrollHeight) { if (scrollStarted) { if (!waitingToReset) { scrollY += delta * scrollSpeed; @@ -408,29 +701,96 @@ void drawTextMessageFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 scrollStartDelay = lastTime; } } - } else { + } else if (!manualScrolling) { scrollY = 0; } - - int scrollOffset = static_cast(scrollY); - int yOffset = -scrollOffset + getTextPositions(display)[1]; - for (int separatorX = 1; separatorX <= (display->getStringWidth(headerStr) + 2); separatorX += 2) { - display->setPixel(separatorX, yOffset + ((isHighResolution) ? 19 : 13)); - } - - // === Render visible lines === - renderMessageContent(display, cachedLines, cachedHeights, x, yOffset, scrollBottom, emotes, numEmotes, isInverted, isBold); - - // Draw header at the end to sort out overlapping elements - graphics::drawCommonHeader(display, x, y, titleStr); +#else + // E-Ink: disable autoscroll + scrollY = 0.0f; + waitingToReset = false; + scrollStarted = false; + lastTime = millis(); #endif + + int finalScroll = (int)scrollY; + int yOffset = -finalScroll + getTextPositions(display)[1]; + + // Render visible lines + for (size_t i = 0; i < cachedLines.size(); ++i) { + int lineY = yOffset; + for (size_t j = 0; j < i; ++j) + lineY += cachedHeights[j]; + + if (lineY > -cachedHeights[i] && lineY < scrollBottom) { + if (isHeader[i]) { + + int w = display->getStringWidth(cachedLines[i].c_str()); + int headerX; + if (isMine[i]) { + // push header left to avoid overlap with scrollbar + headerX = SCREEN_WIDTH - w - SCROLLBAR_WIDTH - RIGHT_MARGIN; + if (headerX < LEFT_MARGIN) + headerX = LEFT_MARGIN; + } else { + headerX = x; + } + display->drawString(headerX, lineY, cachedLines[i].c_str()); + + // Draw ACK/NACK mark for our own messages + if (isMine[i]) { + int markX = headerX - 10; + int markY = lineY; + if (ackForLine[i] == AckStatus::ACKED) { + // Destination ACK + drawCheckMark(display, markX, markY, 8); + } else if (ackForLine[i] == AckStatus::NACKED || ackForLine[i] == AckStatus::TIMEOUT) { + // Failure or timeout + drawXMark(display, markX, markY, 8); + } else if (ackForLine[i] == AckStatus::RELAYED) { + // Relay ACK + drawRelayMark(display, markX, markY, 8); + } + // AckStatus::NONE → show nothing + } + + // Draw underline just under header text + int underlineY = lineY + FONT_HEIGHT_SMALL; + for (int px = 0; px < w; ++px) { + display->setPixel(headerX + px, underlineY); + } + } else { + // Render message line + if (isMine[i]) { + // Calculate actual rendered width including emotes + int renderedWidth = getRenderedLineWidth(display, cachedLines[i], emotes, numEmotes); + int rightX = SCREEN_WIDTH - renderedWidth - SCROLLBAR_WIDTH - RIGHT_MARGIN; + if (rightX < LEFT_MARGIN) + rightX = LEFT_MARGIN; + + drawStringWithEmotes(display, rightX, lineY, cachedLines[i], emotes, numEmotes); + } else { + drawStringWithEmotes(display, x, lineY, cachedLines[i], emotes, numEmotes); + } + } + } + } + int totalContentHeight = totalHeight; + int visibleHeight = usableHeight; + + // Draw scrollbar + drawMessageScrollbar(display, visibleHeight, totalContentHeight, finalScroll, getTextPositions(display)[1]); + graphics::drawCommonHeader(display, x, y, titleStr); graphics::drawCommonFooter(display, x, y); } std::vector generateLines(OLEDDisplay *display, const char *headerStr, const char *messageBuf, int textWidth) { std::vector lines; - lines.push_back(std::string(headerStr)); // Header line is always first + + // Only push headerStr if it's not empty (prevents extra blank line after headers) + if (headerStr && headerStr[0] != '\0') { + lines.push_back(std::string(headerStr)); + } std::string line, word; for (int i = 0; messageBuf[i]; ++i) { @@ -453,10 +813,6 @@ std::vector generateLines(OLEDDisplay *display, const char *headerS } else { word += ch; std::string test = line + word; -// Keep these lines for diagnostics -// LOG_INFO("Char: '%c' (0x%02X)", ch, (unsigned char)ch); -// LOG_INFO("Current String: %s", test.c_str()); -// Note: there are boolean comparison uint16 (getStringWidth) with int (textWidth), hope textWidth is always positive :) #if defined(OLED_UA) || defined(OLED_RU) uint16_t strWidth = display->getStringWidth(test.c_str(), test.length(), true); #else @@ -478,28 +834,71 @@ std::vector generateLines(OLEDDisplay *display, const char *headerS return lines; } - -std::vector calculateLineHeights(const std::vector &lines, const Emote *emotes) +std::vector calculateLineHeights(const std::vector &lines, const Emote *emotes, + const std::vector &isHeaderVec) { + // Tunables for layout control + constexpr int HEADER_UNDERLINE_GAP = 0; // space between underline and first body line + constexpr int HEADER_UNDERLINE_PIX = 1; // underline thickness (1px row drawn) + constexpr int BODY_LINE_LEADING = -4; // default vertical leading for normal body lines + constexpr int MESSAGE_BLOCK_GAP = 4; // gap after a message block before a new header + constexpr int EMOTE_PADDING_ABOVE = 4; // space above emote line (added to line above) + constexpr int EMOTE_PADDING_BELOW = 3; // space below emote line (added to emote line) + std::vector rowHeights; + rowHeights.reserve(lines.size()); - for (const auto &_line : lines) { - int lineHeight = FONT_HEIGHT_SMALL; + for (size_t idx = 0; idx < lines.size(); ++idx) { + const auto &line = lines[idx]; + const int baseHeight = FONT_HEIGHT_SMALL; + + // Detect if THIS line or NEXT line contains an emote bool hasEmote = false; - + int tallestEmote = baseHeight; for (int i = 0; i < numEmotes; ++i) { - const Emote &e = emotes[i]; - if (_line.find(e.label) != std::string::npos) { - lineHeight = std::max(lineHeight, e.height); + if (line.find(emotes[i].label) != std::string::npos) { hasEmote = true; + tallestEmote = std::max(tallestEmote, emotes[i].height); } } - // Apply tighter spacing if no emotes on this line - if (!hasEmote) { - lineHeight -= 2; // reduce by 2px for tighter spacing - if (lineHeight < 8) - lineHeight = 8; // minimum safety + bool nextHasEmote = false; + if (idx + 1 < lines.size()) { + for (int i = 0; i < numEmotes; ++i) { + if (lines[idx + 1].find(emotes[i].label) != std::string::npos) { + nextHasEmote = true; + break; + } + } + } + + int lineHeight = baseHeight; + + if (isHeaderVec[idx]) { + // Header line spacing + lineHeight = baseHeight + HEADER_UNDERLINE_PIX + HEADER_UNDERLINE_GAP; + } else { + // Base spacing for normal lines + int desiredBody = baseHeight + BODY_LINE_LEADING; + + if (hasEmote) { + // Emote line: add overshoot + bottom padding + int overshoot = std::max(0, tallestEmote - baseHeight); + lineHeight = desiredBody + overshoot + EMOTE_PADDING_BELOW; + } else { + // Regular line: no emote → standard spacing + lineHeight = desiredBody; + + // If next line has an emote → add top padding *here* + if (nextHasEmote) { + lineHeight += EMOTE_PADDING_ABOVE; + } + } + + // Add block gap if next is a header + if (idx + 1 < lines.size() && isHeaderVec[idx + 1]) { + lineHeight += MESSAGE_BLOCK_GAP; + } } rowHeights.push_back(lineHeight); @@ -508,22 +907,125 @@ std::vector calculateLineHeights(const std::vector &lines, con return rowHeights; } -void renderMessageContent(OLEDDisplay *display, const std::vector &lines, const std::vector &rowHeights, int x, - int yOffset, int scrollBottom, const Emote *emotes, int numEmotes, bool isInverted, bool isBold) +void handleNewMessage(OLEDDisplay *display, const StoredMessage &sm, const meshtastic_MeshPacket &packet) { - for (size_t i = 0; i < lines.size(); ++i) { - int lineY = yOffset; - for (size_t j = 0; j < i; ++j) - lineY += rowHeights[j]; - if (lineY > -rowHeights[i] && lineY < scrollBottom) { - if (i == 0 && isInverted) { - display->drawString(x, lineY, lines[i].c_str()); - if (isBold) - display->drawString(x, lineY, lines[i].c_str()); - } else { - drawStringWithEmotes(display, x, lineY, lines[i], emotes, numEmotes); + if (packet.from != 0) { + hasUnreadMessage = true; + + // Determine if message belongs to a muted channel + bool isChannelMuted = false; + if (sm.type == MessageType::BROADCAST) { + const meshtastic_Channel channel = channels.getByIndex(packet.channel ? packet.channel : channels.getPrimaryIndex()); + if (channel.settings.has_module_settings && channel.settings.module_settings.is_muted) + isChannelMuted = true; + } + + // Banner logic + const meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(packet.from); + char longName[48] = "???"; + if (node && node->user.long_name) { + strncpy(longName, node->user.long_name, sizeof(longName) - 1); + longName[sizeof(longName) - 1] = '\0'; + } + int availWidth = display->getWidth() - ((currentResolution == ScreenResolution::High) ? 40 : 20); + if (availWidth < 0) + availWidth = 0; + + size_t origLen = strlen(longName); + while (longName[0] && display->getStringWidth(longName) > availWidth) { + longName[strlen(longName) - 1] = '\0'; + } + if (strlen(longName) < origLen) { + strcat(longName, "..."); + } + const char *msgRaw = reinterpret_cast(packet.decoded.payload.bytes); + + char banner[256]; + bool isAlert = false; + + // Check if alert detection is enabled via external notification module + if (moduleConfig.external_notification.alert_bell || moduleConfig.external_notification.alert_bell_vibra || + moduleConfig.external_notification.alert_bell_buzzer) { + for (size_t i = 0; i < packet.decoded.payload.size && i < 100; i++) { + if (msgRaw[i] == '\x07') { + isAlert = true; + break; + } } } + + if (isAlert) { + if (longName && longName[0]) + snprintf(banner, sizeof(banner), "Alert Received from\n%s", longName); + else + strcpy(banner, "Alert Received"); + } else { + // Skip muted channels unless it's an alert + if (isChannelMuted) + return; + + if (longName && longName[0]) { + if (currentResolution == ScreenResolution::UltraLow) { + strcpy(banner, "New Message"); + } else { + snprintf(banner, sizeof(banner), "New Message from\n%s", longName); + } + } else + strcpy(banner, "New Message"); + } + + // Append context (which channel or DM) so the banner shows where the message arrived + { + char contextBuf[64] = ""; + if (sm.type == MessageType::BROADCAST) { + const char *cname = channels.getName(sm.channelIndex); + if (cname && cname[0]) + snprintf(contextBuf, sizeof(contextBuf), "in #%s", cname); + else + snprintf(contextBuf, sizeof(contextBuf), "in Ch%d", sm.channelIndex); + } + + if (contextBuf[0]) { + size_t cur = strlen(banner); + if (cur + 1 < sizeof(banner)) { + if (cur > 0 && banner[cur - 1] != '\n') { + banner[cur] = '\n'; + banner[cur + 1] = '\0'; + cur++; + } + strncat(banner, contextBuf, sizeof(banner) - cur - 1); + } + } + } + + // Shorter banner if already in a conversation (Channel or Direct) + bool inThread = (getThreadMode() != ThreadMode::ALL); + + if (shouldWakeOnReceivedMessage()) { + screen->setOn(true); + } + + screen->showSimpleBanner(banner, inThread ? 1000 : 3000); + } + + // Always focus into the correct conversation thread when a message with real text arrives + const char *msgText = MessageStore::getText(sm); + if (msgText && msgText[0] != '\0') { + setThreadFor(sm, packet); + } + + // Reset scroll for a clean start + resetScrollState(); +} + +void setThreadFor(const StoredMessage &sm, const meshtastic_MeshPacket &packet) +{ + if (packet.to == 0 || packet.to == NODENUM_BROADCAST) { + setThreadMode(ThreadMode::CHANNEL, sm.channelIndex); + } else { + uint32_t localNode = nodeDB->getNodeNum(); + uint32_t peer = (sm.sender == localNode) ? packet.to : sm.sender; + setThreadMode(ThreadMode::DIRECT, -1, peer); } } diff --git a/src/graphics/draw/MessageRenderer.h b/src/graphics/draw/MessageRenderer.h index c15a699f7..7dec6adec 100644 --- a/src/graphics/draw/MessageRenderer.h +++ b/src/graphics/draw/MessageRenderer.h @@ -1,7 +1,11 @@ #pragma once +#include "MessageStore.h" // for StoredMessage +#if HAS_SCREEN #include "OLEDDisplay.h" #include "OLEDDisplayUi.h" #include "graphics/emotes.h" +#include "mesh/generated/meshtastic/mesh.pb.h" // for meshtastic_MeshPacket +#include #include #include @@ -10,6 +14,27 @@ namespace graphics namespace MessageRenderer { +// Thread filter modes +enum class ThreadMode { ALL, CHANNEL, DIRECT }; + +// Setter for switching thread mode +void setThreadMode(ThreadMode mode, int channel = -1, uint32_t peer = 0); + +// Getter for current mode +ThreadMode getThreadMode(); + +// Getter for current channel (valid if mode == CHANNEL) +int getThreadChannel(); + +// Getter for current peer (valid if mode == DIRECT) +uint32_t getThreadPeer(); + +// Registry accessors for menuHandler +const std::vector &getSeenChannels(); +const std::vector &getSeenPeers(); + +void clearThreadRegistries(); + // Text and emote rendering void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string &line, const Emote *emotes, int emoteCount); @@ -20,11 +45,27 @@ void drawTextMessageFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16 std::vector generateLines(OLEDDisplay *display, const char *headerStr, const char *messageBuf, int textWidth); // Function to calculate heights for each line -std::vector calculateLineHeights(const std::vector &lines, const Emote *emotes); +std::vector calculateLineHeights(const std::vector &lines, const Emote *emotes, + const std::vector &isHeaderVec); -// Function to render the message content -void renderMessageContent(OLEDDisplay *display, const std::vector &lines, const std::vector &rowHeights, int x, - int yOffset, int scrollBottom, const Emote *emotes, int numEmotes, bool isInverted, bool isBold); +// Reset scroll state when new messages arrive +void resetScrollState(); + +// Manual scroll control for encoder-style inputs +void nudgeScroll(int8_t direction); + +// Helper to auto-select the correct thread mode from a message +void setThreadFor(const StoredMessage &sm, const meshtastic_MeshPacket &packet); + +// Handles a new incoming/outgoing message: banner, wake, thread select, scroll reset +void handleNewMessage(OLEDDisplay *display, const StoredMessage &sm, const meshtastic_MeshPacket &packet); + +// Clear Message Line Cache from Message Renderer +void clearMessageCache(); + +void scrollUp(); +void scrollDown(); } // namespace MessageRenderer } // namespace graphics +#endif \ No newline at end of file diff --git a/src/graphics/draw/NodeListRenderer.cpp b/src/graphics/draw/NodeListRenderer.cpp index 1a36a6188..e10d8c40a 100644 --- a/src/graphics/draw/NodeListRenderer.cpp +++ b/src/graphics/draw/NodeListRenderer.cpp @@ -23,7 +23,6 @@ extern graphics::Screen *screen; #if defined(M5STACK_UNITC6L) static uint32_t lastSwitchTime = 0; -#else #endif namespace graphics { @@ -46,79 +45,119 @@ void drawScaledXBitmap16x16(int x, int y, int width, int height, const uint8_t * } // Static variables for dynamic cycling -static NodeListMode currentMode = MODE_LAST_HEARD; +static ListMode_Node currentMode_Nodes = MODE_LAST_HEARD; +static ListMode_Location currentMode_Location = MODE_DISTANCE; static int scrollIndex = 0; +// Popup overlay state +static uint32_t popupTime = 0; +static int popupTotal = 0; +static int popupStart = 0; +static int popupEnd = 0; +static int popupPage = 1; +static int popupMaxPage = 1; + +static const uint32_t POPUP_DURATION_MS = 1000; // 1 second visible + +// ============================= +// Scrolling Logic +// ============================= +void scrollUp() +{ + if (scrollIndex > 0) + scrollIndex--; + + popupTime = millis(); // show popup +} + +void scrollDown() +{ + scrollIndex++; + popupTime = millis(); +} // ============================= // Utility Functions // ============================= -const char *getSafeNodeName(OLEDDisplay *display, meshtastic_NodeInfoLite *node) +const char *getSafeNodeName(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int columnWidth) { - const char *name = NULL; - static char nodeName[16] = "?"; - if (config.display.use_long_node_name == true) { - if (node->has_user && strlen(node->user.long_name) > 0) { - name = node->user.long_name; - } else { - snprintf(nodeName, sizeof(nodeName), "(%04X)", (uint16_t)(node->num & 0xFFFF)); - } - } else { - if (node->has_user && strlen(node->user.short_name) > 0) { - name = node->user.short_name; - } else { - snprintf(nodeName, sizeof(nodeName), "(%04X)", (uint16_t)(node->num & 0xFFFF)); - } + static char nodeName[25]; // single static buffer we return + nodeName[0] = '\0'; + + auto writeFallbackId = [&] { + std::snprintf(nodeName, sizeof(nodeName), "(%04X)", static_cast(node ? (node->num & 0xFFFF) : 0)); + }; + + // 1) Choose target candidate (long vs short) only if present + const char *raw = nullptr; + if (node && node->has_user) { + raw = config.display.use_long_node_name ? node->user.long_name : node->user.short_name; } - // Use sanitizeString() function and copy directly into nodeName - std::string sanitized_name = sanitizeString(name ? name : ""); + // 2) Sanitize (empty if raw is null/empty) + std::string s = (raw && *raw) ? sanitizeString(raw) : std::string{}; - if (!sanitized_name.empty()) { - strncpy(nodeName, sanitized_name.c_str(), sizeof(nodeName) - 1); - nodeName[sizeof(nodeName) - 1] = '\0'; + // 3) Fallback if sanitize yields empty; otherwise copy safely (truncate if needed) + if (s.empty() || s == "¿" || s.find_first_not_of("¿") == std::string::npos) { + writeFallbackId(); } else { - snprintf(nodeName, sizeof(nodeName), "(%04X)", (uint16_t)(node->num & 0xFFFF)); + // %.*s ensures null-termination and safe truncation to buffer size - 1 + std::snprintf(nodeName, sizeof(nodeName), "%.*s", static_cast(sizeof(nodeName) - 1), s.c_str()); } - if (config.display.use_long_node_name == true) { - int availWidth = (SCREEN_WIDTH / 2) - 65; + // 4) Width-based truncation + ellipsis (long-name mode only) + if (config.display.use_long_node_name && display) { + int availWidth = columnWidth - ((currentResolution == ScreenResolution::High) ? 65 : 38); if (availWidth < 0) availWidth = 0; - size_t origLen = strlen(nodeName); - while (nodeName[0] && display->getStringWidth(nodeName) > availWidth) { - nodeName[strlen(nodeName) - 1] = '\0'; + const size_t beforeLen = std::strlen(nodeName); + + // Trim from the end until it fits or is empty + size_t len = beforeLen; + while (len && display->getStringWidth(nodeName) > availWidth) { + nodeName[--len] = '\0'; } - // If we actually truncated, append "..." (ensure space remains in buffer) - if (strlen(nodeName) < origLen) { - size_t len = strlen(nodeName); - size_t maxLen = sizeof(nodeName) - 4; // 3 for "..." and 1 for '\0' - if (len > maxLen) { - nodeName[maxLen] = '\0'; - len = maxLen; + // If truncated, append "..." (respect buffer size) + if (len < beforeLen) { + // Make sure there's room for "..." and '\0' + const size_t capForText = sizeof(nodeName) - 1; // leaving space for '\0' + const size_t needed = 3; // "..." + if (len > capForText - needed) { + len = capForText - needed; + nodeName[len] = '\0'; } - strcat(nodeName, "..."); + std::strcat(nodeName, "..."); } } return nodeName; } -const char *getCurrentModeTitle(int screenWidth) +const char *getCurrentModeTitle_Nodes(int screenWidth) { - switch (currentMode) { + switch (currentMode_Nodes) { case MODE_LAST_HEARD: return "Last Heard"; case MODE_HOP_SIGNAL: #ifdef USE_EINK return "Hops/Sig"; #else - return (isHighResolution) ? "Hops/Signal" : "Hops/Sig"; + return (currentResolution == ScreenResolution::High) ? "Hops/Signal" : "Hops/Sig"; #endif + default: + return "Nodes"; + } +} + +const char *getCurrentModeTitle_Location(int screenWidth) +{ + switch (currentMode_Location) { case MODE_DISTANCE: return "Distance"; + case MODE_BEARING: + return "Bearings"; default: return "Nodes"; } @@ -137,10 +176,8 @@ int calculateMaxScroll(int totalEntries, int visibleRows) void drawColumnSeparator(OLEDDisplay *display, int16_t x, int16_t yStart, int16_t yEnd) { - int columnWidth = display->getWidth() / 2; - int separatorX = x + columnWidth - 2; for (int y = yStart; y <= yEnd; y += 2) { - display->setPixel(separatorX, y); + display->setPixel(x, y); } } @@ -152,7 +189,8 @@ void drawScrollbar(OLEDDisplay *display, int visibleNodeRows, int totalEntries, int scrollbarX = display->getWidth() - 2; int scrollbarHeight = display->getHeight() - scrollStartY - 10; int thumbHeight = std::max(4, (scrollbarHeight * visibleNodeRows * columns) / totalEntries); - int maxScroll = calculateMaxScroll(totalEntries, visibleNodeRows); + int perPage = visibleNodeRows * columns; + int maxScroll = std::max(0, (totalEntries - 1) / perPage); int thumbY = scrollStartY + (scrollIndex * (scrollbarHeight - thumbHeight)) / std::max(1, maxScroll); for (int i = 0; i < thumbHeight; i++) { @@ -167,9 +205,9 @@ void drawScrollbar(OLEDDisplay *display, int visibleNodeRows, int totalEntries, void drawEntryLastHeard(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth) { bool isLeftCol = (x < SCREEN_WIDTH / 2); - int timeOffset = (isHighResolution) ? (isLeftCol ? 7 : 10) : (isLeftCol ? 3 : 7); + int timeOffset = (currentResolution == ScreenResolution::High) ? (isLeftCol ? 7 : 10) : (isLeftCol ? 3 : 7); - const char *nodeName = getSafeNodeName(display, node); + const char *nodeName = getSafeNodeName(display, node, columnWidth); char timeStr[10]; uint32_t seconds = sinceLastSeen(node); @@ -188,9 +226,9 @@ void drawEntryLastHeard(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - display->drawString(x + ((isHighResolution) ? 6 : 3), y, nodeName); + display->drawString(x + ((currentResolution == ScreenResolution::High) ? 6 : 3), y, nodeName); if (node->is_favorite) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { drawScaledXBitmap16x16(x, y + 6, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint, display); } else { display->drawXbm(x, y + 5, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint); @@ -209,19 +247,19 @@ void drawEntryHopSignal(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int bool isLeftCol = (x < SCREEN_WIDTH / 2); int nameMaxWidth = columnWidth - 25; - int barsOffset = (isHighResolution) ? (isLeftCol ? 20 : 24) : (isLeftCol ? 15 : 19); - int hopOffset = (isHighResolution) ? (isLeftCol ? 21 : 29) : (isLeftCol ? 13 : 17); + int barsOffset = (currentResolution == ScreenResolution::High) ? (isLeftCol ? 20 : 24) : (isLeftCol ? 15 : 19); + int hopOffset = (currentResolution == ScreenResolution::High) ? (isLeftCol ? 21 : 29) : (isLeftCol ? 13 : 17); int barsXOffset = columnWidth - barsOffset; - const char *nodeName = getSafeNodeName(display, node); + const char *nodeName = getSafeNodeName(display, node, columnWidth); display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - display->drawStringMaxWidth(x + ((isHighResolution) ? 6 : 3), y, nameMaxWidth, nodeName); + display->drawStringMaxWidth(x + ((currentResolution == ScreenResolution::High) ? 6 : 3), y, nameMaxWidth, nodeName); if (node->is_favorite) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { drawScaledXBitmap16x16(x, y + 6, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint, display); } else { display->drawXbm(x, y + 5, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint); @@ -256,9 +294,10 @@ void drawEntryHopSignal(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int void drawNodeDistance(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth) { bool isLeftCol = (x < SCREEN_WIDTH / 2); - int nameMaxWidth = columnWidth - (isHighResolution ? (isLeftCol ? 25 : 28) : (isLeftCol ? 20 : 22)); + int nameMaxWidth = + columnWidth - ((currentResolution == ScreenResolution::High) ? (isLeftCol ? 25 : 28) : (isLeftCol ? 20 : 22)); - const char *nodeName = getSafeNodeName(display, node); + const char *nodeName = getSafeNodeName(display, node, columnWidth); char distStr[10] = ""; meshtastic_NodeInfoLite *ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum()); @@ -311,9 +350,9 @@ void drawNodeDistance(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16 display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - display->drawStringMaxWidth(x + ((isHighResolution) ? 6 : 3), y, nameMaxWidth, nodeName); + display->drawStringMaxWidth(x + ((currentResolution == ScreenResolution::High) ? 6 : 3), y, nameMaxWidth, nodeName); if (node->is_favorite) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { drawScaledXBitmap16x16(x, y + 6, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint, display); } else { display->drawXbm(x, y + 5, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint); @@ -321,26 +360,24 @@ void drawNodeDistance(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16 } if (strlen(distStr) > 0) { - int offset = (isHighResolution) ? (isLeftCol ? 7 : 10) // Offset for Wide Screens (Left Column:Right Column) - : (isLeftCol ? 4 : 7); // Offset for Narrow Screens (Left Column:Right Column) + int offset = (currentResolution == ScreenResolution::High) + ? (isLeftCol ? 7 : 10) // Offset for Wide Screens (Left Column:Right Column) + : (isLeftCol ? 4 : 7); // Offset for Narrow Screens (Left Column:Right Column) int rightEdge = x + columnWidth - offset; int textWidth = display->getStringWidth(distStr); display->drawString(rightEdge - textWidth, y, distStr); } } -void drawEntryDynamic(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth) +void drawEntryDynamic_Nodes(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth) { - switch (currentMode) { + switch (currentMode_Nodes) { case MODE_LAST_HEARD: drawEntryLastHeard(display, node, x, y, columnWidth); break; case MODE_HOP_SIGNAL: drawEntryHopSignal(display, node, x, y, columnWidth); break; - case MODE_DISTANCE: - drawNodeDistance(display, node, x, y, columnWidth); - break; default: break; } @@ -351,15 +388,16 @@ void drawEntryCompass(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16 bool isLeftCol = (x < SCREEN_WIDTH / 2); // Adjust max text width depending on column and screen width - int nameMaxWidth = columnWidth - (isHighResolution ? (isLeftCol ? 25 : 28) : (isLeftCol ? 20 : 22)); + int nameMaxWidth = + columnWidth - ((currentResolution == ScreenResolution::High) ? (isLeftCol ? 25 : 28) : (isLeftCol ? 20 : 22)); - const char *nodeName = getSafeNodeName(display, node); + const char *nodeName = getSafeNodeName(display, node, columnWidth); display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - display->drawStringMaxWidth(x + ((isHighResolution) ? 6 : 3), y, nameMaxWidth, nodeName); + display->drawStringMaxWidth(x + ((currentResolution == ScreenResolution::High) ? 6 : 3), y, nameMaxWidth, nodeName); if (node->is_favorite) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { drawScaledXBitmap16x16(x, y + 6, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint, display); } else { display->drawXbm(x, y + 5, smallbulletpoint_width, smallbulletpoint_height, smallbulletpoint); @@ -374,7 +412,7 @@ void drawCompassArrow(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16 return; bool isLeftCol = (x < SCREEN_WIDTH / 2); - int arrowXOffset = (isHighResolution) ? (isLeftCol ? 22 : 24) : (isLeftCol ? 12 : 18); + int arrowXOffset = (currentResolution == ScreenResolution::High) ? (isLeftCol ? 22 : 24) : (isLeftCol ? 12 : 18); int centerX = x + columnWidth - arrowXOffset; int centerY = y + FONT_HEIGHT_SMALL / 2; @@ -431,11 +469,6 @@ void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t locationScreen = true; else if (strcmp(title, "Distance") == 0) locationScreen = true; -#if defined(M5STACK_UNITC6L) - int columnWidth = display->getWidth(); -#else - int columnWidth = display->getWidth() / 2; -#endif display->clear(); // Draw the battery/time header @@ -444,39 +477,74 @@ void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t // Space below header y += COMMON_HEADER_HEIGHT; + int totalColumns = 1; // Default to 1 column + + if (config.display.use_long_node_name) { + if (SCREEN_WIDTH <= 240) { + totalColumns = 1; + } else if (SCREEN_WIDTH > 240) { + totalColumns = 2; + } + } else { + if (SCREEN_WIDTH <= 64) { + totalColumns = 1; + } else if (SCREEN_WIDTH > 64 && SCREEN_WIDTH <= 240) { + totalColumns = 2; + } else { + totalColumns = 3; + } + } + + int columnWidth = display->getWidth() / totalColumns; + int totalEntries = nodeDB->getNumMeshNodes(); int totalRowsAvailable = (display->getHeight() - y) / rowYOffset; int numskipped = 0; int visibleNodeRows = totalRowsAvailable; -#if defined(M5STACK_UNITC6L) - int totalColumns = 1; -#else - int totalColumns = 2; -#endif - int startIndex = scrollIndex * visibleNodeRows * totalColumns; - if (nodeDB->getMeshNodeByIndex(startIndex)->num == nodeDB->getNodeNum()) { - startIndex++; // skip own node - } - int endIndex = std::min(startIndex + visibleNodeRows * totalColumns, totalEntries); + // Build filtered + ordered list + std::vector drawList; + drawList.reserve(totalEntries); + for (int i = 0; i < totalEntries; i++) { + auto *n = nodeDB->getMeshNodeByIndex(i); + + if (!n) + continue; + if (n->num == nodeDB->getNodeNum()) + continue; + if (locationScreen && !n->has_position) + continue; + + drawList.push_back(n->num); + } + totalEntries = drawList.size(); + int perPage = visibleNodeRows * totalColumns; + + int maxScroll = 0; + if (perPage > 0) { + maxScroll = std::max(0, (totalEntries - 1) / perPage); + } + + if (scrollIndex > maxScroll) + scrollIndex = maxScroll; + int startIndex = scrollIndex * visibleNodeRows * totalColumns; + int endIndex = std::min(startIndex + visibleNodeRows * totalColumns, totalEntries); int yOffset = 0; int col = 0; int lastNodeY = y; int shownCount = 0; int rowCount = 0; - for (int i = startIndex; i < endIndex; ++i) { - if (locationScreen && !nodeDB->getMeshNodeByIndex(i)->has_position) { - numskipped++; - continue; - } + for (int idx = startIndex; idx < endIndex; idx++) { + uint32_t nodeNum = drawList[idx]; + auto *node = nodeDB->getMeshNode(nodeNum); int xPos = x + (col * columnWidth); int yPos = y + yOffset; - renderer(display, nodeDB->getMeshNodeByIndex(i), xPos, yPos, columnWidth); - if (extras) { - extras(display, nodeDB->getMeshNodeByIndex(i), xPos, yPos, columnWidth, heading, lat, lon); - } + renderer(display, node, xPos, yPos, columnWidth); + + if (extras) + extras(display, node, xPos, yPos, columnWidth, heading, lat, lon); lastNodeY = std::max(lastNodeY, yPos + FONT_HEIGHT_SMALL); yOffset += rowYOffset; @@ -495,17 +563,73 @@ void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t // This should correct the scrollbar totalEntries -= numskipped; -#if !defined(M5STACK_UNITC6L) // Draw column separator - if (shownCount > 0) { + if (currentResolution != ScreenResolution::UltraLow && shownCount > 0) { const int firstNodeY = y + 3; - drawColumnSeparator(display, x, firstNodeY, lastNodeY); + for (int horizontal_offset = 1; horizontal_offset < totalColumns; horizontal_offset++) { + drawColumnSeparator(display, columnWidth * horizontal_offset, firstNodeY, lastNodeY); + } } -#endif const int scrollStartY = y + 3; - drawScrollbar(display, visibleNodeRows, totalEntries, scrollIndex, 2, scrollStartY); + drawScrollbar(display, visibleNodeRows, totalEntries, scrollIndex, totalColumns, scrollStartY); graphics::drawCommonFooter(display, x, y); + + // Scroll Popup Overlay + if (millis() - popupTime < POPUP_DURATION_MS) { + popupTotal = totalEntries; + + int perPage = visibleNodeRows * totalColumns; + + popupStart = startIndex + 1; + popupEnd = std::min(startIndex + perPage, totalEntries); + + popupPage = (scrollIndex + 1); + popupMaxPage = std::max(1, (totalEntries + perPage - 1) / perPage); + + char buf[32]; + snprintf(buf, sizeof(buf), "%d-%d/%d Pg %d/%d", popupStart, popupEnd, popupTotal, popupPage, popupMaxPage); + + display->setTextAlignment(TEXT_ALIGN_LEFT); + + // Box padding + int padding = 2; + int textW = display->getStringWidth(buf); + int textH = FONT_HEIGHT_SMALL; + int boxWidth = textW + padding * 3; + int boxHeight = textH + padding * 2; + + // Center of usable screen area: + int headerHeight = FONT_HEIGHT_SMALL - 1; + int footerHeight = FONT_HEIGHT_SMALL + 2; + + int usableTop = headerHeight; + int usableBottom = display->getHeight() - footerHeight; + int usableHeight = usableBottom - usableTop; + + // Center point inside usable area + int boxLeft = (display->getWidth() - boxWidth) / 2; + int boxTop = usableTop + (usableHeight - boxHeight) / 2; + + // Draw Box + display->setColor(BLACK); + display->fillRect(boxLeft - 1, boxTop - 1, boxWidth + 2, boxHeight + 2); + display->fillRect(boxLeft, boxTop - 2, boxWidth, 1); + display->fillRect(boxLeft, boxTop + boxHeight + 1, boxWidth, 1); + display->fillRect(boxLeft - 2, boxTop, 1, boxHeight); + display->fillRect(boxLeft + boxWidth + 1, boxTop, 1, boxHeight); + display->setColor(WHITE); + display->drawRect(boxLeft, boxTop, boxWidth, boxHeight); + display->setColor(BLACK); + display->fillRect(boxLeft, boxTop, 1, 1); + display->fillRect(boxLeft + boxWidth - 1, boxTop, 1, 1); + display->fillRect(boxLeft, boxTop + boxHeight - 1, 1, 1); + display->fillRect(boxLeft + boxWidth - 1, boxTop + boxHeight - 1, 1, 1); + display->setColor(WHITE); + + // Text + display->drawString(boxLeft + padding, boxTop + padding, buf); + } } // ============================= @@ -513,10 +637,11 @@ void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t // ============================= #ifndef USE_EINK -void drawDynamicNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +// Node list for Last Heard and Hop Signal views +void drawDynamicListScreen_Nodes(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { // Static variables to track mode and duration - static NodeListMode lastRenderedMode = MODE_COUNT; + static ListMode_Node lastRenderedMode = MODE_COUNT_NODE; static unsigned long modeStartTime = 0; unsigned long now = millis(); @@ -529,23 +654,65 @@ void drawDynamicNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, } #endif // On very first call (on boot or state enter) - if (lastRenderedMode == MODE_COUNT) { - currentMode = MODE_LAST_HEARD; + if (lastRenderedMode == MODE_COUNT_NODE) { + currentMode_Nodes = MODE_LAST_HEARD; modeStartTime = now; } // Time to switch to next mode? if (now - modeStartTime >= getModeCycleIntervalMs()) { - currentMode = static_cast((currentMode + 1) % MODE_COUNT); + currentMode_Nodes = static_cast((currentMode_Nodes + 1) % MODE_COUNT_NODE); modeStartTime = now; } // Render screen based on currentMode - const char *title = getCurrentModeTitle(display->getWidth()); - drawNodeListScreen(display, state, x, y, title, drawEntryDynamic); + const char *title = getCurrentModeTitle_Nodes(display->getWidth()); + drawNodeListScreen(display, state, x, y, title, drawEntryDynamic_Nodes); // Track the last mode to avoid reinitializing modeStartTime - lastRenderedMode = currentMode; + lastRenderedMode = currentMode_Nodes; +} + +// Node list for Distance and Bearings views +void drawDynamicListScreen_Location(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + // Static variables to track mode and duration + static ListMode_Location lastRenderedMode = MODE_COUNT_LOCATION; + static unsigned long modeStartTime = 0; + + unsigned long now = millis(); + +#if defined(M5STACK_UNITC6L) + display->clear(); + if (now - lastSwitchTime >= 3000) { + display->display(); + lastSwitchTime = now; + } +#endif + // On very first call (on boot or state enter) + if (lastRenderedMode == MODE_COUNT_LOCATION) { + currentMode_Location = MODE_DISTANCE; + modeStartTime = now; + } + + // Time to switch to next mode? + if (now - modeStartTime >= getModeCycleIntervalMs()) { + currentMode_Location = static_cast((currentMode_Location + 1) % MODE_COUNT_LOCATION); + modeStartTime = now; + } + + // Render screen based on currentMode + const char *title = getCurrentModeTitle_Location(display->getWidth()); + + // Render screen based on currentMode_Location + if (currentMode_Location == MODE_DISTANCE) { + drawNodeListScreen(display, state, x, y, title, drawNodeDistance); + } else if (currentMode_Location == MODE_BEARING) { + drawNodeListWithCompasses(display, state, x, y); + } + + // Track the last mode to avoid reinitializing modeStartTime + lastRenderedMode = currentMode_Location; } #endif @@ -566,14 +733,12 @@ void drawHopSignalScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_ #endif drawNodeListScreen(display, state, x, y, title, drawEntryHopSignal); } - void drawDistanceScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { const char *title = "Distance"; drawNodeListScreen(display, state, x, y, title, drawNodeDistance); } #endif - void drawNodeListWithCompasses(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { float heading = 0; diff --git a/src/graphics/draw/NodeListRenderer.h b/src/graphics/draw/NodeListRenderer.h index ea8df8bd9..e212c031b 100644 --- a/src/graphics/draw/NodeListRenderer.h +++ b/src/graphics/draw/NodeListRenderer.h @@ -23,8 +23,11 @@ namespace NodeListRenderer typedef void (*EntryRenderer)(OLEDDisplay *, meshtastic_NodeInfoLite *, int16_t, int16_t, int); typedef void (*NodeExtrasRenderer)(OLEDDisplay *, meshtastic_NodeInfoLite *, int16_t, int16_t, int, float, double, double); -// Node list mode enumeration -enum NodeListMode { MODE_LAST_HEARD = 0, MODE_HOP_SIGNAL = 1, MODE_DISTANCE = 2, MODE_COUNT = 3 }; +// Node list mode enumeration for Last Heard and Hop Signal views +enum ListMode_Node { MODE_LAST_HEARD = 0, MODE_HOP_SIGNAL = 1, MODE_COUNT_NODE = 2 }; + +// Node list mode enumeration for Distance and Bearings views +enum ListMode_Location { MODE_DISTANCE = 0, MODE_BEARING = 1, MODE_COUNT_LOCATION = 2 }; // Main node list screen function void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y, const char *title, @@ -35,7 +38,7 @@ void drawNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t void drawEntryLastHeard(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth); void drawEntryHopSignal(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth); void drawNodeDistance(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth); -void drawEntryDynamic(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth); +void drawEntryDynamic_Nodes(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth); void drawEntryCompass(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16_t x, int16_t y, int columnWidth); // Extras renderers @@ -46,14 +49,20 @@ void drawCompassArrow(OLEDDisplay *display, meshtastic_NodeInfoLite *node, int16 void drawLastHeardScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); void drawHopSignalScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); void drawDistanceScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); -void drawDynamicNodeListScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); +void drawDynamicListScreen_Nodes(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); +void drawDynamicListScreen_Location(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); void drawNodeListWithCompasses(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y); // Utility functions -const char *getCurrentModeTitle(int screenWidth); -const char *getSafeNodeName(meshtastic_NodeInfoLite *node); +const char *getCurrentModeTitle_Nodes(int screenWidth); +const char *getCurrentModeTitle_Location(int screenWidth); +const char *getSafeNodeName(meshtastic_NodeInfoLite *node, int columnWidth); void drawColumns(OLEDDisplay *display, int16_t x, int16_t y, const char **fields); +// Scrolling controls +void scrollUp(); +void scrollDown(); + // Bitmap drawing function void drawScaledXBitmap16x16(int x, int y, int width, int height, const uint8_t *bitmapXBM, OLEDDisplay *display); diff --git a/src/graphics/draw/NotificationRenderer.cpp b/src/graphics/draw/NotificationRenderer.cpp index e95cc1610..8d76b4592 100644 --- a/src/graphics/draw/NotificationRenderer.cpp +++ b/src/graphics/draw/NotificationRenderer.cpp @@ -1,6 +1,6 @@ #include "configuration.h" -#if HAS_SCREEN +#if HAS_SCREEN #include "DisplayFormatters.h" #include "NodeDB.h" #include "NotificationRenderer.h" @@ -38,7 +38,7 @@ extern bool hasUnreadMessage; namespace graphics { - +int bannerSignalBars = -1; InputEvent NotificationRenderer::inEvent; int8_t NotificationRenderer::curSelected = 0; char NotificationRenderer::alertBannerMessage[256] = {0}; @@ -321,7 +321,7 @@ void NotificationRenderer::drawNodePicker(OLEDDisplay *display, OLEDDisplayUiSta } if (i == curSelected) { selectedNodenum = nodeDB->getMeshNodeByIndex(i + 1)->num; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { strncpy(scratchLineBuffer[scratchLineNum], "> ", 3); strncpy(scratchLineBuffer[scratchLineNum] + 2, temp_name, 36); strncpy(scratchLineBuffer[scratchLineNum] + strlen(temp_name) + 2, " <", 3); @@ -449,7 +449,7 @@ void NotificationRenderer::drawAlertBannerOverlay(OLEDDisplay *display, OLEDDisp for (int i = firstOptionToShow; i < alertBannerOptions && linesShown < visibleTotalLines; i++, linesShown++) { if (i == curSelected) { - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { strncpy(lineBuffer, "> ", 3); strncpy(lineBuffer + 2, optionsArrayPtr[i], 36); strncpy(lineBuffer + strlen(optionsArrayPtr[i]) + 2, " <", 3); @@ -477,7 +477,7 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay bool is_picker = false; uint16_t lineCount = 0; - // === Layout Configuration === + // Layout Configuration constexpr uint16_t hPadding = 5; constexpr uint16_t vPadding = 2; bool needs_bell = false; @@ -491,13 +491,32 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay display->setFont(FONT_SMALL); display->setTextAlignment(TEXT_ALIGN_LEFT); + // Track widest line INCLUDING bars (but don't change per-line widths) + uint16_t widestLineWithBars = 0; + while (lines[lineCount] != nullptr) { auto newlinePointer = strchr(lines[lineCount], '\n'); if (newlinePointer) lineLengths[lineCount] = (newlinePointer - lines[lineCount]); // Check for newlines first else // if the newline wasn't found, then pull string length from strlen lineLengths[lineCount] = strlen(lines[lineCount]); + lineWidths[lineCount] = display->getStringWidth(lines[lineCount], lineLengths[lineCount], true); + + // Consider extra width for signal bars on lines that contain "Signal:" + uint16_t potentialWidth = lineWidths[lineCount]; + if (graphics::bannerSignalBars >= 0 && strncmp(lines[lineCount], "Signal:", 7) == 0) { + const int totalBars = 5; + const int barWidth = 3; + const int barSpacing = 2; + const int gap = 6; // space between text and bars + int barsWidth = totalBars * barWidth + (totalBars - 1) * barSpacing + gap; + potentialWidth += barsWidth; + } + + if (potentialWidth > widestLineWithBars) + widestLineWithBars = potentialWidth; + if (!is_picker) { needs_bell |= (strstr(alertBannerMessage, "Alert Received") != nullptr); if (lineWidths[lineCount] > maxWidth) @@ -507,12 +526,16 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay } // count lines + // Ensure box accounts for signal bars if present + if (widestLineWithBars > maxWidth) + maxWidth = widestLineWithBars; + uint16_t boxWidth = hPadding * 2 + maxWidth; -#if defined(M5STACK_UNITC6L) + if (needs_bell) { - if (isHighResolution && boxWidth <= 150) + if ((currentResolution == ScreenResolution::High) && boxWidth <= 150) boxWidth += 26; - if (!isHighResolution && boxWidth <= 100) + if ((currentResolution == ScreenResolution::Low || currentResolution == ScreenResolution::UltraLow) && boxWidth <= 100) boxWidth += 20; } @@ -521,14 +544,17 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay uint8_t visibleTotalLines = std::min(lineCount, (screenHeight - vPadding * 2) / effectiveLineHeight); uint16_t contentHeight = visibleTotalLines * effectiveLineHeight; uint16_t boxHeight = contentHeight + vPadding * 2; - if (visibleTotalLines == 1) - boxHeight += (isHighResolution ? 4 : 3); + if (visibleTotalLines == 1) { + boxHeight += (currentResolution == ScreenResolution::High) ? 4 : 3; + } int16_t boxLeft = (display->width() / 2) - (boxWidth / 2); - if (totalLines > visibleTotalLines) - boxWidth += (isHighResolution ? 4 : 2); + if (totalLines > visibleTotalLines) { + boxWidth += (currentResolution == ScreenResolution::High) ? 4 : 2; + } int16_t boxTop = (display->height() / 2) - (boxHeight / 2); - + boxHeight += (currentResolution == ScreenResolution::High) ? 2 : 1; +#if defined(M5STACK_UNITC6L) if (visibleTotalLines == 1) { boxTop += 25; } @@ -539,127 +565,9 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay if (boxTop < 0) boxTop = 0; } +#endif - // === Draw Box === - display->setColor(BLACK); - display->fillRect(boxLeft, boxTop, boxWidth, boxHeight); - display->setColor(WHITE); - display->drawRect(boxLeft, boxTop, boxWidth, boxHeight); - display->fillRect(boxLeft, boxTop - 2, boxWidth, 1); - display->fillRect(boxLeft - 2, boxTop, 1, boxHeight); - display->fillRect(boxLeft + boxWidth + 1, boxTop, 1, boxHeight); - display->setColor(BLACK); - display->fillRect(boxLeft, boxTop, 1, 1); - display->fillRect(boxLeft + boxWidth - 1, boxTop, 1, 1); - display->fillRect(boxLeft, boxTop + boxHeight - 1, 1, 1); - display->fillRect(boxLeft + boxWidth - 1, boxTop + boxHeight - 1, 1, 1); - display->setColor(WHITE); - int16_t lineY = boxTop + vPadding; - int swingRange = 8; - static int swingOffset = 0; - static bool swingRight = true; - static unsigned long lastSwingTime = 0; - unsigned long now = millis(); - int swingSpeedMs = 10 / (swingRange * 2); - if (now - lastSwingTime >= (unsigned long)swingSpeedMs) { - lastSwingTime = now; - if (swingRight) { - swingOffset++; - if (swingOffset >= swingRange) - swingRight = false; - } else { - swingOffset--; - if (swingOffset <= 0) - swingRight = true; - } - } - for (int i = 0; i < lineCount; i++) { - bool isTitle = (i == 0); - int globalOptionIndex = (i - 1) + firstOptionToShow; - bool isSelectedOption = (!isTitle && globalOptionIndex >= 0 && globalOptionIndex == curSelected); - - uint16_t visibleWidth = 64 - hPadding * 2; - if (totalLines > visibleTotalLines) - visibleWidth -= 6; - char lineBuffer[lineLengths[i] + 1]; - strncpy(lineBuffer, lines[i], lineLengths[i]); - lineBuffer[lineLengths[i]] = '\0'; - - if (isTitle) { - if (visibleTotalLines == 1) { - display->setColor(BLACK); - display->fillRect(boxLeft, boxTop, boxWidth, effectiveLineHeight); - display->setColor(WHITE); - display->drawString(boxLeft + (boxWidth - lineWidths[i]) / 2, boxTop, lineBuffer); - } else { - display->setColor(WHITE); - display->fillRect(boxLeft, boxTop, boxWidth, effectiveLineHeight); - display->setColor(BLACK); - display->drawString(boxLeft + (boxWidth - lineWidths[i]) / 2, boxTop, lineBuffer); - display->setColor(WHITE); - if (needs_bell) { - int bellY = boxTop + (FONT_HEIGHT_SMALL - 8) / 2; - display->drawXbm(boxLeft + (boxWidth - lineWidths[i]) / 2 - 10, bellY, 8, 8, bell_alert); - display->drawXbm(boxLeft + (boxWidth + lineWidths[i]) / 2 + 2, bellY, 8, 8, bell_alert); - } - } - lineY = boxTop + effectiveLineHeight + 1; - } else if (isSelectedOption) { - display->setColor(WHITE); - display->fillRect(boxLeft, lineY, boxWidth, effectiveLineHeight); - display->setColor(BLACK); - if (lineLengths[i] > 15 && lineWidths[i] > visibleWidth) { - int textX = boxLeft + hPadding + swingOffset; - display->drawString(textX, lineY - 1, lineBuffer); - } else { - display->drawString(boxLeft + (boxWidth - lineWidths[i]) / 2, lineY - 1, lineBuffer); - } - display->setColor(WHITE); - lineY += effectiveLineHeight; - } else { - display->setColor(BLACK); - display->fillRect(boxLeft, lineY, boxWidth, effectiveLineHeight); - display->setColor(WHITE); - display->drawString(boxLeft + (boxWidth - lineWidths[i]) / 2, lineY, lineBuffer); - lineY += effectiveLineHeight; - } - } - if (totalLines > visibleTotalLines) { - const uint8_t scrollBarWidth = 5; - int16_t scrollBarX = boxLeft + boxWidth - scrollBarWidth - 2; - int16_t scrollBarY = boxTop + vPadding + effectiveLineHeight; - uint16_t scrollBarHeight = boxHeight - vPadding * 2 - effectiveLineHeight; - float ratio = (float)visibleTotalLines / totalLines; - uint16_t indicatorHeight = std::max((int)(scrollBarHeight * ratio), 4); - float scrollRatio = (float)(firstOptionToShow + lineCount - visibleTotalLines) / (totalLines - visibleTotalLines); - uint16_t indicatorY = scrollBarY + scrollRatio * (scrollBarHeight - indicatorHeight); - display->drawRect(scrollBarX, scrollBarY, scrollBarWidth, scrollBarHeight); - display->fillRect(scrollBarX + 1, indicatorY, scrollBarWidth - 2, indicatorHeight); - } -#else - if (needs_bell) { - if (isHighResolution && boxWidth <= 150) - boxWidth += 26; - if (!isHighResolution && boxWidth <= 100) - boxWidth += 20; - } - - uint16_t screenHeight = display->height(); - uint8_t effectiveLineHeight = FONT_HEIGHT_SMALL - 3; - uint8_t visibleTotalLines = std::min(lineCount, (screenHeight - vPadding * 2) / effectiveLineHeight); - uint16_t contentHeight = visibleTotalLines * effectiveLineHeight; - uint16_t boxHeight = contentHeight + vPadding * 2; - if (visibleTotalLines == 1) { - boxHeight += (isHighResolution) ? 4 : 3; - } - - int16_t boxLeft = (display->width() / 2) - (boxWidth / 2); - if (totalLines > visibleTotalLines) { - boxWidth += (isHighResolution) ? 4 : 2; - } - int16_t boxTop = (display->height() / 2) - (boxHeight / 2); - - // === Draw Box === + // Draw Box display->setColor(BLACK); display->fillRect(boxLeft - 1, boxTop - 1, boxWidth + 2, boxHeight + 2); display->fillRect(boxLeft, boxTop - 2, boxWidth, 1); @@ -675,7 +583,7 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay display->fillRect(boxLeft + boxWidth - 1, boxTop + boxHeight - 1, 1, 1); display->setColor(WHITE); - // === Draw Content === + // Draw Content int16_t lineY = boxTop + vPadding; for (int i = 0; i < lineCount; i++) { int16_t textX = boxLeft + (boxWidth - lineWidths[i]) / 2; @@ -704,17 +612,47 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay lineY += (effectiveLineHeight - 2 - background_yOffset); } else { // Pop-up - display->drawString(textX, lineY, lineBuffer); + // If this is the Signal line, center text + bars as one group + bool isSignalLine = (graphics::bannerSignalBars >= 0 && strstr(lineBuffer, "Signal:") != nullptr); + if (isSignalLine) { + const int totalBars = 5; + const int barWidth = 3; + const int barSpacing = 2; + const int barHeightStep = 2; + const int gap = 6; + + int textWidth = display->getStringWidth(lineBuffer, strlen(lineBuffer), true); + int barsWidth = totalBars * barWidth + (totalBars - 1) * barSpacing + gap; + int totalWidth = textWidth + barsWidth; + int groupStartX = boxLeft + (boxWidth - totalWidth) / 2; + + display->drawString(groupStartX, lineY, lineBuffer); + + int baseX = groupStartX + textWidth + gap; + int baseY = lineY + effectiveLineHeight - 1; + for (int b = 0; b < totalBars; b++) { + int barHeight = (b + 1) * barHeightStep; + int x = baseX + b * (barWidth + barSpacing); + int y = baseY - barHeight; + + if (b < graphics::bannerSignalBars) { + display->fillRect(x, y, barWidth, barHeight); + } else { + display->drawRect(x, y, barWidth, barHeight); + } + } + } else { + display->drawString(textX, lineY, lineBuffer); + } lineY += (effectiveLineHeight); } } - // === Scroll Bar (Thicker, inside box, not over title) === + // Scroll Bar (Thicker, inside box, not over title) if (totalLines > visibleTotalLines) { const uint8_t scrollBarWidth = 5; - int16_t scrollBarX = boxLeft + boxWidth - scrollBarWidth - 2; - int16_t scrollBarY = boxTop + vPadding + effectiveLineHeight; // start after title line + int16_t scrollBarY = boxTop + vPadding + effectiveLineHeight; uint16_t scrollBarHeight = boxHeight - vPadding * 2 - effectiveLineHeight; float ratio = (float)visibleTotalLines / totalLines; @@ -725,7 +663,6 @@ void NotificationRenderer::drawNotificationBox(OLEDDisplay *display, OLEDDisplay display->drawRect(scrollBarX, scrollBarY, scrollBarWidth, scrollBarHeight); display->fillRect(scrollBarX + 1, indicatorY, scrollBarWidth - 2, indicatorHeight); } -#endif } /// Draw the last text message we received diff --git a/src/graphics/draw/UIRenderer.cpp b/src/graphics/draw/UIRenderer.cpp index 1f01640bf..7ce9d5afe 100644 --- a/src/graphics/draw/UIRenderer.cpp +++ b/src/graphics/draw/UIRenderer.cpp @@ -6,10 +6,7 @@ #include "NodeListRenderer.h" #include "UIRenderer.h" #include "airtime.h" -#include "configuration.h" #include "gps/GeoCoord.h" -#include "graphics/Screen.h" -#include "graphics/ScreenFonts.h" #include "graphics/SharedUIDisplay.h" #include "graphics/TimeFormatters.h" #include "graphics/images.h" @@ -29,6 +26,16 @@ namespace graphics NodeNum UIRenderer::currentFavoriteNodeNum = 0; std::vector graphics::UIRenderer::favoritedNodes; +static inline void drawSatelliteIcon(OLEDDisplay *display, int16_t x, int16_t y) +{ + int yOffset = (currentResolution == ScreenResolution::High) ? -5 : 1; + if (currentResolution == ScreenResolution::High) { + NodeListRenderer::drawScaledXBitmap16x16(x, y + yOffset, imgSatellite_width, imgSatellite_height, imgSatellite, display); + } else { + display->drawXbm(x + 1, y + yOffset, imgSatellite_width, imgSatellite_height, imgSatellite); + } +} + void graphics::UIRenderer::rebuildFavoritedNodes() { favoritedNodes.clear(); @@ -56,7 +63,7 @@ extern uint32_t dopThresholds[5]; void UIRenderer::drawGps(OLEDDisplay *display, int16_t x, int16_t y, const meshtastic::GPSStatus *gps) { // Draw satellite image - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { NodeListRenderer::drawScaledXBitmap16x16(x, y - 2, imgSatellite_width, imgSatellite_height, imgSatellite, display); } else { display->drawXbm(x + 1, y + 1, imgSatellite_width, imgSatellite_height, imgSatellite); @@ -76,7 +83,7 @@ void UIRenderer::drawGps(OLEDDisplay *display, int16_t x, int16_t y, const mesht } else { snprintf(textString, sizeof(textString), "%u sats", gps->getNumSatellites()); } - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { display->drawString(x + 18, y, textString); } else { display->drawString(x + 11, y, textString); @@ -244,16 +251,16 @@ void UIRenderer::drawGpsCoordinates(OLEDDisplay *display, int16_t x, int16_t y, // Draw nodes status void UIRenderer::drawNodes(OLEDDisplay *display, int16_t x, int16_t y, const meshtastic::NodeStatus *nodeStatus, int node_offset, - bool show_total, String additional_words) + bool show_total, const char *additional_words) { char usersString[20]; int nodes_online = (nodeStatus->getNumOnline() > 0) ? nodeStatus->getNumOnline() + node_offset : 0; - snprintf(usersString, sizeof(usersString), "%d %s", nodes_online, additional_words.c_str()); + snprintf(usersString, sizeof(usersString), "%d %s", nodes_online, additional_words); if (show_total) { int nodes_total = (nodeStatus->getNumTotal() > 0) ? nodeStatus->getNumTotal() + node_offset : 0; - snprintf(usersString, sizeof(usersString), "%d/%d %s", nodes_online, nodes_total, additional_words.c_str()); + snprintf(usersString, sizeof(usersString), "%d/%d %s", nodes_online, nodes_total, additional_words); } #if (defined(USE_EINK) || defined(ILI9341_DRIVER) || defined(ILI9342_DRIVER) || defined(ST7701_CS) || defined(ST7735_CS) || \ @@ -261,19 +268,19 @@ void UIRenderer::drawNodes(OLEDDisplay *display, int16_t x, int16_t y, const mes defined(HACKADAY_COMMUNICATOR) || defined(USE_ST7796)) && \ !defined(DISPLAY_FORCE_SMALL_FONTS) - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { NodeListRenderer::drawScaledXBitmap16x16(x, y - 1, 8, 8, imgUser, display); } else { display->drawFastImage(x, y + 3, 8, 8, imgUser); } #else - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { NodeListRenderer::drawScaledXBitmap16x16(x, y - 1, 8, 8, imgUser, display); } else { display->drawFastImage(x, y + 1, 8, 8, imgUser); } #endif - int string_offset = (isHighResolution) ? 9 : 0; + int string_offset = (currentResolution == ScreenResolution::High) ? 9 : 0; display->drawString(x + 10 + string_offset, y - 2, usersString); } @@ -321,11 +328,12 @@ void UIRenderer::drawNodeInfo(OLEDDisplay *display, const OLEDDisplayUiState *st int line = 1; // which slot to use next std::string usernameStr; // === 1. Long Name (always try to show first) === -#if defined(M5STACK_UNITC6L) - const char *username = (node->has_user && node->user.long_name[0]) ? node->user.short_name : nullptr; -#else - const char *username = (node->has_user && node->user.long_name[0]) ? node->user.long_name : nullptr; -#endif + const char *username; + if (currentResolution == ScreenResolution::UltraLow) { + username = (node->has_user && node->user.long_name[0]) ? node->user.short_name : nullptr; + } else { + username = (node->has_user && node->user.long_name[0]) ? node->user.long_name : nullptr; + } if (username) { usernameStr = sanitizeString(username); // Sanitize the incoming long_name just in case @@ -501,7 +509,7 @@ void UIRenderer::drawNodeInfo(OLEDDisplay *display, const OLEDDisplayUiState *st const int margin = 4; // --------- PATCH FOR EINK NAV BAR (ONLY CHANGE BELOW) ----------- #if defined(USE_EINK) - const int iconSize = (isHighResolution) ? 16 : 8; + const int iconSize = (currentResolution == ScreenResolution::High) ? 16 : 8; const int navBarHeight = iconSize + 6; #else const int navBarHeight = 0; @@ -559,11 +567,11 @@ void UIRenderer::drawDeviceFocused(OLEDDisplay *display, OLEDDisplayUiState *sta meshtastic_NodeInfoLite *ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum()); // === Header === -#if defined(M5STACK_UNITC6L) - graphics::drawCommonHeader(display, x, y, "Home"); -#else - graphics::drawCommonHeader(display, x, y, ""); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + graphics::drawCommonHeader(display, x, y, "Home"); + } else { + graphics::drawCommonHeader(display, x, y, ""); + } // === Content below header === @@ -578,15 +586,15 @@ void UIRenderer::drawDeviceFocused(OLEDDisplay *display, OLEDDisplayUiState *sta config.display.heading_bold = false; // Display Region and Channel Utilization -#if defined(M5STACK_UNITC6L) - drawNodes(display, x, getTextPositions(display)[line] + 2, nodeStatus, -1, false, "online"); -#else - drawNodes(display, x + 1, getTextPositions(display)[line] + 2, nodeStatus, -1, false, "online"); -#endif + if (currentResolution == ScreenResolution::UltraLow) { + drawNodes(display, x, getTextPositions(display)[line] + 2, nodeStatus, -1, false, "online"); + } else { + drawNodes(display, x + 1, getTextPositions(display)[line] + 2, nodeStatus, -1, false, "online"); + } char uptimeStr[32] = ""; -#if !defined(M5STACK_UNITC6L) - getUptimeStr(millis(), "Up", uptimeStr, sizeof(uptimeStr)); -#endif + if (currentResolution != ScreenResolution::UltraLow) { + getUptimeStr(millis(), "Up", uptimeStr, sizeof(uptimeStr)); + } display->drawString(SCREEN_WIDTH - display->getStringWidth(uptimeStr), getTextPositions(display)[line++], uptimeStr); // === Second Row: Satellites and Voltage === @@ -600,15 +608,8 @@ void UIRenderer::drawDeviceFocused(OLEDDisplay *display, OLEDDisplayUiState *sta } else { displayLine = config.position.gps_mode == meshtastic_Config_PositionConfig_GpsMode_NOT_PRESENT ? "No GPS" : "GPS off"; } - int yOffset = (isHighResolution) ? 3 : 1; - if (isHighResolution) { - NodeListRenderer::drawScaledXBitmap16x16(x, getTextPositions(display)[line] + yOffset - 5, imgSatellite_width, - imgSatellite_height, imgSatellite, display); - } else { - display->drawXbm(x + 1, getTextPositions(display)[line] + yOffset, imgSatellite_width, imgSatellite_height, - imgSatellite); - } - int xOffset = (isHighResolution) ? 6 : 0; + drawSatelliteIcon(display, x, getTextPositions(display)[line]); + int xOffset = (currentResolution == ScreenResolution::High) ? 6 : 0; display->drawString(x + 11 + xOffset, getTextPositions(display)[line], displayLine); } else { UIRenderer::drawGps(display, 0, getTextPositions(display)[line], gpsStatus); @@ -647,21 +648,22 @@ void UIRenderer::drawDeviceFocused(OLEDDisplay *display, OLEDDisplayUiState *sta char chUtilPercentage[10]; snprintf(chUtilPercentage, sizeof(chUtilPercentage), "%2.0f%%", airTime->channelUtilizationPercent()); - int chUtil_x = (isHighResolution) ? display->getStringWidth(chUtil) + 10 : display->getStringWidth(chUtil) + 5; + int chUtil_x = (currentResolution == ScreenResolution::High) ? display->getStringWidth(chUtil) + 10 + : display->getStringWidth(chUtil) + 5; int chUtil_y = getTextPositions(display)[line] + 3; - int chutil_bar_width = (isHighResolution) ? 100 : 50; + int chutil_bar_width = (currentResolution == ScreenResolution::High) ? 100 : 50; if (!config.bluetooth.enabled) { #if defined(USE_EINK) - chutil_bar_width = (isHighResolution) ? 50 : 30; + chutil_bar_width = (currentResolution == ScreenResolution::High) ? 50 : 30; #else - chutil_bar_width = (isHighResolution) ? 80 : 40; + chutil_bar_width = (currentResolution == ScreenResolution::High) ? 80 : 40; #endif } - int chutil_bar_height = (isHighResolution) ? 12 : 7; - int extraoffset = (isHighResolution) ? 6 : 3; + int chutil_bar_height = (currentResolution == ScreenResolution::High) ? 12 : 7; + int extraoffset = (currentResolution == ScreenResolution::High) ? 6 : 3; if (!config.bluetooth.enabled) { - extraoffset = (isHighResolution) ? 6 : 1; + extraoffset = (currentResolution == ScreenResolution::High) ? 6 : 1; } int chutil_percent = airTime->channelUtilizationPercent(); @@ -721,7 +723,7 @@ void UIRenderer::drawDeviceFocused(OLEDDisplay *display, OLEDDisplayUiState *sta // === Fourth & Fifth Rows: Node Identity === int textWidth = 0; int nameX = 0; - int yOffset = (isHighResolution) ? 0 : 5; + int yOffset = (currentResolution == ScreenResolution::High) ? 0 : 5; std::string longNameStr; if (ourNode && ourNode->has_user && strlen(ourNode->user.long_name) > 0) { @@ -759,7 +761,7 @@ void UIRenderer::drawDeviceFocused(OLEDDisplay *display, OLEDDisplayUiState *sta // Start Functions to write date/time to the screen // Helper function to check if a year is a leap year -bool isLeapYear(int year) +constexpr bool isLeapYear(int year) { return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); } @@ -990,15 +992,8 @@ void UIRenderer::drawCompassAndLocationScreen(OLEDDisplay *display, OLEDDisplayU } else { displayLine = config.position.gps_mode == meshtastic_Config_PositionConfig_GpsMode_NOT_PRESENT ? "No GPS" : "GPS off"; } - int yOffset = (isHighResolution) ? 3 : 1; - if (isHighResolution) { - NodeListRenderer::drawScaledXBitmap16x16(x, getTextPositions(display)[line] + yOffset - 5, imgSatellite_width, - imgSatellite_height, imgSatellite, display); - } else { - display->drawXbm(x + 1, getTextPositions(display)[line] + yOffset, imgSatellite_width, imgSatellite_height, - imgSatellite); - } - int xOffset = (isHighResolution) ? 6 : 0; + drawSatelliteIcon(display, x, getTextPositions(display)[line]); + int xOffset = (currentResolution == ScreenResolution::High) ? 6 : 0; display->drawString(x + 11 + xOffset, getTextPositions(display)[line++], displayLine); } else { // Onboard GPS @@ -1156,7 +1151,7 @@ void UIRenderer::drawCompassAndLocationScreen(OLEDDisplay *display, OLEDDisplayU void UIRenderer::drawOEMIconScreen(const char *upperMsg, OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { static const uint8_t xbm[] = USERPREFS_OEM_IMAGE_DATA; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { display->drawXbm(x + (SCREEN_WIDTH - USERPREFS_OEM_IMAGE_WIDTH) / 2, y + (SCREEN_HEIGHT - FONT_HEIGHT_MEDIUM - USERPREFS_OEM_IMAGE_HEIGHT) / 2 + 2, USERPREFS_OEM_IMAGE_WIDTH, USERPREFS_OEM_IMAGE_HEIGHT, xbm); @@ -1181,7 +1176,7 @@ void UIRenderer::drawOEMIconScreen(const char *upperMsg, OLEDDisplay *display, O display->setTextAlignment(TEXT_ALIGN_LEFT); const char *title = USERPREFS_OEM_TEXT; - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { display->drawString(x + getStringCenteredX(title), y + SCREEN_HEIGHT - FONT_HEIGHT_MEDIUM, title); } display->setFont(FONT_SMALL); @@ -1225,15 +1220,15 @@ void UIRenderer::drawNavigationBar(OLEDDisplay *display, OLEDDisplayUiState *sta lastFrameChangeTime = millis(); } - const int iconSize = isHighResolution ? 16 : 8; - const int spacing = isHighResolution ? 8 : 4; - const int bigOffset = isHighResolution ? 1 : 0; + const int iconSize = (currentResolution == ScreenResolution::High) ? 16 : 8; + const int spacing = (currentResolution == ScreenResolution::High) ? 8 : 4; + const int bigOffset = (currentResolution == ScreenResolution::High) ? 1 : 0; const size_t totalIcons = screen->indicatorIcons.size(); if (totalIcons == 0) return; - const int navPadding = isHighResolution ? 24 : 12; // padding per side + const int navPadding = (currentResolution == ScreenResolution::High) ? 24 : 12; // padding per side int usableWidth = SCREEN_WIDTH - (navPadding * 2); if (usableWidth < iconSize) @@ -1300,7 +1295,7 @@ void UIRenderer::drawNavigationBar(OLEDDisplay *display, OLEDDisplayUiState *sta display->setColor(BLACK); } - if (isHighResolution) { + if (currentResolution == ScreenResolution::High) { NodeListRenderer::drawScaledXBitmap16x16(x, y, 8, 8, icon, display); } else { display->drawXbm(x, y, iconSize, iconSize, icon); @@ -1315,7 +1310,7 @@ void UIRenderer::drawNavigationBar(OLEDDisplay *display, OLEDDisplayUiState *sta auto drawArrow = [&](bool rightSide) { display->setColor(WHITE); - const int offset = isHighResolution ? 3 : 1; + const int offset = (currentResolution == ScreenResolution::High) ? 3 : 1; const int halfH = rectHeight / 2; const int top = (y - 2) + (rectHeight - halfH) / 2; diff --git a/src/graphics/draw/UIRenderer.h b/src/graphics/draw/UIRenderer.h index 438d56cc2..6e37b68f2 100644 --- a/src/graphics/draw/UIRenderer.h +++ b/src/graphics/draw/UIRenderer.h @@ -34,7 +34,7 @@ class UIRenderer public: // Common UI elements static void drawNodes(OLEDDisplay *display, int16_t x, int16_t y, const meshtastic::NodeStatus *nodeStatus, - int node_offset = 0, bool show_total = true, String additional_words = ""); + int node_offset = 0, bool show_total = true, const char *additional_words = ""); // GPS status functions static void drawGps(OLEDDisplay *display, int16_t x, int16_t y, const meshtastic::GPSStatus *gpsStatus); @@ -43,9 +43,6 @@ class UIRenderer static void drawGpsAltitude(OLEDDisplay *display, int16_t x, int16_t y, const meshtastic::GPSStatus *gpsStatus); static void drawGpsPowerStatus(OLEDDisplay *display, int16_t x, int16_t y, const meshtastic::GPSStatus *gpsStatus); - // Layout and utility functions - static void drawScrollbar(OLEDDisplay *display, int visibleItems, int totalItems, int scrollIndex, int x, int startY); - // Overlay and special screens static void drawFrameText(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y, const char *text); @@ -83,8 +80,6 @@ class UIRenderer static std::string drawTimeDelta(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds); static int formatDateTime(char *buffer, size_t bufferSize, uint32_t rtc_sec, OLEDDisplay *display, bool showTime); - // Message filtering - static bool shouldDrawMessage(const meshtastic_MeshPacket *packet); // Check if the display can render a string (detect special chars; emoji) static bool haveGlyphs(const char *str); }; // namespace UIRenderer diff --git a/src/graphics/images.h b/src/graphics/images.h index c268b3269..ef9ffef78 100644 --- a/src/graphics/images.h +++ b/src/graphics/images.h @@ -304,58 +304,6 @@ const uint8_t chirpy[] = { 0x01, 0x18, 0x06, 0x18, 0xe0, 0x01, 0x18, 0x06, 0x18, 0xe0, 0x01, 0x0c, 0x03, 0x30, 0xe0, 0x01, 0x0c, 0x03, 0x30, 0xe0, 0x01, 0x00, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0xe0, 0xfe, 0xff, 0xff, 0xff, 0xdf}; -#define chirpy_width_hirez 76 -#define chirpy_height_hirez 100 -const uint8_t chirpy_hirez[] = { - 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0x03, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0xfc, 0x03, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0xf0, 0x3f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0xf0, 0x3f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0xf0, 0x3f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0xc0, 0x0f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, - 0xfc, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, - 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, - 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x00, 0xfc, 0x03, 0xf0, - 0xff, 0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, - 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, - 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, - 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, - 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, - 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, - 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, - 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, - 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, - 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, - 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, - 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, - 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0x3f, 0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xf0, 0xff, - 0x3f, 0xfc, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x00, 0xfc, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, - 0x00, 0xfc, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, - 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0xfc, 0x03, - 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, - 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xc0, 0x03, 0xfc, 0x03, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, - 0xc0, 0x03, 0xfc, 0x03, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xc0, 0x03, 0xfc, 0x03, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xc0, - 0x03, 0xfc, 0x03, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xf0, 0x00, 0x0f, 0x0f, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xf0, 0x00, - 0x0f, 0x0f, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xf0, 0x00, 0x0f, 0x0f, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0xf0, 0x00, 0x0f, - 0x0f, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x3c, 0xc0, 0x03, 0x3c, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x3c, 0xc0, 0x03, 0x3c, - 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x3c, 0xc0, 0x03, 0x3c, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x3c, 0xc0, 0x03, 0x3c, 0x00, - 0x00, 0xfc, 0x03, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0xf0, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0xf0, 0x00, 0x00, - 0xfc, 0x03, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0xf0, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0xf0, 0x00, 0x00, 0xfc, - 0x03, 0x00, 0xc0, 0x03, 0x3c, 0x00, 0xc0, 0x03, 0x00, 0xfc, 0x03, 0x00, 0xc0, 0x03, 0x3c, 0x00, 0xc0, 0x03, 0x00, 0xfc, 0x03, - 0x00, 0xc0, 0x03, 0x3c, 0x00, 0xc0, 0x03, 0x00, 0xfc, 0x03, 0x00, 0xc0, 0x03, 0x3c, 0x00, 0xc0, 0x03, 0x00, 0xfc, 0x03, 0x00, - 0xf0, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0xfc, 0x03, 0x00, 0xf0, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0xfc, 0x03, 0x00, 0xf0, - 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0xfc, 0x03, 0x00, 0xf0, 0x00, 0x0f, 0x00, 0x00, 0x0f, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xf3, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3}; - #define chirpy_small_image_width 8 #define chirpy_small_image_height 8 const uint8_t chirpy_small[] = {0x7f, 0x41, 0x55, 0x55, 0x55, 0x55, 0x41, 0x7f}; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 0085c806b..12d0822f6 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -489,8 +489,6 @@ int32_t KbI2cBase::runOnce() case 0x90: // fn+r INPUT_BROKER_MSG_REBOOT case 0x91: // fn+t case 0xac: // fn+m INPUT_BROKER_MSG_MUTE_TOGGLE - - case 0x8b: // fn+del INPUT_BROKEN_MSG_DISMISS_FRAME case 0xAA: // fn+b INPUT_BROKER_MSG_BLUETOOTH_TOGGLE case 0x8F: // fn+e INPUT_BROKER_MSG_EMOTE_LIST // just pass those unmodified diff --git a/src/mesh/MeshModule.h b/src/mesh/MeshModule.h index e7178bcfe..63f401d18 100644 --- a/src/mesh/MeshModule.h +++ b/src/mesh/MeshModule.h @@ -44,6 +44,7 @@ struct UIFrameEvent { REDRAW_ONLY, // Don't change which frames are show, just redraw, asap REGENERATE_FRAMESET, // Regenerate (change? add? remove?) screen frames, honoring requestFocus() REGENERATE_FRAMESET_BACKGROUND, // Regenerate screen frames, Attempt to remain on the same frame throughout + SWITCH_TO_TEXTMESSAGE // Jump directly to the Text Message screen } action = REDRAW_ONLY; // We might want to pass additional data inside this struct at some point diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index 297404747..c63e6d2d2 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -7,10 +7,12 @@ #include "../concurrency/Periodic.h" #include "BluetoothCommon.h" // needed for updateBatteryLevel, FIXME, eventually when we pull mesh out into a lib we shouldn't be whacking bluetooth from here #include "MeshService.h" +#include "MessageStore.h" #include "NodeDB.h" #include "PowerFSM.h" #include "RTC.h" #include "TypeConversions.h" +#include "graphics/draw/MessageRenderer.h" #include "main.h" #include "mesh-pb-constants.h" #include "meshUtils.h" @@ -192,8 +194,16 @@ void MeshService::handleToRadio(meshtastic_MeshPacket &p) p.id = generatePacketId(); // If the phone didn't supply one, then pick one p.rx_time = getValidTime(RTCQualityFromNet); // Record the time the packet arrived from the phone - // (so we update our nodedb for the local node) +#if HAS_SCREEN + if (p.decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP && p.decoded.payload.size > 0 && p.to != NODENUM_BROADCAST && + p.to != 0) // DM only + { + perhapsDecode(&p); + const StoredMessage &sm = messageStore.addFromPacket(p); + graphics::MessageRenderer::handleNewMessage(nullptr, sm, p); // notify UI + } +#endif // Send the packet into the mesh DEBUG_HEAP_BEFORE; auto a = packetPool.allocCopy(p); diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index 73ee26903..5af9ebaac 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -7,12 +7,15 @@ #include "Channels.h" #include "FSCommon.h" #include "MeshService.h" +#include "MessageStore.h" #include "NodeDB.h" #include "SPILock.h" #include "buzz.h" #include "detect/ScanI2C.h" +#include "gps/RTC.h" #include "graphics/Screen.h" #include "graphics/SharedUIDisplay.h" +#include "graphics/draw/MessageRenderer.h" #include "graphics/draw/NotificationRenderer.h" #include "graphics/emotes.h" #include "graphics/images.h" @@ -21,6 +24,7 @@ #include "mesh/generated/meshtastic/cannedmessages.pb.h" #include "modules/AdminModule.h" #include "modules/ExternalNotificationModule.h" // for buzzer control +extern MessageStore messageStore; #if HAS_TRACKBALL #include "input/TrackballInterruptImpl1.h" #endif @@ -41,6 +45,75 @@ // Remove Canned message screen if no action is taken for some milliseconds #define INACTIVATE_AFTER_MS 20000 +// Tokenize a message string into emote/text segments +static std::vector> tokenizeMessageWithEmotes(const char *msg) +{ + std::vector> tokens; + int msgLen = strlen(msg); + int pos = 0; + while (pos < msgLen) { + const graphics::Emote *foundEmote = nullptr; + int foundLen = 0; + for (int j = 0; j < graphics::numEmotes; j++) { + const char *label = graphics::emotes[j].label; + int labelLen = strlen(label); + if (labelLen == 0) + continue; + if (strncmp(msg + pos, label, labelLen) == 0) { + if (!foundEmote || labelLen > foundLen) { + foundEmote = &graphics::emotes[j]; + foundLen = labelLen; + } + } + } + if (foundEmote) { + tokens.emplace_back(true, String(foundEmote->label)); + pos += foundLen; + } else { + // Find next emote + int nextEmote = msgLen; + for (int j = 0; j < graphics::numEmotes; j++) { + const char *label = graphics::emotes[j].label; + if (!label || !*label) + continue; + const char *found = strstr(msg + pos, label); + if (found && (found - msg) < nextEmote) { + nextEmote = found - msg; + } + } + int textLen = (nextEmote > pos) ? (nextEmote - pos) : (msgLen - pos); + if (textLen > 0) { + tokens.emplace_back(false, String(msg + pos).substring(0, textLen)); + pos += textLen; + } else { + break; + } + } + } + return tokens; +} + +// Render a single emote token centered vertically on a row +static void renderEmote(OLEDDisplay *display, int &nextX, int lineY, int rowHeight, const String &label) +{ + const graphics::Emote *emote = nullptr; + for (int j = 0; j < graphics::numEmotes; j++) { + if (label == graphics::emotes[j].label) { + emote = &graphics::emotes[j]; + break; + } + } + if (emote) { + int emoteYOffset = (rowHeight - emote->height) / 2; // vertically center the emote + display->drawXbm(nextX, lineY + emoteYOffset, emote->width, emote->height, emote->bitmap); + nextX += emote->width + 2; // spacing between tokens + } +} + +namespace graphics +{ +extern int bannerSignalBars; +} extern ScanI2C::DeviceAddress cardkb_found; extern bool graphics::isMuted; extern bool osk_found; @@ -72,18 +145,16 @@ CannedMessageModule::CannedMessageModule() void CannedMessageModule::LaunchWithDestination(NodeNum newDest, uint8_t newChannel) { - // Use the requested destination, unless it's "broadcast" and we have a previous node/channel - if (newDest == NODENUM_BROADCAST && lastDestSet) { - newDest = lastDest; - newChannel = lastChannel; - } + // Do NOT override explicit broadcast replies + // Only reuse lastDest in LaunchRepeatDestination() + dest = newDest; channel = newChannel; + lastDest = dest; lastChannel = channel; lastDestSet = true; - // Rest of function unchanged... // Upon activation, highlight "[Select Destination]" int selectDestination = 0; for (int i = 0; i < messagesCount; ++i) { @@ -100,6 +171,8 @@ void CannedMessageModule::LaunchWithDestination(NodeNum newDest, uint8_t newChan UIFrameEvent e; e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; notifyObservers(&e); + + LOG_DEBUG("[CannedMessage] LaunchWithDestination dest=0x%08x ch=%d", dest, channel); } void CannedMessageModule::LaunchRepeatDestination() @@ -113,13 +186,12 @@ void CannedMessageModule::LaunchRepeatDestination() void CannedMessageModule::LaunchFreetextWithDestination(NodeNum newDest, uint8_t newChannel) { - // Use the requested destination, unless it's "broadcast" and we have a previous node/channel - if (newDest == NODENUM_BROADCAST && lastDestSet) { - newDest = lastDest; - newChannel = lastChannel; - } + // Do NOT override explicit broadcast replies + // Only reuse lastDest in LaunchRepeatDestination() + dest = newDest; channel = newChannel; + lastDest = dest; lastChannel = channel; lastDestSet = true; @@ -129,6 +201,8 @@ void CannedMessageModule::LaunchFreetextWithDestination(NodeNum newDest, uint8_t UIFrameEvent e; e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; notifyObservers(&e); + + LOG_DEBUG("[CannedMessage] LaunchFreetextWithDestination dest=0x%08x ch=%d", dest, channel); } static bool returnToCannedList = false; @@ -150,7 +224,7 @@ int CannedMessageModule::splitConfiguredMessages() String canned_messages = cannedMessageModuleConfig.messages; // Copy all message parts into the buffer - strncpy(this->messageStore, canned_messages.c_str(), sizeof(this->messageStore)); + strncpy(this->messageBuffer, canned_messages.c_str(), sizeof(this->messageBuffer)); // Temporary array to allow for insertion const char *tempMessages[CANNED_MESSAGE_MODULE_MESSAGE_MAX_COUNT + 3] = {0}; @@ -167,16 +241,16 @@ int CannedMessageModule::splitConfiguredMessages() #endif // First message always starts at buffer start - tempMessages[tempCount++] = this->messageStore; - int upTo = strlen(this->messageStore) - 1; + tempMessages[tempCount++] = this->messageBuffer; + int upTo = strlen(this->messageBuffer) - 1; // Walk buffer, splitting on '|' while (i < upTo) { - if (this->messageStore[i] == '|') { - this->messageStore[i] = '\0'; // End previous message + if (this->messageBuffer[i] == '|') { + this->messageBuffer[i] = '\0'; // End previous message if (tempCount >= CANNED_MESSAGE_MODULE_MESSAGE_MAX_COUNT) break; - tempMessages[tempCount++] = (this->messageStore + i + 1); + tempMessages[tempCount++] = (this->messageBuffer + i + 1); } i += 1; } @@ -194,25 +268,23 @@ int CannedMessageModule::splitConfiguredMessages() } void CannedMessageModule::drawHeader(OLEDDisplay *display, int16_t x, int16_t y, char *buffer) { - if (graphics::isHighResolution) { + if (graphics::currentResolution == graphics::ScreenResolution::High) { if (this->dest == NODENUM_BROADCAST) { - display->drawStringf(x, y, buffer, "To: Broadcast@%s", channels.getName(this->channel)); + display->drawStringf(x, y, buffer, "To: #%s", channels.getName(this->channel)); } else { - display->drawStringf(x, y, buffer, "To: %s", getNodeName(this->dest)); + display->drawStringf(x, y, buffer, "To: @%s", getNodeName(this->dest)); } } else { if (this->dest == NODENUM_BROADCAST) { - display->drawStringf(x, y, buffer, "To: Broadc@%.5s", channels.getName(this->channel)); + display->drawStringf(x, y, buffer, "To: #%.20s", channels.getName(this->channel)); } else { - display->drawStringf(x, y, buffer, "To: %s", getNodeName(this->dest)); + display->drawStringf(x, y, buffer, "To: @%s", getNodeName(this->dest)); } } } void CannedMessageModule::resetSearch() { - LOG_INFO("Resetting search, restoring full destination list"); - int previousDestIndex = destIndex; searchQuery = ""; @@ -274,6 +346,10 @@ void CannedMessageModule::updateDestinationSelectionList() } } + meshtastic_MeshPacket *p = allocDataPacket(); + p->pki_encrypted = true; + p->channel = 0; + // Populate active channels std::vector seenChannels; seenChannels.reserve(channels.getNumChannels()); @@ -285,15 +361,6 @@ void CannedMessageModule::updateDestinationSelectionList() } } - /* As the nodeDB is sorted, can skip this step - // Sort by favorite, then last heard - std::sort(this->filteredNodes.begin(), this->filteredNodes.end(), [](const NodeEntry &a, const NodeEntry &b) { - if (a.node->is_favorite != b.node->is_favorite) - return a.node->is_favorite > b.node->is_favorite; - return a.lastHeard < b.lastHeard; - }); - */ - scrollIndex = 0; // Show first result at the top destIndex = 0; // Highlight the first entry if (nodesChanged && runState == CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION) { @@ -361,16 +428,7 @@ int CannedMessageModule::handleInputEvent(const InputEvent *event) return handleEmotePickerInput(event); case CANNED_MESSAGE_RUN_STATE_INACTIVE: - if (isSelect) { - return 0; // Main button press no longer runs through powerFSM - } - // Let LEFT/RIGHT pass through so frame navigation works - if (event->inputEvent == INPUT_BROKER_LEFT || event->inputEvent == INPUT_BROKER_RIGHT) { - break; - } - // Handle UP/DOWN: activate canned message list! - if (event->inputEvent == INPUT_BROKER_UP || event->inputEvent == INPUT_BROKER_DOWN || - event->inputEvent == INPUT_BROKER_ALT_LONG) { + if (event->inputEvent == INPUT_BROKER_ALT_LONG) { LaunchWithDestination(NODENUM_BROADCAST); return 1; } @@ -384,6 +442,7 @@ int CannedMessageModule::handleInputEvent(const InputEvent *event) // Immediately process the input in the new state (freetext) return handleFreeTextInput(event); } + return 0; break; // (Other states can be added here as needed) @@ -574,7 +633,7 @@ bool CannedMessageModule::handleMessageSelectorInput(const InputEvent *event, bo if (runState == CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION) return false; - // === Handle Cancel key: go inactive, clear UI state === + // Handle Cancel key: go inactive, clear UI state if (runState != CANNED_MESSAGE_RUN_STATE_INACTIVE && (event->inputEvent == INPUT_BROKER_CANCEL || event->inputEvent == INPUT_BROKER_ALT_LONG)) { runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; @@ -603,7 +662,7 @@ bool CannedMessageModule::handleMessageSelectorInput(const InputEvent *event, bo } else if (isSelect) { const char *current = messages[currentMessageIndex]; - // === [Select Destination] triggers destination selection UI === + // [Select Destination] triggers destination selection UI if (strcmp(current, "[Select Destination]") == 0) { returnToCannedList = true; runState = CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION; @@ -614,7 +673,7 @@ bool CannedMessageModule::handleMessageSelectorInput(const InputEvent *event, bo return true; } - // === [Exit] returns to the main/inactive screen === + // [Exit] returns to the main/inactive screen if (strcmp(current, "[Exit]") == 0) { // Set runState to inactive so we return to main UI runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; @@ -628,7 +687,7 @@ bool CannedMessageModule::handleMessageSelectorInput(const InputEvent *event, bo return true; } - // === [Free Text] triggers the free text input (virtual keyboard) === + // [Free Text] triggers the free text input (virtual keyboard) #if defined(USE_VIRTUAL_KEYBOARD) if (strcmp(current, "[-- Free Text --]") == 0) { runState = CANNED_MESSAGE_RUN_STATE_FREETEXT; @@ -643,9 +702,9 @@ bool CannedMessageModule::handleMessageSelectorInput(const InputEvent *event, bo if (osk_found && screen) { char headerBuffer[64]; if (this->dest == NODENUM_BROADCAST) { - snprintf(headerBuffer, sizeof(headerBuffer), "To: Broadcast@%s", channels.getName(this->channel)); + snprintf(headerBuffer, sizeof(headerBuffer), "To: #%s", channels.getName(this->channel)); } else { - snprintf(headerBuffer, sizeof(headerBuffer), "To: %s", getNodeName(this->dest)); + snprintf(headerBuffer, sizeof(headerBuffer), "To: @%s", getNodeName(this->dest)); } screen->showTextInput(headerBuffer, "", 300000, [this](const std::string &text) { if (!text.empty()) { @@ -694,20 +753,12 @@ bool CannedMessageModule::handleMessageSelectorInput(const InputEvent *event, bo if (runState == CANNED_MESSAGE_RUN_STATE_INACTIVE || runState == CANNED_MESSAGE_RUN_STATE_DISABLED) { } else { #if CANNED_MESSAGE_ADD_CONFIRMATION - // Show confirmation dialog before sending canned message - NodeNum destNode = dest; - ChannelIndex chan = channel; - graphics::menuHandler::showConfirmationBanner("Send message?", [this, destNode, chan, current]() { - this->sendText(destNode, chan, current, false); - payload = runState; - runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; - currentMessageIndex = -1; - - // Notify UI to regenerate frame set and redraw - UIFrameEvent e; - e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; - notifyObservers(&e); - screen->forceDisplay(); + const int savedIndex = currentMessageIndex; + graphics::menuHandler::showConfirmationBanner("Send message?", [this, savedIndex]() { + this->currentMessageIndex = savedIndex; + this->payload = this->runState; + this->runState = CANNED_MESSAGE_RUN_STATE_ACTION_SELECT; + this->setIntervalFromNow(0); }); #else payload = runState; @@ -806,7 +857,7 @@ bool CannedMessageModule::handleFreeTextInput(const InputEvent *event) } #endif // USE_VIRTUAL_KEYBOARD - // ---- All hardware keys fall through to here (CardKB, physical, etc.) ---- + // All hardware keys fall through to here (CardKB, physical, etc.) if (event->kbchar == INPUT_BROKER_MSG_EMOTE_LIST) { runState = CANNED_MESSAGE_RUN_STATE_EMOTE_PICKER; @@ -950,57 +1001,110 @@ void CannedMessageModule::sendText(NodeNum dest, ChannelIndex channel, const cha lastDest = dest; lastChannel = channel; lastDestSet = true; - // === Prepare packet === + meshtastic_MeshPacket *p = allocDataPacket(); p->to = dest; p->channel = channel; p->want_ack = true; + p->decoded.dest = dest; // Mirror picker: NODENUM_BROADCAST or node->num - // Save destination for ACK/NACK UI fallback this->lastSentNode = dest; this->incoming = dest; - // Copy message payload + // Manually find the node by number to check PKI capability + meshtastic_NodeInfoLite *node = nullptr; + size_t numMeshNodes = nodeDB->getNumMeshNodes(); + for (size_t i = 0; i < numMeshNodes; ++i) { + meshtastic_NodeInfoLite *n = nodeDB->getMeshNodeByIndex(i); + if (n && n->num == dest) { + node = n; + break; + } + } + + NodeNum myNodeNum = nodeDB->getNodeNum(); + if (node && node->num != myNodeNum && node->has_user && node->user.public_key.size == 32) { + p->pki_encrypted = true; + p->channel = 0; // force PKI + } + + // Track this packet’s request ID for matching ACKs + this->lastRequestId = p->id; + + // Copy payload p->decoded.payload.size = strlen(message); memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size); - // Optionally add bell character if (moduleConfig.canned_message.send_bell && p->decoded.payload.size < meshtastic_Constants_DATA_PAYLOAD_LEN) { - p->decoded.payload.bytes[p->decoded.payload.size++] = 7; // Bell - p->decoded.payload.bytes[p->decoded.payload.size] = '\0'; // Null-terminate + p->decoded.payload.bytes[p->decoded.payload.size++] = 7; + p->decoded.payload.bytes[p->decoded.payload.size] = '\0'; } - // Mark as waiting for ACK to trigger ACK/NACK screen this->waitingForAck = true; - // Log outgoing message - LOG_INFO("Send message id=%u, dest=%x, msg=%.*s", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes); - - if (p->to != 0xffffffff) { - // Only add as favorite if our role is NOT CLIENT_BASE - if (config.device.role != 12) { - LOG_INFO("Proactively adding %x as favorite node", p->to); - nodeDB->set_favorite(true, p->to); - } else { - LOG_DEBUG("Not favoriting node %x as we are CLIENT_BASE role", p->to); - } - - screen->setFrames(graphics::Screen::FOCUS_PRESERVE); - p->pki_encrypted = true; - p->channel = 0; - } - - // Send to mesh and phone (even if no phone connected, to track ACKs) + // Send to mesh (PKI-encrypted if conditions above matched) service->sendToMesh(p, RX_SRC_LOCAL, true); - // === Simulate local message to clear unread UI === + // Show banner immediately if (screen) { - meshtastic_MeshPacket simulatedPacket = {}; - simulatedPacket.from = 0; // Local device - screen->handleTextMessage(&simulatedPacket); + graphics::BannerOverlayOptions opts; + opts.message = "Sending..."; + opts.durationMs = 2000; + screen->showOverlayBanner(opts); } + + // Save outgoing message + StoredMessage sm; + + // Always use our local time, consistent with other paths + uint32_t nowSecs = getValidTime(RTCQuality::RTCQualityDevice, true); + sm.timestamp = (nowSecs > 0) ? nowSecs : millis() / 1000; + sm.isBootRelative = (nowSecs == 0); + + sm.sender = nodeDB->getNodeNum(); // us + sm.channelIndex = channel; + size_t len = strnlen(message, MAX_MESSAGE_SIZE - 1); + sm.textOffset = MessageStore::storeText(message, len); + sm.textLength = len; + + // Classify broadcast vs DM + if (dest == NODENUM_BROADCAST) { + sm.dest = NODENUM_BROADCAST; + sm.type = MessageType::BROADCAST; + } else { + sm.dest = dest; + sm.type = MessageType::DM_TO_US; + // Only add as favorite if our role is NOT CLIENT_BASE + if (config.device.role != 12) { + LOG_INFO("Proactively adding %x as favorite node", dest); + nodeDB->set_favorite(true, dest); + } else { + LOG_DEBUG("Not favoriting node %x as we are CLIENT_BASE role", dest); + } + } + sm.ackStatus = AckStatus::NONE; + + messageStore.addLiveMessage(std::move(sm)); + + // Auto-switch thread view on outgoing message + if (sm.type == MessageType::BROADCAST) { + graphics::MessageRenderer::setThreadMode(graphics::MessageRenderer::ThreadMode::CHANNEL, sm.channelIndex); + } else { + graphics::MessageRenderer::setThreadMode(graphics::MessageRenderer::ThreadMode::DIRECT, -1, sm.dest); + } + playComboTune(); + + this->runState = CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE; + this->payload = wantReplies ? 1 : 0; + requestFocus(); + + // Tell Screen to switch to TextMessage frame via UIFrameEvent + UIFrameEvent e; + e.action = UIFrameEvent::Action::SWITCH_TO_TEXTMESSAGE; + notifyObservers(&e); } + int32_t CannedMessageModule::runOnce() { if (this->runState == CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION && needsUpdate) { @@ -1021,7 +1125,6 @@ int32_t CannedMessageModule::runOnce() graphics::OnScreenKeyboardModule::instance().stop(false); } - temporaryMessage = ""; return INT32_MAX; } @@ -1063,7 +1166,6 @@ int32_t CannedMessageModule::runOnce() (this->runState == CANNED_MESSAGE_RUN_STATE_ACK_NACK_RECEIVED) || (this->runState == CANNED_MESSAGE_RUN_STATE_MESSAGE_SELECTION)) { this->runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; - temporaryMessage = ""; e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; this->currentMessageIndex = -1; this->freetext = ""; @@ -1072,15 +1174,11 @@ int32_t CannedMessageModule::runOnce() } // Handle SENDING_ACTIVE state transition after virtual keyboard message else if (this->runState == CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE && this->payload == 0) { - // This happens after virtual keyboard message sending is complete - LOG_INFO("Virtual keyboard message sending completed, returning to inactive state"); this->runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; - temporaryMessage = ""; - e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; this->currentMessageIndex = -1; this->freetext = ""; this->cursor = 0; - this->notifyObservers(&e); + return INT32_MAX; } else if (((this->runState == CANNED_MESSAGE_RUN_STATE_ACTIVE) || (this->runState == CANNED_MESSAGE_RUN_STATE_FREETEXT)) && !Throttle::isWithinTimespanMs(this->lastTouchMillis, INACTIVATE_AFTER_MS)) { // Reset module on inactivity @@ -1106,7 +1204,21 @@ int32_t CannedMessageModule::runOnce() } else if (this->payload == CANNED_MESSAGE_RUN_STATE_FREETEXT) { if (this->freetext.length() > 0) { sendText(this->dest, this->channel, this->freetext.c_str(), true); - this->runState = CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE; + + // Clean up state but *don’t* deactivate yet + this->currentMessageIndex = -1; + this->freetext = ""; + this->cursor = 0; + + // Tell Screen to jump straight to the TextMessage frame + UIFrameEvent e; + e.action = UIFrameEvent::Action::SWITCH_TO_TEXTMESSAGE; + this->notifyObservers(&e); + + // Now deactivate this module + this->runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; + + return INT32_MAX; // don’t fall back into canned list } else { this->runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; } @@ -1120,37 +1232,59 @@ int32_t CannedMessageModule::runOnce() return INT32_MAX; } else { sendText(this->dest, this->channel, this->messages[this->currentMessageIndex], true); + + // Clean up state + this->currentMessageIndex = -1; + this->freetext = ""; + this->cursor = 0; + + // Tell Screen to jump straight to the TextMessage frame + UIFrameEvent e; + e.action = UIFrameEvent::Action::SWITCH_TO_TEXTMESSAGE; + this->notifyObservers(&e); + + // Now deactivate this module + this->runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; + + return INT32_MAX; // don’t fall back into canned list } - this->runState = CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE; } else { this->runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; } } + // fallback clean-up if nothing above returned this->currentMessageIndex = -1; this->freetext = ""; this->cursor = 0; + + UIFrameEvent e; + e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; this->notifyObservers(&e); - return 2000; + + // Immediately stop, don’t linger on canned screen + return INT32_MAX; } // Highlight [Select Destination] initially when entering the message list else if ((this->runState != CANNED_MESSAGE_RUN_STATE_FREETEXT) && (this->currentMessageIndex == -1)) { - int selectDestination = 0; - for (int i = 0; i < this->messagesCount; ++i) { - if (strcmp(this->messages[i], "[Select Destination]") == 0) { - selectDestination = i; - break; + // Only auto-highlight [Select Destination] if we’re ACTIVELY browsing, + // not when coming back from a sent message. + if (this->runState == CANNED_MESSAGE_RUN_STATE_ACTIVE) { + int selectDestination = 0; + for (int i = 0; i < this->messagesCount; ++i) { + if (strcmp(this->messages[i], "[Select Destination]") == 0) { + selectDestination = i; + break; + } } + this->currentMessageIndex = selectDestination; + e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; } - this->currentMessageIndex = selectDestination; - e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; - this->runState = CANNED_MESSAGE_RUN_STATE_ACTIVE; } else if (this->runState == CANNED_MESSAGE_RUN_STATE_ACTION_UP) { if (this->messagesCount > 0) { this->currentMessageIndex = getPrevIndex(); this->freetext = ""; this->cursor = 0; this->runState = CANNED_MESSAGE_RUN_STATE_ACTIVE; - LOG_DEBUG("MOVE UP (%d):%s", this->currentMessageIndex, this->getCurrentMessage()); } } else if (this->runState == CANNED_MESSAGE_RUN_STATE_ACTION_DOWN) { if (this->messagesCount > 0) { @@ -1158,7 +1292,6 @@ int32_t CannedMessageModule::runOnce() this->freetext = ""; this->cursor = 0; this->runState = CANNED_MESSAGE_RUN_STATE_ACTIVE; - LOG_DEBUG("MOVE DOWN (%d):%s", this->currentMessageIndex, this->getCurrentMessage()); } } else if (this->runState == CANNED_MESSAGE_RUN_STATE_FREETEXT || this->runState == CANNED_MESSAGE_RUN_STATE_ACTIVE) { switch (this->payload) { @@ -1208,7 +1341,7 @@ int32_t CannedMessageModule::runOnce() this->freetext.substring(this->cursor); } this->cursor++; - uint16_t maxChars = meshtastic_Constants_DATA_PAYLOAD_LEN - (moduleConfig.canned_message.send_bell ? 1 : 0); + const uint16_t maxChars = 200 - (moduleConfig.canned_message.send_bell ? 1 : 0); if (this->freetext.length() > maxChars) { this->cursor = maxChars; this->freetext = this->freetext.substring(0, maxChars); @@ -1264,7 +1397,10 @@ const char *CannedMessageModule::getNodeName(NodeNum node) bool CannedMessageModule::shouldDraw() { - return (currentMessageIndex != -1) || (this->runState != CANNED_MESSAGE_RUN_STATE_INACTIVE); + // Only allow drawing when we're in an interactive UI state. + return (this->runState == CANNED_MESSAGE_RUN_STATE_ACTIVE || this->runState == CANNED_MESSAGE_RUN_STATE_FREETEXT || + this->runState == CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION || + this->runState == CANNED_MESSAGE_RUN_STATE_EMOTE_PICKER); } // Has the user defined any canned messages? @@ -1291,16 +1427,6 @@ int CannedMessageModule::getPrevIndex() return this->currentMessageIndex - 1; } } -void CannedMessageModule::showTemporaryMessage(const String &message) -{ - temporaryMessage = message; - UIFrameEvent e; - e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; // We want to change the list of frames shown on-screen - notifyObservers(&e); - runState = CANNED_MESSAGE_RUN_STATE_MESSAGE_SELECTION; - // run this loop again in 2 seconds, next iteration will clear the display - setIntervalFromNow(2000); -} #if defined(USE_VIRTUAL_KEYBOARD) @@ -1523,7 +1649,7 @@ void CannedMessageModule::drawDestinationSelectionScreen(OLEDDisplay *display, O display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - // === Header === + // Header int titleY = 2; String titleText = "Select Destination"; titleText += searchQuery.length() > 0 ? " [" + searchQuery + "]" : " [ ]"; @@ -1531,7 +1657,7 @@ void CannedMessageModule::drawDestinationSelectionScreen(OLEDDisplay *display, O display->drawString(display->getWidth() / 2, titleY, titleText); display->setTextAlignment(TEXT_ALIGN_LEFT); - // === List Items === + // List Items int rowYOffset = titleY + (FONT_HEIGHT_SMALL - 4); int numActiveChannels = this->activeChannelIndices.size(); int totalEntries = numActiveChannels + this->filteredNodes.size(); @@ -1540,7 +1666,7 @@ void CannedMessageModule::drawDestinationSelectionScreen(OLEDDisplay *display, O if (this->visibleRows < 1) this->visibleRows = 1; - // === Clamp scrolling === + // Clamp scrolling if (scrollIndex > totalEntries / columns) scrollIndex = totalEntries / columns; if (scrollIndex < 0) @@ -1558,26 +1684,44 @@ void CannedMessageModule::drawDestinationSelectionScreen(OLEDDisplay *display, O // Draw Channels First if (itemIndex < numActiveChannels) { uint8_t channelIndex = this->activeChannelIndices[itemIndex]; - snprintf(entryText, sizeof(entryText), "@%s", channels.getName(channelIndex)); + snprintf(entryText, sizeof(entryText), "#%s", channels.getName(channelIndex)); } // Then Draw Nodes else { int nodeIndex = itemIndex - numActiveChannels; if (nodeIndex >= 0 && nodeIndex < static_cast(this->filteredNodes.size())) { meshtastic_NodeInfoLite *node = this->filteredNodes[nodeIndex].node; + if (node && node->user.long_name) { + strncpy(entryText, node->user.long_name, sizeof(entryText) - 1); + entryText[sizeof(entryText) - 1] = '\0'; + } + int availWidth = display->getWidth() - + ((graphics::currentResolution == graphics::ScreenResolution::High) ? 40 : 20) - + ((node && node->is_favorite) ? 10 : 0); + if (availWidth < 0) + availWidth = 0; + + size_t origLen = strlen(entryText); + while (entryText[0] && display->getStringWidth(entryText) > availWidth) { + entryText[strlen(entryText) - 1] = '\0'; + } + if (strlen(entryText) < origLen) { + strcat(entryText, "..."); + } + + // Prepend "* " if this is a favorite + if (node && node->is_favorite) { + size_t len = strlen(entryText); + if (len + 2 < sizeof(entryText)) { + memmove(entryText + 2, entryText, len + 1); + entryText[0] = '*'; + entryText[1] = ' '; + } + } if (node) { - if (node->is_favorite) { -#if defined(M5STACK_UNITC6L) - snprintf(entryText, sizeof(entryText), "* %s", node->user.short_name); - } else { + if (display->getWidth() <= 64) { snprintf(entryText, sizeof(entryText), "%s", node->user.short_name); } -#else - snprintf(entryText, sizeof(entryText), "* %s", node->user.long_name); - } else { - snprintf(entryText, sizeof(entryText), "%s", node->user.long_name); - } -#endif } } } @@ -1585,18 +1729,19 @@ void CannedMessageModule::drawDestinationSelectionScreen(OLEDDisplay *display, O if (strlen(entryText) == 0 || strcmp(entryText, "Unknown") == 0) strcpy(entryText, "?"); - // === Highlight background (if selected) === + // Highlight background (if selected) if (itemIndex == destIndex) { int scrollPadding = 8; // Reserve space for scrollbar display->fillRect(0, yOffset + 2, display->getWidth() - scrollPadding, FONT_HEIGHT_SMALL - 5); display->setColor(BLACK); } - // === Draw entry text === + // Draw entry text display->drawString(xOffset + 2, yOffset, entryText); display->setColor(WHITE); - // === Draw key icon (after highlight) === + // Draw key icon (after highlight) + /* if (itemIndex >= numActiveChannels) { int nodeIndex = itemIndex - numActiveChannels; if (nodeIndex >= 0 && nodeIndex < static_cast(this->filteredNodes.size())) { @@ -1614,6 +1759,7 @@ void CannedMessageModule::drawDestinationSelectionScreen(OLEDDisplay *display, O } } } + */ } // Scrollbar @@ -1650,6 +1796,9 @@ void CannedMessageModule::drawEmotePickerScreen(OLEDDisplay *display, OLEDDispla int _visibleRows = (display->getHeight() - listTop - 2) / rowHeight; int numEmotes = graphics::numEmotes; + // keep member variable in sync + this->visibleRows = _visibleRows; + // Clamp highlight index if (emotePickerIndex < 0) emotePickerIndex = 0; @@ -1671,7 +1820,7 @@ void CannedMessageModule::drawEmotePickerScreen(OLEDDisplay *display, OLEDDispla // Draw emote rows display->setTextAlignment(TEXT_ALIGN_LEFT); - for (int vis = 0; vis < visibleRows; ++vis) { + for (int vis = 0; vis < _visibleRows; ++vis) { int emoteIdx = topIndex + vis; if (emoteIdx >= numEmotes) break; @@ -1698,11 +1847,11 @@ void CannedMessageModule::drawEmotePickerScreen(OLEDDisplay *display, OLEDDispla } // Draw scrollbar if needed - if (numEmotes > visibleRows) { - int scrollbarHeight = visibleRows * rowHeight; + if (numEmotes > _visibleRows) { + int scrollbarHeight = _visibleRows * rowHeight; int scrollTrackX = display->getWidth() - 6; display->drawRect(scrollTrackX, listTop, 4, scrollbarHeight); - int scrollBarLen = std::max(6, (scrollbarHeight * visibleRows) / numEmotes); + int scrollBarLen = std::max(6, (scrollbarHeight * _visibleRows) / numEmotes); int scrollBarPos = listTop + (scrollbarHeight * topIndex) / numEmotes; display->fillRect(scrollTrackX, scrollBarPos, 4, scrollBarLen); } @@ -1715,104 +1864,25 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - // === Draw temporary message if available === - if (temporaryMessage.length() != 0) { - requestFocus(); // Tell Screen::setFrames to move to our module's frame - LOG_DEBUG("Draw temporary message: %s", temporaryMessage.c_str()); - display->setTextAlignment(TEXT_ALIGN_CENTER); - display->setFont(FONT_MEDIUM); - display->drawString(display->getWidth() / 2 + x, 0 + y + 12, temporaryMessage); - return; + // Never draw if state is outside our UI modes + if (!(runState == CANNED_MESSAGE_RUN_STATE_ACTIVE || runState == CANNED_MESSAGE_RUN_STATE_FREETEXT || + runState == CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION || runState == CANNED_MESSAGE_RUN_STATE_EMOTE_PICKER)) { + return; // bail if not in a UI state that should render } - // === Emote Picker Screen === + // Emote Picker Screen if (this->runState == CANNED_MESSAGE_RUN_STATE_EMOTE_PICKER) { drawEmotePickerScreen(display, state, x, y); // <-- Call your emote picker drawer here return; } - // === Destination Selection === + // Destination Selection if (this->runState == CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION) { drawDestinationSelectionScreen(display, state, x, y); return; } - // === ACK/NACK Screen === - if (this->runState == CANNED_MESSAGE_RUN_STATE_ACK_NACK_RECEIVED) { - requestFocus(); - EINK_ADD_FRAMEFLAG(display, COSMETIC); - display->setTextAlignment(TEXT_ALIGN_CENTER); - -#ifdef USE_EINK - display->setFont(FONT_SMALL); - int yOffset = y + 10; -#else - display->setFont(FONT_MEDIUM); -#if defined(M5STACK_UNITC6L) - int yOffset = y; -#else - int yOffset = y + 10; -#endif -#endif - - // --- Delivery Status Message --- - if (this->ack) { - if (this->lastSentNode == NODENUM_BROADCAST) { - snprintf(buffer, sizeof(buffer), "Broadcast Sent to\n%s", channels.getName(this->channel)); - } else if (this->lastAckHopLimit > this->lastAckHopStart) { - snprintf(buffer, sizeof(buffer), "Delivered (%d hops)\nto %s", this->lastAckHopLimit - this->lastAckHopStart, - getNodeName(this->incoming)); - } else { - snprintf(buffer, sizeof(buffer), "Delivered\nto %s", getNodeName(this->incoming)); - } - } else { - snprintf(buffer, sizeof(buffer), "Delivery failed\nto %s", getNodeName(this->incoming)); - } - - // Draw delivery message and compute y-offset after text height - int lineCount = 1; - for (const char *ptr = buffer; *ptr; ptr++) { - if (*ptr == '\n') - lineCount++; - } - - display->drawString(display->getWidth() / 2 + x, yOffset, buffer); -#if defined(M5STACK_UNITC6L) - yOffset += lineCount * FONT_HEIGHT_MEDIUM - 5; // only 1 line gap, no extra padding -#else - yOffset += lineCount * FONT_HEIGHT_MEDIUM; // only 1 line gap, no extra padding -#endif -#ifndef USE_EINK - // --- SNR + RSSI Compact Line --- - if (this->ack) { - display->setFont(FONT_SMALL); -#if defined(M5STACK_UNITC6L) - snprintf(buffer, sizeof(buffer), "SNR: %.1f dB \nRSSI: %d", this->lastRxSnr, this->lastRxRssi); -#else - snprintf(buffer, sizeof(buffer), "SNR: %.1f dB RSSI: %d", this->lastRxSnr, this->lastRxRssi); -#endif - display->drawString(display->getWidth() / 2 + x, yOffset, buffer); - } -#endif - - return; - } - - // === Sending Screen === - if (this->runState == CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE) { - EINK_ADD_FRAMEFLAG(display, COSMETIC); - requestFocus(); -#ifdef USE_EINK - display->setFont(FONT_SMALL); -#else - display->setFont(FONT_MEDIUM); -#endif - display->setTextAlignment(TEXT_ALIGN_CENTER); - display->drawString(display->getWidth() / 2 + x, 0 + y + 12, "Sending..."); - return; - } - - // === Disabled Screen === + // Disabled Screen if (this->runState == CANNED_MESSAGE_RUN_STATE_DISABLED) { display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); @@ -1820,7 +1890,7 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st return; } - // === Free Text Input Screen === + // Free Text Input Screen if (this->runState == CANNED_MESSAGE_RUN_STATE_FREETEXT) { requestFocus(); #if defined(USE_EINK) && defined(USE_EINK_DYNAMICDISPLAY) @@ -1833,10 +1903,10 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - // --- Draw node/channel header at the top --- + // Draw node/channel header at the top drawHeader(display, x, y, buffer); - // --- Char count right-aligned --- + // Char count right-aligned if (runState != CANNED_MESSAGE_RUN_STATE_DESTINATION_SELECTION) { uint16_t charsLeft = meshtastic_Constants_DATA_PAYLOAD_LEN - this->freetext.length() - (moduleConfig.canned_message.send_bell ? 1 : 0); @@ -1932,51 +2002,10 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st String msgWithCursor = this->drawWithCursor(this->freetext, this->cursor); // Tokenize input into (isEmote, token) pairs - std::vector> tokens; const char *msg = msgWithCursor.c_str(); - int msgLen = strlen(msg); - int pos = 0; - while (pos < msgLen) { - const graphics::Emote *foundEmote = nullptr; - int foundLen = 0; - for (int j = 0; j < graphics::numEmotes; j++) { - const char *label = graphics::emotes[j].label; - int labelLen = strlen(label); - if (labelLen == 0) - continue; - if (strncmp(msg + pos, label, labelLen) == 0) { - if (!foundEmote || labelLen > foundLen) { - foundEmote = &graphics::emotes[j]; - foundLen = labelLen; - } - } - } - if (foundEmote) { - tokens.emplace_back(true, String(foundEmote->label)); - pos += foundLen; - } else { - // Find next emote - int nextEmote = msgLen; - for (int j = 0; j < graphics::numEmotes; j++) { - const char *label = graphics::emotes[j].label; - if (!label || !*label) - continue; - const char *found = strstr(msg + pos, label); - if (found && (found - msg) < nextEmote) { - nextEmote = found - msg; - } - } - int textLen = (nextEmote > pos) ? (nextEmote - pos) : (msgLen - pos); - if (textLen > 0) { - tokens.emplace_back(false, String(msg + pos).substring(0, textLen)); - pos += textLen; - } else { - break; - } - } - } + std::vector> tokens = tokenizeMessageWithEmotes(msg); - // ===== Advanced word-wrapping (emotes + text, split by word, wrap by char if needed) ===== + // Advanced word-wrapping (emotes + text, split by word, wrap inside word if needed) std::vector>> lines; std::vector> currentLine; int lineWidth = 0; @@ -2001,7 +2030,7 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st } else { // Text: split by words and wrap inside word if needed String text = token.second; - pos = 0; + int pos = 0; while (pos < static_cast(text.length())) { // Find next space (or end) int spacePos = text.indexOf(' ', pos); @@ -2047,18 +2076,8 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st int nextX = x; for (const auto &token : line) { if (token.first) { - const graphics::Emote *emote = nullptr; - for (int j = 0; j < graphics::numEmotes; j++) { - if (token.second == graphics::emotes[j].label) { - emote = &graphics::emotes[j]; - break; - } - } - if (emote) { - int emoteYOffset = (rowHeight - emote->height) / 2; - display->drawXbm(nextX, yLine + emoteYOffset, emote->width, emote->height, emote->bitmap); - nextX += emote->width + 2; - } + // Emote rendering centralized in helper + renderEmote(display, nextX, yLine, rowHeight, token.second); } else { display->drawString(nextX, yLine, token.second); nextX += display->getStringWidth(token.second); @@ -2071,12 +2090,12 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st return; } - // === Canned Messages List === + // Canned Messages List if (this->messagesCount > 0) { display->setTextAlignment(TEXT_ALIGN_LEFT); display->setFont(FONT_SMALL); - // ====== Precompute per-row heights based on emotes (centered if present) ====== + // Precompute per-row heights based on emotes (centered if present) const int baseRowSpacing = FONT_HEIGHT_SMALL - 4; int topMsg; @@ -2096,7 +2115,7 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st : 0; int countRows = std::min(messagesCount, _visibleRows); - // --- Build per-row max height based on all emotes in line --- + // Build per-row max height based on all emotes in line for (int i = 0; i < countRows; i++) { const char *msg = getMessageByIndex(topMsg + i); int maxEmoteHeight = 0; @@ -2114,7 +2133,7 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st rowHeights.push_back(std::max(baseRowSpacing, maxEmoteHeight + 2)); } - // --- Draw all message rows with multi-emote support --- + // Draw all message rows with multi-emote support int yCursor = listYOffset; for (int vis = 0; vis < countRows; vis++) { int msgIdx = topMsg + vis; @@ -2123,52 +2142,8 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st int rowHeight = rowHeights[vis]; bool _highlight = (msgIdx == currentMessageIndex); - // --- Multi-emote tokenization --- - std::vector> tokens; // (isEmote, token) - int pos = 0; - int msgLen = strlen(msg); - while (pos < msgLen) { - const graphics::Emote *foundEmote = nullptr; - int foundLen = 0; - - // Look for any emote label at this pos (prefer longest match) - for (int j = 0; j < graphics::numEmotes; j++) { - const char *label = graphics::emotes[j].label; - int labelLen = strlen(label); - if (labelLen == 0) - continue; - if (strncmp(msg + pos, label, labelLen) == 0) { - if (!foundEmote || labelLen > foundLen) { - foundEmote = &graphics::emotes[j]; - foundLen = labelLen; - } - } - } - if (foundEmote) { - tokens.emplace_back(true, String(foundEmote->label)); - pos += foundLen; - } else { - // Find next emote - int nextEmote = msgLen; - for (int j = 0; j < graphics::numEmotes; j++) { - const char *label = graphics::emotes[j].label; - if (label[0] == 0) - continue; - const char *found = strstr(msg + pos, label); - if (found && (found - msg) < nextEmote) { - nextEmote = found - msg; - } - } - int textLen = (nextEmote > pos) ? (nextEmote - pos) : (msgLen - pos); - if (textLen > 0) { - tokens.emplace_back(false, String(msg + pos).substring(0, textLen)); - pos += textLen; - } else { - break; - } - } - } - // --- End multi-emote tokenization --- + // Multi-emote tokenization + std::vector> tokens = tokenizeMessageWithEmotes(msg); // Vertically center based on rowHeight int textYOffset = (rowHeight - FONT_HEIGHT_SMALL) / 2; @@ -2189,19 +2164,8 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st // Draw all tokens left to right for (const auto &token : tokens) { if (token.first) { - // Emote - const graphics::Emote *emote = nullptr; - for (int j = 0; j < graphics::numEmotes; j++) { - if (token.second == graphics::emotes[j].label) { - emote = &graphics::emotes[j]; - break; - } - } - if (emote) { - int emoteYOffset = (rowHeight - emote->height) / 2; - display->drawXbm(nextX, lineY + emoteYOffset, emote->width, emote->height, emote->bitmap); - nextX += emote->width + 2; - } + // Emote rendering centralized in helper + renderEmote(display, nextX, lineY, rowHeight, token.second); } else { // Text display->drawString(nextX, lineY + textYOffset, token.second); @@ -2228,43 +2192,166 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st } } +// Return SNR limit based on modem preset +static float getSnrLimit(meshtastic_Config_LoRaConfig_ModemPreset preset) +{ + switch (preset) { + case meshtastic_Config_LoRaConfig_ModemPreset_LONG_SLOW: + case meshtastic_Config_LoRaConfig_ModemPreset_LONG_MODERATE: + case meshtastic_Config_LoRaConfig_ModemPreset_LONG_FAST: + return -6.0f; + case meshtastic_Config_LoRaConfig_ModemPreset_MEDIUM_SLOW: + case meshtastic_Config_LoRaConfig_ModemPreset_MEDIUM_FAST: + return -5.5f; + case meshtastic_Config_LoRaConfig_ModemPreset_SHORT_SLOW: + case meshtastic_Config_LoRaConfig_ModemPreset_SHORT_FAST: + case meshtastic_Config_LoRaConfig_ModemPreset_SHORT_TURBO: + return -4.5f; + default: + return -6.0f; + } +} + +// Return Good/Fair/Bad label and set 1–5 bars based on SNR and RSSI +static const char *getSignalGrade(float snr, int32_t rssi, float snrLimit, int &bars) +{ + // 5-bar logic: strength inside Good/Fair/Bad category + if (snr > snrLimit && rssi > -10) { + bars = 5; // very strong good + return "Good"; + } else if (snr > snrLimit && rssi > -20) { + bars = 4; // normal good + return "Good"; + } else if (snr > 0 && rssi > -50) { + bars = 3; // weaker good (on edge of fair) + return "Good"; + } else if (snr > -10 && rssi > -100) { + bars = 2; // fair + return "Fair"; + } else { + bars = 1; // bad + return "Bad"; + } +} + ProcessMessage CannedMessageModule::handleReceived(const meshtastic_MeshPacket &mp) { - if (mp.decoded.portnum == meshtastic_PortNum_ROUTING_APP && waitingForAck) { + // Only process routing ACK/NACK packets that are responses to our own outbound + if (mp.decoded.portnum == meshtastic_PortNum_ROUTING_APP && waitingForAck && mp.to == nodeDB->getNodeNum() && + mp.decoded.request_id == this->lastRequestId) // only ACKs for our last sent packet + { if (mp.decoded.request_id != 0) { - // Trigger screen refresh for ACK/NACK feedback - UIFrameEvent e; - e.action = UIFrameEvent::Action::REGENERATE_FRAMESET; - requestFocus(); - this->runState = CANNED_MESSAGE_RUN_STATE_ACK_NACK_RECEIVED; - // Decode the routing response meshtastic_Routing decoded = meshtastic_Routing_init_default; pb_decode_from_bytes(mp.decoded.payload.bytes, mp.decoded.payload.size, meshtastic_Routing_fields, &decoded); - // Track hop metadata - this->lastAckWasRelayed = (mp.hop_limit != mp.hop_start); - this->lastAckHopStart = mp.hop_start; - this->lastAckHopLimit = mp.hop_limit; - - // Determine ACK status + // Determine ACK/NACK status bool isAck = (decoded.error_reason == meshtastic_Routing_Error_NONE); bool isFromDest = (mp.from == this->lastSentNode); bool wasBroadcast = (this->lastSentNode == NODENUM_BROADCAST); // Identify the responding node if (wasBroadcast && mp.from != nodeDB->getNodeNum()) { - this->incoming = mp.from; // Relayed by another node + this->incoming = mp.from; // relayed by another node } else { - this->incoming = this->lastSentNode; // Direct reply + this->incoming = this->lastSentNode; // direct reply } - // Final ACK confirmation logic - this->ack = isAck && (wasBroadcast || isFromDest); + // Final ACK/NACK logic + if (wasBroadcast) { + // Any ACK counts for broadcast + this->ack = isAck; + waitingForAck = false; + } else if (isFromDest) { + // Only ACK from destination counts as final + this->ack = isAck; + waitingForAck = false; + } else if (isAck) { + // Relay ACK → mark as RELAYED, still no final ACK + this->ack = false; + waitingForAck = false; + } else { + // Explicit failure + this->ack = false; + waitingForAck = false; + } - waitingForAck = false; - this->notifyObservers(&e); - setIntervalFromNow(3000); // Time to show ACK/NACK screen + // Update last sent StoredMessage with ACK/NACK/RELAYED result + if (!messageStore.getMessages().empty()) { + StoredMessage &last = const_cast(messageStore.getMessages().back()); + if (last.sender == nodeDB->getNodeNum()) { // only update our own messages + if (wasBroadcast && isAck) { + last.ackStatus = AckStatus::ACKED; + } else if (isFromDest && isAck) { + last.ackStatus = AckStatus::ACKED; + } else if (!isFromDest && isAck) { + last.ackStatus = AckStatus::RELAYED; + } else { + last.ackStatus = AckStatus::NACKED; + } + } + } + + // Capture radio metrics + this->lastRxRssi = mp.rx_rssi; + this->lastRxSnr = mp.rx_snr; + + // Show overlay banner + if (screen) { + auto *display = screen->getDisplayDevice(); + graphics::BannerOverlayOptions opts; + static char buf[128]; + + const char *channelName = channels.getName(this->channel); + const char *src = getNodeName(this->incoming); + char nodeName[48]; + strncpy(nodeName, src, sizeof(nodeName) - 1); + nodeName[sizeof(nodeName) - 1] = '\0'; + + int availWidth = + display->getWidth() - ((graphics::currentResolution == graphics::ScreenResolution::High) ? 60 : 30); + if (availWidth < 0) + availWidth = 0; + + size_t origLen = strlen(nodeName); + while (nodeName[0] && display->getStringWidth(nodeName) > availWidth) { + nodeName[strlen(nodeName) - 1] = '\0'; + } + if (strlen(nodeName) < origLen) { + strcat(nodeName, "..."); + } + + // Calculate signal quality and bars based on preset, SNR, and RSSI + float snrLimit = getSnrLimit(config.lora.modem_preset); + int bars = 0; + const char *qualityLabel = getSignalGrade(this->lastRxSnr, this->lastRxRssi, snrLimit, bars); + + if (this->ack) { + if (this->lastSentNode == NODENUM_BROADCAST) { + snprintf(buf, sizeof(buf), "Message sent to\n#%s\n\nSignal: %s", + (channelName && channelName[0]) ? channelName : "unknown", qualityLabel); + } else { + snprintf(buf, sizeof(buf), "DM sent to\n@%s\n\nSignal: %s", + (nodeName && nodeName[0]) ? nodeName : "unknown", qualityLabel); + } + } else if (isAck && !isFromDest) { + // Relay ACK banner + snprintf(buf, sizeof(buf), "DM Relayed\n(Status Unknown)\n%s\n\nSignal: %s", + (nodeName && nodeName[0]) ? nodeName : "unknown", qualityLabel); + } else { + if (this->lastSentNode == NODENUM_BROADCAST) { + snprintf(buf, sizeof(buf), "Message failed to\n#%s", + (channelName && channelName[0]) ? channelName : "unknown"); + } else { + snprintf(buf, sizeof(buf), "DM failed to\n@%s", (nodeName && nodeName[0]) ? nodeName : "unknown"); + } + } + + opts.message = buf; + opts.durationMs = 3000; + graphics::bannerSignalBars = bars; // tell banner renderer how many bars to draw + screen->showOverlayBanner(opts); // this triggers drawNotificationBox() + } } } diff --git a/src/modules/CannedMessageModule.h b/src/modules/CannedMessageModule.h index 5b0481ac7..3d7c09d87 100644 --- a/src/modules/CannedMessageModule.h +++ b/src/modules/CannedMessageModule.h @@ -75,7 +75,6 @@ class CannedMessageModule : public SinglePortModule, public Observablekbchar) { // Fn key symbols case INPUT_BROKER_MSG_FN_SYMBOL_ON: - IF_SCREEN(screen->setFunctionSymbol("Fn")); - return 0; case INPUT_BROKER_MSG_FN_SYMBOL_OFF: - IF_SCREEN(screen->removeFunctionSymbol("Fn")); return 0; // Brightness case INPUT_BROKER_MSG_BRIGHTNESS_UP: @@ -78,6 +78,9 @@ int SystemCommandsModule::handleInputEvent(const InputEvent *event) case INPUT_BROKER_MSG_REBOOT: IF_SCREEN(screen->showSimpleBanner("Rebooting...", 0)); nodeDB->saveToDisk(); +#if HAS_SCREEN + messageStore.saveToFlash(); +#endif rebootAtMsec = millis() + DEFAULT_REBOOT_SECONDS * 1000; // runState = CANNED_MESSAGE_RUN_STATE_INACTIVE; return true; diff --git a/src/modules/Telemetry/EnvironmentTelemetry.cpp b/src/modules/Telemetry/EnvironmentTelemetry.cpp index 29e815092..41062662b 100644 --- a/src/modules/Telemetry/EnvironmentTelemetry.cpp +++ b/src/modules/Telemetry/EnvironmentTelemetry.cpp @@ -378,7 +378,7 @@ void EnvironmentTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiSt int line = 1; // === Set Title - const char *titleStr = (graphics::isHighResolution) ? "Environment" : "Env."; + const char *titleStr = (graphics::currentResolution == graphics::ScreenResolution::High) ? "Environment" : "Env."; // === Header === graphics::drawCommonHeader(display, x, y, titleStr); diff --git a/src/modules/Telemetry/PowerTelemetry.cpp b/src/modules/Telemetry/PowerTelemetry.cpp index 29dd1def8..9047c7cd4 100644 --- a/src/modules/Telemetry/PowerTelemetry.cpp +++ b/src/modules/Telemetry/PowerTelemetry.cpp @@ -117,7 +117,7 @@ void PowerTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *s int line = 1; // === Set Title - const char *titleStr = (graphics::isHighResolution) ? "Power Telem." : "Power"; + const char *titleStr = (graphics::currentResolution == graphics::ScreenResolution::High) ? "Power Telem." : "Power"; // === Header === graphics::drawCommonHeader(display, x, y, titleStr); diff --git a/src/modules/TextMessageModule.cpp b/src/modules/TextMessageModule.cpp index aee359158..76e063436 100644 --- a/src/modules/TextMessageModule.cpp +++ b/src/modules/TextMessageModule.cpp @@ -1,10 +1,14 @@ #include "TextMessageModule.h" #include "MeshService.h" +#include "MessageStore.h" #include "NodeDB.h" #include "PowerFSM.h" #include "buzz.h" #include "configuration.h" #include "graphics/Screen.h" +#include "graphics/SharedUIDisplay.h" +#include "graphics/draw/MessageRenderer.h" +#include "main.h" TextMessageModule *textMessageModule; ProcessMessage TextMessageModule::handleReceived(const meshtastic_MeshPacket &mp) @@ -15,14 +19,26 @@ ProcessMessage TextMessageModule::handleReceived(const meshtastic_MeshPacket &mp #endif // We only store/display messages destined for us. - // Keep a copy of the most recent text message. devicestate.rx_text_message = mp; devicestate.has_rx_text_message = true; +#if HAS_SCREEN + // Guard against running in MeshtasticUI + if (config.display.displaymode != meshtastic_Config_DisplayConfig_DisplayMode_COLOR) { + // Store in the central message history + const StoredMessage &sm = messageStore.addFromPacket(mp); + // Pass message to renderer (banner + thread switching + scroll reset) + // Use the global Screen singleton to retrieve the current OLED display + auto *display = screen ? screen->getDisplayDevice() : nullptr; + graphics::MessageRenderer::handleNewMessage(display, sm, mp); + } +#endif // Only trigger screen wake if configuration allows it if (shouldWakeOnReceivedMessage()) { powerFSM.trigger(EVENT_RECEIVED_MSG); } + + // Notify any observers (e.g. external modules that care about packets) notifyObservers(&mp); return ProcessMessage::CONTINUE; // Let others look at this message also if they want diff --git a/src/modules/TextMessageModule.h b/src/modules/TextMessageModule.h index cc0b0f9d5..e719f1abc 100644 --- a/src/modules/TextMessageModule.h +++ b/src/modules/TextMessageModule.h @@ -3,7 +3,13 @@ #include "SinglePortModule.h" /** - * Text message handling for meshtastic - draws on the OLED display the most recent received message + * Text message handling for Meshtastic. + * + * This module is responsible for receiving and storing incoming text messages + * from the mesh. It updates device state and notifies observers so that other + * components (such as the MessageRenderer) can later display or process them. + * + * Rendering of messages on screen is no longer done here. */ class TextMessageModule : public SinglePortModule, public Observable { @@ -15,10 +21,10 @@ class TextMessageModule : public SinglePortModule, public Observable getTime()) - return devicestate.has_rx_waypoint = true; - - // Expired, or deleted - else - return devicestate.has_rx_waypoint = false; + return wp.expire > getTime(); } - - // If decoding failed - LOG_ERROR("Failed to decode waypoint"); - devicestate.has_rx_waypoint = false; - return false; + return false; // no LOG_ERROR, no flag writes #else return false; #endif @@ -85,53 +79,46 @@ bool WaypointModule::shouldDraw() /// Draw the last waypoint we received void WaypointModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { - if (screen == nullptr) + if (!screen) return; - // Prepare to draw - display->setFont(FONT_SMALL); + display->clear(); display->setTextAlignment(TEXT_ALIGN_LEFT); + display->setFont(FONT_SMALL); + int line = 1; - // Handle inverted display - // Unsure of expected behavior: for now, copy drawNodeInfo - if (config.display.displaymode != meshtastic_Config_DisplayConfig_DisplayMode_INVERTED) - display->fillRect(0 + x, 0 + y, x + display->getWidth(), y + FONT_HEIGHT_SMALL); + // === Set Title + const char *titleStr = "Waypoint"; + + // === Header === + graphics::drawCommonHeader(display, x, y, titleStr); + + const int w = display->getWidth(); + const int h = display->getHeight(); // Decode the waypoint const meshtastic_MeshPacket &mp = devicestate.rx_waypoint; - meshtastic_Waypoint wp; - memset(&wp, 0, sizeof(wp)); + meshtastic_Waypoint wp{}; if (!pb_decode_from_bytes(mp.decoded.payload.bytes, mp.decoded.payload.size, &meshtastic_Waypoint_msg, &wp)) { - // This *should* be caught by shouldDrawWaypoint, but we'll short-circuit here just in case - display->drawStringMaxWidth(0 + x, 0 + y, x + display->getWidth(), "Couldn't decode waypoint"); devicestate.has_rx_waypoint = false; return; } // Get timestamp info. Will pass as a field to drawColumns - static char lastStr[20]; + char lastStr[20]; getTimeAgoStr(sinceReceived(&mp), lastStr, sizeof(lastStr)); // Will contain distance information, passed as a field to drawColumns - static char distStr[20]; + char distStr[20]; // Get our node, to use our own position meshtastic_NodeInfoLite *ourNode = nodeDB->getMeshNode(nodeDB->getNodeNum()); - // Text fields to draw (left of compass) - // Last element must be NULL. This signals the end of the char*[] to drawColumns - const char *fields[] = {"Waypoint", lastStr, wp.name, distStr, NULL}; - // Dimensions / co-ordinates for the compass/circle - int16_t compassX = 0, compassY = 0; - uint16_t compassDiam = graphics::CompassRenderer::getCompassDiam(display->getWidth(), display->getHeight()); - - if (config.display.displaymode == meshtastic_Config_DisplayConfig_DisplayMode_DEFAULT) { - compassX = x + display->getWidth() - compassDiam / 2 - 5; - compassY = y + display->getHeight() / 2; - } else { - compassX = x + display->getWidth() - compassDiam / 2 - 5; - compassY = y + FONT_HEIGHT_SMALL + (display->getHeight() - FONT_HEIGHT_SMALL) / 2; - } + const uint16_t compassDiam = graphics::CompassRenderer::getCompassDiam(w, h); + const int16_t compassX = x + w - (compassDiam / 2) - 5; + const int16_t compassY = (config.display.displaymode == meshtastic_Config_DisplayConfig_DisplayMode_DEFAULT) + ? y + h / 2 + : y + FONT_HEIGHT_SMALL + (h - FONT_HEIGHT_SMALL) / 2; // If our node has a position: if (ourNode && (nodeDB->hasValidPosition(ourNode) || screen->hasHeading())) { @@ -141,7 +128,7 @@ void WaypointModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, myHeading = 0; } else { if (screen->hasHeading()) - myHeading = (screen->getHeading()) * PI / 180; // gotta convert compass degrees to Radians + myHeading = degToRad(screen->getHeading()); else myHeading = screen->estimatedHeading(DegD(op.latitude_i), DegD(op.longitude_i)); } @@ -157,46 +144,35 @@ void WaypointModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, graphics::CompassRenderer::drawNodeHeading(display, compassX, compassY, compassDiam, bearingToOther); float bearingToOtherDegrees = (bearingToOther < 0) ? bearingToOther + 2 * PI : bearingToOther; - bearingToOtherDegrees = bearingToOtherDegrees * 180 / PI; + bearingToOtherDegrees = radToDeg(bearingToOtherDegrees); // Distance to Waypoint float d = GeoCoord::latLongToMeter(DegD(wp.latitude_i), DegD(wp.longitude_i), DegD(op.latitude_i), DegD(op.longitude_i)); if (config.display.units == meshtastic_Config_DisplayConfig_DisplayUnits_IMPERIAL) { - if (d < (2 * MILES_TO_FEET)) - snprintf(distStr, sizeof(distStr), "%.0fft %.0f°", d * METERS_TO_FEET, bearingToOtherDegrees); - else - snprintf(distStr, sizeof(distStr), "%.1fmi %.0f°", d * METERS_TO_FEET / MILES_TO_FEET, bearingToOtherDegrees); + float feet = d * METERS_TO_FEET; + snprintf(distStr, sizeof(distStr), feet < (2 * MILES_TO_FEET) ? "%.0fft %.0f°" : "%.1fmi %.0f°", + feet < (2 * MILES_TO_FEET) ? feet : feet / MILES_TO_FEET, bearingToOtherDegrees); } else { - if (d < 2000) - snprintf(distStr, sizeof(distStr), "%.0fm %.0f°", d, bearingToOtherDegrees); - else - snprintf(distStr, sizeof(distStr), "%.1fkm %.0f°", d / 1000, bearingToOtherDegrees); + snprintf(distStr, sizeof(distStr), d < 2000 ? "%.0fm %.0f°" : "%.1fkm %.0f°", d < 2000 ? d : d / 1000, + bearingToOtherDegrees); } - } - // If our node doesn't have position else { - // ? in the compass display->drawString(compassX - FONT_HEIGHT_SMALL / 4, compassY - FONT_HEIGHT_SMALL / 2, "?"); // ? in the distance field - if (config.display.units == meshtastic_Config_DisplayConfig_DisplayUnits_IMPERIAL) - strncpy(distStr, "? mi ?°", sizeof(distStr)); - else - strncpy(distStr, "? km ?°", sizeof(distStr)); + snprintf(distStr, sizeof(distStr), "? %s ?°", + (config.display.units == meshtastic_Config_DisplayConfig_DisplayUnits_IMPERIAL) ? "mi" : "km"); } // Draw compass circle display->drawCircle(compassX, compassY, compassDiam / 2); - // Undo color-inversion, if set prior to drawing header - // Unsure of expected behavior? For now: copy drawNodeInfo - if (config.display.displaymode != meshtastic_Config_DisplayConfig_DisplayMode_INVERTED) { - display->setColor(BLACK); - } - - // Must be after distStr is populated - graphics::NodeListRenderer::drawColumns(display, x, y, fields); + display->setTextAlignment(TEXT_ALIGN_LEFT); // Something above me changes to a different alignment, forcing a fix here! + display->drawString(0, graphics::getTextPositions(display)[line++], lastStr); + display->drawString(0, graphics::getTextPositions(display)[line++], wp.name); + display->drawString(0, graphics::getTextPositions(display)[line++], wp.description); + display->drawString(0, graphics::getTextPositions(display)[line++], distStr); } #endif diff --git a/src/platform/portduino/PortduinoGlue.cpp b/src/platform/portduino/PortduinoGlue.cpp index 1b601f9b4..dab9e8d8c 100644 --- a/src/platform/portduino/PortduinoGlue.cpp +++ b/src/platform/portduino/PortduinoGlue.cpp @@ -427,11 +427,13 @@ void portduinoSetup() } getMacAddr(dmac); +#ifndef UNIT_TEST if (dmac[0] == 0 && dmac[1] == 0 && dmac[2] == 0 && dmac[3] == 0 && dmac[4] == 0 && dmac[5] == 0) { std::cout << "*** Blank MAC Address not allowed!" << std::endl; std::cout << "Please set a MAC Address in config.yaml using either MACAddress or MACAddressSource." << std::endl; exit(EXIT_FAILURE); } +#endif printf("MAC ADDRESS: %02X:%02X:%02X:%02X:%02X:%02X\n", dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5]); // Rather important to set this, if not running simulated. randomSeed(time(NULL)); diff --git a/variants/nrf52840/heltec_mesh_solar/platformio.ini b/variants/nrf52840/heltec_mesh_solar/platformio.ini index 9e73e745a..fa8965a64 100644 --- a/variants/nrf52840/heltec_mesh_solar/platformio.ini +++ b/variants/nrf52840/heltec_mesh_solar/platformio.ini @@ -48,6 +48,14 @@ build_flags = ${heltec_mesh_solar_base.build_flags} -DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates -DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached. -DEINK_HASQUIRK_GHOSTING ; Display model is identified as "prone to ghosting" + -DENABLE_MESSAGE_PERSISTENCE=0 ; Disable flash persistence for space-limited build + -DMESHTASTIC_EXCLUDE_WIFI=1 + -DMESHTASTIC_EXCLUDE_EXTERNALNOTIFICATION=1 + -DMESHTASTIC_EXCLUDE_PAXCOUNTER=1 + -DMESHTASTIC_EXCLUDE_REMOTEHARDWARE=1 + -DMESHTASTIC_EXCLUDE_STOREFORWARD=1 + -DMESHTASTIC_EXCLUDE_CANNEDMESSAGES=1 + -DMESHTASTIC_EXCLUDE_WAYPOINT=1 lib_deps = ${heltec_mesh_solar_base.lib_deps} # renovate: datasource=git-refs depName=meshtastic-GxEPD2 packageName=https://github.com/meshtastic/GxEPD2 gitBranch=master