Files
brnQuanFW/app/messages.c
T
BRNSystemsandClaude Opus 5.5 aabd0f4956 BRN-POCSAG: 171 channels, 26 x 40 char messages, make toggles
EEPROM layout (settings.h), replacing the broken 24 byte layout from
85fb527 (200 x 24 B channels overran the VFOs, attributes and settings;
names were saved at ch*24 but read at ch*20):
- 32 byte channel records with the 10 char name and the 16 bit scrambler
  inside, 171 channels in two areas (0x0000, 0x11E0); VFOs at 0x1020 with
  the same record; attributes moved to 0x0F50.
- Message store from 0x1900: 26 messages of 40 characters.
- Reset VFO no longer wipes the memory channels and messages.
- Static asserts that no area overlaps another or the calibration.

Identity: reports "BRN-POCSAG v4.0" to CHIRP and in the packed image, so
stock / egzumer / F4HWN drivers do not accept the new layout; the name is
shown on the welcome screen.

Make toggles: ENABLE_POCSAG, ENABLE_POCSAG_DEBUG, ENABLE_POCSAG_DEV_TUNING,
ENABLE_OOK_SENDER (POCSAG code and the OOK sender compile out completely).

Fonts: removed the unused big font, big digits and small digits; the 3x5
font is only built with the spectrum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:43:05 +01:00

275 lines
9.2 KiB
C

//
// Created by bruno on 3/30/25.
//
#include "messages.h"
#include "ui/ui.h"
#include "driver/uart.h"
#include "driver/systick.h"
#include "app/pocsag.h"
#define T9Count 9
// 0x1E00+ holds the radio calibration; the message store must never reach it.
_Static_assert(sizeof(StoredPacket) == MESSAGES_SLOT_SIZE, "StoredPacket layout");
_Static_assert(MESSAGES_START + MESSAGES_COUNT * MESSAGES_SLOT_SIZE <= MESSAGES_END,
"message store overlaps calibration EEPROM: lower MESSAGES_COUNT or POCSAG_MAX_MESSAGE_LENGTH");
_Static_assert(MESSAGES_PER_PAGE >= 1U, "POCSAG_MAX_MESSAGE_LENGTH too long for the display");
_Static_assert(MESSAGES_COUNT >= 1U && MESSAGES_COUNT <= 36U, "MESSAGES_COUNT out of range");
const char T9Table[10][T9Count] = {
{'#', '(', ')', ';', ':', '<', '>', '/', '0'},
{',', '.', '?', '&', '!', ' ', '-', '_', '1'},
{'a', 'b', 'c', 'A', 'B', 'C', '[', ']', '2'},
{'d', 'e', 'f', 'D', 'E', 'F', '@', '%', '3'},
{'g', 'h', 'i', 'G', 'H', 'I', '~', '$', '4'},
{'j', 'k', 'l', 'J', 'K', 'L', '|', '*', '5'},
{'m', 'n', 'o', 'M', 'N', 'O', '{', '}', '6'},
{'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S', '7'},
{'t', 'u', 'v', 'T', 'U', 'V', '"', '\'', '8'},
{'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z', '9'}
};
uint8_t gActiveMessage = 0;
uint8_t gActiveMessageBank = 0;
StoredPacket loadedPacket;
char gMessageText[MESSAGES_TEXT_LENGTH + 1U];
uint8_t gKeyTimeout = 0;
static size_t MESSAGES_MaxTextLength(void)
{
// the SMS packet carries DataPacketDataSize bytes including the terminator
return MODEM_IS_POCSAG(gEeprom.FSKMode) ? MESSAGES_TEXT_LENGTH : DataPacketDataSize - 1U;
}
void MESSAGES_SAVE(const uint32_t src, const uint8_t seq, const uint8_t ttl, const uint8_t flags,
const uint8_t *text, size_t length) {
uint8_t Data[8];
StoredPacket stored;
EEPROM_ReadBuffer(SEQParameterEEPROM, Data, 8);
// Data[1] = slot of the latest message (0 .. MESSAGES_COUNT-1), see UI_DisplayMessages()
uint8_t msgIndex = (uint8_t) ((Data[1] + 1U) % MESSAGES_COUNT);
if (Data[1] >= MESSAGES_COUNT)
msgIndex = 0;
Data[1] = msgIndex;
EEPROM_WriteBuffer(SEQParameterEEPROM, Data);
memset(&stored, 0, sizeof(stored));
stored.src = src;
stored.seq = seq;
stored.ttl = ttl;
stored.flags = flags;
if (length > MESSAGES_TEXT_LENGTH)
length = MESSAGES_TEXT_LENGTH;
for (size_t i = 0; i < length && text[i] != '\0'; i++)
stored.data[i] = text[i];
const uint16_t address = MESSAGES_START + (msgIndex * sizeof(StoredPacket));
for (unsigned int offset = 0; offset < sizeof(stored); offset += 8U)
EEPROM_WriteBuffer(address + offset, (const uint8_t *) &stored + offset);
}
void MESSAGES_GET() {
if (gActiveMessage == MESSAGES_COUNT) {
memset(&loadedPacket, 0, sizeof(loadedPacket));
loadedPacket.src = dataPacket.src;
loadedPacket.seq = dataPacket.seq;
loadedPacket.ttl = dataPacket.ttl;
loadedPacket.flags = dataPacket.flags;
memcpy(loadedPacket.data, gMessageText, MESSAGES_TEXT_LENGTH);
} else {
EEPROM_ReadBuffer(MESSAGES_START + (gActiveMessage * sizeof(StoredPacket)), &loadedPacket,
sizeof(StoredPacket));
}
}
void MESSAGES_GetText(char *text) {
size_t i = 0;
for (; i < MESSAGES_TEXT_LENGTH; i++) {
const uint8_t c = loadedPacket.data[i];
if (c == 0x00 || c == 0xFF) // NUL padding or erased EEPROM
break;
text[i] = (c < 0x20 || c > 0x7E) ? '?' : (char) c;
}
text[i] = '\0';
}
void MESSAGES_ShowNewest(void) {
gActiveMessageBank = MESSAGES_PAGES - 1U; // pages run oldest .. newest
}
void MESSAGES_DELETE() {
}
void updatePrevChar(KEY_Code_t Key) {
if (Key != prevKey) {
prevKey = Key;
}
}
void MESSAGES_TimeSlice500ms(void) {
if (gKeyTimeout && gKeyTimeout++ > 2) {
gKeyTimeout = 0;
prevKey = KEY_EXIT;
}
}
static void MESSAGES_ClearText(void) {
memset(gMessageText, 0, sizeof(gMessageText));
dataPTR = gMessageText;
}
static void MESSAGES_Send(void) {
#ifdef ENABLE_POCSAG
if (MODEM_IS_POCSAG(gEeprom.FSKMode)) {
MSG_POCSAGSend(dataPacket.dest, gMessageText);
return;
}
#endif
memset(dataPacket.data, 0, DataPacketDataSize);
memcpy(dataPacket.data, gMessageText, DataPacketDataSize - 1U);
MSG_FSKSendData(&dataPacket);
}
void MESSAGES_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) {
#ifdef ENABLE_OOK_SENDER
if(Key == KEY_STAR) {
gRequestDisplayScreen = DISPLAY_OOKSENDER;
return;
}
#endif
gKeyTimeout = 1;
#if POCSAG_DEVIATION_TUNING
// POCSAG deviation tuning: 2 = more, 8 = less (send a page and check it on an SDR)
if (MODEM_IS_POCSAG(gEeprom.FSKMode) && gEnteringSMS == SMS_NOT_ENTERING &&
(Key == KEY_2 || Key == KEY_8)) {
if (bKeyPressed && !bKeyHeld)
POCSAG_AdjustDeviation(Key == KEY_2 ? 0x40 : -0x40);
return;
}
#endif
if (gEnteringSMS != SMS_NOT_ENTERING && Key <= KEY_9) {
if (!bKeyHeld) {
if (!dataPTR || dataPTR < gMessageText) {
dataPTR = gMessageText;
}
}
if (gEnteringSMS == SMS_ENTERING_MESSAGE) {
if (bKeyPressed) {
if (prevKey != Key) {
if ((size_t) (dataPTR - gMessageText) >= MESSAGES_MaxTextLength()) {
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
return;
}
dataPTR++;
prevLetter = 0;
}
*(dataPTR - 1) = T9Table[Key][prevLetter];
if (!bKeyHeld) {
prevLetter++;
} else {
prevLetter--;
}
prevLetter = (prevLetter) % T9Count;
updatePrevChar(Key);
}
return;
} else if (gEnteringSMS == SMS_ENTERING_DEST) {
if (!bKeyHeld) {
if (bKeyPressed) {
const uint32_t dest = dataPacket.dest * 10U + Key;
// in POCSAG mode the destination is the pager RIC (21 bits)
if (MODEM_IS_POCSAG(gEeprom.FSKMode) && dest > POCSAG_MAX_RIC) {
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
return;
}
dataPacket.dest = dest;
prevLetter = 0;
}
return;
}
}
}
switch (Key) {
case KEY_PTT:
case KEY_MENU:
if (gEnteringSMS == SMS_NOT_ENTERING) {
if (bKeyPressed) {
gEnteringSMS = SMS_ENTERING_DEST;
updatePrevChar(Key);
}
} else if (gEnteringSMS == SMS_ENTERING_DEST) {
if (bKeyPressed) {
MESSAGES_ClearText();
prepareDataPacket();
dataPacket.flags = 0x80 | (gCurrentVfo->OUTPUT_POWER & 0x07);
gEnteringSMS = SMS_ENTERING_MESSAGE;
}
return;
} else if (gEnteringSMS == SMS_ENTERING_MESSAGE) {
if (bKeyPressed) {
if (strlen(gMessageText)) {
MESSAGES_Send();
gEnteringSMS = SMS_NOT_ENTERING;
}
}
return;
}
break;
case KEY_UP:
if (gEnteringSMS == SMS_NOT_ENTERING && bKeyPressed) {
gActiveMessageBank = (uint8_t) ((gActiveMessageBank + 1U) % MESSAGES_PAGES);
gUpdateDisplay = true;
}
break;
case KEY_DOWN:
if (gEnteringSMS == SMS_NOT_ENTERING && bKeyPressed) {
gActiveMessageBank = gActiveMessageBank ? gActiveMessageBank - 1U : MESSAGES_PAGES - 1U;
gUpdateDisplay = true;
}
break;
case KEY_EXIT:
if (gEnteringSMS != SMS_NOT_ENTERING) {
if (bKeyHeld) {
if (bKeyPressed) {
gEnteringSMS = SMS_NOT_ENTERING;
MESSAGES_ClearText();
}
} else {
if (bKeyPressed) {
if (gEnteringSMS == SMS_ENTERING_MESSAGE) {
if (gMessageText <= dataPTR) {
*dataPTR = '\0';
if (gMessageText < dataPTR) {
dataPTR--;
}
*dataPTR = '\0';
}
} else if (gEnteringSMS == SMS_ENTERING_DEST) {
dataPacket.dest /= 10;
}
}
}
} else {
if (bKeyPressed) {
gRequestDisplayScreen = DISPLAY_MAIN;
}
}
updatePrevChar(Key);
break;
default:
if (!bKeyHeld && bKeyPressed)
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
break;
}
}