Description
The classic initButton() uses centre & size, whereas the initButtonUL() uses upper-left corner & size.
Signature
void TFT_eSPI_Button::initButton(TFT_eSPI *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t outline, uint16_t fill, uint16_t textcolor, char *label, uint8_t textsize)void TFT_eSPI_Button::initButtonUL(TFT_eSPI *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h, uint16_t outline, uint16_t fill, uint16_t textcolor, char *label, uint8_t textsize)
Parameters
TFT_eSPI*gfx: the TFT object;int16_tx: x position (center forinitButton()or left forinitButtonUL());int16_ty: y position (center forinitButton()or top forinitButtonUL());uint16_tw: width;uint16_th: height;uint16_toutline: text color;uint16_tfill: background color;uint16_ttextcolor: border color;char*label: button text;uint8_ttextsize: text font.
Result
None.
Example
#include <TFT_eSPI.h>
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // TFT instance
TFT_eSPI_Button btn; // Button instance
void setup() {
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// Initialize and draw the button
btn.initButton(&tft, 120, 100, 100, 40, TFT_WHITE, TFT_RED, TFT_WHITE, "Click", 2);
btn.drawButton();
}
void loop() {
uint16_t x = 0, y = 0; // Variables to store touch coordinates
// Check for touch
if (tft.getTouch(&x, &y)) {
if (btn.contains(x, y)) {
tft.fillScreen(TFT_GREEN);
delay(500);
tft.fillScreen(TFT_BLACK);
btn.drawButton();
}
}
delay(50); // Small delay to debounce
}
In this example, we begin by initializing the screen with a black background. We then create a
TFT_eSPI_Button object named btn, which is configured and drawn in the setup() function using the
drawButton() method.
Inside the loop() function, we detect touch input using the getTouch() method, which returns the coordinates of the
touch point. These coordinates are passed to the contains() method to check whether the touch occurred
within the button's boundaries. If the button is pressed, we perform an action—in this case, filling the screen with
green to indicate a successful press, then restoring the button display.
Note
This example assumes you have a touchscreen properly connected to your ESP32 or ESP8266 board, and that the
TFT_eSPI library has been correctly configured for your specific display and touch controller.