// // Created by bruno on 3/30/25. // #include "messages.h" // Convert a 32-bit number to a string (10 digits, zero-padded) void u32_to_str(uint32_t num, char *str) { for (int i = 9; i >= 0; i--) { str[i] = '0' + (num % 10); num /= 10; } str[10] = '\0'; } // Convert an 8-bit number to a 2-digit string (zero-padded) void u8_to_str(uint8_t num, char *str) { str[0] = '0' + (num / 100); // Hundreds place str[1] = '0' + ((num / 10) % 10); // Tens place str[2] = '0' + (num % 10); // Ones place str[3] = '\0'; } // Print text wrapped over `lines` display lines starting at `line`. static void UI_PrintWrapped(const char *text, const uint8_t line, const uint8_t lines) { char chunk[MESSAGES_LINE_CHARS + 1U]; const size_t length = strlen(text); for (uint8_t l = 0; l < lines && l * MESSAGES_LINE_CHARS < length; l++) { strncpy(chunk, text + l * MESSAGES_LINE_CHARS, MESSAGES_LINE_CHARS); chunk[MESSAGES_LINE_CHARS] = '\0'; UI_PrintStringSmallNormal(chunk, 1, 0, line + l); } } void UI_DisplayMessages(void) { char String[19]; UI_DisplayClear(); if (gEnteringSMS != SMS_NOT_ENTERING) { if (gEnteringSMS == SMS_ENTERING_DEST) { UI_PrintString(MODEM_IS_POCSAG(gEeprom.FSKMode) ? "RIC" : "SMS dest", 0, 0, 0 /*, 8 */); u32_to_str(dataPacket.dest, String); UI_PrintStringSmallNormal(String, 0, 0, 2); } else if (gEnteringSMS == SMS_ENTERING_MESSAGE) { const size_t length = strlen(gMessageText); UI_PrintString(MODEM_IS_POCSAG(gEeprom.FSKMode) ? "Page text" : "SMS data", 0, 0, 0 /*, 8 */); UI_PrintWrapped(gMessageText, 1, MESSAGES_TEXT_LINES); // cursor block after the last character const uint8_t cursorLine = 1U + length / MESSAGES_LINE_CHARS; if (length < MESSAGES_TEXT_LENGTH && cursorLine < 8U) memset(gFrameBuffer[cursorLine] + 2 + (7 * (length % MESSAGES_LINE_CHARS)), gKeyTimeout == 0 ? 0xFF : 0x80, 6); } } else { char text[MESSAGES_TEXT_LENGTH + 1U]; uint8_t Data[8]; EEPROM_ReadBuffer(SEQParameterEEPROM, Data, 8); uint8_t msgIndex = Data[1]; // index of latest written message // Determine oldest message index (next to be overwritten) uint8_t startIndex = (msgIndex + 1) % MESSAGES_COUNT; // Pages run from oldest to newest uint8_t pageOffset = gActiveMessageBank * MESSAGES_PER_PAGE; for (uint8_t ixa = 0; ixa < MESSAGES_PER_PAGE; ixa++) { // If out of stored range, stop if (pageOffset + ixa >= MESSAGES_COUNT) break; // Logical order from oldest to newest gActiveMessage = (startIndex + pageOffset + ixa) % MESSAGES_COUNT; MESSAGES_GET(); // Build the display string if (loadedPacket.flags & MESSAGE_FLAG_POCSAG) { char capcodeString[11]; u32_to_str(loadedPacket.src, capcodeString); String[0] = 'P'; String[1] = ':'; memcpy(String + 2, capcodeString + 3, 7); String[9] = ' '; } else { u32_to_str(loadedPacket.src, &String[0]); } String[10] = '-'; u8_to_str(loadedPacket.seq, &String[11]); String[14] = '-'; // Display a visual index (0,1,2,3... not EEPROM slot) uint8_t visualIndex = ixa + pageOffset; String[15] = (visualIndex < 10) ? ('0' + visualIndex) : ('A' + visualIndex - 10); String[16] = '-'; String[17] = (gActiveMessage < 10) ? ('0' + gActiveMessage) : ('A' + gActiveMessage - 10); String[18] = 0; // header line, then the text wrapped over MESSAGES_TEXT_LINES lines const uint8_t line = ixa * (1U + MESSAGES_TEXT_LINES); UI_PrintString(String, 2, 0, line); MESSAGES_GetText(text); UI_PrintWrapped(text, line + 1U, MESSAGES_TEXT_LINES); } } ST7565_BlitFullScreen(); }