UTFT Library and the F() Macro

Hi everyone,

When using Henning's UTFT library, I've seen some people recommend using the F() macro to save some SRAM space but I have not been able to use this successfully.

For example, instead of: myGLCD.print("Testing 1-2-3",1 ,1);  // Print text

You'd use:

 myGLCD.print(F("Testing 1-2-3"),1 ,1);  // Print text

Is there something I need to change in the UTFT.cpp file? I think the way things are now is that it actually changes the string I pass onto it then changes it into an array? Not sure if there is anything I can do....

Cheers, Armen

armeniki: ..I have not been able to use this successfully...

What error type? Not compile?

Care to show us the code, and tell us what the symptoms are when you use the F() macro? We are not mind readers.

lar3ry: We are not mind readers.

Mate, there's no reason to be impolite.

nid69ita: What error type? Not compile?

Hi nid69ita, yes basically I was getting "Error: no instance of overloaded function "UTFT::print"" but this makes sense now because I got in touch with Henning and this is what he said about using F() macros with UTFT:

Hello Armen, I am sorry but none of my libraries support the use of the F() macro. I do not know how this can be included in the libraries, and this is the first inquiry about it so I do not know if it will be included. Regards Henning Karlsen

In any case, I think he's done a great job and love using his library regardless. The only reason I was asking this question is because I noticed one of the users on this forum had recommended doing this - see reply number 124 on this topic: http://forum.arduino.cc/index.php?topic=66054.120

Cheers, Armen

In the 124 post:
“I’m not sure if it works here but it should” → Gammon suppose that library is F() compatible.

You need to find how to modify the library creating a copy of the .print() method, using a pointer to PROGMEM instead a simple char pointer.
In the library version history, for version 1.1 added the support for String (using print).
Now library have 2 method to print,

void print(char *st, int x, int y, int deg=0);
void print(String st, int x, int y, int deg=0);

The print() base command use this type declaration:

size_t print(const __FlashStringHelper *);

And in code print one char at a time using the internal command pgm_read_byte:

size_t Print::print(const __FlashStringHelper *ifsh)
{  const char PROGMEM *p = (const char PROGMEM *)ifsh;
  size_t n = 0;
  while (1) 
  { unsigned char c = pgm_read_byte(p++);
    if (c == 0) break;
    n += write(c);
  }
  return n;
}

The UTFT library have the method “printChar” so you can send one char at a time:

void UTFT::print(const __FlashStringHelper *ifsh, int x, int y, int deg)
{ int stl, i;
  unsigned char c;
  const char PROGMEM *p= (const char PROGMEM *)ifsh;
  stl = strlen(ifsh);                       // work ? 
  if (orient==PORTRAIT)
  { if (x==RIGHT)   x=(disp_x_size+1)-(stl*cfont.x_size);
    if (x==CENTER) x=((disp_x_size+1)-(stl*cfont.x_size))/2;
  } else
  { if (x==RIGHT)   x=(disp_y_size+1)-(stl*cfont.x_size);
    if (x==CENTER) x=((disp_y_size+1)-(stl*cfont.x_size))/2;
  }
  for (i=0; i<stl; i++)
  { c= pgm_read_byte(p++);
    if (deg==0)
      printChar(c, x + (i*(cfont.x_size)), y);
    else
      rotateChar(c, x, y, i, deg);
  }
}

I tried, not tested !!!
I don’t know if strlen() accept to calculate the lenght of string if it is in flash.
If not we can calculate the lenght (remove strlen line) :

const char PROGMEM *p= (const char PROGMEM *)ifsh;
stl = 0;
do { c= pgm_read_byte(p+stl);
  stl++;
} while(c != 0);