Hello
I want to create a wrapper library around the TFT library in order to manage e.g. deletion of the screen before writing new text and to implement a kind of screen saver if the application in waiting for user input and thus not changing for a while.
In order to do that I created a new class that holds some variables (such as the text to erase before writing new text) and has some functions to write to different areas of the Adafruit TFT display.
I need to initialize an instance of the TFT class from the TFT library. I tried different ways to do that. The last one was to insert a forward declaration as private variable in the class definition within the header file and to actually create the instance in the constructor code of the new class variable.
So my code fragments look like this:
Now the Arduino IDE compiler gives me an error message, that the TFTScreen is out of scope for the member function. The error message is as follows:
NewTFT.cpp: In member function 'void NewTFT::WriteText(char)':
NewTFT.cpp:36: error: 'TFTscreen' was not declared in this scope
How to you declare and initiate a class within a class definition?
Thanks for help.
after the previous reply and some further reading I understand, that I should inherit the TFT class in the NewTFT class. So i modified the code as below (btw: I am using the Ardiono 1.0.5 IDE):
TFTNew.h
// ***** TFTNew.h *****
#ifndef _TFTNew_H
#define _TFTNew_H
#include <TFT.h>
class NewTFT : public TFT { //line 35: in original file
public:
/// constructor
NewTFT(uint8_t CS, uint8_t RS, uint8_t RST);
void WriteText(char *text);
private:
char oldstr[8];
};
#endif // _TFTNew_H
TFTNew.cpp
// ***** TFTNew.cpp *****
include "TFTNew.h"
include <TFT.h>
NewTFT::NewTFT(uint8_t CS, uint8_t RS, uint8_t RST)
: TFT(CS, RS, RST) //line 24: in original file
{
// ...
}
void NewTFT::WriteText(char *text)
{
setTextSize(2); //line 37: in original file
//...
}
Main sketch
#include "TFTNew.h"
include <TFT.h>
NewTFT td = NewTFT(10,9,8);
char txt[] = "Hello";
void setup()
{
// I know that the TFT.begin() method is still missing but I am still stuck with the compile errors
}
void loop()
{
td.WriteText(txt);
}
I put all files in the same directory in order to avoid the library path complexity I read about in other postings.
So when I compile check this sketch I get the following error log:
In file included from TFTNew.cpp:1:
TFTNew.h:35: error: expected class-name before '{' token
TFTNew.cpp: In constructor 'NewTFT::NewTFT(uint8_t, uint8_t, uint8_t)':
TFTNew.cpp:24: error: class 'NewTFT' does not have any field named 'TFT'
TFTNew.cpp: In member function 'void NewTFT::WriteText(char *)':
TFTNew.cpp:37: error: 'setTextsize' was not declared in this scope
I maybe blind, but I could not find anything wrong with the above code.
PaulS: Thanks for directing my, however, I am not sure which part should go into the .h or .cpp file (or even the .ino)
Thanks for your help.
Thomas
C++ is complex and non-intuitive in these areas I reckon.
Best is to find an existing example of inheritance somewhere and work from that, whilst reading
up on C++ constructors and scope and so forth. Start with the simplest class and make one change
at a time.
You actual problem can be done in two ways, either inherit/extend or delegate - if I were delegating I'd
try to make the target class passed as a reference in the constructor of the wrapper class. Delegating
involves less assumptions about the class you are referring to, but you may have to write lots of
boilerplate.
You also have the option of not using a class which may be simpler!
hello all - i am equally interested in this topic as tlingenthal is. Any latest guidance on his thread. I have a similar need to address in my project (http://alibe.codeplex.com/)
Hello all
I am trying to inherit from the "Servo" class to implement a few additional methods that address my project needs. I must also be blind, what am i missing to get this resolved. any and all help is greatly appreciated. Thanks all in advance
The code snippets and the compilation error details are below:
// ALIBESteer.cpp file
#include <Arduino.h>
#include <Servo.h>
#include <ALIBESteer.h>
ALIBESteer::ALIBESteer(){
// attaches the servo on pin ALIBE_STEER_SERVO_PIN to the servo object
_steer.attach(ALIBE_STEER_SERVO_PIN);
turnStraight();
}
//public
bool ALIBESteer::turnLeft(){
_steer.write(0); // tell servo to go to position 0d - left
delay(15); // waits 15ms for the servo to reach the position
}
bool ALIBESteer::turnRight(){
_steer.write(180); // tell servo to go to position 180d - right
delay(15); // waits 15ms for the servo to reach the position
}
bool ALIBESteer::turnStraight(){
_steer.write(90); // tell servo to go to position 90d - straight ahead
delay(15); // waits 15ms for the servo to reach the position
}
In file included from ALIBESteer.ino:1:
C:\Arduino\libraries\ALIBESteer/ALIBESteer.h:8: error: expected class-name before '{' token
C:\Arduino\libraries\ALIBESteer/ALIBESteer.h:15: error: 'Servo' does not name a type
// ALIBESteer.cpp file
#include <Arduino.h>
#include <Servo.h>
#include <ALIBESteer.h>
ALIBESteer::ALIBESteer(){
//
}
//public
void ALIBESteer::begin(){
// attaches the servo on pin ALIBE_STEER_SERVO_PIN to the servo object
_steer.attach(ALIBE_STEER_SERVO_PIN);
turnStraight();
}
bool ALIBESteer::turnLeft(){
_steer.write(0); // tell servo to go to position 0d - left
delay(15); // waits 15ms for the servo to reach the position
}
bool ALIBESteer::turnRight(){
_steer.write(180); // tell servo to go to position 180d - right
delay(15); // waits 15ms for the servo to reach the position
}
bool ALIBESteer::turnStraight(){
_steer.write(90); // tell servo to go to position 90d - straight ahead
delay(15); // waits 15ms for the servo to reach the position
}