Skip to content

Commit a8ed8b0

Browse files
authored
Merge pull request #251 from sparkfun/Fix_profileName_defaulting
Correct getProfileName to fix default profileName
2 parents 534cf54 + 7071fd7 commit a8ed8b0

File tree

2 files changed

+1728
-1711
lines changed

2 files changed

+1728
-1711
lines changed

Firmware/RTK_Everywhere/NVM.ino

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ void loadSettings()
6666
int resetCount = settings.resetCount;
6767
SystemState stateFromLFS = settings.lastState;
6868

69-
bool readFromSD = false;
70-
if (loadSystemSettingsFromFileSD(settingsFileName) == true)
71-
readFromSD = true;
69+
loadSystemSettingsFromFileSD(settingsFileName);
7270

7371
settings.resetCount = resetCount;
7472

@@ -77,11 +75,9 @@ void loadSettings()
7775

7876
// Change empty profile name to 'Profile1' etc
7977
if (strlen(settings.profileName) == 0)
78+
{
8079
snprintf(settings.profileName, sizeof(settings.profileName), "Profile%d", profileNumber + 1);
8180

82-
// Only re-record settings if SD was accessed
83-
if (readFromSD == true)
84-
{
8581
// Record these settings to LittleFS and SD file to be sure they are the same
8682
recordSystemSettings();
8783
}
@@ -593,6 +589,9 @@ void recordSystemSettingsToFile(File *settingsFile)
593589
// Don't update settings when searching.
594590
bool loadSystemSettingsFromFileSD(char *fileName, const char *findMe, char *found, int len)
595591
{
592+
if ((findMe != nullptr) && (found != nullptr))
593+
*found = 0; // If searching, set found to NULL
594+
596595
bool gotSemaphore = false;
597596
bool status = false;
598597
bool wasSdCardOnline;
@@ -719,6 +718,9 @@ bool loadSystemSettingsFromFileLFS(char *fileName, const char *findMe, char *fou
719718
{
720719
// log_d("reading setting fileName: %s", fileName);
721720

721+
if ((findMe != nullptr) && (found != nullptr))
722+
*found = 0; // If searching, set found to NULL
723+
722724
File settingsFile = LittleFS.open(fileName, FILE_READ);
723725
if (!settingsFile)
724726
{
@@ -768,10 +770,19 @@ bool loadSystemSettingsFromFileLFS(char *fileName, const char *findMe, char *fou
768770
else
769771
{
770772
// Check if line contains findMe. If it does, return the remainder of the line in found.
773+
// Don't copy the \r or \n
771774
const char *ptr = strstr(line, findMe);
772775
if (ptr != nullptr)
773776
{
774-
strncpy(found, ptr + strlen(findMe), len);
777+
ptr += strlen(findMe);
778+
for (size_t i = strlen(findMe); i < strlen(line); i++)
779+
{
780+
if ((line[i] == '\r') || (line[i] == '\n'))
781+
{
782+
line[i] = 0;
783+
}
784+
}
785+
strncpy(found, ptr, len);
775786
break;
776787
}
777788
}
@@ -1450,21 +1461,21 @@ void setProfileName(uint8_t ProfileNumber)
14501461
// Looks at LittleFS first, then SD
14511462
bool getProfileName(char *fileName, char *profileName, uint8_t profileNameLength)
14521463
{
1453-
char profileNameLFS[50] = "";
1464+
char profileNameLFS[50];
14541465
loadSystemSettingsFromFileLFS(fileName, "profileName=", profileNameLFS, sizeof(profileNameLFS));
1455-
char profileNameSD[50] = "";
1466+
char profileNameSD[50];
14561467
loadSystemSettingsFromFileSD(fileName, "profileName=", profileNameSD, sizeof(profileNameSD));
14571468

14581469
// Zero terminate the profile name
14591470
*profileName = 0;
14601471

14611472
// If we have a profile in both LFS and SD, SD wins
1462-
if (profileNameLFS != "")
1473+
if (strlen(profileNameLFS) > 0)
14631474
strncpy(profileName, profileNameLFS, profileNameLength); // snprintf handles null terminator
1464-
if (profileNameSD != "")
1475+
if (strlen(profileNameSD) > 0)
14651476
strncpy(profileName, profileNameSD, profileNameLength); // snprintf handles null terminator
14661477

1467-
return ((profileNameLFS != "") || (profileNameSD != ""));
1478+
return ((strlen(profileNameLFS) > 0) || (strlen(profileNameSD) > 0));
14681479
}
14691480

14701481
// Loads a given profile name.

0 commit comments

Comments
 (0)