Very carefully. You have to know EXACTLY how high and how wide the text will be, so that you can center the text at the center of the area.
Knowing how wide a text string will be depends on the font (fixed or variable width) and (possibly) exactly which characters are in the string (for variable width fonts).
If you are trying to display a known string, with a known font, at a known size, just experiment with the origin until the text looks centered.
PaulS:
Very carefully. You have to know EXACTLY how high and how wide the text will be, so that you can center the text at the center of the area.
Knowing how wide a text string will be depends on the font (fixed or variable width) and (possibly) exactly which characters are in the string (for variable width fonts).
If you are trying to display a known string, with a known font, at a known size, just experiment with the origin until the text looks centered.
Yes … I could do it the hard way... count the pixels on a graph sheet and using fixed width font I got what I wanted. But then if I could leave the maths to the complier by just defining the text Area and telling what to print inside it, life would be so much easy. Maybe I am getting lazy...
OK the problem is narrowing down to this … after I define the textArea, I try to use the DrawString() but it does not work. Not sure what is wrong in the code below ? … the font has already been set to System5x7
Yes what does "does not work" mean.
Also, we need to see the full sketch.
I'm assuming this is a small test sketch, so post the entire sketch so I can see what you are doing to see if I can replicate it.
bperrybap:
Yes what does "does not work" mean.
Also, we need to see the full sketch.
I'm assuming this is a small test sketch, so post the entire sketch so I can see what you are doing to see if I can replicate it.
--- bill
Sure here we go …
/*
13 Aug 2018 : PROGRAMMABLE ENUDRANCE CONTROLLER
Hardware Used : Arduino Mega2560 Module.
128 x 64 GLCD Cordinates :
Top Left : 0,0
Top Right : 127,0
Bott Left : 0,63
Bott Right : 127,63
Center : 64,32
*/
//************************************
//--------------DEFINES---------------
//************************************
#include <openGLCD.h>
gText textAreaFull;
gText textAreaBot;
gText textAreaNums;
gText textAreaUpRt;
gText myTextArea;
//************************************
//------------VARIABLES --------------
//************************************
char tempKey;
//************************************
//--------------SETUP ----------------
//************************************
void setup() {
Serial.begin(9600);
GLCD.Init(); // GLCD Init ( works OK )
GLCD.ClearScreen();
GLCD.SelectFont(Arial_bold_14);
GLCD.DrawString("Endurance Rig", 18, 12, eraseTO_EOL);
GLCD.DrawString("Version : 1.0", 24, 38, eraseTO_EOL);
//GLCD.DrawString("Press 'D' to continue .....", 10, 50, eraseNONE );
GLCD.DrawRect(GLCD.Left, GLCD.Top, GLCD.Width, GLCD.Height); // Clear and Draw border
GLCD.DrawRect(GLCD.Left + 1, GLCD.Top + 1, GLCD.Width - 2, GLCD.Height - 2); // Make it thick (OK)
//checkOutputs();
GLCD.ClearScreen();
//GLCD.SelectFont(Iain5x7);
GLCD.SelectFont(System5x7);
myTextArea.DefineArea(GLCD.Left, GLCD.Top, GLCD.CenterX, GLCD.CenterY);
GLCD.DrawString(F("Hello"), gTextfmt_right, gTextfmt_top);
// Prints Hello on top right of screen and not in area specified by me. If I replace GLCD with myTextArea
// nothing prints and teh screen is blank..
}
//************************************
//------------L O O P ---------------
//************************************
void loop() {
tempKey = my_btns.getKey();
}
There are many ways to call DefineArea() for creating the text area for a textarea object.
The way you have used it creates the text area based on the pixel width and height of the main/default GLCD text area.
But when doing it that way, no font for the text area is specified.
While you have set and modified the text area for the GLCD text area, you never set the font for your myTextArea.
(Each text area has its own font)
You can't print anything to a text area until you set the font for the text area.
Have a look at the DefineArea() example sketch to see examples of the various ways to use DefineArea().
bperrybap:
There are many ways to call DefineArea() for creating the text area for a textarea object.
The way you have used it creates the text area based on the pixel width and height of the main/default GLCD text area.
But when doing it that way, no font for the text area is specified.
While you have set and modified the text area for the GLCD text area, you never set the font for your myTextArea.
(Each text area has its own font)
You can't print anything to a text area until you set the font for the text area.
Have a look at the DefineArea() example sketch to see examples of the various ways to use DefineArea().
--- bill
Yes never realised that I need to define the font for the defined text area. Though I read it in the documentation , it never occurred when coding. Thanks.
Now there is one last thing .. having defined a text area and also the font, when I call ,
I expect the string to be printed on the center of my defined area but what actually happens is that the string is printed to the default GLCD center. Why is that ?
To get the string print to my defined area this code works ...
You haven't shown the code for what isn't working, so I can't see what you have done that isn't working.
However the code that you have that is "working" has some issue (as it really isn't really working) which is likely causing the issue.
You are passing invalid parameters into DefineArea() and the call is failing which causes the text area to be initialized to the full display. (check the return status and it will be non zero)
Look closer at the API parameters for the DefineArea() form you are using. They are:
DefineArea(x1, y1, x2, y2) not DefineArea(x1, x2, width, height)
The width of the display is an invalid x coordinate so the defineArea() will fail.
Also you are attempting to create a text area that is not a multiple of the font size and is larger than the rectangle you drew.
While the font is 7 pixels high it consumes 8 pixels on the display due to pixel padding. If you want the size of the font on the display you can use GetAreaProp() to get the font height which returns the height in pixels including the current padding.
But it is likely easier to just create the text area using the alternate form of DefineArea() that creates it for a given cols/rows as it will do all that math for you.
Its been quite a learning and the concepts of the GLCD are getting clear as I stumble upon issues one after the other. Thanks for your patient tutoring.
Yes I see that the define area expects the top left and bottom right coordinates and I was passing different ones. And yes the Define area with Col / row is a good alternative.
Right now even though it takes time I am using the CursorToXY() and have managed to complete three different screens. With the points given by you shall start using more refined methods of the TextArea and DrawString() function.
A big value of the text areas is that they can wrap and scroll lines of text and be cleared like a mini terminal window.
If you don't need those capabilities, then you can use the default GLCD area to position text wherever you want.
bperrybap:
A big value of the text areas is that they can wrap and scroll lines of text and be cleared like a mini terminal window.
If you don't need those capabilities, then you can use the default GLCD area to position text wherever you want.
--- bill
Initially I thought of text areas as not of much value. But then realized their powerful text manipulation options. Like with any new peripheral there is this learning curve. I still remember the day when I could manage to show the first line of characters on a 16x2 LCD !!
On these fonts that come with the GLCD library is there any easy method to visualize how they will look before using them on the GLCD ? Like in MS Word if we click the font list, each of them are actually shown as they look - makes it easy to chose.