From 5ca2419e0d9efe878dec565ab52d9ddcd5a903b8 Mon Sep 17 00:00:00 2001 From: philmoz Date: Tue, 20 Aug 2024 09:51:06 +1000 Subject: [PATCH] chore: replace f_puts and f_printf functions to save FLASH space (#5459) --- radio/src/lib_file.cpp | 27 +++++++++++++++++++++++++++ radio/src/logs.cpp | 10 ++++------ radio/src/storage/modelslist.cpp | 27 +++++++++++++++++---------- radio/src/thirdparty/FatFs/ffconf.h | 4 ---- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/radio/src/lib_file.cpp b/radio/src/lib_file.cpp index 5e2bc415c..3523b41ef 100644 --- a/radio/src/lib_file.cpp +++ b/radio/src/lib_file.cpp @@ -20,6 +20,7 @@ #include #include +#include const char * getFileExtension(const char * filename, uint8_t size, uint8_t extMaxLen, uint8_t * fnlen, uint8_t * extlen) { @@ -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 diff --git a/radio/src/logs.cpp b/radio/src/logs.cpp index 5ea2c3827..0e0f029a5 100644 --- a/radio/src/logs.cpp +++ b/radio/src/logs.cpp @@ -183,7 +183,6 @@ void writeHeader() f_puts("Time,", &g_oLogFile); #endif - char label[TELEM_LABEL_LEN+7]; for (int i=0; i 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; } /** diff --git a/radio/src/thirdparty/FatFs/ffconf.h b/radio/src/thirdparty/FatFs/ffconf.h index 7ea64f871..69d72880f 100644 --- a/radio/src/thirdparty/FatFs/ffconf.h +++ b/radio/src/thirdparty/FatFs/ffconf.h @@ -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