Skip to content

Commit 8340d0e

Browse files
authored
Merge pull request #1104 from zjs81/Fix-BW-setting-and-returning
Refactor float conversion in CommonCLI
2 parents a9397c1 + 850d57a commit 8340d0e

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
@@ -231,8 +231,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
231231
strcpy(tmp, &command[10]);
232232
const char *parts[5];
233233
int num = mesh::Utils::parseTextParts(tmp, parts, 5);
234-
float freq = num > 0 ? atof(parts[0]) : 0.0f;
235-
float bw = num > 1 ? atof(parts[1]) : 0.0f;
234+
float freq = num > 0 ? strtof(parts[0], nullptr) : 0.0f;
235+
float bw = num > 1 ? strtof(parts[1], nullptr) : 0.0f;
236236
uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
237237
uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
238238
int temp_timeout_mins = num > 4 ? atoi(parts[4]) : 0;
@@ -287,7 +287,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
287287
} else if (memcmp(config, "radio", 5) == 0) {
288288
char freq[16], bw[16];
289289
strcpy(freq, StrHelper::ftoa(_prefs->freq));
290-
strcpy(bw, StrHelper::ftoa(_prefs->bw));
290+
strcpy(bw, StrHelper::ftoa3(_prefs->bw));
291291
sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)_prefs->sf, (uint32_t)_prefs->cr);
292292
} else if (memcmp(config, "rxdelay", 7) == 0) {
293293
sprintf(reply, "> %s", StrHelper::ftoa(_prefs->rx_delay_base));
@@ -417,8 +417,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
417417
strcpy(tmp, &config[6]);
418418
const char *parts[4];
419419
int num = mesh::Utils::parseTextParts(tmp, parts, 4);
420-
float freq = num > 0 ? atof(parts[0]) : 0.0f;
421-
float bw = num > 1 ? atof(parts[1]) : 0.0f;
420+
float freq = num > 0 ? strtof(parts[0], nullptr) : 0.0f;
421+
float bw = num > 1 ? strtof(parts[1], nullptr) : 0.0f;
422422
uint8_t sf = num > 2 ? atoi(parts[2]) : 0;
423423
uint8_t cr = num > 3 ? atoi(parts[3]) : 0;
424424
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)