I'm working with Adafruit RA8875, Adafruit 5" touch screen (PRODUCT ID: 1596 ) and Arduino Mega 2560. I'm trying to create display with touchscreen buttons, label the buttons and add function to the buttons. I've tested the screen using the Adafruit RA8875 library and the examples work perfectly. I'm trying to make a functional button so I've #included Adafruit_GFX.cpp in my sketch and used the following function
btn1.initButton(&tft, 500, 200, 100, 100, RA8875_GREEN, RA8875_BLACK, RA8875_GREEN, "Btn1", 4);
which is included in Adafruit_GFX.cpp but the issue is that the text "Btn1" (button label) doesn't show on the button. I've searched, and looked at different examples but I can't understand what I'm doing wrong. Any help will be appreciated.
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#define RA8875_INT 3
#define RA8875_CS 10
#define RA8875_RESET 9
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;
Adafruit_GFX_Button btn1;
Adafruit_GFX_Button btn2;
void setup()
{
Serial.begin(9600);
Serial.println("RA8875 start");
/* Initialise the display using 'RA8875_800x480' */
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
//Screen backgroud
tft.fillScreen(RA8875_BLACK);
tft.touchEnable(true);
btn1.initButton(&tft, 500, 200, 100, 100, RA8875_GREEN, RA8875_BLACK, RA8875_GREEN, "Btn1", 1);
btn2.initButton(&tft, 500, 200, 100, 100, RA8875_GREEN, RA8875_BLUE, RA8875_GREEN, "Btn2", 1);
btn1.drawButton(false); //Draw Button 1 on screen
Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
Serial.println("Waiting for touch events ...");
}
void loop()
{
int Xposn, Yposn;
if (tft.touched()) { //Verify the touched area
Serial.print("Touch: ");
tft.touchRead(&tx, &ty); //Read the x and y value of touched area
Serial.print(tx); Serial.print(", "); Serial.println(ty);
if (tx > 580 && tx < 680 && ty > 370 && ty < 535)
{
btn2.drawButton(); //Draw bnt 2
delay(100);
btn1.drawButton();
}
}
}