Here is an example sketch for UTFT. If a string does not fit, it prints on the next line (at same X position)
Although GFX programs can handle letter wrap, they do not break on words. And the linefeed always goes to column #0.
You can adapt the helper function for a GFX style program.
You can adapt for char arrays in PROGMEM or for String class arguments.
David.
#include <UTFT.h>
// Declare which fonts we will be using
extern uint8_t BigFont[], SmallFont[];
extern uint8_t SevenSegNumFont[];
//UTFT myGLCD(model,RS,WR,CS,RST,[ALE]); //assumes that nRD is tied high
//UTFT myGLCD(ILI9481, 38, 39, 40, 41);
UTFT myGLCD(ILI9486, 38, 39, 40, 41);
//UTFT myGLCD(R61581, 38, 39, 40, 41);
#define TFT_WHITE 0xFFFF
#define TFT_BLACK 0x0000
#define TFT_BLUE 0x001F
int widchar, htchar; //global variables
void printwrap(const char *msg, int x, int y)
{
int maxchars = (myGLCD.getDisplayXSize() - x) / widchar;
int len = strlen(msg), pos, i;
char buf[maxchars + 1];
while (len > 0) {
pos = 0;
for (i = 0; i < len && i < maxchars; i++) {
char c = msg[i];
if (c == '\n' || c == ' ') pos = i;
if (c == '\n') break;
}
if (pos == 0) pos = i; //did not find terminator
if (i == len) pos = len; //whole string fits
strncpy(buf, msg, pos); //copy the portion to print
buf[pos] = '\0'; //terminate with NUL
myGLCD.print(buf, x, y); //go use UTFT method
msg += pos + 1; //skip the space or linefeed
len -= pos + 1; //ditto
y += htchar; //perform linefeed
}
}
void setup()
{
myGLCD.InitLCD(PORTRAIT);
myGLCD.clrScr();
myGLCD.setColor(TFT_WHITE); //text colour
myGLCD.setBackColor(TFT_BLUE); //shows up trailing spaces
const uint8_t *f = BigFont;
myGLCD.setFont(f); //set font and fetch dimensions
widchar = pgm_read_byte(f + 0);
htchar = pgm_read_byte(f + 1);
}
void loop()
{
printwrap("piston time out.. please make sure sensor in place and try to run again", 0, 20);
printwrap("earholes are cheap today, cheaper than yesterday.", 20, 100);
printwrap("David Prentice\nWormshill\nKent", 60, 180);
while (1) ;
}
In practice, most GFX programs would embed linefeeds in a message string to ensure that words wrap nicely. (disadvantage is that linefeeds always go to left hand column)