Touchshield v2 clear buffer

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Retxed68
 
Posts: 2
Joined: Thu May 03, 2012 4:37 pm

Touchshield v2 clear buffer

Post by Retxed68 »

Hello,

My name is Chris, I just received the touchshield V2 and am playing with it, it is very cool!
I have one question about the touch part, in my code I put the following code:

// See if there's any touch data for us
if (ts.bufferEmpty()) {
return;

Like in the paint example, if I am slow at clicking it will draw lots of points (or give lots of touches registered instead of 1). I would like to put an anti-rebound part to avoid having 2 touches registered in the same 100ms for example.

I tried to find the code to do this in the library but cannot find what I am looking for. Is there a way to clear the buffer after reading the first value of x and y or to do a add an anti-rebound to avoid this?

I tried the other function:
// You can also wait for a touch
if (! ts.touched()) {
return;
}

by putting p.x = 0; and p.y = 0; after doing what I wanted I thought it would work but no, the touch screen keeps sending the last value that was touched.

I was then thinking of putting a condition on when to get the new values (like if they are the same as before, do not use them), but I would still have this rebound issue..


Thank you!

Best regards,
Chris


PS: code below.

Code: Select all

#include <Adafruit_GFX.h>    // Core graphics library
#include <SPI.h>
#include <Wire.h>      // this is needed even tho we aren't using it
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000

// This is the definition of the different positions for the logo
#define LOGOX 120
#define LOGOY 120
#define LOGORADIUS 80

// The STMPE610 uses hardware SPI on the shield, and #8
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);

// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);


int oldcolor;
int currentcolor = ILI9341_BLUE;
int increment = 0;

void setup(void) {
 // while (!Serial);     // used for leonardo debugging
 
  Serial.begin(9600);
  Serial.println(F("Test!"));
  
  if (!ts.begin()) {
    Serial.println("Couldn't start touchscreen controller");
    while (1);
  }
  Serial.println("Touchscreen started");
  
  tft.begin();
  tft.fillScreen(ILI9341_WHITE);
  
  // make the logo in blue
  tft.fillCircle(LOGOX, LOGOY, LOGORADIUS, currentcolor);

  // Write text1 in blue
  tft.setCursor(30, 220);
  tft.setTextColor(currentcolor);
  tft.setTextSize(5);
  tft.println("text1");
  
  // Write text2 in black
  tft.setCursor(36, 260);
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(1);
  tft.println("text2");
  
  //delay(2000);
   
}

void loop() {
  
  // See if there's any  touch data for us
  //if (ts.bufferEmpty()) {
  //  return;
  //}
  
  // You can also wait for a touch
  if (! ts.touched()) {
    return;
  }
    
  // Retrieve a point  
  TS_Point p = ts.getPoint();
  
  // Scale from ~0->4000 to tft.width using the calibration #'s
  p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  
  
    if ((p.y < (LOGOY + LOGORADIUS)) && (p.y > (LOGOY - LOGORADIUS))) {
    

      if ((p.x < (LOGOX + LOGORADIUS)) && (p.x > (LOGOX - LOGORADIUS))) { 
        
        //oldcolor = currentcolor;

        Serial.print("p.x: ");
        Serial.println(p.x);
        Serial.print("p.y: ");
        Serial.println(p.y);

        p.x = 0;
        p.y = 0;
        Serial.print("p.x: ");
        Serial.println(p.x);
        Serial.print("p.y: ");
        Serial.println(p.y);
       
       if (increment == 0){
         currentcolor = ILI9341_BLUE;
       }
       if (increment == 1){
         currentcolor = ILI9341_RED;
       }
       if (increment == 2){
         currentcolor = ILI9341_GREEN;
       }
       if (increment == 3){
         currentcolor = ILI9341_YELLOW;
       }
       
       
       
       /*
        switch (increment) {
          case 1:
            currentcolor = ILI9341_RED;
          break;
          case 2:
            currentcolor = ILI9341_GREEN;
          break;
          case 3:
            currentcolor = ILI9341_YELLOW;
          break;
          default:
            currentcolor = ILI9341_BLUE;
          break;        
          } 
          */
        Serial.print("increment: ");
        Serial.println(increment);
       
        
  
        tft.fillCircle(LOGOX, LOGOY, LOGORADIUS, currentcolor);
        tft.setCursor(30, 220);
        tft.setTextColor(currentcolor);
        tft.setTextSize(5);
        tft.println("text1");
        tft.setCursor(36, 260);
        tft.setTextColor(ILI9341_BLACK);
        tft.setTextSize(1);
        tft.println("text2");
        increment++;
        
        if (increment == 4){
          increment = 0;
        }
      delay(1000);
      }
    }
  }
Last edited by adafruit_support_mike on Tue Jan 28, 2014 3:29 am, edited 1 time in total.
Reason: please use CODE tags when posting code

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: Touchshield v2 clear buffer

Post by adafruit »

We dont have an explict buffer flushing function *but* you can get the current buffer size with:

uint8_t Adafruit_STMPE610::bufferSize(void) {
return readRegister8(STMPE_FIFO_SIZE);
}
so you can just keep readData()'ing until the bufferSize is zero

Retxed68
 
Posts: 2
Joined: Thu May 03, 2012 4:37 pm

Re: Touchshield v2 clear buffer

Post by Retxed68 »

Thank you! I will try this and see if it solves my issue. I would like in the future to add a virtual keyboard and I was scared that because of this rebound I would have multiple times the same number when just pressing once. Hopefully this solution will solve this.

Best regards,
Chris

ewd007
 
Posts: 15
Joined: Thu Jan 09, 2014 5:38 pm

Re: Touchshield v2 clear buffer

Post by ewd007 »

I am having the same issue. I tried to use the example code you provided to create a function that clears the buffer.

I tried add the following to the code I have, but it gives me an error compiling. Error below.

Code: Select all

uint8_t Adafruit_STMPE610::bufferSize(void) {
  return readRegister8(STMPE_FIFO_SIZE);
}
ERROR:
Adafruit_STMPE610\Adafruit_STMPE610.cpp.o: In function `Adafruit_STMPE610::bufferSize()':
C:\Users\ewd0001\Documents\Arduino\libraries\Adafruit_STMPE610/Adafruit_STMPE610.cpp:142: multiple definition of `Adafruit_STMPE610::bufferSize()'
ALPHA.cpp.o:C:\Program Files (x86)\Arduino/ALPHA.ino:159: first defined here

So I thought that since it seems that it is already define that I could just use the readRegister8(STMPE_FIFO_SIZE) function to get the current buffer size but that does not work either.

User avatar
lorens
 
Posts: 10
Joined: Thu Nov 27, 2014 3:22 am

Re: Touchshield v2 clear buffer

Post by lorens »

Hi!

I have the same problem and I can't solve it. I must erase the buffer, because it the previous touched point affects in the last one.

The solution given does not work inside a loop(). Isn't it any other solution?


Thank you in advance

User avatar
killercatfish
 
Posts: 20
Joined: Sun Feb 22, 2015 9:04 am

Re: Touchshield v2 clear buffer

Post by killercatfish »

I am trying a similar method with:

Code: Select all

  while (! ts.bufferEmpty()) 
  {
    ts.readData( &p.x, &p.y, &p.z );
  }
However I am confused, because this is throwing an error that I will post below. Looking through the header files and such I am finding what seems to be inconsistencies. the ts point is created with all int16_t, but the readData function wants two uint16_t and one uint8_t. Havent really done much work at this level, so perhaps someone could explain why this is so, and if it is suppose to be this way, how do I use the readData(...) function?
Arduino: 1.6.0 (Mac OS X), Board: "Arduino Uno"

TicTacToeStruct.ino: In function 'void loop()':
TicTacToeStruct.ino:334:35: error: no matching function for call to 'Adafruit_STMPE610::readData(int16_t*, int16_t*, int16_t*)'
TicTacToeStruct.ino:334:35: note: candidate is:
In file included from TicTacToeStruct.ino:22:0:
/Applications/Arduino.app/Contents/Resources/Java/libraries/Adafruit_STMPE610-master/Adafruit_STMPE610.h:137:8: note: void Adafruit_STMPE610::readData(uint16_t*, uint16_t*, uint8_t*)
void readData(uint16_t *x, uint16_t *y, uint8_t *z);
^
/Applications/Arduino.app/Contents/Resources/Java/libraries/Adafruit_STMPE610-master/Adafruit_STMPE610.h:137:8: note: no known conversion for argument 3 from 'int16_t* {aka int*}' to 'uint8_t* {aka unsigned char*}'
Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

User avatar
killercatfish
 
Posts: 20
Joined: Sun Feb 22, 2015 9:04 am

Re: Touchshield v2 clear buffer

Post by killercatfish »

SOLVED.

Code: Select all

TS_Point p=ts.getPoint();
while(ts.touched())
 p= ts.getPoint();
this way, you will get the last point you touched before release. You can change your mind until you "untouch" the screen.

User avatar
Nactan
 
Posts: 1
Joined: Sun Mar 01, 2015 10:40 pm

Re: Touchshield v2 clear buffer

Post by Nactan »

I've had a similar issue but with the buffer retaining the last touch point regardless of using the while(ts.touched()) method. So clicking a button works fine until you want to touch another location or button on the screen. Instead of running the function for the second button pressed, the program will run the code for the previous button. Then on the next touch it will run the second button.

User avatar
lorens
 
Posts: 10
Joined: Thu Nov 27, 2014 3:22 am

Re: Touchshield v2 clear buffer

Post by lorens »

This finally works for me. It takes te point you want, makes the mapping and if the point is between the selected values, puts the screen in blue.

if (ts.touched())
{
TS_Point p = ts.getPoint();
while ( ! ts.bufferEmpty() )
{
p = ts.getPoint();
}

p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());

int y = tft.height() - p.x;
int x = p.y;

if((x >= 45) && (x <= 265)) //
{
if ((y >= 134) && (y <= 189))
{
tft.fillScreen(ILI9341_BLUE);
}
}
}

User avatar
ajadan
 
Posts: 1
Joined: Thu May 05, 2016 7:06 am

Re: Touchshield v2 clear buffer

Post by ajadan »

The following worked for me:
if (ts.touched())
{
TS_Point p = ts.getPoint();
while (!ts.bufferEmpty() || ts.touched())
{
p = ts.getPoint();
}//while ( ! ts.bufferEmpty() )

y_coor = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
x_coor = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0);
p.x = x_coor;
p.y = y_coor;
}

User avatar
cmartu
 
Posts: 1
Joined: Sun Apr 26, 2015 11:23 am

Re: Touchshield v2 clear buffer

Post by cmartu »

adafruit wrote:We dont have an explict buffer flushing function *but* you can get the current buffer size with:

uint8_t Adafruit_STMPE610::bufferSize(void) {
return readRegister8(STMPE_FIFO_SIZE);
}
so you can just keep readData()'ing until the bufferSize is zero

Hi adafruit,

I am looking for a list with the functions provided by the Libraries recommended by you for this display: Adafruit_ILI9341, Adafruit_STMPE610, Adafruit_GFX.
The only thing I was able to find in this mater was the tutorial for the GFX library on your site, what is describing just a small number of functions in this particular library.

Please point me to the right place to find a complete list of functions that can be used and some details about it.
This would be a great help for me and everybody here who tries to work with this display.
Thanks

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Touchshield v2 clear buffer

Post by Franklin97355 »

There is no official documentation but the functions are prototyped in the .h files for the libraries so you can look at them there.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”