I haven't had any luck getting my grid of buttons to act the way I want and I'm not sure what to change.
My code is using a switch statement to change the color of the button when touched but I get a compile error (not declared in this scope) when I try to convert it to work with the RA8875 driver. I'm not sure what the terminology of the RA8875.h file is meaning and any help in direction to go is appreciated.
old code
TS_Point p;
int x, y;
const byte row = 4;
const byte col = 2;
byte lastHit = 0;
byte currentHit = 0;
Adafruit_GFX_Button btn[row*col];
void loop() {
if (!ts.bufferEmpty()) {
p = ts.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 (ts.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;
}
}
What I tried for code
#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, tz;
int x, y;
const byte row = 4;
const byte col = 2;
byte lastHit = 0;
byte currentHit = 0;
Adafruit_GFX_Button btn[row * col];
const int TS_MAXX = 980, TS_MINX = 60, TS_MAXY = 920, TS_MINY = 80;
void setup() {
Serial.begin(9600);
Serial.println("RA8875 start");
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);
pinMode(RA8875_INT, INPUT);
digitalWrite(RA8875_INT, HIGH);
tft.touchEnable(true);
btnGrid();
Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
Serial.println("Waiting for touch events ...");
}
void loop() {
if (tft.touched()); {
switch (rotation) {
case 0:
tx = map(tx, TS_MINX, TS_MAXX, 0, tft.width());
ty = map(ty, TS_MINY, TS_MAXY, 0, tft.height());
break;
case 1:
// p.x, p.y reversed //
tx = map(ty, TS_MINY, TS_MAXY, 0, tft.width());
ty = map(tx, TS_MAXX, TS_MINX, 0, tft.height());
break;
case 2:
tx = map(tx, TS_MAXX, TS_MINX, 0, tft.width());
ty = map(ty, TS_MAXY, TS_MINY, 0, tft.height());
break;
case 3:
// p.x, p.y reversed //
tx = map(ty, TS_MAXY, TS_MINY, 0, tft.width());
ty = map(tx, TS_MINX, TS_MAXX, 0, tft.height());
break;
}
}
while (tft.touched()) {
for (uint8_t b = 0; b < row * col; b++) {
if (btn[b].contains(tx, ty)) {
btn[b].press(true);
btn[b].drawButton(true);
currentHit = b;
} else if (btn[b].contains(tx, ty) == false) {
btn[b].press(false);
if (b == lastHit) {
btn[b].drawButton(false);
}
} else {
return;
}
}
lastHit = currentHit;
delay(500);
}
}
void btnGrid() {
int left, top;
int l = 10;
int t = 30;
int w = 200;
int h = 60;
byte hgap = 40;
byte vgap = 40;
byte id = 0;
char *titleStr[row * col] = {"4", "5", "6", "8", "11", "13", "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++;
}
}
}