I built a class (eg."DoInput") with a pin-number in the constructor. No problem.
Now I want to use the library "DirectIO", which uses class templates. Is it possible to do deliver the adress of an instance of DirectIO to the constructor? If this is not possible, the adress of the pinIn.read() function?
Yes, I want to declare a constructor (and a variable) taking a pointer of the type Input* or Input* (not the address).
When I want to declare a pointer:
Input* myInput
the error message is:
type/value mismatch at argument 1 in template parameter list for 'template class Input'
Nobsi:
Yes, I want to declare a constructor (and a variable) taking a pointer of the type Input* or Input* (not the address).
When I want to declare a pointer:
Input* myInput
the error message is:
type/value mismatch at argument 1 in template parameter list for 'template class Input'
What is the correct type declaration?
why are you not just using inheritance?
will a DoInput instance ever contain more than one DirectIO object?
#include <DirectIO.h>
class DoInput{
public:
Input<byte>* myInput;
unsigned long cnt;
DoInput(Input<byte> *myInput_) {
myInput = myInput_;
cnt=0;
}
void myfunc() {
if ((*myInput).read()) cnt++;
}
};
Input<2> pinIn;
DoInput doInput(*pinIn);
void setup() {
}
void loop() {
delayMicroseconds(100); // delay makes no sense here, in real doing a lot of other things...
doInput.myfunc();
}
If this will not work, I'll try to set a pointer direct to the function "pinIn.read()" and put it into the constructor.
So, looking at the library source code, it looks like the template for the 'Input' class want a pin number as it’s parameter, like:
Input<2> pinIn;
So, it’s not clear to me what you want to happen when you give it a data type instead:
Input<byte>* myInput;
Also, this:
DoInput doInput(*pinIn);
Is calling your constructor with an argument that results from de-referencing your pinIn object. I can’t even imagine what that might be, but I’m pretty sure it’s not useful.
Finally, the constructor for the ‘Input’ class in the DirectIO library calls ‘pinMode()’ directly. I’ve heard that may not be the best thing to do.
All template parameters must be specified or deducible at compile time. The Input class requires a uint8 as its template parameter. It requires a number, not a type.
The easiest way to fix this is to make your DoInput class a template as wall, and pass the pin number as a template parameter. That will prevent you from binding it to different pins at run time. That is part of the tradoff of doing direct I/O: flexibility is traded for speed.
If you must be able to change pins at runtime, another way is to rewrite the DirectIO classes to inherit from a non-template abstract class and take advantage of polymorphism. This reduces some of the benefit of a DirectIO library though, since virtual functions add some pointer dereferencing into the function calls, reducing performance.
Someone else already said it: This is not a trivial undertaking. You'll really be exercising your class design skills with this.
Thanks for your answers,
it seems to be difficult to put a template class in an other class' constructor. I'm not a c++ expert, so I will not rewrite the DirectIO-class.
OK, I made some wrong (de)referenced pointers in my example, but this is solvable for me. Thx, gfvalvo for your hints.
Maybe I'll try to make my own class as template - but I've to learn more of the template usage.
Otherwise I'll use the global instances of directIO directly in my class, with switch/case determination (OK, it's a dirty solution, but it will work and is fast enough).
Some of my DirectIO instances will be called during interupt routines, so they should be fast.
By the way: I also wondered the usage of pinMode in the constructor. But it worked in my case, and abviously also for the writer of DirectIO. Maybe the set of the propriate registers will survive all the other init processes. This is very helpful in my project with a lot of digital pins (half of my Mega pins are used). It's more clear to declare the variables and the state of the pins in the same place. During functional extensions of my sketch I sometimes forgot the use of pinMode....
That's very simple. It's just the other way round to declare a directIO instance inside my classes. I'll play around a little with this, also with the pointer version.
I'm busy this Weekend so it will take some time...
Thx for your detailed answers! It's just copy&paste to test it
I tested both methods in all my used classes, with one, two or three template parameters. It all works great and it's simple. I used the version without pointer.
Now my code is more simple to read and digital IO is faster.