Skip to content

Commit

Permalink
chore: replace f_puts and f_printf functions to save FLASH space (#5459)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz authored Aug 19, 2024
1 parent 06db4c7 commit 5ca2419
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
27 changes: 27 additions & 0 deletions radio/src/lib_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <inttypes.h>
#include <cinttypes>
#include <stdarg.h>

const char * getFileExtension(const char * filename, uint8_t size, uint8_t extMaxLen, uint8_t * fnlen, uint8_t * extlen)
{
Expand Down Expand Up @@ -107,3 +108,29 @@ FRESULT sdReadDir(DIR * dir, FILINFO * fno, bool & firstTime)
firstTime = false;
return res;
}

#if !defined(BOOT) && !defined(SIMU)

// Replace FatFS implementation of f_puts and f_printf

int f_puts(const char* s, FIL* fp)
{
unsigned int written;
unsigned int len = strlen(s);
FRESULT result = f_write(fp, s, len, &written);
return (len == written && result == FR_OK) ? 0 : EOF;
}

#define MAX_FPRINTF_LEN 100

int f_printf (FIL* fp, const TCHAR* str, ...)
{
char s[MAX_FPRINTF_LEN];
va_list argptr;
va_start(argptr, str);
vsnprintf(s, MAX_FPRINTF_LEN, str, argptr);
va_end(argptr);
return f_puts(s, fp);
}

#endif
10 changes: 4 additions & 6 deletions radio/src/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ void writeHeader()
f_puts("Time,", &g_oLogFile);
#endif


char label[TELEM_LABEL_LEN+7];
for (int i=0; i<MAX_TELEMETRY_SENSORS; i++) {
if (isTelemetryFieldAvailable(i)) {
Expand All @@ -207,16 +206,16 @@ void writeHeader()
auto n_inputs = adcGetMaxInputs(ADC_INPUT_MAIN);
for (uint8_t i = 0; i < n_inputs; i++) {
const char* p = analogGetCanonicalName(ADC_INPUT_MAIN, i);
while (*p) { f_putc(*(p++), &g_oLogFile); }
f_putc(',', &g_oLogFile);
f_puts(p, &g_oLogFile);
f_puts(",", &g_oLogFile);
}

n_inputs = adcGetMaxInputs(ADC_INPUT_FLEX);
for (uint8_t i = 0; i < n_inputs; i++) {
if (!IS_POT_AVAILABLE(i)) continue;
const char* p = analogGetCanonicalName(ADC_INPUT_FLEX, i);
while (*p) { f_putc(*(p++), &g_oLogFile); }
f_putc(',', &g_oLogFile);
f_puts(p, &g_oLogFile);
f_puts(",", &g_oLogFile);
}

for (uint8_t i = 0; i < switchGetMaxSwitches(); i++) {
Expand Down Expand Up @@ -288,7 +287,6 @@ void logsWrite()
return;
}


#if defined(RTCLOCK)
{
static struct gtm utm;
Expand Down
27 changes: 17 additions & 10 deletions radio/src/storage/modelslist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,19 +1404,26 @@ void ModelsList::updateCurrentModelCell()

bool ModelsList::readNextLine(char *line, int maxlen)
{
if (f_gets(line, maxlen, &file) != NULL) {
int curlen = strlen(line) - 1;
if (line[curlen] ==
'\n') { // remove unwanted chars if file was edited using windows
if (line[curlen - 1] == '\r') {
line[curlen - 1] = 0;
} else {
line[curlen] = 0;
while (maxlen > 0) {
unsigned int br;
FRESULT res = f_read(&file, line, 1, &br);
if (res == FR_OK && br == 1) {
// Return if line read
if (*line == 0 || *line == '\n') {
*line = 0;
return true;
}
// Move to next char unless '\r' read
if (*line != '\r') {
line += 1;
maxlen -= 1;
}
} else {
return false;
}
return true;
}
return false;
*line = 0;
return true;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions radio/src/thirdparty/FatFs/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@
/* This option switches f_forward() function. (0:Disable or 1:Enable) */


#if defined(BOOT)
#define FF_USE_STRFUNC 0
#else
#define FF_USE_STRFUNC 1
#endif
#define FF_PRINT_LLI 1
#define FF_PRINT_FLOAT 1
#define FF_STRF_ENCODE 3
Expand Down

0 comments on commit 5ca2419

Please sign in to comment.