Skip to content

Commit

Permalink
Use pointers to avoid some string copying
Browse files Browse the repository at this point in the history
Size: 60664 -> 60464
  • Loading branch information
JuantAldea authored and egzumer committed Dec 25, 2023
1 parent 1281bbf commit f8ef687
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 181 deletions.
22 changes: 11 additions & 11 deletions app/dtmf.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ bool DTMF_ValidateCodes(char *pCode, const unsigned int size)
#ifdef ENABLE_DTMF_CALLING
bool DTMF_GetContact(const int Index, char *pContact)
{
int i = -1;
if (Index >= 0 && Index < MAX_DTMF_CONTACTS && pContact != NULL)
{
EEPROM_ReadBuffer(0x1C00 + (Index * 16), pContact, 16);
i = (int)pContact[0] - ' ';
if (Index < 0 || Index >= MAX_DTMF_CONTACTS || pContact == NULL) {
return false;
}
return (i >= 0 && i < 95);

EEPROM_ReadBuffer(0x1C00 + (Index * 16), pContact, 16);

// check whether the first character is printable or not
return (pContact[0] >= ' ' && pContact[0] < 127);
}

bool DTMF_FindContact(const char *pContact, char *pResult)
Expand Down Expand Up @@ -331,13 +332,12 @@ void DTMF_HandleRequest(void)

if (gDTMF_RX_index >= 2)
{ // look for ACK reply
char *pPrintStr = "AB";

strcpy(String, "AB");
Offset = gDTMF_RX_index - strlen(pPrintStr);

Offset = gDTMF_RX_index - strlen(String);

if (CompareMessage(gDTMF_RX + Offset, String, strlen(String), true))
{ // ends with "AB"
if (CompareMessage(gDTMF_RX + Offset, pPrintStr, strlen(pPrintStr), true)) {
// ends with "AB"

if (gDTMF_ReplyState != DTMF_REPLY_NONE) // 1of11
// if (gDTMF_CallState != DTMF_CALL_STATE_NONE) // 1of11
Expand Down
20 changes: 10 additions & 10 deletions ui/aircopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@

void UI_DisplayAircopy(void)
{
char String[16];
char String[16] = {0};
char *pPrintStr = { 0 };

memset(gFrameBuffer, 0, sizeof(gFrameBuffer));
if (gAircopyState == AIRCOPY_READY) {
pPrintStr = "AIR COPY(RDY)";
} else if (gAircopyState == AIRCOPY_TRANSFER) {
pPrintStr = "AIR COPY";
} else {
pPrintStr = "AIR COPY(CMP)";
}

if (gAircopyState == AIRCOPY_READY)
strcpy(String, "AIR COPY(RDY)");
else
if (gAircopyState == AIRCOPY_TRANSFER)
strcpy(String, "AIR COPY");
else
strcpy(String, "AIR COPY(CMP)");
UI_PrintString(String, 2, 127, 0, 8);
UI_PrintString(pPrintStr, 2, 127, 0, 8);

if (gInputBoxIndex == 0)
{
Expand Down
91 changes: 33 additions & 58 deletions ui/fmradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,57 @@

void UI_DisplayFM(void)
{
unsigned int i;
char String[16];

char String[16] = {0};
char *pPrintStr = String;
memset(gFrameBuffer, 0, sizeof(gFrameBuffer));

memset(String, 0, sizeof(String));
strcpy(String, "FM");
UI_PrintString(String, 0, 127, 0, 12);
UI_PrintString("FM", 0, 127, 0, 12);

memset(String, 0, sizeof(String));
if (gAskToSave)
{
strcpy(String, "SAVE?");
}
else
if (gAskToDelete)
{
strcpy(String, "DEL?");
}
else
{
if (gFM_ScanState == FM_SCAN_OFF)
{
if (!gEeprom.FM_IsMrMode)
{
for (i = 0; i < 20; i++)
{
if (gEeprom.FM_FrequencyPlaying == gFM_Channels[i])
{
sprintf(String, "VFO(CH%02u)", i + 1);
break;
}
if (gAskToSave) {
pPrintStr = "SAVE?";
} else if (gAskToDelete) {
pPrintStr = "DEL?";
} else if (gFM_ScanState == FM_SCAN_OFF) {
if (gEeprom.FM_IsMrMode) {
sprintf(String, "MR(CH%02u)", gEeprom.FM_SelectedChannel + 1);
pPrintStr = String;
} else {
pPrintStr = "VFO";
for (unsigned int i = 0; i < 20; i++) {
if (gEeprom.FM_FrequencyPlaying == gFM_Channels[i]) {
sprintf(String, "VFO(CH%02u)", i + 1);
pPrintStr = String;
break;
}

if (i == 20)
strcpy(String, "VFO");
}
else
sprintf(String, "MR(CH%02u)", gEeprom.FM_SelectedChannel + 1);
}
else
{
if (!gFM_AutoScan)
strcpy(String, "M-SCAN");
else
sprintf(String, "A-SCAN(%u)", gFM_ChannelPosition + 1);
}
} else if (gFM_AutoScan) {
sprintf(String, "A-SCAN(%u)", gFM_ChannelPosition + 1);
pPrintStr = String;
} else {
pPrintStr = "M-SCAN";
}
UI_PrintString(String, 0, 127, 2, 10);

UI_PrintString(pPrintStr, 0, 127, 2, 10);

memset(String, 0, sizeof(String));
if (gAskToSave || (gEeprom.FM_IsMrMode && gInputBoxIndex > 0))
{
if (gAskToSave || (gEeprom.FM_IsMrMode && gInputBoxIndex > 0)) {
UI_GenerateChannelString(String, gFM_ChannelPosition);
}
else
if (!gAskToDelete)
{
if (gInputBoxIndex == 0)
{
} else if (gAskToDelete) {
sprintf(String, "CH-%02u", gEeprom.FM_SelectedChannel + 1);
} else {
if (gInputBoxIndex == 0) {
sprintf(String, "%3d.%d", gEeprom.FM_FrequencyPlaying / 10, gEeprom.FM_FrequencyPlaying % 10);
UI_DisplayFrequency(String, 32, 4, true);
}
else {
} else {
const char * ascii = INPUTBOX_GetAscii();
sprintf(String, "%.3s.%.1s",ascii, ascii + 3);
UI_DisplayFrequency(String, 32, 4, false);
}

UI_DisplayFrequency(String, 32, 4, gInputBoxIndex == 0);
ST7565_BlitFullScreen();
return;
}
else
{
sprintf(String, "CH-%02u", gEeprom.FM_SelectedChannel + 1);
}

UI_PrintString(String, 0, 127, 4, 10);

ST7565_BlitFullScreen();
Expand Down
28 changes: 15 additions & 13 deletions ui/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ void UI_GenerateChannelString(char *pString, const uint8_t Channel)

void UI_GenerateChannelStringEx(char *pString, const bool bShowPrefix, const uint8_t ChannelNumber)
{
if (gInputBoxIndex > 0)
{
unsigned int i;
for (i = 0; i < 3; i++)
if (gInputBoxIndex > 0) {
for (unsigned int i = 0; i < 3; i++) {
pString[i] = (gInputBox[i] == 10) ? '-' : gInputBox[i] + '0';
}

pString[3] = 0;
return;
}

if (bShowPrefix)
if (bShowPrefix) {
// BUG here? Prefixed NULLs are allowed
sprintf(pString, "CH-%03u", ChannelNumber + 1);
else
if (ChannelNumber == 0xFF)
} else if (ChannelNumber == 0xFF) {
strcpy(pString, "NULL");
else
} else {
sprintf(pString, "%03u", ChannelNumber + 1);
}
}

void UI_PrintString(const char *pString, uint8_t Start, uint8_t End, uint8_t Line, uint8_t Width)
Expand Down Expand Up @@ -112,10 +114,10 @@ void UI_PrintStringSmall(const char *pString, uint8_t Start, uint8_t End, uint8_
{
const size_t Length = strlen(pString);
size_t i;

if (End > Start)
Start += (((End - Start) - (Length * 8)) + 1) / 2;

const unsigned int char_width = ARRAY_SIZE(gFontSmallBold[0]);
const unsigned int char_spacing = char_width + 1;
uint8_t *pFb = gFrameBuffer[Line] + Start;
Expand Down Expand Up @@ -171,7 +173,7 @@ void UI_DisplayFrequency(const char *string, uint8_t X, uint8_t Y, bool center)
*pFb1 = 0x60; pFb0++; pFb1++;
continue;
}

}
else if (center) {
pFb0 -= 6;
Expand All @@ -182,7 +184,7 @@ void UI_DisplayFrequency(const char *string, uint8_t X, uint8_t Y, bool center)
}
}

void UI_DrawPixelBuffer(uint8_t (*buffer)[128], uint8_t x, uint8_t y, bool black)
void UI_DrawPixelBuffer(uint8_t (*buffer)[128], uint8_t x, uint8_t y, bool black)
{
const uint8_t pattern = 1 << (y % 8);
if(black)
Expand Down Expand Up @@ -228,7 +230,7 @@ void UI_DrawRectangleBuffer(uint8_t (*buffer)[128], int16_t x1, int16_t y1, int1
UI_DrawLineBuffer(buffer, x1,y2, x2,y2, black);
}

void UI_DisplayPopup(const char *string)
void UI_DisplayPopup(const char *string)
{
for(uint8_t i = 0; i < 7; i++) {
memset(gFrameBuffer[i], 0x00, 128);
Expand Down
3 changes: 1 addition & 2 deletions ui/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ static void Render(void)
memset(gStatusLine, 0, sizeof(gStatusLine));
memset(gFrameBuffer, 0, sizeof(gFrameBuffer));

strcpy(String, "LOCK");
UI_PrintString(String, 0, 127, 1, 10);
UI_PrintString("LOCK", 0, 127, 1, 10);
for (i = 0; i < 6; i++)
String[i] = (gInputBox[i] == 10) ? '-' : '*';
String[6] = 0;
Expand Down
6 changes: 2 additions & 4 deletions ui/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,7 @@ void UI_DisplayMain(void)

center_line = CENTER_LINE_DTMF_DEC;

strcpy(String, "DTMF ");
strcat(String, gDTMF_RX_live + idx);
sprintf(String, "DTMF %s", gDTMF_RX_live + idx);
UI_PrintStringSmall(String, 2, 0, 3);
}
#else
Expand All @@ -789,8 +788,7 @@ void UI_DisplayMain(void)

center_line = CENTER_LINE_DTMF_DEC;

strcpy(String, "DTMF ");
strcat(String, gDTMF_RX + idx);
sprintf(String, "DTMF %s", gDTMF_RX_live + idx);
UI_PrintStringSmall(String, 2, 0, 3);
}
#endif
Expand Down
Loading

0 comments on commit f8ef687

Please sign in to comment.