Help understanding touch screen code - basic questions.

Hi all,

Bit of a noob here and I'm having trouble understanding some bits of code for the adafruit TFT touch screen shield for arduino. The first bit of code can be found here: Arduin-o-Phone Sketch | Arduin-o-Phone | Adafruit Learning System

What I don't understand is this part here:

    void loop(void) {
      TS_Point p;
      
      if (ts.bufferSize()) {
        p = ts.getPoint(); 
      } else {
        // this is our way of tracking touch 'release'!
        p.x = p.y = p.z = -1;
      }
      
      // Scale from ~0->4000 to tft.width using the calibration #'s
      if (p.z != -1) {
        p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
        p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
        Serial.print("("); Serial.print(p.x); Serial.print(", "); 
        Serial.print(p.y); Serial.print(", "); 
        Serial.print(p.z); Serial.println(") ");
      }

What exactly is the 2nd line? TS_Point p;

Is that some sort of definition or initialization of p? Then, as I see it, p is assigned some properties in the line p = ts.getPoint();

is that right?

The second bit of code I'm having problems with is from adafruit's library for this screen.

void loop() {
  uint16_t x, y;
  uint8_t z;
  if (touch.touched()) {
    // read x & y & z;
    while (! touch.bufferEmpty()) {
      Serial.print(touch.bufferSize());
      touch.readData(&x, &y, &z);
      Serial.print("->("); 
      Serial.print(x); Serial.print(", "); 
      Serial.print(y); Serial.print(", "); 
      Serial.print(z);
      Serial.println(")");
    }
    touch.writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints
  }
  delay(10);
}

In this example, it's the touch.readData(&x, &y, &z);
line that I am a bit confused about. The '&' is a reference I found out on google, but the first search I did showed some confusion between a reference vs pointer usage. A link to a good description would be helpful if anyone has one, but I will google it further....

Thanks for any help,
Randy

You pass the variables x, y, z by reference to the function readData(). The function modifies those variables and the new values are then available in the sketch after the function returns. If there was only one item of data required it would be more common to return the value from the function than pass it a variable by reference but that's not so easy when there are three data to return.

See the "Arguments passed by value and by reference" section of:
http://www.cplusplus.com/doc/tutorial/functions/

Thanks pert for the explanation and link! Useful reading in that link!

But I'm still looking for an answer to my first example.

 void loop(void) {
      TS_Point p;
     
      if (ts.bufferSize()) {
        p = ts.getPoint();
      } else {
        // this is our way of tracking touch 'release'!
        p.x = p.y = p.z = -1;
      }
     
      // Scale from ~0->4000 to tft.width using the calibration #'s
      if (p.z != -1) {
        p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
        p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
        Serial.print("("); Serial.print(p.x); Serial.print(", ");
        Serial.print(p.y); Serial.print(", ");
        Serial.print(p.z); Serial.println(") ");
      }

What exactly is the 2nd line? TS_Point p;

Is that some sort of definition or initialization of p? Then, as I see it, p is assigned some properties in the line p = ts.getPoint();

is that right?

Thanks
Randy

2nd line defines (but not initialises) p as a variable of type TS_point which is presumably defined in some library somewhere that you haven't shown.

Steve