Swap most string operations to bounded versions

This commit is contained in:
Reed Nightingale 2020-01-19 19:00:45 -08:00
parent 9b224699ee
commit 315d9348f0
3 changed files with 33 additions and 48 deletions

View File

@ -1,5 +1,4 @@
#include <Arduino.h>
#include "freeRam.h"
#include "morse.h"
#include "nano_gui.h"
#include "setup.h"
@ -67,11 +66,11 @@ void displayDialog(const char* title,
displayClear(COLOR_BACKGROUND);
displayRect(LAYOUT_OUTER_BORDER_X,LAYOUT_OUTER_BORDER_Y,LAYOUT_OUTER_BORDER_WIDTH,LAYOUT_OUTER_BORDER_HEIGHT, COLOR_ACTIVE_BORDER);
displayRect(LAYOUT_INNER_BORDER_X,LAYOUT_INNER_BORDER_Y,LAYOUT_INNER_BORDER_WIDTH,LAYOUT_INNER_BORDER_HEIGHT, COLOR_ACTIVE_BORDER);
strcpy_P(b,title);
strncpy_P(b,title,sizeof(b));
displayText(b, LAYOUT_TITLE_X, LAYOUT_TITLE_Y, LAYOUT_TITLE_WIDTH, LAYOUT_TITLE_HEIGHT, COLOR_TEXT, COLOR_TITLE_BACKGROUND, COLOR_ACTIVE_BORDER);
strcpy_P(b,instructions);
strncpy_P(b,instructions,sizeof(b));
displayText(b, LAYOUT_INSTRUCTIONS_TEXT_X, LAYOUT_INSTRUCTIONS_TEXT_Y, LAYOUT_INSTRUCTIONS_TEXT_WIDTH, LAYOUT_INSTRUCTIONS_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND);
strcpy_P(b,(const char*)F("Push Tune to Save"));
strncpy_P(b,(const char*)F("Push Tune to Save"),sizeof(b));
displayText(b, LAYOUT_CONFIRM_TEXT_X, LAYOUT_CONFIRM_TEXT_Y, LAYOUT_CONFIRM_TEXT_WIDTH, LAYOUT_CONFIRM_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND);
}
@ -155,7 +154,7 @@ ssCwToneChange(const long int new_value, char* buff_out, const size_t buff_out_s
globalSettings.cwSideToneFreq = new_value;
tone(CW_TONE, globalSettings.cwSideToneFreq);
ltoa(globalSettings.cwSideToneFreq,buff_out,10);
strcat_P(buff_out,(const char*)F("Hz"));
strncat_P(buff_out,(const char*)F("Hz"),buff_out_size - strlen(buff_out));
}
ssCwToneFinalize(const long int final_value)
{
@ -186,7 +185,6 @@ void ssLocalOscInitialize(long int* start_value_out){
si5351bx_setfreq(0, globalSettings.usbCarrierFreq); //set back the carrier oscillator, cw tx switches it off
}
*start_value_out = globalSettings.oscillatorCal;
Serial.println(freeRam());
}
void ssLocalOscValidate(const long int candidate_value_in, long int* validated_value_out)
{
@ -198,11 +196,11 @@ void ssLocalOscChange(const long int new_value, char* buff_out, const size_t buf
setFrequency(GetActiveVfoFreq());
const long int u = abs(new_value);
if(new_value != u){
strcpy_P(buff_out,(const char*)F("-"));
strncpy_P(buff_out,(const char*)F("-"),buff_out_size);
++buff_out;
}
formatFreq(u,buff_out,buff_out_size);
strcat_P(buff_out,(const char*)F("Hz"));
formatFreq(u,buff_out,buff_out_size - strlen(buff_out));
strncat_P(buff_out,(const char*)F("Hz"),buff_out_size - strlen(buff_out));
}
void ssLocalOscFinalize(const long int final_value)
{
@ -261,7 +259,7 @@ void setupCwDelay(){
prev_cw_delay = globalSettings.cwActiveTimeoutMs;
ltoa(globalSettings.cwActiveTimeoutMs, b, 10);
strcat_P(b,(const char*)F(" msec"));
strncat_P(b,(const char*)F(" msec"),sizeof(b) - strlen(b));
displayText(b, LAYOUT_SETTING_VALUE_X, LAYOUT_SETTING_VALUE_Y, LAYOUT_SETTING_VALUE_WIDTH, LAYOUT_SETTING_VALUE_HEIGHT, COLOR_TEXT, COLOR_SETTING_BACKGROUND, COLOR_BACKGROUND);
while (!btnDown()){
@ -275,7 +273,7 @@ void setupCwDelay(){
continue; //don't update the frequency or the display
ltoa(globalSettings.cwActiveTimeoutMs, b, 10);
strcat_P(b,(const char*)F(" msec"));
strncat_P(b,(const char*)F(" msec"),sizeof(b) - strlen(b));
displayText(b, LAYOUT_SETTING_VALUE_X, LAYOUT_SETTING_VALUE_Y, LAYOUT_SETTING_VALUE_WIDTH, LAYOUT_SETTING_VALUE_HEIGHT, COLOR_TEXT, COLOR_SETTING_BACKGROUND, COLOR_BACKGROUND);
}
@ -384,7 +382,7 @@ void setupCwTone(){
void setupResetAll()
{
//displayDialog(F("Reset all cals and settings?"),F("Press tune to Confirm"));
strcpy_P(b,(const char*)F("No"));
strncpy_P(b,(const char*)F("No"),sizeof(b));
displayText(b, LAYOUT_SETTING_VALUE_X, LAYOUT_SETTING_VALUE_Y, LAYOUT_SETTING_VALUE_WIDTH, LAYOUT_SETTING_VALUE_HEIGHT, COLOR_TEXT, COLOR_SETTING_BACKGROUND, COLOR_BACKGROUND);
bool reset_all = false;
@ -402,10 +400,10 @@ void setupResetAll()
}
if(reset_all){
strcpy_P(b,(const char*)F("Yes"));
strncpy_P(b,(const char*)F("Yes"),sizeof(b));
}
else{
strcpy_P(b,(const char*)F("No"));
strncpy_P(b,(const char*)F("No"),sizeof(b));
}
displayText(b, LAYOUT_SETTING_VALUE_X, LAYOUT_SETTING_VALUE_Y, LAYOUT_SETTING_VALUE_WIDTH, LAYOUT_SETTING_VALUE_HEIGHT, COLOR_TEXT, COLOR_SETTING_BACKGROUND, COLOR_BACKGROUND);
@ -474,15 +472,15 @@ void drawMenu(const MenuItem_t* const items, const uint16_t num_items)
displayClear(COLOR_BACKGROUND);
MenuItem_t mi = {"",nullptr};
memcpy_P(&mi,&items[0],sizeof(mi));
strcpy_P(b,mi.ItemName);
strncpy_P(b,mi.ItemName,sizeof(b));
displayText(b, LAYOUT_TITLE_X, LAYOUT_TITLE_Y, LAYOUT_TITLE_WIDTH, LAYOUT_TITLE_HEIGHT, COLOR_TEXT, COLOR_TITLE_BACKGROUND, COLOR_ACTIVE_BORDER);
for(unsigned int i = 1; i < num_items; ++i){
memcpy_P(&mi,&items[i],sizeof(mi));
strcpy_P(b,mi.ItemName);
strncpy_P(b,mi.ItemName,sizeof(b));
displayText(b, LAYOUT_ITEM_X, LAYOUT_ITEM_Y + (i-1)*LAYOUT_ITEM_PITCH_Y, LAYOUT_ITEM_WIDTH, LAYOUT_ITEM_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_INACTIVE_BORDER);
}
memcpy_P(&mi,&exitMenu,sizeof(mi));
strcpy_P(b,mi.ItemName);
strncpy_P(b,mi.ItemName,sizeof(b));
displayText(b, LAYOUT_ITEM_X, LAYOUT_ITEM_Y + (num_items-1)*LAYOUT_ITEM_PITCH_Y, LAYOUT_ITEM_WIDTH, LAYOUT_ITEM_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_INACTIVE_BORDER);
}

View File

@ -123,13 +123,6 @@ void catReadEEPRom(void)
//for remove warnings
byte temp0 = cat[0];
byte temp1 = cat[1];
/*
itoa((int) cat[0], b, 16);
strcat(b, ":");
itoa((int) cat[1], c, 16);
strcat(b, c);
printLine2(b);
*/
cat[0] = 0;
cat[1] = 0;
@ -385,10 +378,6 @@ void processCATCommand2(byte* cmd) {
default:
//somehow, get this to print the four bytes
ultoa(*((unsigned long *)cmd), c, 16);
/*itoa(cmd[4], b, 16);
strcat(b, ">");
strcat(b, c);
printLine2(b);*/
response[0] = 0x00;
Serial.write(response[0]);
}

View File

@ -1,5 +1,4 @@
#include <Arduino.h>
#include "freeRam.h"
#include "morse.h"
#include "settings.h"
#include "setup.h"
@ -166,10 +165,10 @@ int getValueByKnob(int minimum, int maximum, int step_size, int initial, const
active_delay(200);
knob_value = initial;
strcpy_P(b,(const char*)prefix);
strncpy_P(b,(const char*)prefix,sizeof(b));
itoa(knob_value, c, 10);
strcat(b, c);
strcat_P(b, (const char*)postfix);
strncat(b, c, sizeof(b) - strlen(b));
strncat_P(b, (const char*)postfix, sizeof(b) - strlen(b));
drawCommandbar(b);
while(!btnDown() && digitalRead(PTT) == HIGH){
@ -180,10 +179,10 @@ int getValueByKnob(int minimum, int maximum, int step_size, int initial, const
if (knob_value < maximum && knob > 0)
knob_value += step_size;
strcpy_P(b,(const char*)prefix);
strncpy_P(b,(const char*)prefix,sizeof(b));
itoa(knob_value, c, 10);
strcat(b, c);
strcat_P(b,(const char*)postfix);
strncat(b, c, sizeof(b) - strlen(b));
strncat_P(b,(const char*)postfix, sizeof(b) - strlen(b));
drawCommandbar(b);
}
checkCAT();
@ -335,8 +334,8 @@ void displayRIT(){
c[0] = 0;
displayFillrect(LAYOUT_MODE_TEXT_X,LAYOUT_MODE_TEXT_Y,LAYOUT_MODE_TEXT_WIDTH,LAYOUT_MODE_TEXT_HEIGHT, COLOR_BACKGROUND);
if(globalSettings.ritOn){
strcpy_P(c,(const char*)F("TX:"));
formatFreq(globalSettings.ritFrequency, c+3, sizeof(c)-3);
strncpy_P(c,(const char*)F("TX:"),sizeof(c));
formatFreq(globalSettings.ritFrequency, c+3, sizeof(c)-strlen(c));
if (VFO_A == globalSettings.activeVfo)
displayText(c, LAYOUT_VFO_LABEL_X + 0*LAYOUT_VFO_LABEL_PITCH_X, LAYOUT_MODE_TEXT_Y, LAYOUT_VFO_LABEL_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND);
else
@ -350,9 +349,9 @@ void fastTune(){
active_delay(50);
active_delay(300);
strcpy_P(c,(const char*)F("Fast tune"));
strncpy_P(c,(const char*)F("Fast tune"),sizeof(c));
displayText(c, LAYOUT_MODE_TEXT_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND);
while(1){
while(true){
checkCAT();
//exit after debouncing the btnDown
@ -472,8 +471,8 @@ void enterFreq(){
}//switch
}//if button hit test
}// end of the button scanning loop
strcpy(b, c);
strcat_P(b,(const char*)F(" KHz"));
strncpy(b, c, sizeof(b));
strncat_P(b,(const char*)F(" KHz"),sizeof(b) - strlen(b));
displayText(b, LAYOUT_MODE_TEXT_X, LAYOUT_MODE_TEXT_Y, LAYOUT_MODE_TEXT_WIDTH, LAYOUT_MODE_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND);
active_delay(300);
while(readTouch())
@ -482,21 +481,21 @@ void enterFreq(){
}
void drawCWStatus(){
strcpy_P(b,(const char*)F(" cw: "));
strncpy_P(b,(const char*)F(" cw: "),sizeof(b));
int wpm = 1200/globalSettings.cwDitDurationMs;
itoa(wpm,c, 10);
strcat(b, c);
strcat_P(b,(const char*)F("wpm, "));
strncat(b, c, sizeof(b) - strlen(b));
strncat_P(b,(const char*)F("wpm, "), sizeof(b) - strlen(b));
itoa(globalSettings.cwSideToneFreq, c, 10);
strcat(b, c);
strcat_P(b,(const char*)F("hz"));
strncat(b, c, sizeof(b) - strlen(b));
strncat_P(b,(const char*)F("hz"), sizeof(b) - strlen(b));
displayText(b, LAYOUT_CW_TEXT_X, LAYOUT_CW_TEXT_Y, LAYOUT_CW_TEXT_WIDTH, LAYOUT_CW_TEXT_HEIGHT, COLOR_TEXT, COLOR_BACKGROUND, COLOR_BACKGROUND);
}
void drawTx(){
if (globalSettings.txActive){
strcpy_P(b,(const char*)F("TX"));
strncpy_P(b,(const char*)F("TX"), sizeof(b));
displayText(b, LAYOUT_TX_X, LAYOUT_TX_Y, LAYOUT_TX_WIDTH, LAYOUT_TX_HEIGHT, COLOR_ACTIVE_TEXT, COLOR_ACTIVE_BACKGROUND, COLOR_BACKGROUND);
}
else{
@ -901,7 +900,6 @@ void doCommand(Button* button){
}
case BUTTON_MNU:
{
Serial.println(freeRam());
doSetup2();
break;
}