Skip to content

Commit 850d57a

Browse files
committed
Refactor float conversion in CommonCLI to use strtof for improved precision and add ftoa3 function for formatting floats with three decimal places in TxtDataHelpers to fix display issue in app and repeater config ui in web
REPO: 1. Flash a repeater 2. Connect over lora 3. Set bw to 42.7 KHZ It will revert back due to converting a double to a float. REPO2: 1.Flash Repeater 2. Apply float fix 3. Set to say 20.8 4. try to get value via app or web cli repeater config It wil show blank because it doesnt return a good value. It would be something like 20.7999992 which the app and web apps dont like so the ftoa3 rounds it and returns a 3 decimal point float
1 parent 8dbb0f5 commit 850d57a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/helpers/CommonCLI.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
228228
strcpy(tmp, &command[10]);
229229
const char *parts[5];
230230
int num = mesh::Utils::parseTextParts(tmp, parts, 5);
231-
float freq = num > 0 ? atof(parts[0]) : 0.0f;
232-
float bw = num > 1 ? atof(parts[1]) : 0.0f;
231+
float freq = num > 0 ? strtof(parts[0], nullptr) : 0.0f;
232+
float bw = num > 1 ? strtof(parts[1], nullptr) : 0.0f;
233233
uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
234234
uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
235235
int temp_timeout_mins = num > 4 ? atoi(parts[4]) : 0;
@@ -284,7 +284,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
284284
} else if (memcmp(config, "radio", 5) == 0) {
285285
char freq[16], bw[16];
286286
strcpy(freq, StrHelper::ftoa(_prefs->freq));
287-
strcpy(bw, StrHelper::ftoa(_prefs->bw));
287+
strcpy(bw, StrHelper::ftoa3(_prefs->bw));
288288
sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)_prefs->sf, (uint32_t)_prefs->cr);
289289
} else if (memcmp(config, "rxdelay", 7) == 0) {
290290
sprintf(reply, "> %s", StrHelper::ftoa(_prefs->rx_delay_base));
@@ -407,8 +407,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
407407
strcpy(tmp, &config[6]);
408408
const char *parts[4];
409409
int num = mesh::Utils::parseTextParts(tmp, parts, 4);
410-
float freq = num > 0 ? atof(parts[0]) : 0.0f;
411-
float bw = num > 1 ? atof(parts[1]) : 0.0f;
410+
float freq = num > 0 ? strtof(parts[0], nullptr) : 0.0f;
411+
float bw = num > 1 ? strtof(parts[1], nullptr) : 0.0f;
412412
uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
413413
uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
414414
if (freq >= 300.0f && freq <= 2500.0f && sf >= 5 && sf <= 12 && cr >= 5 && cr <= 8 && bw >= 7.0f && bw <= 500.0f) {

src/helpers/TxtDataHelpers.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ const char* StrHelper::ftoa(float f) {
140140
return tmp;
141141
}
142142

143+
const char* StrHelper::ftoa3(float f) {
144+
static char s[16];
145+
int v = (int)(f * 1000.0f + (f >= 0 ? 0.5f : -0.5f)); // rounded ×1000
146+
int w = v / 1000; // whole
147+
int d = abs(v % 1000); // decimals
148+
snprintf(s, sizeof(s), "%d.%03d", w, d);
149+
for (int i = strlen(s) - 1; i > 0 && s[i] == '0'; i--)
150+
s[i] = 0;
151+
int L = strlen(s);
152+
if (s[L - 1] == '.') s[L - 1] = 0;
153+
return s;
154+
}
155+
143156
uint32_t StrHelper::fromHex(const char* src) {
144157
uint32_t n = 0;
145158
while (*src) {

src/helpers/TxtDataHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class StrHelper {
1212
static void strncpy(char* dest, const char* src, size_t buf_sz);
1313
static void strzcpy(char* dest, const char* src, size_t buf_sz); // pads with trailing nulls
1414
static const char* ftoa(float f);
15+
static const char* ftoa3(float f); //Converts float to string with 3 decimal places
1516
static bool isBlank(const char* str);
1617
static uint32_t fromHex(const char* src);
1718
};

0 commit comments

Comments
 (0)