I've been trying to find some way to write a 3,4, or 5 digit number centered in the same spot on the 240x240 TFT display on this board https://copperhilltech.com/teensy-4-1-triple-can-bus-board-with-240x240-lcd-and-ethernet/
FWIW, this is my first attempt at graphic LCDs.
here's what I'm doing that works, more or less:
I did see some reference in the ST7735_t3.cpp file about "centering":
// added support for drawing strings/numbers/floats with centering
// modified from tft_ili9341_ESP github library
// Handle numbers
int16_t drawNumber(long long_num,int poX, int poY);
but I tried drawNumber() and it just seemed to do the standard left-justified thing.
Is there a smarter way to center a variable-width text field (number) on a TFT panel?
Also, note the "fudgeFactorOffset" in my snippet - for some reason the w&h that I'm getting back from getTextBounds doesn't really seem to center the text exactly so I have to fudge it to look right (but it's close).
Anyone know what's going on there?
FWIW, this is my first attempt at graphic LCDs.
here's what I'm doing that works, more or less:
Code:
void tft_update( int rate ) {
int dynamicX;
int dynamicY;
itoa( packetRate, packetRateString, 10 ); // convert integer to char array (number, string, base)
tft.setFont( packetRateTextSize );
tft.setTextColor( ST77XX_BLACK );
tft.getTextBounds( packetRateString, x, y, &posX, &posY, &w, &h );
dynamicX = centerX - w / 2 - fudgeFactorOffset; // subtract fudge factor for some unexplained misalignment/bug in getTextBounds()
dynamicY = centerY - h / 2;
tft.fillRoundRect( dynamicX, dynamicY, w, h, whiteBoxCornerRad, ST77XX_WHITE ); //clear the screen for the next write
tft.setCursor( dynamicX, dynamicY ); // locate the upper left cursor point based on the bounding box size
tft.println( packetRateString );
return;
}
I did see some reference in the ST7735_t3.cpp file about "centering":
// added support for drawing strings/numbers/floats with centering
// modified from tft_ili9341_ESP github library
// Handle numbers
int16_t drawNumber(long long_num,int poX, int poY);
but I tried drawNumber() and it just seemed to do the standard left-justified thing.
Is there a smarter way to center a variable-width text field (number) on a TFT panel?
Also, note the "fudgeFactorOffset" in my snippet - for some reason the w&h that I'm getting back from getTextBounds doesn't really seem to center the text exactly so I have to fudge it to look right (but it's close).
Anyone know what's going on there?