Hier ist der funktionierende Sketch vom Hersteller:
//web: http://www.buydisplay.com
//EastRising Technology Co.,LTD
//Examples for ER-TFTM090-1 with Capacitive Touch Panel
// This program is a demo of how to use most of the functions
// of the library with a supported display modules.
// This demo was made for modules with a screen resolution
// of 800x480 pixels.
// This program requires the UTFT library.
//
#include <stdint.h>
#include <UTFT.h>
#include <SPI.h>
#include <Wire.h>
uint8_t addr = 0x5d; //CTP IIC ADDRESS
// Declare which fonts we will be using
extern uint8_t SmallFont[];
// Set the pins to the correct ones for your development shield
// Standard Arduino Mega/Due shield
#define GT9271_RESET 41 //CTP RESET
#define GT9271_INT 48 //CTP INT
UTFT myGLCD(SSD1963_800480,38,39,40,41); //(byte model, int RS, int WR, int CS, int RST)
unsigned char GTP_CFG_DATA[] =
{
0x00,0x20,0x03,0xE0,0x01,0x0A,0x0D,0x00,
0x01,0x0A,0x28,0x0F,0x50,0x32,0x03,0x08,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x8E,0x2E,0x88,0x23,0x21,
0x31,0x0D,0x00,0x00,0x00,0x01,0x03,0x1D,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x1E,0x50,0x94,0xC5,0x02,
0x07,0x00,0x00,0x04,0x80,0x21,0x00,0x6B,
0x28,0x00,0x59,0x31,0x00,0x4B,0x3B,0x00,
0x3F,0x48,0x00,0x3F,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x15,0x14,0x11,0x10,0x0F,0x0E,0x0D,0x0C,
0x09,0x08,0x07,0x06,0x05,0x04,0x01,0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,
0x04,0x06,0x07,0x08,0x0A,0x0C,0x0D,0x0F,
0x10,0x11,0x12,0x13,0x19,0x1B,0x1C,0x1E,
0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,
0x27,0x28,0xFF,0xFF,0xFF,0xFF,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x4D,0x01
};
struct TouchLocation
{
uint16_t x;
uint16_t y;
};
TouchLocation touchLocations[10];
void inttostr(uint16_t value,uint8_t *str);
uint8_t GT9271_Send_Cfg(uint8_t * buf,uint16_t cfg_len);
void writeGT9271TouchRegister( uint16_t regAddr,uint8_t *val, uint16_t cnt);
uint8_t readGT9271TouchAddr( uint16_t regAddr, uint8_t * pBuf, uint8_t len );
uint8_t readGT9271TouchLocation( TouchLocation * pLoc, uint8_t num );
uint32_t dist(const TouchLocation & loc);
uint32_t dist(const TouchLocation & loc1, const TouchLocation & loc2);
bool sameLoc( const TouchLocation & loc, const TouchLocation & loc2 );
uint8_t buf[80];
uint8_t GT9271_Send_Cfg(uint8_t * buf,uint16_t cfg_len)
{
//uint8_t ret=0;
uint8_t retry=0;
for(retry=0;retry<2;retry++)
{
writeGT9271TouchRegister(0x8047,buf,cfg_len);
//if(ret==0)break;
delay(10);
}
//return ret;
}
void writeGT9271TouchRegister( uint16_t regAddr,uint8_t *val, uint16_t cnt)
{ uint16_t i=0;
Wire.beginTransmission(addr);
Wire.write( regAddr>>8 ); // register 0
Wire.write( regAddr); // register 0
for(i=0;i<cnt;i++,val++)//data
{
Wire.write( *val ); // value
}
uint8_t retVal = Wire.endTransmission();
}
uint8_t readGT9271TouchAddr( uint16_t regAddr, uint8_t * pBuf, uint8_t len )
{
Wire.beginTransmission(addr);
Wire.write( regAddr>>8 ); // register 0
Wire.write( regAddr); // register 0
uint8_t retVal = Wire.endTransmission();
uint8_t returned = Wire.requestFrom(addr, len); // request 1 bytes from slave device #2
uint8_t i;
for (i = 0; (i < len) && Wire.available(); i++)
{
pBuf[i] = Wire.read();
}
return i;
}
uint8_t readGT9271TouchLocation( TouchLocation * pLoc, uint8_t num )
{
uint8_t retVal;
uint8_t i;
uint8_t k;
uint8_t ss[1];
do
{
if (!pLoc) break; // must have a buffer
if (!num) break; // must be able to take at least one
ss[0]=0;
readGT9271TouchAddr( 0x814e, ss, 1);
uint8_t status=ss[0];
if ((status & 0x0f) == 0) break; // no points detected
uint8_t hitPoints = status & 0x0f;
Serial.print("number of hit points = ");
Serial.println( hitPoints );
uint8_t tbuf[32]; uint8_t tbuf1[32];uint8_t tbuf2[16];
readGT9271TouchAddr( 0x8150, tbuf, 32);
readGT9271TouchAddr( 0x8150+32, tbuf1, 32);
readGT9271TouchAddr( 0x8150+64, tbuf2,16);
if(hitPoints<=4)
{
for (k=0,i = 0; (i < 4*8)&&(k < num); k++, i += 8)
{
pLoc[k].x = tbuf[i+1] << 8 | tbuf[i+0];
pLoc[k].y = tbuf[i+3] << 8 | tbuf[i+2];
}
}
if(hitPoints>4)
{
for (k=0,i = 0; (i < 4*8)&&(k < num); k++, i += 8)
{
pLoc[k].x = tbuf[i+1] << 8 | tbuf[i+0];
pLoc[k].y = tbuf[i+3] << 8 | tbuf[i+2];
}
for (k=4,i = 0; (i < 4*8)&&(k < num); k++, i += 8)
{
pLoc[k].x = tbuf1[i+1] << 8 | tbuf1[i+0];
pLoc[k].y = tbuf1[i+3] << 8 | tbuf1[i+2];
}
}
if(hitPoints>8)
{
for (k=0,i = 0; (i < 4*8)&&(k < num); k++, i += 8)
{
pLoc[k].x = tbuf[i+1] << 8 | tbuf[i+0];
pLoc[k].y = tbuf[i+3] << 8 | tbuf[i+2];
}
for (k=4,i = 0; (i < 4*8)&&(k < num); k++, i += 8)
{
pLoc[k].x = tbuf1[i+1] << 8 | tbuf1[i+0];
pLoc[k].y = tbuf1[i+3] << 8 | tbuf1[i+2];
}
for (k=8,i = 0; (i < 2*8)&&(k < num); k++, i += 8)
{
pLoc[k].x = tbuf2[i+1] << 8 | tbuf2[i+0];
pLoc[k].y = tbuf2[i+3] << 8 | tbuf2[i+2];
}
}
retVal = hitPoints;
} while (0);
ss[0]=0;
writeGT9271TouchRegister( 0x814e,ss,1);
delay(2);
return retVal;
}
void setup()
{
randomSeed(analogRead(0));
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
delay(300);
pinMode(GT9271_RESET, OUTPUT);
pinMode (GT9271_INT, OUTPUT);
digitalWrite(GT9271_RESET, LOW);
delay(20);
digitalWrite(GT9271_INT, LOW);
delay(50);
digitalWrite(GT9271_RESET, HIGH);
delay(100);
pinMode (GT9271_INT, INPUT);
delay(100);
uint8_t re=GT9271_Send_Cfg((uint8_t*)GTP_CFG_DATA,sizeof(GTP_CFG_DATA));
pinMode(GT9271_RESET, OUTPUT);
pinMode (GT9271_INT, OUTPUT);
digitalWrite(GT9271_RESET, LOW);
delay(20);
digitalWrite(GT9271_INT, LOW);
delay(50);
digitalWrite(GT9271_RESET, HIGH);
delay(100);
pinMode (GT9271_INT, INPUT);
delay(100);
re=GT9271_Send_Cfg((uint8_t*)GTP_CFG_DATA,sizeof(GTP_CFG_DATA));
uint8_t bb[2];
readGT9271TouchAddr(0x8047,bb,2);
while(bb[1]!=32)
{ Serial.println("Capacitive touch screen initialized failure");
pinMode(GT9271_RESET, OUTPUT);
pinMode (GT9271_INT, OUTPUT);
digitalWrite(GT9271_RESET, LOW);
delay(20);
digitalWrite(GT9271_INT, LOW);
delay(50);
digitalWrite(GT9271_RESET, HIGH);
delay(100);
pinMode (GT9271_INT, INPUT);
delay(100);
uint8_t re=GT9271_Send_Cfg((uint8_t*)GTP_CFG_DATA,sizeof(GTP_CFG_DATA));
}
Serial.println("Capacitive touch screen initialized success");
// Setup the LCD
myGLCD.InitLCD();
// -------------------------------------------------------------
pinMode(8, OUTPUT); //backlight
digitalWrite(8, HIGH);//on
// -------------------------------------------------------------
myGLCD.setFont(SmallFont);
/*
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 0, 799, 479);
uint8_t bb[32];
readGT9271TouchAddr(0x8047,bb,32);
myGLCD.setColor(0, 255, 0);
if (bb[1]==32)Serial.println("Capacitive touch screen initialized success");
else
{ myGLCD.print("* Capacitive touch screen initialized failure,Please,Press the reset button Reset again ",LEFT , 1);
while(1);
}
*/
/*
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 0, 799, 479);
uint8_t bb[32];
readGT9271TouchAddr(0x8047,bb,32);
myGLCD.setColor(255, 0, 0);
myGLCD.print("* www.buydiplay.com Read CFG",LEFT , 3);
uint8_t in=0;
while(in<16)
{
snprintf((char*)buf,sizeof(buf),"(%3d)",bb[in]);
myGLCD.print((const char *)buf,CENTER,20+16*in);
in++;
}
delay(3000);
*/
}
uint32_t dist(const TouchLocation & loc)
{
uint32_t retVal = 0;
uint32_t x = loc.x;
uint32_t y = loc.y;
retVal = x*x + y*y;
return retVal;
}
uint32_t dist(const TouchLocation & loc1, const TouchLocation & loc2)
{
uint32_t retVal = 0;
uint32_t x = loc1.x - loc2.x;
uint32_t y = loc1.y - loc2.y;
retVal = sqrt(x*x + y*y);
return retVal;
}
bool sameLoc( const TouchLocation & loc, const TouchLocation & loc2 )
{
return dist(loc,loc2) < 50;
}
void loop()
{
int buf[798];
int x, x2;
int y, y2;
int r;
// Clear the screen and draw the frame
myGLCD.clrScr();
uint8_t flag = 1;
static uint16_t w = 800;
static uint16_t h = 480;
float xScale = 1024.0F/w;
float yScale = 1024.0F/h;
uint32_t thisTouchTime = millis();
uint8_t i=0;
static
uint32_t lastTouchTime = thisTouchTime;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 0, 799, 479);
myGLCD.setColor(255, 0, 0);
myGLCD.print("* www.buydiplay.com Capacitive touch screen test.Please touch the screen!", CENTER, 1);
myGLCD.setColor(0, 255, 255);
myGLCD.print("*EXIT", LEFT, 460);
myGLCD.setColor(0, 255, 0);
myGLCD.print("*CLEAR",RIGHT, 460);
while(flag)
{
/* Wait around for touch events */
/* pinMode (GT9271_INT, INPUT);
uint8_t st=digitalRead(GT9271_INT);
if(!st) //Hardware touch interrupt
*/
uint8_t ss[1];
readGT9271TouchAddr( 0x814e, ss, 1);
uint8_t status=ss[0];
if ((status & 0x80) != 0) // touch status Software touch interrupt
{
Serial.println("Touch: ");
uint8_t count = readGT9271TouchLocation( touchLocations, 10 );
//static uint8_t lastCount = count;
if (count)
{
static TouchLocation lastTouch = touchLocations[0];
if (((thisTouchTime - lastTouchTime) > 10000) && sameLoc( touchLocations[0],lastTouch ) )
{
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 0, 799, 479);
lastTouchTime = thisTouchTime;
}
Serial.print("Time delta = ");
Serial.print(thisTouchTime - lastTouchTime);
Serial.print(", dist = ");
Serial.println( dist(touchLocations[0],lastTouch) );
lastTouch = touchLocations[0];
Serial.println("Locations: ");
for (i = 0; i < count; i++)
{
// myGLCD.setColor(255, 0, 0);
// myGLCD.print("* www.buydiplay.com Capacitive touch screen test.Please touch the screen!", CENTER, 1);
snprintf((char*)buf,sizeof(buf),"(%3d,%3d)",touchLocations[i].x,touchLocations[i].y);
myGLCD.print((const char *)buf,CENTER,280+16*i);
if(touchLocations[i].x>=3&&touchLocations[i].x<=40&&touchLocations[i].y>=450&&touchLocations[i].y<=480 )flag=0;
if(i==0)
myGLCD.setColor(255, 0, 0),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4);
else if(i==1)
myGLCD.setColor(0, 255, 0),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4);
else if(i==2)
myGLCD.setColor(0, 0, 255),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==3)
myGLCD.setColor(255, 255, 255),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==4)
myGLCD.setColor(0, 255, 255),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==5)
myGLCD.setColor(255, 255,0 ),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==6)
myGLCD.setColor(255,0,255),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==7)
myGLCD.setColor(50,0,150),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==8)
myGLCD.setColor(150,100,0),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
else if(i==9)
myGLCD.setColor(0,150,50),
myGLCD.fillCircle(touchLocations[i].x,touchLocations[i].y, 4 );
if(touchLocations[i].x>=3&&touchLocations[i].x>=750&&touchLocations[i].y>=450&&touchLocations[i].y<=480 )
{ myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 0, 799, 479);
myGLCD.setColor(255, 0, 0);
myGLCD.print("* www.buydiplay.com Capacitive touch screen test.Please touch the screen!", CENTER, 1);
myGLCD.setColor(0, 255, 255);
myGLCD.print("*EXIT", LEFT, 460);
myGLCD.setColor(0, 255, 0);
myGLCD.print("*CLEAR",RIGHT, 460);
}
}
}
}
}
myGLCD.clrScr();
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 799, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 466, 799, 479);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print("* EastRising Technology *", CENTER, 1);
myGLCD.setBackColor(64, 64, 64);
myGLCD.setColor(255,255,0);
myGLCD.print(" http://www.buydisplay.com", CENTER, 467);
myGLCD.setColor(0, 0, 255);
myGLCD.drawRect(0, 14, 799, 465);
// Draw crosshairs
myGLCD.setColor(0, 0, 255);
myGLCD.setBackColor(0, 0, 0);
myGLCD.drawLine(399, 15, 399, 464);
myGLCD.drawLine(1, 239, 798, 239);
for (int i=9; i<790; i+=10)
myGLCD.drawLine(i, 237, i, 242);
for (int i=19; i<470; i+=10)
myGLCD.drawLine(397, i, 402, i);
// Draw sin-, cos- and tan-lines
myGLCD.setColor(0,255,255);
myGLCD.print("Sin", 5, 15);
for (int i=1; i<798; i++)
{
myGLCD.drawPixel(i,239+(sin(((i*1.13)*3.14)/180)*200));
}
myGLCD.setColor(255,0,0);
myGLCD.print("Cos", 5, 27);
for (int i=1; i<798; i++)
{
myGLCD.drawPixel(i,239+(cos(((i*1.13)*3.14)/180)*200));
}
myGLCD.setColor(255,255,0);
myGLCD.print("Tan", 5, 39);
for (int i=1; i<798; i++)
{
myGLCD.drawPixel(i,239+(tan(((i*0.9)*3.14)/180)));
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
myGLCD.setColor(0, 0, 255);
myGLCD.setBackColor(0, 0, 0);
myGLCD.drawLine(399, 15, 399, 464);
myGLCD.drawLine(1, 239, 798, 239);
// Draw a moving sinewave
x=1;
for (int i=1; i<(798*20); i++)
{
x++;
if (x==799)
x=1;
if (i>799)
{
if ((x==399)||(buf[x-1]==239))
myGLCD.setColor(0,0,255);
else
myGLCD.setColor(0,0,0);
myGLCD.drawPixel(x,buf[x-1]);
}
myGLCD.setColor(0,255,255);
y=239+(sin(((i*1.65)*3.14)/180)*(200-(i / 100)));
myGLCD.drawPixel(x,y);
buf[x-1]=y;
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some random filled rectangles
for (int i=0; i<50; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=2+random(746);
y=16+random(397);
x2=x+50;
y2=y+50;
myGLCD.fillRect(x, y, x2, y2);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some random filled, rounded rectangles
for (int i=0; i<50; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=2+random(746);
y=16+random(397);
x2=x+50;
y2=y+50;
myGLCD.fillRoundRect(x, y, x2, y2);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some random filled circles
for (int i=0; i<50; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=27+random(746);
y=41+random(397);
myGLCD.fillCircle(x, y, 25);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some lines in a pattern
myGLCD.setColor (255,0,0);
for (int i=15; i<463; i+=5)
{
myGLCD.drawLine(1, i, (i*1.66)-10, 463);
}
myGLCD.setColor (255,0,0);
for (int i=463; i>15; i-=5)
{
myGLCD.drawLine(798, i, (i*1.66)+30, 15);
}
myGLCD.setColor (0,255,255);
for (int i=463; i>15; i-=5)
{
myGLCD.drawLine(1, i, 770-(i*1.66), 15);
}
myGLCD.setColor (0,255,255);
for (int i=15; i<463; i+=5)
{
myGLCD.drawLine(798, i, 810-(i*1.66), 463);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some random circles
for (int i=0; i<250; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=32+random(736);
y=45+random(386);
r=random(30);
myGLCD.drawCircle(x, y, r);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some random rectangles
for (int i=0; i<250; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=2+random(796);
y=16+random(447);
x2=2+random(796);
y2=16+random(447);
myGLCD.drawRect(x, y, x2, y2);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
// Draw some random rounded rectangles
for (int i=0; i<250; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=2+random(796);
y=16+random(447);
x2=2+random(796);
y2=16+random(447);
myGLCD.drawRoundRect(x, y, x2, y2);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
for (int i=0; i<250; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
x=2+random(796);
y=16+random(447);
x2=2+random(796);
y2=16+random(447);
myGLCD.drawLine(x, y, x2, y2);
}
delay(2000);
myGLCD.setColor(0,0,0);
myGLCD.fillRect(1,15,798,464);
for (int i=0; i<10000; i++)
{
myGLCD.setColor(random(255), random(255), random(255));
myGLCD.drawPixel(2+random(796), 16+random(447));
}
delay(2000);
myGLCD.fillScr(0, 0, 255);
myGLCD.setColor(255, 0, 0);
myGLCD.fillRoundRect(320, 190, 479, 289);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print("www.buydisplay.com", CENTER, 195);
myGLCD.print("That's it!", CENTER, 213);
myGLCD.print("Restarting in a", CENTER, 239);
myGLCD.print("few seconds...", CENTER, 252);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 255);
myGLCD.print("Runtime: (msecs)", CENTER, 450);
myGLCD.printNumI(millis(), CENTER, 465);
delay (10000);
}