using template to pass aditional argument

I need to pass a (function with one argument) as an argument to another function,
this another function is fixed - can not be changed becasuse it is already written.

I have came across using templates and thought it might solve the problem, but for some reason it doesn't compile. Can anyone help me fix it please? Or if it is not possible, can you explain it to me and maybe any other smart solutions using only one function?

Thank you for your help in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int16_t phi_inc=0;

void IN_enc_fun();
byte IN_encA=22;
byte IN_encB=23;

void setup() {
    attachInterrupt(digitalPinToInterrupt(IN_encA), IN_enc_fun<0>, CHANGE);
    attachInterrupt(digitalPinToInterrupt(IN_encB), IN_enc_fun<1>, CHANGE);
    }

void loop() {
    int a=digitalRead(IN_encA);
    int b=digitalRead(IN_encB);

    }

template <int DIR>
void IN_enc_fun(){
    switch((digitalRead(IN_encA)<<2)|(digitalRead(IN_encB)<<1)|DIR){
        case 0: case 3: case 5: case 6:
            phi_inc--;
        break;
        case 1: case 2: case 4: case 7:
            phi_inc++;
        break;
        }
    }


Fixes to be made:
I get error on line 8 and 9.
Calling function is defined in line 19.
Calling function might be wrongly declared in line 3.
Last edited on
closed account (E0p9LyTq)
Function pointers are your friend.

http://www.learncpp.com/cpp-tutorial/78-function-pointers/
What exactly do you mean. What line should i look at? can you be a bit more specific?
Move your template definition above where you first try to instantiate your template.
In other words, move lines 18 to 28 above line 7.

This is assuming your attachInterrupt function looks something like:
1
2
3
4
5
6
void attachInterrupt(some_type foo, void (*func)(), some_type bar)
{
	// ...
	func();
	// ...
}
Last edited on
1. I have found mistake, line 18 and line 19 must be in the same line.
and it already works

2. One more questin, in line 3, does it have any meaning to change it to:
 
template <int DIR> void IN_enc_fun(); 

or leave it as it is, this is:
 
void IN_enc_fun();

It works either way. But does it make any diference? Speed of execution? Does it compile with using less space? Or is it exactly the same?
Last edited on
1. It doesn't matter whether or not lines 18 and 19 are on the same actual line, whitespace doesn't matter to a compiler (except when making strings/multi-line macros).

2. Yes, that's the easier way that I should have suggested in my previous post. Do that instead.
1. In arduino environment IDE appernetly there shoulnd not be a line between a "template" and function. Found this here:
https://arduino.stackexchange.com/questions/28098/pass-templated-function-as-attachinterrupt-parameter

2. True, i have found that in some cases, leaving it as it is causes error on compiling. Changing it to:
 
template <int DIR> void IN_enc_fun();

fixed all the problems.

Thank you a lot for all your help! Everything is solved now.
Last edited on
Topic archived. No new replies allowed.