Hi, I'm working on a software to control a multi function hardware (signal generator, frequency counter, state analyzer) with a touch display. The software shall run on a Due.
- Data and features of the functions are defined by classes/objects, i.e. one class per function
- For every function there are several input/output boxes on the display, i.e. they look differently. To display them there is a draw() method - different for every function.
- Only one of the functions is active at a time
For finding a good solution for the control of the functions I wrote a small test:
- There is a base class, MuFu_Base, with methods init() and draw().
- There is a first function class, MN, inheriting from MuFu_Base, with methods draw() and process()
- There is a second function class, SG, inheriting from MuFu_Base, also with methods draw() and process()
- The methods draw() and process() have different contents, of course
To call the methods I want to use a single pointer with type casting to point to MuFu_Base or MN or SG.
And here is my problem how to do this. Here is the sketch for my tests:
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h" // erbt ueber Adafruit_SPITFT von Adafruit_GFX
#include "MyClasses.h"
#define TFT_DC 12
#define TFT_CS 71
#define TFT_RST 70
// Initialize tft display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
MuFu_Base *ptNFunc = NULL; // trial 1/3
// void *ptNFunc = NULL; // trial 2
void setup() {
ptNFunc = new(MuFu_Base); // trial 1/3
// ptNFunc = (MuFu_Base *)(new(MuFu_Base)); // trial 2
ptNFunc->init(50, 10, 50, 20);
ptNFunc->draw(&tft);
delete(ptNFunc);
ptNFunc = (MN*)(new(MN));
// ptNFunc = dynamic_cast<MN *>(MuFu_Base); // trial 3
ptNFunc->init(50, 40, 50, 20);
ptNFunc->draw(&tft);
ptNFunc->process(&tft); // line 32
delete(ptNFunc);
ptNFunc = (SG*)(new(SG));
ptNFunc->init(50, 70, 50, 20);
ptNFunc->draw(&tft);
ptNFunc->process(&tft); // line 38
}
void loop() {
// put your main code here, to run repeatedly:
}
Declarations and definitions of the classes are attached.
I tested three versions how to define and use the single pointer ptNFunc.
trial 1:
see sketch above.
compiler error (shortened):
In function 'void setup()':
...\MultiFunction.ino:32:12: error: 'class MuFu_Base' has no member named 'process'
ptNFunc->process(&tft);
^
...\MultiFunction.ino:38:12: error: 'class MuFu_Base' has no member named 'process'
ptNFunc->process(&tft);
trial 2, lines with comments "trial 2" activated, others commented out:
// MuFu_Base *ptNFunc = NULL; // trial 1/3
void *ptNFunc = NULL; // trial 2
void setup() {
// ptNFunc = new(MuFu_Base); // trial 1
ptNFunc = (MuFu_Base *)(new(MuFu_Base)); // trial 2
ptNFunc->init(50, 10, 50, 20); // line 24
ptNFunc->draw(&tft); // line 25
compiler errors (shortened):
In function 'void setup()':
...\MultiFunction.ino:24:10: error: 'void*' is not a pointer-to-object type
ptNFunc->init(50, 10, 50, 20);
^
...\MultiFunction.ino:25:10: error: 'void*' is not a pointer-to-object type
ptNFunc->draw(&tft);
^
... a lot more errors
trial 3, with dynamic_cast:
MuFu_Base *ptNFunc = NULL; // trial 1/3
// void *ptNFunc = NULL; // trial 2
void setup() {
ptNFunc = new(MuFu_Base); // trial 1/3
// ptNFunc = (MuFu_Base *)(new(MuFu_Base)); // trial 2
ptNFunc->init(50, 10, 50, 20);
ptNFunc->draw(&tft);
delete(ptNFunc);
// ptNFunc = (MN*)(new(MN));
ptNFunc = dynamic_cast<MN *>(MuFu_Base); // trial 3
ptNFunc->init(50, 40, 50, 20);
compiler error:
In function 'void setup()':
...\MultiFunction.ino:29:41: error: expected primary-expression before ')' token
ptNFunc = dynamic_cast<MN *>(MuFu_Base); // trial 3
^
... a lot more errors
What is wrong with my type castings?
MyClasses.cpp (1.4 KB)
MyClasses.h (1.1 KB)