lexus2k / ssd1306

Driver for SSD1306, SSD1331, SSD1351, IL9163, ILI9341, ST7735, PCD8544, Nokia 5110 displays running on Arduino/ESP32/Linux (Rasperry) platforms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to display double or floats

Oberson-Antoine opened this issue · comments

Hello, first of all thank you for making this librairy, for a project i need to display
floating point number which incremented or decremented with a rotary encoder, i saw how to do it with int but not with floating point numbers!
Thank you !

Hello,

For now the library doesn't has built-in function to display float numbers, but here is an example how to to that:

#include "ssd1306.h"
#include <stdio.h>

void printFloatNumber(int x, int y, float number)
{
    char intStr[16];
    snprintf(intStr, sizeof(intStr), "%f", number );
    ssd1306_printFixed (x, y, intStr, STYLE_NORMAL);
}

Thanks for the fast answer! sadly i can't get it to work, it just freezes the atmega and display a "?".

By default, Arduino does not include full support for floating point in printf(). Try using dtostrf() instead.

The dtostrf slows waaay too much my code which makes it "skip" the inputs from the encoder :/

If you are only incrementing a floating point number with an encoder input then I would definitely suggest looking into the option of eliminating your floats altogether and replacing them with a fixed-point notation using integers. This should speed up your code / reduce the memory footprint too.

In other words, if you need at most 2 decimal places, then your internal counter uses “1562” to represent “15.62” and you simply use division/100 when converting back to a “floating point” string, perhaps something like the following:

int32_t valInt;
sprintf(str,”%03d.%02d”, valInt/100, valInt%100)

Alternately, you may also be able to use floats and perform a similar means of constructing the string by parts: (Didn’t try compiling this so might need correction :)

double valFloat;
sprintf(str,”%03d.%02d”, int16_t(valFloat),int16_t(valFloat*100) - int16_t(valFloat)*100);

Thank you all for your help ! I kind of optimised my code and used dtostrf, have a nice day !