connect characters?

Hello,

I'm working with a display that accepts char messages like this:

uint8_t xpos = 0;
uint8_t ypos = 1;
char[8] theMessage = "";
uint16_t value = 1000;


//****** Convert value to Char
ltoa (value,theMessage,10);


DOG.string(xpos, ypos, font_16x32nums, theMessage);

DOG.string(xpos, 11, font_16x32nums, "unit");

Now the problem is when i print out something the lines of the display look like this

**********
999  unit
1      unit
**********

this looks very bad, i would have to be the resprective digits fixed like:

**********
999  unit
 10  unit
   1  unit
**********

for this i created function

if(value <100){
ypos = 1;
}
if (value <10){
ypos = 2;
}

which works, but sadly this thing is font dependend. The positions for a new characters are different for each display font ( 8x8, 8x6, 8x16, 16x16) and are line depending. So this function is needed to be individual for each value print.

Is there any way to do this in a neutral way like you can do with float?

if i convert a float to a string, its lenght is fixed by setting the lenght:

float test  = 3.141;
float test2  = 13.141;
char theMessage[8];

dtostrf(test, 5,3, theMessage);  //char will be 3.141
dtostrf(test, 5,2, theMessage);  //char will be 13.14

but sadly ITOA does not provide this...

Any work around?

I think you messed up the examples, I think you get

**********
999  unit
1  unit
**********

And want

**********
999  unit
1    unit
**********

Correct?

Just get the length of the string after you converted it to a string and pad it with spaces (assuming fixed with characters).

uint8_t xpos = 0;
uint8_t ypos = 1;
char[8] theMessage = "";
uint16_t value = 1000;

byte fixedLen = 5;


//****** Convert value to Char
ltoa (value, theMessage, 10);
byte len = strlen(theMessage);
while(len < fixedLen){
  theMessage[len++] = ' '; //add a space
  theMessage[len] = '\0'; //be sure to NULL terminate it again
}


DOG.string(xpos, ypos, font_16x32nums, theMessage);

Yes the examples are messed up, but not like you posted, i want to have a representing digit always at the same place.

eg: lets say your number is 4321.

4321
321
21
1

a once digit number should always be rightmost and then the digits should add to the left. Else, it looks very odd if the numbers jump around when going from 1000 to 100

What about something like

if (myNum < 1000) {
  Serial.print(' ');
}
if (myNum < 100) {
  Serial.print(' ');
}
if (myNum < 10) {
  Serial.print(' ');
}
Serial.print(myNum);

...R

uint8_t xpos = 0;
uint8_t ypos = 1;
char[8] theMessage = "";
uint16_t value = 1000;
//****** Make a string...
snprintf(theMessage, sizeof(theMessage), "%4d unit", value);
DOG.string(xpos, ypos, font_16x32nums, theMessage);

look at the C format specifiers

Yeah, snprintf() can do that as well but is larger in footprint but it is save against having a to small buffer.

My take on reply #1 but padding it left:

uint8_t xpos = 0;
uint8_t ypos = 1;
char theMessage[11] = "";
uint16_t value = 1000;

byte fixedLen = 5;

void setup(){
  Serial.begin(115200);

  ltoa (value, theMessage, 10);
  byte len = strlen(theMessage);
  
  //be sure to null-terminate
  theMessage[6] = '\0';
  
  byte padding = fixedLen - len;
  do{
    theMessage[len + padding] = theMessage[len];
    theMessage[len] = ' ';
  }
  while(len--);
  
  Serial.println(theMessage);
}

void loop(){
  
}