drawString() - positioning confusion
-
Working through some examples to learn how to use the screen on my DinMeter...
The example code uses the snippet below to display a value manipulated by the encoder knob - the numeric text is always centered on the display...
DinMeter.Display.drawString(String(newPosition), DinMeter.Display.width() / 2, DinMeter.Display.height() / 2);
Researching the functions, it seems like drawCentreString() should do that ??? Shouldn't drawString() reference the top left of the current setTextDatum() command? If I need to display text in a specific area, how do you code that?
PS - looks like I'm the only one here lol
-
@TomKatt Sure, there's lots of us here.
You have to consider the world is on a 24hr cycle.
I would help you but it would not be what you want.
This is TFT_eSPI one of the most famous graphics drivers:
GFX, LVGL, TFT_eSPI .
LOTS of reference and examples.
As I said you have a M5StampS3 which is an ESP32 S3 wth a ST7789V2 LCD, also very common. All small M5Stack products use it. The one from the CardPuter will work in an old StickCPlus.
Here is the Canvas API:
https://docs.m5stack.com/en/arduino/m5gfx/m5gfx_canvasHave you tried replacing
DinMeter.Display.width() / 2, DinMeter.Display.height() / 2);
with literals, like 50, 50?
Experiment, research, have fun. -
@teastain
Hello again!Yes, I've been experimenting and replaced the references to the display width and height with literal values and the result was as expected - the specified xy position is the center of the drawn string. Beyond the confusion of expecting xy to locate the string by upper left corner, the API states this is how drawString() should work...
drawString() Description: Draw a character Syntax: drawString(const char *string, int32_t poX, int32_t poY, uint8_t font); drawString(const char *string, int32_t poX, int32_t poY); drawString(const String& string, int32_t poX, int32_t poY, uint8_t font); drawString(const String& string, int32_t poX, int32_t poY); Function argument: argument type Description **poX int32_t Coordinate X (upper left)** **poY int32_t Coordinate Y (upper left)** string const char * / String & String font uint8_t 1: If use the loaded font Function return value: None
-
@teastain
Found the issue - this was hidden in the Setup routine:DinMeter.Display.setTextDatum(middle_center);
The default is top left as expected, this line overrides the code below. Problem solved - user error strikes again lol
On the topic of different libraries - I note you have suggested the Canvas API before... Is this preferred over the TFT_eSPI API ?
Thanks!
-
@TomKatt BTW, I love your Blown Away by Maxell.
The ‘Canvas’ is M5’s name for TFT_eSPI !
Keep up the good work!