I am using an ESP32 uProcessor to drive an MSP2807, ILI9341, 240 x 320 display using the Adafruit_GFX and Adafruit_ILI9341 libraries. I want to print a text line, centered horizontally, on the ILI9341 display.
I thought that I have a function which will calculate the cursor position required to print the text centered horizontally.
My simple sketch, which does not work, is attached below.
// ########################################################
// ########################################################
// #
// # Sketch: Draw_Centered_String
// #
// # Date: 1-22-24
// #
// # This sketch is draws a centered text string on the
// # MSP2807 ILI9341 TFT Display
// #
// # This sketch uses an ILI9341 Constructor of the form:
// #
// # Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST, TFT_MISO);
// #
// # TFT_MISO and TFT_SCK pin connections must also be defined and
// # connected to the ESP32 for this sketch to work
// #
// # Microprocessor: ESP32 Dev Module
// # 30-Pin, ESP32 DEVKIT V1
// # www.doit.am
// #
// # Tool Kit: Board Model: ESP32 Dev Module
// # Serial Port: COM4
// #
// # Display: MSP2807
// # TFT LCD, 320 x 240 Pixels
// # Control IC: ILI9341
// #
// # Silicon Labs CP210X Driver is used to connect ESP32 to COM4
// #
// ####################################################################
// ####################################################################
// #
// # Define standard boilerplate Bodmer pin connections between the
// # ILI9341 display and the ESP32 uP driving it.
// #
// # This is the same boilerplate pin mapping as used in the User_Setup
// # Setup1_ILI9341 in the TFT_eSPI Library
// #
// ####################################################################
#define TFT_CS 15 // TFT CS pin conn to ESP32 uP pin 15 Brown
#define TFT_RST 4 // TFT RST pin conn to ESP32 uP pin 4 Red
#define TFT_DC 2 // TFT DC pin conn to ESP32 uP pin 2 Orange
// ####################################################################
// #
// # Other standard, additional boilerplate Bodmer pin connections
// # between the ILI9341 display and the ESP32 uP are listed below
// #
// ####################################################################
#define TFT_MOSI 23 // TFT MOSI pin conn to ESP32 uP pin 23 Yellow
#define TFT_SCK 18 // TFT SCK pin conn to ESP32 uP pin 18 Green
#define TFT_MISO 19 // TFT MISO pin conn to ESP32 uP pin 19 Gray - Used
// #define TFT_LED 3.3V // TFT LED conn to ESP32 uP pin 3.3V
// ####################################################################
// ####################################################################
#include <SPI.h>
#include <Adafruit_GFX.h> // include Adafruit graphics library
#include <Adafruit_ILI9341.h> // include Adafruit ILI9341 TFT library
// initialize the ILI9341 TFT library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST, TFT_MISO);
// ####################################################################
// ####################################################################
void setup(void) {
Serial.begin(9600);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
tft.setTextColor(ILI9341_WHITE);
tft.setCursor (0, 75);
tft.setTextSize(2);
tft.print("Original Text Line");
// Try to print the "Centered Text Line" with a call to
// drawMyCenterString
drawMyCenterString("Centered Text Line", 0, 100);
void drawMyCenterString(const String &buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); // calc width of new string
display.setCursor(x - w / 2, y);
display.print(buf);
}
}
How can I fix my sketch?