/***************************************************
This is our GFX example for the Adafruit ILI9341 Breakout and Shield
----> http://www.adafruit.com/products/1651
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include "SPI.h"
#include "Ticker.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Adafruit_FT6206.h>
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
class stateManager{
public:
uint8_t stateCode;
void refresh(boolean s);
void reset();
void set();
bool thereIs(uint8_t c);
};
void stateManager::reset(){
refresh(false);
}
void stateManager::set(){
refresh(true);
}
void stateManager::refresh(boolean s){
boolean oldS=thereIs(0x1);
uint8_t sc=s*0x1+oldS*0x2;
sc=sc+((s&&(!oldS))*0x4)+(((!s)&&oldS)*0x8);
stateCode=sc;
}
bool stateManager::thereIs(uint8_t c){
return ((c&stateCode)>0);
}
class rectIcon{
public:
rectIcon(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
rectIcon(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t f, uint32_t d, uint8_t dc);
rectIcon(uint16_t x, uint16_t y, uint16_t w, uint16_t h, boolean c, uint32_t f, uint32_t d, uint8_t dc);
uint16_t startX, startY, rectW, rectH;
uint32_t fillColor, drawColor;
uint8_t drawCode;
stateManager state;
//Fuctions
void acquire(uint16_t x, uint16_t y);
void begin(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
void beginCenter(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
bool mouseHere(uint16_t x, uint16_t y);
void setColors(uint32_t f, uint32_t d);
void setColors(uint32_t f, uint32_t d, uint8_t dc);
bool thereIs(uint8_t s);
};
rectIcon::rectIcon(uint16_t x, uint16_t y, uint16_t w, uint16_t h){
begin(x,y,w,h);
}
rectIcon::rectIcon(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t f, uint32_t d, uint8_t dc){
begin(x,y,w,h);
setColors(f,d,dc);
}
rectIcon::rectIcon(uint16_t x, uint16_t y, uint16_t w, uint16_t h, boolean c, uint32_t f, uint32_t d, uint8_t dc){
if(c){
beginCenter(x,y,w,h);
}
else{
begin(x,y,w,h);
}
setColors(f,d,dc);
}
void rectIcon::acquire(uint16_t x, uint16_t y){
state.refresh(mouseHere(x,y));
}
void rectIcon::begin(uint16_t x, uint16_t y, uint16_t w, uint16_t h){
startX=x;
startY=y;
rectW=y;
rectH=h;
}
void rectIcon::beginCenter(uint16_t x, uint16_t y, uint16_t w, uint16_t h){
begin(x-w/2,y-h/2,w,h);
}
bool rectIcon::mouseHere(uint16_t x, uint16_t y){
bool ok=((x>startX)&&(x<startX+rectW));
ok=ok&&((y>startY)&&(y<startY+rectH));
return ok;
}
void rectIcon::setColors(uint32_t f, uint32_t d){
fillColor=f;
drawColor=d;
}
void rectIcon::setColors(uint32_t f, uint32_t d, uint8_t dc){
setColors(f,d);
drawCode=dc;
}
bool rectIcon::thereIs(uint8_t s){
return state.thereIs(s);
}
class ILI9341CP_iconSupport{
public:
ILI9341CP_iconSupport(Adafruit_ILI9341 *s, Adafruit_FT6206 *t);
Adafruit_ILI9341 *screen;
Adafruit_FT6206 *touch;
void acquireRectIcon(rectIcon *r);
void drawRectIcon(rectIcon *r);
TS_Point getScreenPoint();
void init(Adafruit_ILI9341 *s, Adafruit_FT6206 *t);
bool screenTouched();
};
ILI9341CP_iconSupport::ILI9341CP_iconSupport(Adafruit_ILI9341 *s, Adafruit_FT6206 *t){
init(s,t);
}
void ILI9341CP_iconSupport::acquireRectIcon(rectIcon *r){
if(screenTouched()){
TS_Point p=getScreenPoint();
r->acquire(p.x,p.y);
}
else{
stateManager *sm=&(r->state);
sm->reset();
}
}
void ILI9341CP_iconSupport::drawRectIcon(rectIcon *r){
if((r->drawCode)&0x1){
screen->fillRect(r->startX,r->startY,r->rectW,r->rectH,r->fillColor);
}
if((r->drawCode)&0x2){
screen->drawRect(r->startX,r->startY,r->rectW,r->rectH,r->drawColor);
}
}
TS_Point ILI9341CP_iconSupport::getScreenPoint(){
return (touch->getPoint());
}
void ILI9341CP_iconSupport::init(Adafruit_ILI9341 *s, Adafruit_FT6206 *t){
screen=s;
touch=t;
}
bool ILI9341CP_iconSupport::screenTouched(){
return (touch->touched());
}
void dispRef();
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
Ticker dispTimer(dispRef, 1000, 0, MILLIS); // internal resolution is milli seconds
// // The display also uses hardware SPI, plus #9 & #10
// #define TFT_CS 10
// #define TFT_DC 9
//#define TFT_DC 2
//#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Size of the color selection boxes and the paintbrush size
#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, currentcolor;
rectIcon main_but(50,25,100,50, false, ILI9341_RED,ILI9341_WHITE,0x3);
ILI9341CP_iconSupport iconSup(&tft, &ctp);
void dispRef(){
tft.fillScreen(ILI9341_BLACK);
iconSup.drawRectIcon(&main_but);
}
void setup() {
Serial.begin(115200);
Serial.println(F("Cap Touch Paint!"));
tft.begin();
//dispTimer.start();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(2);
dispRef();
pinMode(4, OUTPUT);
}
void loop(){
if(!ctp.touched()){
}
else{
//TS_Point p = ctp.getPoint();
iconSup.acquireRectIcon(&main_but);
if(main_but.thereIs(0x4)){
Serial.println("---Button Pressed---");
}
if(main_but.thereIs(0x8)){
Serial.println("---Button Released---");
}
}
Serial.println(String("Touched="+String(ctp.touched())+"\nButton state code: 0x"+String(main_but.state.stateCode,HEX)));
digitalWrite(4,main_but.thereIs(0x1));
//dispTimer.update();
}
Loading
ili9341-cap-touch
ili9341-cap-touch