Daisy crashes using PrintLine with %f

I found out about the Logger today and am using the pod/SimpleLed example program to experiment with it. When I try to print a float with the %f format string (I also tried %e and %g), it seems to crash the Daisy.

Here’s my program:

#include "daisy_pod.h"

using namespace daisy;

DaisyPod  hw;
Parameter p_knob1, p_knob2;

int main(void)
{
    hw.Init();
    // Wait for the PC to connect before continuing.
    hw.seed.StartLog(true);
    float r = 0, g = 0, b = 0;
    int previousR = -1;
    p_knob1.Init(hw.knob1, 0, 1, Parameter::LINEAR);
    p_knob2.Init(hw.knob2, 0, 1, Parameter::LINEAR);

    hw.StartAdc();

    while(1)
    {
        r = p_knob1.Process();
        g = p_knob2.Process();

        if ((int)(r * 10) != previousR)
        {
            previousR = (int)(r * 10);
            hw.seed.PrintLine("previousR is %d", previousR);
            // The following line is the one that causes problems.
            hw.seed.PrintLine("r is %f", r);
        }

        hw.led1.Set(r, g, b);

        hw.UpdateLeds();
    }
}

When I run this program I see this in the terminal window and the LED is not lit:

Daisy is online
===============
previousR is 0

If I comment out the problem line I get this and the program works as expected:

Daisy is online
===============
previousR is 0
previousR is 4
previousR is 5
previousR is 6
previousR is 5
previousR is 6
previousR is 5

If I change the problem line to hw.seed.PrintLine("r is %d", r); I get this and the LED works (but the value is obviously wrong because the format string doesn’t match the data type):

Daisy is online
===============
previousR is 0
r is 603995752
previousR is 6
r is 603995752
previousR is 5
r is 603995752
previousR is 6
r is 603995752

Why does this not work with %f? Does PrintLine not use the same formatting strings as printf in C++?

Printf / sprintf float support is not enabled by default (google stm32 printf float etc) - from what I’ve read it could be enabled but it can add a pretty significant burden to the codespace (like an additional 15K etc).

Anytime I needed to print a floating point value I just multiplied it and printed the result as an integer.

Or, I mean you could do this (on my phone so no guarantees but this should work…) :
float f = ;
int32_t integerportion = (int32_t) f; //get the integer portion (and the sign)
f -= integerportion; //remove the integer portion
f = fabs(f); // remove the sign - we have that for the integer portion

Now we have the integer portion as an int32 and f has the fraction (as a positive value so we don’t get two negatives printed). Multiply by 10 for each digit in the decimal portion you’re interested in :
f *= 1000; // get 3 digits of precision
uint32_t fraction = (uint32_t) f; // get the digits as an integer
Then just printf the values as integers with your own “.” for a decimal.

Hope that helps (and works…!)

There’s also the daisy::FixedCapStr class that has methods for appending floating point values. You can build the message with this class and then transmit the finished FixedCapStr.

2 Likes

Thanks for the info. Now I can stop trying to figure out what I’m doing wrong! :grin: