Subclassing the TFTLCD library

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
rna
 
Posts: 20
Joined: Fri Mar 18, 2011 9:54 pm

Subclassing the TFTLCD library

Post by rna »

If this is the wrong place to ask, please redirect me, (and thkx).
Up until now, I have added my own project functionality to my copy of the TFTLCD library, (for the 2.8" TFT LCD with touchscreen breakout board).
I would like to do this the right way, (by subclassing the TFTLCD library)?
The idea is to initialize the TFTLCD library in the normal manner, (setup function), and then have my library of routines be able to call any TFTLCD methods. i.e.: My library might call several TFTLCD methods to construct a "window" or a shape on the screen.

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Subclassing the TFTLCD library

Post by adafruit_support_bill »

I would like to do this the right way, (by subclassing the TFTLCD library)?
Subclassing is a reasonable way to do that.

User avatar
rna
 
Posts: 20
Joined: Fri Mar 18, 2011 9:54 pm

Re: Subclassing the TFTLCD library

Post by rna »

Sorry, I never actually asked the question.

I have constructed an "Icon" class, (class Icons : public TFTLCD { ... }), and don't know how to proceed.
Do I call TFTLCD methods from the .CPP file with the tft. prefix, (i.e.: tft.drawTriangle())?
I need to know where to read about these details.
So far...
// Icons.h file:
class Icons : public TFTLCD {
public:
void constructor (void);

void Arrows (int which, uint16_t x, uint16_t y, uint16_t color);
void Display (uint16_t x, uint16_t y, uint16_t color);
void Home (uint16_t x, uint16_t y, uint16_t color);
void Left (uint16_t x, uint16_t y, uint16_t color);
void Menu (uint16_t x, uint16_t y, uint16_t color);
void Search (uint16_t x, uint16_t y, uint16_t color);
};

// Icons.cpp file:
#include "Icons.h"

void Icons::constructor (void) { // not yet determined }

void Icons::Display (uint16_t x, uint16_t y, uint16_t color) {
Left(x+0, y+5, color);
Menu(x+20, y, color);
Home(x+40, y, color);
Search(x+60, y, color);
}

void Icons::Home (uint16_t x, uint16_t y, uint16_t color) {
drawTriangle(x, y+7, x+14, y+7, x+7, y, color); // Roof.
drawHorizontalLine(x+10, y+1, 3, color); // Chimney top.
drawVerticalLine( x+10, y+2, 1, color); // Chimney left.
drawVerticalLine( x+12, y+2, 3, color); // Chimney right.
drawRect(x+2, y+7, 11, 6, color); // Walls and floor.
drawRect(x+6, y+9, 3, 4, color); // Door.
}

// etc

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Subclassing the TFTLCD library

Post by adafruit_support_bill »

If you are a subclass, you inherit all the public methods of the base class so you can call them as your own. No prefix is needed.

There are many good reference sources for C++ programming. Here is one: http://www.cplusplus.com/reference/

User avatar
rna
 
Posts: 20
Joined: Fri Mar 18, 2011 9:54 pm

Re: Subclassing the TFTLCD library

Post by rna »

So far, so good. Now, it looks like TFTLCD is being re-instantiated. I am trying to have the Icon class use the existing TFTLCD methods without re-instantiating the TFTLCD class.

The Arduino environment errors are:
C:\arduino-1.0\work\libraries\Icons\Icons.cpp: In constructor 'Icons::Icons()':
C:\arduino-1.0\work\libraries\Icons\Icons.cpp:18: error: no matching function for call to 'TFTLCD::TFTLCD()'
C:\arduino-1.0\work\libraries\TFTLCD/TFTLCD.h:327: note: candidates are: TFTLCD::TFTLCD(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\arduino-1.0\work\libraries\TFTLCD/TFTLCD.h:325: note: TFTLCD::TFTLCD(const TFTLCD&)

Also, I have read in a forum that the Icons class files might have to be in the same directory as the TFTLCD files!?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Subclassing the TFTLCD library

Post by adafruit_support_bill »

Your "Icons" constructor is implicitly calling the "TFTLCD" constructor (because you can't construct the derived class instance without first constructing the base class). Since you did not tell it otherwise, it is assuming a default constructor with no arguments. And, since TFTLCD has none, you get this error.

Search for "C++ base class constructor" for how to explicitly pass arguments to the base class constructor.

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

Return to “Arduino”