Damn I love McVitties. Can't beat a Tim Tam tho or a VoVo.
Well, I played with your code and got a demo working that I can use. Just has one menu which simply outputs to serial what button is pressed. I can work with this.
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x8230
const int TS_LEFT=908,TS_RT=106,TS_TOP=70,TS_BOT=888;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
// most apps use Portrait or Landscape. No need for all 4 cases
switch (tft.getRotation()) {
case 0:
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
break;
case 1:
pixel_x = map(p.y, TS_TOP, TS_BOT, 0, tft.width());
pixel_y = map(p.x, TS_RT, TS_LEFT, 0, tft.height());
break;
case 2:
pixel_x = map(p.x, TS_RT, TS_LEFT, 0, tft.width());
pixel_y = map(p.y, TS_BOT, TS_TOP, 0, tft.height());
break;
case 3:
pixel_x = map(p.y, TS_BOT, TS_TOP, 0, tft.width());
pixel_y = map(p.y, TS_LEFT, TS_RT, 0, tft.height());
break;
}
}
return pressed;
}
Adafruit_GFX_Button test1_btn, test2_btn, test3_btn, test4_btn, test5_btn;
// easier to handle a list of button pointers
Adafruit_GFX_Button *screen0[] = {&test1_btn, &test2_btn, &test3_btn, &test4_btn, &test5_btn, NULL };
int flag = 0, screen = 0;
// INITIALISE GPIO, TOUCH, TFT, BUTTONS
void setup(void)
{ Serial.begin(9600);
tft.begin(tft.readID());
tft.setRotation(0); //PORTRAIT
test1_btn.initButton(&tft, 120, 20, 240, 40, TFT_WHITE, TFT_CYAN, TFT_BLACK, "Test1", 1);
test2_btn.initButton(&tft, 120, 64, 240, 40, TFT_WHITE, TFT_CYAN, TFT_BLACK, "Test2", 1);
test3_btn.initButton(&tft, 120, 108, 240, 40, TFT_WHITE, TFT_CYAN, TFT_BLACK, "Test3", 1);
test4_btn.initButton(&tft, 120, 152, 240, 40, TFT_WHITE, TFT_CYAN, TFT_BLACK, "Test4", 1);
test5_btn.initButton(&tft, 120, 196, 240, 40, TFT_WHITE, TFT_CYAN, TFT_BLACK, "Test5", 1);
tft.fillScreen(TFT_BLACK);
test1_btn.drawButton(false);
test2_btn.drawButton(false);
test3_btn.drawButton(false);
test4_btn.drawButton(false);
test5_btn.drawButton(false);
}
void loop(void)
{
static uint16_t color = TFT_BLACK;
// READ TOUCH
if (screen == 0) {
update_button_list(screen0); //process all buttons
if (test1_btn.justPressed()) {
Serial.println("Test button 1 pressed");
}
if (test2_btn.justPressed()) {
Serial.println("Test button 2 pressed");
}
if (test3_btn.justPressed()) {
Serial.println("Test button 3 pressed");
}
if (test4_btn.justPressed()) {
Serial.println("Test button 4 pressed");
}
if (test5_btn.justPressed()) {
Serial.println("Test button 5 pressed");
}
}
}
/* update the state of a button and redraw as reqd
*
* main program can use isPressed(), justPressed() etc
*/
bool update_button(Adafruit_GFX_Button *b, bool down)
{
b->press(down && b->contains(pixel_x, pixel_y));
if (b->justReleased())
b->drawButton(false);
if (b->justPressed())
b->drawButton(true);
return down;
}
/* most screens have different sets of buttons
* life is easier if you process whole list in one go
*/
bool update_button_list(Adafruit_GFX_Button **pb)
{
bool down = Touch_getXY();
for (int i = 0 ; pb[i] != NULL; i++) {
update_button(pb[i], down);
}
return down;
}
void draw_button_list(Adafruit_GFX_Button **pb)
{
tft.fillScreen(TFT_BLACK);
for (int i = 0 ; pb[i] != NULL; i++) {
pb[i]->drawButton(false);
}
}