adafruit / Adafruit_STMPE610

Arduino library for STMPE610/811 resistive touch screen controllers
MIT License
38 stars 32 forks source link

getPoint doesn't return the last pressure #10

Closed zencuke closed 5 years ago

zencuke commented 7 years ago

getPoint appears to return the second to last value. For example if you press hard the value may go down below 10. If you release slowly the value increases and can get higher than 135. However if you release suddenly successive calls keep returning 12 as if there is still contact. I see this with the TFT featherwing 3315. I've tried several of them.

This pretty much makes the pressure value useless.

Below is the code I used to show the problem. Hacked from the touchpaint_featherwing demo:

TP_Point old;
void loop() {
  // Retrieve a point  
  TS_Point p = ts.getPoint();
  int change = 0;

  if ( p.z != old.z ) {
    // Don't clutter the screen if nothing changes.
    old = p;
    Serial.print("\tPressure = "); Serial.println(p.z);  
  }
}
zencuke commented 7 years ago

It may be that the values are not updated in the controller if the controller doesn't detect a touch. If so a work around to getPoint() would be to check the touched state first and and return a canned result (z = 255?) if there is no touch. Of course the user could do this if the behavior was documented so maybe the correct fix is to update the documentation?

alloro commented 7 years ago

See solve via pr

jrullan commented 6 years ago

Just adding my 2 cents here,

I just started working with the TFT Feather and for the first time working with this STMPE610 GPIO expander chipset. I struggled for 3 days with this issue (and another one) that it sometimes returned a copy of the previous to last pressed coordinates instead of the last one, causing my project to misbehave. After reading the datasheet for the STMPE610, it states that you should "continue to read until the FIFO buffer size is below the threshold size...". Upon checking the implementation of this driver, in the begin() method you find that the threshold was set to 1. So ideally upon only one read it will return the last pressed data, but it wasn't. So my solution was to modify the getPoint() method directly and ensure that I read the data until the buffer was empty. This way I always get the last pressed location when calling getPoint() making it much more easier (and deterministic) to integrate it into my project.

Just modify the Adafruit_STMPE610::getPoint() method as follows:

TS_Point Adafruit_STMPE610::getPoint(void) {
    uint16_t x, y;
    uint8_t z;

    /* Making sure that we are reading all data before leaving */
    while(!bufferEmpty()){
        readData(&x,&y,&z);
    }

    if (bufferEmpty()) 
        writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints

    return TS_Point(x, y, z);
}

and then modify this one as follows to comment out the check for buffer empty:

void Adafruit_STMPE610::readData(uint16_t *x, uint16_t *y, uint8_t *z) {
  uint8_t data[4];

  for (uint8_t i=0; i<4; i++) {
    data[i] = readRegister8(0xD7); 
  }

  *x = data[0];
  *x <<= 4;
  *x |= (data[1] >> 4);
  *y = data[1] & 0x0F; 
  *y <<= 8;
  *y |= data[2]; 
  *z = data[3];

  /* Commented these lines because we moved them to getPoint() */
  //if (bufferEmpty())
    //writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints
}
karenerobinson commented 5 years ago

Thanks @jrullan , your fix solved my problem.
hi @ladyada , consider making this change if this repo is active.

ladyada commented 5 years ago

@karenerobinson hiya karen yep we're in the process of doing a sweep update of this library, we'll probably look at this issue in the next week or two

hoffmannjan commented 5 years ago

works fine for me, added to #15

hoffmannjan commented 5 years ago

Please let's try the latest code, It's integrated. Good work @karenerobinson 👍 .

zencuke commented 5 years ago

Jan,

I try. :-) I's hard to remenber but I'm pretty sure it was the latest code when I reported it six months ago. My feather ttf display had just arrived and I needed the library to run the demos. Since then I haven't used that display. I've beeneaiting for this to close.

-steve

zencuke commented 5 years ago

Jan,

Thanks, It is good to hear that it works now. I'll check my current version, update to library latest, then rerun my test.

About "latest code: Thanks for the reminder. I do try ;-) but I'm pretty sure I was running current code when I reported it 18 months ago. There is a time gap in functional edits to this file between 5 years ago and 11 days ago. I haven't proven it but if it works now it was likely fixed in the last 11 days.

-steve (aka zen2cuke)

karenerobinson commented 5 years ago

these libraries are fantastic, for the record the new version works great