Go Down

Topic: Converting floating points to String (Read 175 times) previous topic - next topic

carguy

Sep 18, 2016, 01:25 pm Last Edit: Sep 18, 2016, 01:28 pm by carguy
Hello,

I am kind of toying with a function right now that is supposed to turn numerical values, either integers or floats, into a string array. This string array will then be used to display bitmaps on an Adafruit 1.4'' color TFT that are stored in progmem. These bitmaps are the numbers from 0 to 9 and a dot.

I know that this is kind of a roundabout way of displaying numbers on a screen, but I'm trying to make it work to see if in principle, it will look better on the screen than the Adafruit-supplied free fonts. Also, since these numbers bitmaps have a white background, it would eliminate the need to call tft.fillScreen() when the data readings on the screen are updated.


So here goes:

Code: [Select]
// Numbers as Progmem

const unsigned char c0 [] PROGMEM = {
  0x12, 0x00, 0x19, 0x00,
  0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0x78, 0x78, 0x78, 0x78, 0xF8, 0xF0, 0xE0, 0xC0, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF,
  0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x1F, 0x3F, 0x7E, 0x78, 0x78, 0x78, 0x78, 0x7E,
  0x3F, 0x1F, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c1 [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xF8, 0xF8, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x07, 0x07, 0x03, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F, 0x7F, 0x7F,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c2 [] PROGMEM = {
  0x00, 0x00, 0x80, 0xE0, 0xF0, 0xF0, 0xF8, 0x78, 0x78, 0x78, 0x78, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0,
  0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0x78, 0x7F, 0x3F,
  0x0F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x60, 0x78, 0x7C, 0x7E, 0x7F, 0x7F, 0x7B, 0x79, 0x78, 0x78,
  0x78, 0x78, 0x78, 0x78, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c3 [] PROGMEM = {
  0x00, 0x00, 0x80, 0xE0, 0xF0, 0xF0, 0xF8, 0x78, 0x78, 0x78, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x38, 0x38, 0x38, 0x3C, 0x7F, 0xFF, 0xE7,
  0xC3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F, 0x1F, 0x3F, 0x7E, 0x78, 0x78, 0x78, 0x78, 0x7C,
  0x3F, 0x3F, 0x1F, 0x07, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c4 [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF8, 0xF8, 0xF8, 0xF8, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xF0, 0x78, 0x3E, 0x0F, 0x07, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x7F, 0x7F,
  0x7F, 0x7F, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c5 [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0xF8, 0xF8, 0xF8, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x3F, 0x3F, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0xF8, 0xF0,
  0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x1C, 0x3C, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38,
  0x1F, 0x1F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c6 [] PROGMEM = {
  0x00, 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0x78, 0x78, 0x78, 0xF8, 0xF0, 0xF0, 0xE0, 0x80,
  0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0x78, 0x3C, 0x3C, 0x3C, 0x3C, 0x7C, 0xF9, 0xF9,
  0xF1, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x1F, 0x3F, 0x3C, 0x78, 0x78, 0x78, 0x78, 0x7C,
  0x3F, 0x3F, 0x1F, 0x07, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c7 [] PROGMEM = {
  0x00, 0x00, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0xF8, 0xF8, 0xF8, 0xF8, 0x78,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xF8, 0xFC, 0x7F, 0x1F, 0x07, 0x01,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x7F, 0x7F, 0x7F, 0x07, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c8 [] PROGMEM = {
  0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF0, 0x78, 0x38, 0x38, 0x38, 0x38, 0x78, 0xF0, 0xF0, 0xE0, 0xC0,
  0x00, 0x00, 0x00, 0x00, 0x83, 0xC7, 0xEF, 0xFF, 0x7C, 0x38, 0x38, 0x38, 0x38, 0x7C, 0xFF, 0xEF,
  0xC7, 0x83, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1F, 0x3F, 0x3F, 0x78, 0x70, 0x70, 0x70, 0x70, 0x78,
  0x3F, 0x3F, 0x1F, 0x07, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char c9 [] PROGMEM = {
  0x00, 0x00, 0x80, 0xE0, 0xF0, 0xF0, 0xF8, 0x78, 0x78, 0x78, 0x78, 0xF0, 0xF0, 0xE0, 0xC0, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x0F, 0x3F, 0x7F, 0x7F, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0x78, 0xFF, 0xFF,
  0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x06, 0x1E, 0x3E, 0x7E, 0x7C, 0x78, 0x78, 0x78, 0x7C, 0x3C,
  0x3F, 0x1F, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00,
};

// and finally, a dot for float numbers

const unsigned char cd [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x78, 0x78, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};


// Function to turn numbers into a series of displayed bitmaps on screen

// x_0 and y_0 as starting points, x_dim and y_dim are the
// dimensions (width and height) of the individual bitmaps.

// This is supposed to make the function more flexible
// for use with different bitmap sizes.

void numbersToProgmem(String valueString, uint16_t x_0, uint16_t y_0, uint16_t x_dim, uint16_t y_dim) {

  // Putting the numbers into a string array

  int stringLength = valueString.length();
  char charBuf[stringLength];
  valueString.toCharArray(charBuf, stringLength);

  int i = 0;

  int start_x = x_0;
  int start_y = y_0;

  // Printing out the numbers on the screen as a series of bitmaps

  while (i <= stringLength) {

    // changing the floating point dot to "d"
    if (charBuf[i] == ".") charBuf[i] = "d";

    // composing bitmap names
    int bitmapName = "c" + charBuf[i];

    // drawing the bitmap
    tft.drawBitmap(start_x, start_y, bitmapName, x_dim, y_dim);

    // setting new starting point for next bitmap
    start_x = start_x + x_dim + 1;
  }
}



So far, this does not compile. According to the error messages I get, the main issue seems to be converting floating point numbers into strings. How is that best done?

Also, somehow the drawBitmap function needs more arguments?

OldSteve

#1
Sep 18, 2016, 01:56 pm Last Edit: Sep 18, 2016, 02:05 pm by OldSteve
I'm not completely sure what you're asking for, but if you want to convert a float variable to a char array, use 'dtostrf()':-
Code: [Select]
//  dtostrf(val, width, precision, buffer);
float fVal = 123.456;
char buffer[8];
dtostrf(fVal, 7, 3, buffer);  // Stores "123.456" in 'buffer'.

Serial.println(buffer);  // Print the result


Edit: Or, for an int, use 'itoa()':-
Code: [Select]
// itoa( int value, char * str, int base );
int iVal = 12345;
char buffer[6];
itoa(iVal, buffer, 10);

Serial.println(buffer);
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

carguy

Yes, I want to have a char array, the members of which I want to call individually and use to construct a bitmap name from, which is then called from PROGMEM.

But I think you've given me a few ideas now on how best to do that... the function isn't working yet, but I think you've pointed me in the right direction, as usual  :)

Go Up