Allows you to create a GUI for a TFT LCD screen on an arduino. You will need the following libraries: "Adafruit_GFX.h", "Adafruit_TFTLCD.h" and "TouchScreen.h" to get it working.
// Defines
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
// Touch screen pins
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 7 // can be a digital pin
#define XP 6 // can be a digital pin
// Pression defines
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// LCD pins
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4 // optional
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Create touchscreen & TFTscreen objects
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// Create GUI handler object
GUI gui(&tft);
// Ornements (add custom stuff on your buttons)
// You have to inherite from Ornement and override
// "void draw(Adafruit_TFTLCD* tft)"
// use getX(), getY(), getW() and getH() to get position and size
class UpArrow : public Ornement
{
virtual void draw(Adafruit_TFTLCD* tft)
{
int x = getX();
int y = getY();
int w = getW();
int h = getH();
tft->fillTriangle(x + w/2, y + (w)/3,
x + w/3, y + 2*(w)/3,
x + 2*(w)/3, y + 2*(w)/3,
MAGENTA);
}
};
// Same to write text on button
class TextOrnment : public Ornement
{
virtual void draw(Adafruit_TFTLCD* tft)
{
int x = getX();
int y = getY();
int h = getH();
tft->setCursor(x, y + 7);
tft->setTextColor(BLACK); tft->setTextSize(h-2);
tft->print("Hello");
}
};
// Callbacks (will be called when pressed)
// You have to inherite from Callback and override
// "void execute()"
class PrintText : public Callback
{
public:
PrintText(char* text)
{
text_ = text;
}
void execute()
{
Serial.println(text_);
}
private:
char* text_;
};
// Setup
void setup(void)
{
// Init tft
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.fillScreen(BLACK);
pinMode(13, OUTPUT);
// 0x07E0 = GREEN
// Add rect button to GUI
// RectButton(int x, int y, int w, int h, long color);
gui.addButton((new RectButton(10, 10, 100, 100, GREEN))->setOrnement(new UpArrow)->setCallback(new PrintText("Up button")));
// Add rect button to GUI
// RoundButton(int x, int y, int r, long color);
gui.addButton((new RoundButton(70, 230, 50, GREEN))->setCallback(new PrintText("Round button")));
// Draw GUI
gui.draw();
}
// Main loop
void loop()
{
// Get input
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
// if sharing pins, you'll need to fix the directions of the touchscreen pins
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// scale from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height()-80, 0); // May need ajustment, some screens are strange...
// Update return true if a button has been press
// Corresponding callback will be called
if (gui.update(p))
{
tft.fillScreen(BLACK); // Clear screen
gui.draw(); // Draw GUI
delay(50); // Must wait for the screen to have finished drawing
}
}
}