I have a 7" TFT screen that uses the RA8875 controller to run it and was trying to manipulate some existing code I have that I used with a smaller screen that used the STMPE610 touch controller.
I have been able to get graphics and touch events to work on the RA8875 with a simple on/off button where one side of the button turns green and the other turns red upon touching but can't figure out how to change a grid of buttons using the RA8875.
I get a compile error using the If statements for collecting the touch events using the following code and I'm just not good enough with code to decipher the RA8875.h file to put in the correct call to do what I want. The graphics all come out as coded but the touch events don't work because of the compile error.
The code in question is this part where I don't know what to call instead of it for it to work with the RA8875.
if (!tft.bufferEmpty()) {
TS_Point p = tft.getPoint();
switch (rotation) {
This is the full code for you to see everything I'm doing and I will attach the RA8875.h file that I'm using.
#include <SPI.h>
#include <Wire.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;
byte rotation = 0;
int x, y;
const byte row = 4;
const byte col = 3;
byte lastHit = 0;
byte currentHit = 0;
Adafruit_GFX_Button btn[row * col];
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
void setup() {
Serial.begin(9600);
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
Serial.println("Found RA8875");
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);
tft.fillScreen(RA8875_BLACK);
tft.textMode();
tft.textSetCursor(20, 15);
tft.textEnlarge(2);
tft.textColor(RA8875_WHITE, RA8875_RED);
tft.textWrite("Choose how many petals you need.");
tft.graphicsMode();
tft.touchEnable(true);
btnGrid();
}
void loop() {
if (!tft.bufferEmpty()) {
TS_Point p = tft.getPoint();
switch (rotation) {
case 0:
x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
break;
case 1:
// p.x, p.y reversed //
x = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
y = map(p.x, TS_MAXX, TS_MINX, 0, tft.height());
break;
case 2:
x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
break;
case 3:
// p.x, p.y reversed //
x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
break;
}
}
while (tft.touched()) {
for (uint8_t b = 0; b < row * col; b++) {
if (btn[b].contains(x, y)) {
btn[b].press(true);
btn[b].drawButton(true);
currentHit = b;
} else if (btn[b].contains(x, y) == false) {
btn[b].press(false);
if (b == lastHit) {
btn[b].drawButton(false);
}
} else {
return;
}
}
lastHit = currentHit;
}
}
void btnGrid() {
int left, top;
int l = 50;
int t = 90;
int w = 200;
int h = 60;
byte hgap = 40;
byte vgap = 40;
byte id = 0;
char *titleStr[row * col] = {"4", "6", "8", "9", "12", "16", "Load", "1"};
for (byte j = 0; j < row; j++) {
for (byte i = 0; i < col; i++) {
left = l + i * (w + vgap);
top = t + j * (h + hgap);
btn[id].initButtonUL( &tft, left, top, w, h, RA8875_WHITE, RA8875_RED, RA8875_GREEN, titleStr[id], 3 );
if (id == currentHit) {
// inverted
btn[id].drawButton(true);
} else {
btn[id].drawButton(false);
}
id++;
}
}
}
Adafruit_RA8875.h (19.5 KB)