Information About "dtostrf" Support on SPRESENSE Arduino IDE

Asked 2 years ago, Updated 2 years ago, 74 views

Programming with SPRESENSE Arduino IDE (Ver1.8.16).

Regarding the function dtoostrf, which is often used to format float and double values in the Arduino language, the following compilation error occurs:

 dtostrf_test:13:24:error:'dtostrf' was not decoded in this scope
      dtostrf(val1,3,1,s);
                           ^
exit status1
'dtostrf' was not decoded in this scope

In Arduino language, the sprintf function does not support float/double, so I often use dtoostrf, but is it not supported by the SPRESENSE library?

Use Cases:

chars[7];
chat[7];
char buf[20];
float val1 = -10.254;
float val2 = 97.623;
dtostrf(val1,3,1,s);
dtostrf(val2,3,1,t);
sprintf(buf, "%s, %s", %s, t);

Thank you for your cooperation.

spresense arduino

2022-09-30 18:19

1 Answers

No.1
Implement without dtostrf as sprintf supports floating-point.

sprintf(buf, "%3.1f, %3.1f", val1, val2);

No. 2
Use the String class to convert to a string.

Strings (val1,1);
  String tt(val2,1);
  sprintf(buf, "%s, %s", ss.c_str(), tt.c_str());

Third

if dtoostrf is already heavily used and difficult to replace Extract the dtostrf function in the String class and define it locally to utilize it.

#ifdef ARDUINO_ARCH_SPRESENSE
char*dtostrf (double value, unsigned int width, unsigned int decimalPlaces, char*buf)
{
  char fmt [20];

  snprintf(fmt,20, "%%%d.%df", width, decimalPlaces);
  sprintf(buf, fmt, value);
  return buf;
}
#endif

Personally, dtostrf is too special, so
I recommend が, which is a common way to use the C language standard library.


2022-09-30 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.