By default, floating point support is disabled to save memory. You have three primary ways to print floats:
1. Enable standard %f support
If you have sufficient flash memory, you can enable standard printf float support by adding a flag to your application's Makefile. This allows you to use the %f specifier directly.
Makefile flag:
LDFLAGS += -u _printf_float
2. Use Logger macros (Low memory cost)
For minimal memory impact, use the FLT_FMT and FLT_VAR macros provided by the Logger class. These allow you to specify precision without the large overhead of the standard float library.
3. Use FixedCapStr (Flexible/UI focused)
Use the FixedCapStr class and its AppendFloat method. This is useful for building complex strings or UIs. You must ensure the template size is large enough for the resulting string.
Note: AppendFloat rounds to 2 decimal places by default unless a second argument is provided.
// Option 1: Standard %f (requires LDFLAGS += -u _printf_float)
float my_flt = 123.456f;
hw.PrintLine("My Float: %f", my_flt);
// Option 2: Macros (Low memory cost)
// Using fixed precision (3 decimal places)
hw.PrintLine("My Float: " FLT_FMT3, FLT_VAR3(my_flt));
// Using generic precision (6 decimal places)
hw.PrintLine("My Float: " FLT_FMT(6), FLT_VAR(6, my_flt));
// Option 3: FixedCapStr
FixedCapStr<16> str("Value: ");
str.AppendFloat(123.456f, 3); // 3 decimal places
hw.PrintLine(str);