hey gang!
got the display going but this time I am having a hard time with buttons...
display is MCUFRIEND_kbv 2.8inch tft touch screen
got my calibration data correct... (unless affected by orientation?)
inserted my calibration in the code
got a demo button code going.
modified it for my application works GREAT... until... I changed the orientation to Landscape from Portrait???
I dont get it looks like the buttons staid almost (not quite!) where they were in Portrait... even thow the drawings moved? Help!
Richard VE2DX
here is my code
/* an alternative approach. swap the #if 1 / 0 values to try it
*
*/
#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP=8,XM=A2,YP=A3,YM=9 ; //240x320 ID=0x4535
const int TS_LEFT=843,TS_RT=171,TS_TOP=888,TS_BOT=117;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button stop_btn, tune_btn, up_btn, down_btn;
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);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
void setup(void)
{
Serial.begin(9600);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT [b]This is where I change to a 1 for Landscape try it![/b]
tft.fillScreen(BLACK);
stop_btn.initButton(&tft, 30, 200, 50, 40, BLUE, RED, BLACK, "STOP", 2);
tune_btn.initButton(&tft, 90, 200, 50, 40, BLUE, BLUE, BLACK, "TUNE", 2);
stop_btn.drawButton(false);
tune_btn.drawButton(false);
up_btn.initButton(&tft, 150, 200, 50, 40, BLUE, GREEN, BLACK, "UP", 2);
down_btn.initButton(&tft, 210, 200, 50, 40, BLUE, GREEN, BLACK, "DOWN", 2);
up_btn.drawButton(false);
down_btn.drawButton(false);
tft.fillRect(40, 80, 160, 80, RED);
}
/*
* updating multiple buttons from a list
*
* anything more than two buttons gets repetitive
*
* you can place button addresses in separate lists
* e.g. for separate menu screens
*/
// Array of button addresses to behave like a list
Adafruit_GFX_Button *buttons[] = {&stop_btn, &tune_btn,&up_btn, &down_btn, NULL};
/* 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;
}
/* compare the simplicity of update_button_list()
*/
void loop(void)
{
update_button_list(buttons); //use helper function
if (stop_btn.justPressed()) {
tft.fillRect(40, 80, 160, 80, GREEN);
stop_btn.drawButton(true);
}
if (tune_btn.justPressed()) {
tft.fillRect(40, 80, 160, 80, RED);
tune_btn.drawButton(true);
tft.fillRect(40, 80, 160, 80, YELLOW);
delay (1000);
tft.fillRect(40, 80, 160, 80, RED);
}
//update_button_list(buttons); //use helper function
if (up_btn.justPressed()) {
tft.fillRect(40, 80, 160, 80, BLUE);
up_btn.drawButton(true);
}
if (down_btn.justPressed()) {
tft.fillRect(40, 80, 160, 80, YELLOW);
down_btn.drawButton(true);
}
}
#endif