SparkFun Forums 

Where electronics enthusiasts find answers.

General MicroView Support area - You have questions, find your answers here.

Moderators: Help@GeekAmmo, marcus@geekammo

By AUArduino
#173227
Is there a command to concatenate a string. If not, is there some way to find the length of a string or where it'll end. Hoping there is a way to do it so I don't have to hard code the set cursor command after every word or variable. Thanks.
By Help@GeekAmmo
#173336
Hi @AuArduino,

You can use the command getFontWidth() to return the current font width, then calculate the length of your string, and multiply them to get the total width. Then compare with getLCDWidth() to check if the string fit on the LCD width. Below is the example. Hope this helps.

Cheers
JP
Code: Select all
#include <MicroView.h>

void setup() {
  // put your setup code here, to run once:
  uView.begin();
  uView.clear(PAGE);
}

void loop() {
  // put your main code here, to run repeatedly: 
  int fontWidth = uView.getFontWidth();
  char strTest[]="Hello";
  char strTest2[]="Hello World Test" ;
  
  int strTestLen=strlen(strTest);
  int strTest2Len=strlen(strTest2);
  
  if ((strTestLen*fontWidth)<uView.getLCDWidth()) 
    uView.print("Fit");
  else 
    uView.print("Not Fit");
  
  uView.setCursor(0,10);
  if ((strTest2Len*fontWidth)<uView.getLCDWidth()) 
    uView.print("Fit");
  else 
    uView.print("NotFit");
  
  uView.display();

  while(1){}  
}
By scotta
#173920
JP,
Is your code example correct? I did a test with the 5 x 7 font and found that getFontWidth() returns 5, which doesn't account for the extra pixel space between characters. At least for the 5 x 7 font, I think you have to multiply the string length by (uView.getFontWidth() + 1).
By Help@GeekAmmo
#174198
Hi Scott,

In the actual font file, there is no extra pixel space, so getFontWidth() returning 5 is technically correct.

Depending on how a user wish to display the text, if user is using print() then adding 1 to the getFontWidth() will get the desired result, if the user wish to drawChar() manually without extra pixel space, then getFontWidth() without adding 1 will be correct.

So it all depends on the situation. My example only serves as a basis for the explanation.