I have cobbled together a test sketch for drawing a button and making it functional. The button appears where I want it to, but the hit box is not rotating 90 degs on this sketch. I have played with an modified one of the test sketches at that came with the screen, and I can move it where I want. I am using the same rotation alignment for the touchscreen but is not working. I am running this on an Uno and the screen is 2.8 TFT Touch Shield for Arduino w/Capacitive Touch : ID 1947 : $44.95 : Adafruit Industries, Unique & fun DIY electronics and kits 2.8" capacitive shield.
Here is my sketch:
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ts = Adafruit_FT6206();
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BUTTON_X 60
#define BUTTON_Y 80
#define BUTTON_W 100
#define BUTTON_H 50
#define BUTTON_SPACING_X 20
#define BUTTON_SPACING_Y 20
#define BUTTON_TEXTSIZE 2
//char buttonlabel="Hi";
Adafruit_GFX_Button buttons;
void msgon()
{
tft.fillRect(10,150,100,50, ILI9341_BLACK);
tft.setCursor(10,150);
tft.print("on");
}
void msgoff()
{
tft.fillRect(10,150,100,50, ILI9341_BLACK);
tft.setCursor(10,150);
tft.print("off");
}
void setup() {
Serial.begin(9600);
tft.begin();
if (!ts.begin(40)) {
Serial.println("Unable to start touchscreen.");
}
else {
Serial.println("Touchscreen started.");
}
tft.fillScreen(ILI9341_BLACK);
// origin = left,top landscape (USB left upper)
tft.setRotation(1);
buttons.initButton(&tft, BUTTON_X, BUTTON_Y, BUTTON_W, BUTTON_H, ILI9341_WHITE,ILI9341_BLUE, ILI9341_WHITE, "HI" ,BUTTON_TEXTSIZE);
buttons.drawButton();
}
void loop() {
// See if there's any touch data for us
TS_Point p;
p = ts.getPoint();
if (ts.touched() ) {
//rotate the button map 90 deg to match screen
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
int y = tft.height() - p.x;
int x = p.y;
}
if (buttons.contains(p.x,p.y)) {
buttons.press(true);
} else {
buttons.press(false);
}
if (buttons.justReleased()){
buttons.drawButton();
msgoff();
}
if (buttons.justPressed()){
buttons.drawButton(true);
msgon();
}
}