user10977: I am trying to write some code to make it easy to add different buttons to a touch screen. I need each object to be a different object of class 'touchButton'. I'd like to be able to run a touchButton.Draw function, which in turn will run LCD library commands. Is there a way I could do that? Here is my source code:
TouchScreen.ino:
#include "touchButton.h"
#include <UTFT.h>
#include <UTouch.h>
#include <avr/pgmspace.h>
UTFT    myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);
extern uint8_t SmallFont[];

void setup() {
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myGLCD.fillScr(51, 51, 51);
  //what I want to be able to do:
  //touchButton Button1 (10, 10, 20, 20, 'p');
  //Button1.Draw();
}

void loop() {
}

touchButton.h
#ifndef touchButton_h
#define touchButton_h
#include "Arduino.h"
class touchButton {
  public:
    touchButton(int x, int y, int xs, int ys, char use);
    void Draw();
    int x1;
    int y1;
    int xsize;
    int ysize;
    char use;
    boolean toggle = false;
};
#endif

touchButton.cpp
  #include "Arduino.h"
  #include "touchButton.h"
  touchButton::touchButton(int x, int y, int xs, int ys, char myuse) {
    x1 = x;
    y1 = y;
    xsize = xs;
    ysize = ys;
    use = myuse;
  }

  void touchButton::Draw() {
    switch (use) {
      case 'p':
      //this is what I would like to have called from the main file.
      //myGLCD.drawRoundRect(x1, y1, x1+xsize, y1+ysize);
        break;

       default:
        break;
    }
  }


user4143: The best way is to provide the myGLCD object as a parameter to the constructor:
touchButton::touchButton(UTFT &glcddev, int x, int y, int xs, int ys, char myuse) {
  x1 = x;
  y1 = y;
  xsize = xs;
  ysize = ys;
  use = myuse;
  glcd = &glcddev;
}

Then in your header you provide a:
private: 
  UTFT *glcd;

Your library code then uses the glcd pointer to the object:
glcd->drawRoundedRectangle(...);

Note the use of -> since glcd is a pointer to the object (as created by the & operator when it was assigned in the constructor).
Of course, as Miagma mentions, you need to ensure that anywhere that references any UTFT functions or objects has the UTFT.h header included or it won't know what you're talking about.
You can also do the same with the UTouch class - pass that object as a parameter, then each widget can have its own routine to deal with the touch events.
You can see all this kind of thing in action in the same context in the Widget class of my DisplayCore system for chipKIT boards:  

https://github.com/MajenkoLibraries/DisplayCore/blob/master/DisplayCore/DisplayCore.h#L464
https://github.com/MajenkoLibraries/DisplayCore/blob/master/DisplayCore/DisplayCore.cpp#L1587


user13432: The simplest solution (ignoring possible improvements) is to add the following to touchButton.cpp.
#include "Arduino.h"
#include "touchButton.h"

// Extern reference to the LCD instance
#include <UTFT.h>
extern UTFT myGLCD;

touchButton::touchButton(int x, int y, int xs, int ys, char myuse) {
  x1 = x;
  y1 = y;
  xsize = xs;
  ysize = ys;
  use = myuse;
}

void touchButton::Draw() {
  switch (use) {
    case 'p':
      myGLCD.drawRoundRect(x1, y1, x1+xsize, y1+ysize);
    break;

   default:
    break;
  }
}

There are several improvements such as passing the UTFT instance reference as parameter, moving myGLCD to a separate .h/.cpp file, etc. 
An example of "extern" object reference may be found in HardwareSerial.h. 
Cheers!

10977: <p>I am trying to write some code to make it easy to add different buttons to a touch screen. I need each object to be a different object of class 'touchButton'. I'd like to be able to run a <code>touchButton.Draw</code> function, which in turn will run LCD library commands. Is there a way I could do that? Here is my source code:</p>

<p>TouchScreen.ino:</p>

<pre><code>#include "touchButton.h"
#include &lt;UTFT.h&gt;
#include &lt;UTouch.h&gt;
#include &lt;avr/pgmspace.h&gt;
UTFT    myGLCD(ITDB32S, 38, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);
extern uint8_t SmallFont[];

void setup() {
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myGLCD.fillScr(51, 51, 51);
  //what I want to be able to do:
  //touchButton Button1 (10, 10, 20, 20, 'p');
  //Button1.Draw();
}

void loop() {
}
</code></pre>

<p>touchButton.h</p>

<pre><code>#ifndef touchButton_h
#define touchButton_h
#include "Arduino.h"
class touchButton {
  public:
    touchButton(int x, int y, int xs, int ys, char use);
    void Draw();
    int x1;
    int y1;
    int xsize;
    int ysize;
    char use;
    boolean toggle = false;
};
#endif
</code></pre>

<p>touchButton.cpp</p>

<pre><code>  #include "Arduino.h"
  #include "touchButton.h"
  touchButton::touchButton(int x, int y, int xs, int ys, char myuse) {
    x1 = x;
    y1 = y;
    xsize = xs;
    ysize = ys;
    use = myuse;
  }

  void touchButton::Draw() {
    switch (use) {
      case 'p':
      //this is what I would like to have called from the main file.
      //myGLCD.drawRoundRect(x1, y1, x1+xsize, y1+ysize);
        break;

       default:
        break;
    }
  }
</code></pre>
 4143: <p>The best way is to provide the myGLCD object as a parameter to the constructor:</p>

<pre><code>touchButton::touchButton(UTFT &amp;glcddev, int x, int y, int xs, int ys, char myuse) {
  x1 = x;
  y1 = y;
  xsize = xs;
  ysize = ys;
  use = myuse;
  glcd = &amp;glcddev;
}
</code></pre>

<p>Then in your header you provide a:</p>

<pre><code>private: 
  UTFT *glcd;
</code></pre>

<p>Your library code then uses the glcd pointer to the object:</p>

<pre><code>glcd-&gt;drawRoundedRectangle(...);
</code></pre>

<p>Note the use of <code>-&gt;</code> since <code>glcd</code> is a <em>pointer</em> to the object (as created by the <code>&amp;</code> operator when it was assigned in the constructor).</p>

<p>Of course, as Miagma mentions, you need to ensure that anywhere that references any UTFT functions or objects has the UTFT.h header included or it won't know what you're talking about.</p>

<p>You can also do the same with the UTouch class - pass that object as a parameter, then each widget can have its own routine to deal with the touch events.</p>

<p>You can see all this kind of thing in action in the same context in the Widget class of my DisplayCore system for chipKIT boards:  </p>

<ul>
<li><a href="https://github.com/MajenkoLibraries/DisplayCore/blob/master/DisplayCore/DisplayCore.h#L464" rel="nofollow">https://github.com/MajenkoLibraries/DisplayCore/blob/master/DisplayCore/DisplayCore.h#L464</a></li>
<li><a href="https://github.com/MajenkoLibraries/DisplayCore/blob/master/DisplayCore/DisplayCore.cpp#L1587" rel="nofollow">https://github.com/MajenkoLibraries/DisplayCore/blob/master/DisplayCore/DisplayCore.cpp#L1587</a></li>
</ul>
 13432: <p>The simplest solution (ignoring possible improvements) is to add the following to touchButton.cpp.</p>

<pre><code>#include "Arduino.h"
#include "touchButton.h"

// Extern reference to the LCD instance
#include &lt;UTFT.h&gt;
extern UTFT myGLCD;

touchButton::touchButton(int x, int y, int xs, int ys, char myuse) {
  x1 = x;
  y1 = y;
  xsize = xs;
  ysize = ys;
  use = myuse;
}

void touchButton::Draw() {
  switch (use) {
    case 'p':
      myGLCD.drawRoundRect(x1, y1, x1+xsize, y1+ysize);
    break;

   default:
    break;
  }
}
</code></pre>

<p>There are several improvements such as passing the UTFT instance reference as parameter, moving myGLCD to a separate .h/.cpp file, etc. </p>

<p>An example of "extern" object reference may be found in <a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h#L143" rel="nofollow">HardwareSerial.h</a>. </p>

<p>Cheers!</p>
