sumotoy / RA8875

A library for RAiO RA8875 display driver for Teensy3.x or LC/Arduino's/Energia/Spark
GNU General Public License v3.0
79 stars 55 forks source link

RA8875 TFT Capacitive touch screen not responding #134

Open Amomah opened 5 years ago

Amomah commented 5 years ago

Hi There,

Can anyone help please me understand why only one of the two Codes work with my 7" TFT RA8875 Capactive TFT touch Screen and already uncommented RA8875UserSettings.h file and uncomment USE_FT5206_TOUCH. I am using Mega 2560 Arduino.

There are 2 points I seek understanding (Code1 = works) and (Code2 = doesn't work):

1- The following Code work downloaded from BuyDisplayOnline website;

/*** //Web: http://www.buydisplay.com EastRising Technology Co.,LTD Examples for ER-TFTM070-5 with Capacitive Touch Panel Display is Hardward SPI 4-Wire SPI Interface and 5V Power Supply Capacitive Touch Panel is I2C Interface Tested and worked with: Works with Arduino 1.6.0 IDE ****/

include

include

include

uint8_t addr = 0x38;

include "Adafruit_GFX.h"

include "Adafruit_RA8875.h"

//LCD:hardware SPI CTP:hardware IIC //Arduino DUE,Arduino mega2560,Arduino UNO

define RA8875_INT 4

define RA8875_CS 10

define RA8875_RESET 9

define FT5206_WAKE 6

define FT5206_INT 7

Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET); uint16_t tx, ty;

enum { eNORMAL = 0, eTEST = 0x04, eSYSTEM = 0x01 };

struct TouchLocation { uint16_t x; uint16_t y; };

TouchLocation touchLocations[5];

uint8_t readFT5206TouchRegister( uint8_t reg ); uint8_t readFT5206TouchLocation( TouchLocation pLoc, uint8_t num ); uint8_t readFT5206TouchAddr( uint8_t regAddr, uint8_t pBuf, uint8_t len );

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[30];

uint8_t readFT5206TouchRegister( uint8_t reg ) { Wire.beginTransmission(addr); Wire.write( reg ); // register 0 uint8_t retVal = Wire.endTransmission();

uint8_t returned = Wire.requestFrom(addr, uint8_t(1) ); // request 6 uint8_ts from slave device #2

if (Wire.available()) { retVal = Wire.read(); }

return retVal; }

uint8_t readFT5206TouchAddr( uint8_t regAddr, uint8_t * pBuf, uint8_t len ) { Wire.beginTransmission(addr); 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 readFT5206TouchLocation( TouchLocation * pLoc, uint8_t num ) { uint8_t retVal = 0; uint8_t i; uint8_t k;

do { if (!pLoc) break; // must have a buffer if (!num) break; // must be able to take at least one

uint8_t status = readFT5206TouchRegister(2);

static uint8_t tbuf[40];

if ((status & 0x0f) == 0) break; // no points detected

uint8_t hitPoints = status & 0x0f;

Serial.print("number of hit points = ");
Serial.println( hitPoints );

readFT5206TouchAddr( 0x03, tbuf, hitPoints*6);

for (k=0,i = 0; (i < hitPoints*6)&&(k < num); k++, i += 6)
{
  pLoc[k].x = (tbuf[i+0] & 0x0f) << 8 | tbuf[i+1];
  pLoc[k].y = (tbuf[i+2] & 0x0f) << 8 | tbuf[i+3];
}

retVal = k;

} while (0);

return retVal; }

void writeFT5206TouchRegister( uint8_t reg, uint8_t val) { Wire.beginTransmission(addr); Wire.write( reg ); // register 0 Wire.write( val ); // value

uint8_t retVal = Wire.endTransmission();
}

void readOriginValues(void) { writeFT5206TouchRegister(0, eTEST); delay(1); //uint8_t originLength = readFT5206TouchAddr(0x08, buf, 8 ); uint8_t originXH = readFT5206TouchRegister(0x08); uint8_t originXL = readFT5206TouchRegister(0x09); uint8_t originYH = readFT5206TouchRegister(0x0a); uint8_t originYL = readFT5206TouchRegister(0x0b);

uint8_t widthXH = readFT5206TouchRegister(0x0c); uint8_t widthXL = readFT5206TouchRegister(0x0d); uint8_t widthYH = readFT5206TouchRegister(0x0e); uint8_t widthYL = readFT5206TouchRegister(0x0f);

//if (originLength) { Serial.print("Origin X,Y = "); Serial.print( uint16_t((originXH<<8) | originXL) ); Serial.print(", "); Serial.println( uint16_t((originYH<<8) | originYL) );

Serial.print("Width X,Y = ");
Serial.print( uint16_t((widthXH<<8) | widthXL) );
Serial.print(", ");
Serial.println( uint16_t((widthYH<<8) | widthYL) );

}

} // 1234567890 void setup() { Serial.begin(9600); Serial.println("RA8875 start"); Wire.begin(); // join i2c bus (address optional for master)

readOriginValues(); pinMode (FT5206_WAKE, INPUT); digitalWrite(FT5206_WAKE, HIGH ); writeFT5206TouchRegister(0, eNORMAL); // device mode = Normal

uint8_t periodMonitor = readFT5206TouchRegister(0x89);

Serial.print("periodMonitor = "); Serial.println( periodMonitor, HEX );

uint8_t lenLibVersion = readFT5206TouchAddr(0x0a1, buf, 2 ); if (lenLibVersion) { uint16_t libVersion = (buf[0] << 8) | buf[1];

Serial.print("lib version = ");
Serial.println( libVersion, HEX);

} else { Serial.println("lib version length is zero"); }

uint8_t firmwareId = readFT5206TouchRegister( 0xa6 );

Serial.print("firmware ID = "); Serial.println( firmwareId);

/* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */

//if (!tft.begin(RA8875_800x480)) //{ // Serial.println("RA8875 Not Found!"); // while (1); //}

while (!tft.begin(RA8875_800x480)) { Serial.println("RA8875 Not Found!"); delay(100); }

Serial.println("Found RA8875");

tft.displayOn(true); tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight tft.PWM1out(255);

// With hardware accelleration this is instant tft.fillScreen(RA8875_WHITE);

  tft.textMode();
  tft.textSetCursor(0,0);
  tft.textColor(RA8875_RED, RA8875_BLACK);
  tft.textWrite("www.buydiplay.com  Backlight brightness adjustment test ");
  tft.graphicsMode();

// Play with PWM for (uint8_t i=255; i!=0; i-=5 ) { tft.PWM1out(i); delay(10); }
for (uint8_t i=0; i!=255; i+=5 ) { tft.PWM1out(i); delay(10); } tft.PWM1out(255);

tft.fillScreen(RA8875_RED); delay(500); tft.fillScreen(RA8875_YELLOW); delay(500); tft.fillScreen(RA8875_GREEN); delay(500); tft.fillScreen(RA8875_CYAN); delay(500); tft.fillScreen(RA8875_MAGENTA); delay(500); tft.fillScreen(RA8875_BLACK);

// Try some GFX acceleration! tft.drawCircle(100, 100, 50, RA8875_BLACK); tft.fillCircle(100, 100, 49, RA8875_GREEN);

tft.fillRect(11, 11, 700, 400, RA8875_BLUE); tft.drawRect(10, 10, 400, 200, RA8875_GREEN); tft.fillRoundRect(200, 10, 200, 100, 10, RA8875_RED); tft.fillRoundRect(480, 100, 300, 200, 10, RA8875_RED); tft.drawPixel(10,10,RA8875_BLACK); tft.drawPixel(11,11,RA8875_BLACK); //tft.drawLine(10, 10, 200, 100, RA8875_RED); tft.drawTriangle(200, 15, 250, 100, 150, 125, RA8875_BLACK); //tft.fillTriangle(200, 16, 249, 99, 151, 460, RA8875_YELLOW); tft.drawEllipse(300, 100, 100, 40, RA8875_BLACK); tft.fillEllipse(300, 100, 98, 38, RA8875_GREEN); // Argument 5 (curvePart) is a 2-bit value to control each corner (select 0, 1, 2, or 3) tft.drawCurve(50, 100, 80, 40, 2, RA8875_BLACK);
tft.fillCurve(50, 100, 78, 38, 2, RA8875_WHITE);

pinMode (FT5206_INT, INPUT); //digitalWrite(FT5206_INT, HIGH );

tft.touchEnable(false);

Serial.print("Status: "); Serial.println(tft.readStatus(), HEX); Serial.println("Waiting for touch events ...");

randomSeed(analogRead(0)); }

uint32_t dist(const TouchLocation & loc) { uint32_t retVal = 0;

uint32_t x = loc.x; uint32_t y = loc.y;

retVal = xx + yy;

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(xx + yy);

return retVal; }

bool sameLoc( const TouchLocation & loc, const TouchLocation & loc2 ) { return dist(loc,loc2) < 50; }

void loop() { static uint16_t total = 256; static uint16_t w = tft.width(); static uint16_t h = tft.height();

float xScale = 1024.0F/w; float yScale = 1024.0F/h;

uint8_t attention = digitalRead(FT5206_INT); static uint8_t oldAttention = 1;

uint32_t thisTouchTime = millis(); uint8_t i=0;

static uint32_t lastTouchTime = thisTouchTime;

while(total!=0)

{ total--; if ((total & 0x3ff) == 0) { //Serial.println(total); } // void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); int16_t x = random(w); int16_t y = random(h); int16_t x1 = random(w); int16_t y1 = random(h); int16_t x2 = random(w); int16_t y2 = random(h); uint16_t c = (((random(64)<<6)|random(64))<<6)|random(31); uint16_t bg = (((random(64)<<6)|random(64))<<6)|random(31); int16_t th = random(h); int16_t tw = random(w); int16_t op = random(9); int16_t en = random(8);

switch( op )
{
default:
case 0:
  tft.fillRect(x,y, tw, th, c);
  break;
case 1:
  tft.fillEllipse(x,y,tw/2,th/2,c);
  break;
case 2:
  tft.fillCircle(x,y,tw/2,c);
  break;
case 3:
  tft.fillTriangle(x,y,x1,y1,x2,y2,c);
  break;
case 4:
  tft.textMode();
  tft.textSetCursor(x,y);
  tft.textColor(c,bg);
  tft.textEnlarge(en);
  tft.textWrite("Hallowell",9);
  tft.graphicsMode();
  break;
case 5:
  tft.drawRect(x,y, tw, th, c);
  break;
case 6:
  tft.drawEllipse(x,y,tw/2,th/2,c);
  break;
case 7:
  tft.drawCircle(x,y,tw/2,c);
  break;
case 8:
  tft.drawTriangle(x,y,x1,y1,x2,y2,c);
  break;
}
 if(total==0)
   {  tft.fillScreen(RA8875_BLACK);
     tft.textMode();
    tft.textSetCursor(0,0);
    tft.textColor(RA8875_RED, RA8875_BLACK);
     tft.textEnlarge(0);
    tft.textWrite("www.buydiplay.com  Capacitive touch screen test.Please touch the screen!");   
   }      

}

 /* Wait around for touch events */
if (!attention && oldAttention ) 
{   
  Serial.println("Touch: ");

  uint8_t count = readFT5206TouchLocation( touchLocations, 5 );

  //static uint8_t lastCount = count;

 if (count)
  {
    static TouchLocation lastTouch = touchLocations[0];

    if (((thisTouchTime - lastTouchTime) > 10000) && sameLoc( touchLocations[0],lastTouch ) )
    {
      tft.fillScreen(RA8875_BLACK);
      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++)
    {
      //Serial.print(i);
      //Serial.print(": ");
      //Serial.print(touchLocations[i].x);
      //Serial.print(", ");
      //Serial.print(touchLocations[i].y);
      //Serial.println(".");
      tft.textMode();
    tft.textSetCursor(0,0);
    tft.textColor(RA8875_RED, RA8875_BLACK);
     tft.textEnlarge(0);
    tft.textWrite("www.buydiplay.com  Capacitive touch screen test.Please touch the screen!");
      //tft.textSetCursor(touchLocations[i].x,touchLocations[i].y);
      tft.textSetCursor(380,380+16*i);
      tft.textColor(RA8875_WHITE, RA8875_BLACK);
      tft.textEnlarge(0);
      snprintf((char*)buf,sizeof(buf),"(%3d,%3d)",touchLocations[i].x,touchLocations[i].y);
      tft.textWrite((const char *)buf,9);
    tft.graphicsMode();  
    if(i==0)  tft.fillCircle(touchLocations[i].x,touchLocations[i].y, 5, RA8875_RED);  
    else if(i==1)  tft.fillCircle(touchLocations[i].x,touchLocations[i].y, 5, RA8875_GREEN); 
    else if(i==2)  tft.fillCircle(touchLocations[i].x,touchLocations[i].y, 5, RA8875_BLUE);        
    else if(i==3)  tft.fillCircle(touchLocations[i].x,touchLocations[i].y, 5, RA8875_CYAN); 
     else if(i==4)  tft.fillCircle(touchLocations[i].x,touchLocations[i].y, 5, RA8875_MAGENTA);       
    }
  }

// for (; i < 5; i++) // { // tft.textMode(); // //tft.textSetCursor(touchLocations[i].x,touchLocations[i].y); // tft.textSetCursor(380,380+16i); // tft.textColor(RA8875_WHITE, RA8875_BLACK); // tft.textEnlarge(0); // snprintf((char)buf,sizeof(buf)," "); // tft.textWrite((const char )buf,9); // } //lastCount = count; // if (tft.touched()) // USB // { // Serial.print("Touch: "); // tft.touchRead(&tx, &ty); // Serial.print(tx); Serial.print(", "); Serial.println(ty); // / Draw a circle */ // tft.fillCircle((uint16_t)(tx/xScale), (uint16_t)(ty/yScale), 4, RA8875_WHITE); // } }

else
{
}

oldAttention = attention;

} ****/

2- The following Code does NOT work. Downloaded from Github.

****/

/ Serial.begin(38400); long unsigned debug_start = millis (); while (!Serial && ((millis () - debug_start) <= 5000)) ; / tft.begin(RA8875_800x480);

if defined(USE_FT5206_TOUCH)

tft.useCapINT(RA8875_INT);//we use the capacitive chip Interrupt out! //the following set the max touches (max 5) //it can be placed inside loop but BEFORE touched() //to limit dinamically the touches (for example to 1) tft.setTouchLimit(MAXTOUCHLIMIT); //tft.setRotation(0);//this works in any rotation mode! tft.enableCapISR(true);//capacitive touch screen interrupt it's armed

else

tft.print("you should open RA8875UserSettings.h file and uncomment USE_FT5206_TOUCH!");

endif

tft.setTextColor(RA8875_WHITE,RA8875_BLACK); }

void loop(){

if defined(USE_FT5206_TOUCH)

if (tft.touched()){//if touched(true) detach isr //at this point we need to fill the FT5206 registers... tft.updateTS();//now we have the data inside library tft.setCursor(CENTER,CENTER); tft.print(" "); tft.setCursor(CENTER,CENTER); tft.print("touches:"); tft.print(tft.getTouches()); tft.print(" | gesture:"); tft.print(tft.getGesture(),HEX); tft.print(" | state:"); tft.print(tft.getTouchState(),HEX); //you need to get the coordinates? We need a bidimensional array uint16_t coordinates[MAXTOUCHLIMIT][2];//to hold coordinates tft.getTScoordinates(coordinates);//done //now coordinates has the x,y of all touches //now draw something.... uint16_t tempCol; for (uint8_t i=1;i<=tft.getTouches();i++){ if (i == 1)tempCol = RA8875_RED; if (i == 2)tempCol = RA8875_GREEN; if (i == 3)tempCol = RA8875_MAGENTA; if (i == 4)tempCol = RA8875_CYAN; if (i == 5)tempCol = RA8875_YELLOW; tft.fillCircle(coordinates[i-1][0],coordinates[i-1][1],10,tempCol); } tft.enableCapISR();//rearm ISR if needed (touched(true)) //otherwise it doesn't do nothing... }

endif

}

****/

Appreciate your time and help. AMOMAH

atilla44 commented 4 years ago

Hello i have a 9inch ra8875 display with capacitive touch 4 wire i tried everything but the touch has no reaction i would be grateful for any help thanks

gilphilbert commented 3 years ago

The buydisplay RA8875 capacitive touchscreen models don't use the same touchscreen as most other RA8875 boards; they use GLS1680 displays. They're not easy to get working, so you may need to hunt around for the right library