Problems creating a library

Somehow the Touch library uses the UTFT library

Most likely because IT, not the sketch, creates the required instance.

PaulS:

Somehow the Touch library uses the UTFT library

Most likely because IT, not the sketch, creates the required instance.

If I initialise it in the library then how do I use it in the sketch and vice versa?

Sorry, I've not done this before.

If I initialise it in the library then how do I use it in the sketch

You provide a method to get the GLCD instance.

and vice versa?

You provide a method to pass the instance (by reference, probably) to your class instance.

PaulS:

If I initialise it in the library then how do I use it in the sketch

You provide a method to get the GLCD instance.

and vice versa?

You provide a method to pass the instance (by reference, probably) to your class instance.

Paul,

Sorry I don't understand the answer.

Do you mean that I have to create a corresponding function for every function in the other library?

Do you mean that I have to create a corresponding function for every function in the other library?

No. You simply have to make your class aware that there is a an instance of a class, somewhere, that can do some work.

Add

GLCD &aGLCD;

to your header file.

Add a method

void setGLCDInstance(GLCD &theGLCD)
{
   aGCLD = theGLCD;
}

to your header file.

In the sketch, call that method:

myTFTLib.setGLCDInstance(myGLCD);

Then, in your source file, use aGLCD instead of myGLCD.

Thanks Paul, I appreciate all the help but can I just check a few things...

  1. add "GLCD &aGLCD" to my header file? >>> Is this the library .h file? If so where do I add it?

  2. you say add a method:

void setGLCDInstance(GLCD &theGLCD)
{
   aGCLD = theGLCD;
}

to the header file?

Do you mean to the .cpp file? as per the other functions i.e.

void TFTlib::setGLCDInstance(GLCD &theGLCD){
   aGCLD = theGLCD;
}

with the corresponding entry in the .h file

I think I've got the rest.

add "GLCD &aGLCD" to my header file? >>> Is this the library .h file?

Yes.

If so where do I add it?

In the private: section.

you say add a method:

Code:

void setGLCDInstance(GLCD &theGLCD)
{
aGCLD = theGLCD;
}

to the header file?

Do you mean to the .cpp file? as per the other functions i.e.

You can split the code into declaration (in the header file) and implementation (in the source file) if you want. Or, you can put it all in the header file. For a one line function, I don't see the advantage of separating the declaration and implementation. But, if you do, that's fine.

Paul,

It's looking nearer and nearer.....

I just have this now:

TFTlib.cpp: In constructor 'TFTlib::TFTlib()':
TFTlib.cpp:11: error: uninitialized reference member 'TFTlib::aUTFT'
TFTlib.cpp:11: error: uninitialized reference member 'TFTlib::aUTouch'

  1. I added:

UTFT &aUTFT;
UTouch &aUTouch;

into the public declarations in the .h file.

  1. I added (This is the only way I know to do it):

void TFTlib::setGLCDInstance(UTFT &theUTFT){
aUTFT = theUTFT;
}

void TFTlib::setTouchInstance(UTouch &theTouch){
aUTouch = theTouch;
}

in the .cpp file with the corresponding entries in the public section of the .h file

  1. I've changed all my references to myGLCD to aUTFT and myTouch to aUTouch

I'm confused. I thought we were trying to make the class aware of myGLCD. That is not an instance of UTFT or UTouch, is it?

PaulS:
I'm confused. I thought we were trying to make the class aware of myGLCD. That is not an instance of UTFT or UTouch, is it?

Sorry I mixed it up, I changed the name aGLCD to aUTFT.... It is changed back now so it is aGLCD

So the error is:

TFTlib.cpp: In constructor 'TFTlib::TFTlib()':
TFTlib.cpp:11: error: uninitialized reference member 'TFTlib::aGLCD'
TFTlib.cpp:11: error: uninitialized reference member 'TFTlib::aUTouch'

The extra line is because I need to do the same with the UTouch library i.e.:

void TFTlib::setGLCDInstance(UTFT &theUTFT){
   aGLCD = theUTFT;
}

void TFTlib::setTouchInstance(UTouch &theTouch){
   aUTouch = theTouch;
}

OK. You might be able to set the reference to NULL, until the proper method is called.

Or, change the references to pointers. That requires a different way of dereferencing (aGLCD-> instead of aGLCD.).

PaulS:
OK. You might be able to set the reference to NULL, until the proper method is called.

Or, change the references to pointers. That requires a different way of dereferencing (aGLCD-> instead of aGLCD.).

Paul,

OK thanks, that makes sense but I don't actually know how to do either.....

Header:

GLCD *pGLCD;

void setGLCDInstance(GLCD *pSomeInstance);

Source:

void setGLCDInstance(GLCD *pSomeInstance)
{
   pGLCD = pSomeInstance;
}

Sketch:

myTFTLib.setGLCDInstance(&myGLCD);

Source methods:

pGCLD->MakeItDoSomething();

Thanks Paul,

  1. I've added:

myTFTlin.setGLCDInstance(&myGLCD);
myTFTlin.setTouchInstance(&myTouch);

to the sketch's setup() function.

  1. I've changed the .h files as follows:
#ifndef TFTlib_h
#define TFTlib_h

#include "Arduino.h"
#include "SPI.h"
#include <UTFT.h>
#include <UTouch.h>

#define maxbuttons 25
#define stateDrawn 1
#define statePressed 2

class TFTlib{
  public:
    TFTlib();
    void setGLCDInstance(UTFT *pSomeInstance);
    void setTouchInstance(UTouch *pSomeInstance);
    void addButton(int buttonnumber,word x,word y,word xs,word ys,long colour,long borcolour,long textcolour,long presscolour,long presstextcolour,byte borwidth,String top,int xo,int yo);
    void drawButton(int buttonnumber);
    void drawButton_pressed(int buttonnumber);
    bool checkButton(word buttonnumber);
    int GUIbutton_x[maxbuttons];
    int GUIbutton_y[maxbuttons];
    int GUIbutton_xs[maxbuttons];
    int GUIbutton_ys[maxbuttons];
    int GUIbutton_xo[maxbuttons];
    int GUIbutton_yo[maxbuttons];
    long GUIbutton_colour[maxbuttons];
    long GUIbutton_borcolour[maxbuttons];
    long GUIbutton_textcolour[maxbuttons];
    long GUIbutton_pressedcolour[maxbuttons];
    long GUIbutton_pressedtextcolour[maxbuttons];
    int GUIbutton_borwidth[maxbuttons];
    String GUIbutton_top[maxbuttons];
    byte GUIbutton_currentstate[maxbuttons];
    UTFT    *pGLCD;
    UTouch  *pUTouch;

};

#endif
  1. I've changed the functions in the .cpp file:
void TFTlib::setGLCDInstance(UTFT *pSomeInstance){
   pGLCD = pSomeInstance;
}

void TFTlib::setTouchInstance(UTouch *pSomeInstance){
   pUTouch = pSomeInstance;
}

Re: Source methods: "pGCLD->MakeItDoSomething();"

Sorry I don't understand this.

&

What do I do with the functions i.e.:

aGLCD.setColor((GUIbutton_pressedcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_pressedcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_pressedcolour[buttonnumber]&0xFF));
aGLCD.fillRoundRect (GUIbutton_x[buttonnumber],GUIbutton_y[buttonnumber], GUIbutton_x[buttonnumber]+GUIbutton_xs[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_ys[buttonnumber]);

or

int current_x = aUTouch.getX();

Re: Source methods: "pGCLD->MakeItDoSomething();"

Sorry I don't understand this.

&

What do I do with the functions i.e.:

aGLCD.setColor((GUIbutton_pressedcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_pressedcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_pressedcolour[buttonnumber]&0xFF));

setColor is a way to make the aGLCD instance do something. aGLCD. becomes pGLCD->:

  pGLCD->setColor((GUIbutton_pressedcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_pressedcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_pressedcolour[buttonnumber]&0xFF));

Thanks, changed the entries but I'm now getting this error:

UI_lib_TEST.ino: In function 'void setup()':

GUI_lib_TEST:20: error: 'class TFTlib' has no member named 'setGLCDInstance'
GUI_lib_TEST:21: error: 'class TFTlib' has no member named 'setTouchInstance'

in relation to the lines:

myTFTlib.setGLCDInstance(&myGLCD);
myTFTlib.setTouchInstance(&myTouch);

if I remark out the two lines I get numerous errors:

/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp: In member function 'void TFTlib::drawButton(int)':
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp:30: error: 'myGLCD' was not declared in this scope
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp:30: error: 'BigFont' was not declared in this scope
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp: In member function 'void TFTlib::drawButton_pressed(int)':
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp:45: error: 'myGLCD' was not declared in this scope
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp:45: error: 'BigFont' was not declared in this scope
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp: In member function 'bool TFTlib::checkButton(word)':
/Users/darren/Documents/Arduino/libraries/TFTlib/TFTlib.cpp:59: error: 'myTouch' was not declared in this scope

This is the .cpp file:

#include "SPI.h"
#include <UTFT.h>
#include <UTouch.h>
#include "TFTlib.h"
extern uint8_t BigFont[];


TFTlib::TFTlib(){
  for (byte i = 0; i < maxbuttons; i++) {GUIbutton_currentstate[i]=false;} 
}

void TFTlib::setGLCDInstance(UTFT *pSomeInstance){
   pGLCD = pSomeInstance;
}

void TFTlib::setTouchInstance(UTouch *pSomeInstance){
   pUTouch = pSomeInstance;
}

void TFTlib::addButton(int buttonnumber,word x,word y,word xs,word ys,long colour,long borcolour,long textcolour,long presscolour,long presstextcolour,byte borwidth,String top,int xo,int yo){
  GUIbutton_x[buttonnumber]=x;
  GUIbutton_y[buttonnumber]=y;
  GUIbutton_xs[buttonnumber]=xs;
  GUIbutton_ys[buttonnumber]=ys;
  GUIbutton_xo[buttonnumber]=xo;
  GUIbutton_yo[buttonnumber]=yo;
  GUIbutton_colour[buttonnumber]=colour;
  GUIbutton_borcolour[buttonnumber]=colour;
  GUIbutton_textcolour[buttonnumber]=textcolour;
  GUIbutton_pressedcolour[buttonnumber]=presscolour;
  GUIbutton_pressedtextcolour[buttonnumber]=presstextcolour;
  GUIbutton_borwidth[buttonnumber]=borwidth;
  GUIbutton_top[buttonnumber]=top;
}

void TFTlib::drawButton(int buttonnumber){
  if (GUIbutton_currentstate[buttonnumber]!=stateDrawn){
    GUIbutton_currentstate[buttonnumber]=stateDrawn;
    pGLCD->setColor((GUIbutton_colour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_colour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_colour[buttonnumber]&0xFF));
    pGLCD->fillRoundRect (GUIbutton_x[buttonnumber],GUIbutton_y[buttonnumber], GUIbutton_x[buttonnumber]+GUIbutton_xs[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_ys[buttonnumber]);
    pGLCD->setColor((GUIbutton_borcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_borcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_borcolour[buttonnumber]&0xFF));
    pGLCD->drawRoundRect (GUIbutton_x[buttonnumber],GUIbutton_y[buttonnumber],GUIbutton_x[buttonnumber]+GUIbutton_xs[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_ys[buttonnumber]);
    pGLCD->setColor((GUIbutton_textcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_textcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_textcolour[buttonnumber]&0xFF));
    pGLCD->setBackColor((GUIbutton_colour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_colour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_colour[buttonnumber]&0xFF));
    pGLCD->print(GUIbutton_top[buttonnumber],GUIbutton_x[buttonnumber]+GUIbutton_xo[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_yo[buttonnumber]);
    pGLCD->setBackColor (0, 0, 0);
  }
}

void TFTlib::drawButton_pressed(int buttonnumber){
  if (GUIbutton_currentstate[buttonnumber]!=statePressed){
    GUIbutton_currentstate[buttonnumber]=statePressed;
    pGLCD->setColor((GUIbutton_pressedcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_pressedcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_pressedcolour[buttonnumber]&0xFF));
    pGLCD->fillRoundRect (GUIbutton_x[buttonnumber],GUIbutton_y[buttonnumber], GUIbutton_x[buttonnumber]+GUIbutton_xs[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_ys[buttonnumber]);
    pGLCD->setColor((GUIbutton_borcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_borcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_borcolour[buttonnumber]&0xFF));
    pGLCD->drawRoundRect (GUIbutton_x[buttonnumber],GUIbutton_y[buttonnumber],GUIbutton_x[buttonnumber]+GUIbutton_xs[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_ys[buttonnumber]);
    pGLCD->setColor((GUIbutton_pressedtextcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_pressedtextcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_pressedtextcolour[buttonnumber]&0xFF));
    pGLCD->setBackColor((GUIbutton_pressedcolour[buttonnumber]&0xFF0000)/0x10000,(GUIbutton_pressedcolour[buttonnumber]&0xFFFF)/0x100,(GUIbutton_pressedcolour[buttonnumber]&0xFF));
    pGLCD->print(GUIbutton_top[buttonnumber],GUIbutton_x[buttonnumber]+GUIbutton_xo[buttonnumber],GUIbutton_y[buttonnumber]+GUIbutton_yo[buttonnumber]);
    pGLCD->setBackColor (0, 0, 0);
  }
}

bool TFTlib::checkButton(word buttonnumber){
  int current_x = pUTouch->getX();
  int current_y = pUTouch->getY();
  if ( (current_x>GUIbutton_x[buttonnumber]) && (current_y>GUIbutton_y[buttonnumber]) && (current_x<(GUIbutton_x[buttonnumber]+GUIbutton_xs[buttonnumber])) 
       && (current_y<(GUIbutton_y[buttonnumber]+GUIbutton_ys[buttonnumber])) ){ return true; } else { return false;}
}

This is the .h file:

#ifndef TFTlib_h
#define TFTlib_h

#include "Arduino.h"
#include "SPI.h"
#include <UTFT.h>
#include <UTouch.h>

#define maxbuttons 25
#define stateDrawn 1
#define statePressed 2



class TFTlib{
  public:
    TFTlib();
    void setGLCDInstance(UTFT *pSomeInstance);
    void setTouchInstance(UTouch *pSomeInstance);
    void addButton(int buttonnumber,word x,word y,word xs,word ys,long colour,long borcolour,long textcolour,long presscolour,long presstextcolour,byte borwidth,String top,int xo,int yo);
    void drawButton(int buttonnumber);
    void drawButton_pressed(int buttonnumber);
    bool checkButton(word buttonnumber);
    int GUIbutton_x[maxbuttons];
    int GUIbutton_y[maxbuttons];
    int GUIbutton_xs[maxbuttons];
    int GUIbutton_ys[maxbuttons];
    int GUIbutton_xo[maxbuttons];
    int GUIbutton_yo[maxbuttons];
    long GUIbutton_colour[maxbuttons];
    long GUIbutton_borcolour[maxbuttons];
    long GUIbutton_textcolour[maxbuttons];
    long GUIbutton_pressedcolour[maxbuttons];
    long GUIbutton_pressedtextcolour[maxbuttons];
    int GUIbutton_borwidth[maxbuttons];
    String GUIbutton_top[maxbuttons];
    byte GUIbutton_currentstate[maxbuttons];
    UTFT    *pGLCD;
    UTouch  *pUTouch;

};

#endif

and the sketch:

#include "SPI.h"
#include <UTFT.h>
#include <UTouch.h>
#include <TFTlib.h>

#define FLASH_CS_PIN 52 
#define SPI_RATE 2

UTFT myGLCD(CTE70); 
UTouch myTouch(6,5,32,3,2);
TFTlib myTFTlib;

// Declare which fonts we will be using
extern uint8_t BigFont[];

void setup(){ 
  delay(500);
  Serial.begin(115200);
  //myTFTlib.setGLCDInstance(&myGLCD);
  //myTFTlib.setTouchInstance(&myTouch);
  
  myTFTlib.addButton(1,200,200,75,50,0x0000FF,0xFF0000,0xFFFFFF,0xFF0000,0xFFFFFF,2,"test",5,15);
  myTFTlib.drawButton(1);
  myTFTlib.addButton(2,400,200,100,50,0x0000FF,0xFF0000,0xFFFFFF,0xFF0000,0xFFFFFF,2,"hello",10,15);
  myTFTlib.drawButton(2);
}


void loop(){

  long x, y;
  String p;
  delay(10);
  myTouch.read();
  x = myTouch.getX();
  y = myTouch.getY();
  String test_string = String(x)+" , "+String(y) + "                            ";
  myGLCD.setColor(255, 255, 0);
  myGLCD.Put_Text(test_string,0,115,50);
  if (myTFTlib.checkButton(1)){ myTFTlib.drawButton_pressed(1); } else { myTFTlib.drawButton(1);}
  if (myTFTlib.checkButton(2)){ myTFTlib.drawButton_pressed(2); } else { myTFTlib.drawButton(2);}
  
}

Question: according to the CTE shield schematic, TP_DIN is on pen 4, and is shared with the SD_CS function. But everyone uses pen 32, Which is labeled DB8. As shown in your sketch UTouch myTouch(6,5,32,3,2);. Shouldn't it read UTouch myTouch(6,5,4,3,2); instead, or is there a problem with my schematic.

The data in function to the touchscreen controller may not be used much but it has a purpose. If you set it up using the wrong pin it may functionally be correct until you need to write a command to the touch controller. Then a problem will show up. If you're going to adapt the drivers you should use the correct pin numbers.

promacjoe.

promacjoe:
Question: according to the CTE shield schematic, TP_DIN is on pen 4, and is shared with the SD_CS function. But everyone uses pen 32, Which is labeled DB8. As shown in your sketch UTouch myTouch(6,5,32,3,2);. Shouldn't it read UTouch myTouch(6,5,4,3,2); instead, or is there a problem with my schematic.

The data in function to the touchscreen controller may not be used much but it has a purpose. If you set it up using the wrong pin it may functionally be correct until you need to write a command to the touch controller. Then a problem will show up. If you're going to adapt the drivers you should use the correct pin numbers.

promacjoe.

The example sketch used the same pin connections. I am using a due if this makes any difference, the touch functionality is working correctly with my connections. I actually checked them all with the meter previously ready to use Eagle to complete the entire board. ... I'm just using the shield whilst writing the initial software.