Go Down

Topic: using template to pass aditional argument (Read 102 times) previous topic - next topic

mcf3lmnfs

I am writing a code to read some data from input using attachInterrupt. I am trying to use the same function in both attachInterupt-s. The problem is I need to pass a parameter to calling function to work properly.  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!

Code: [Select]

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;
        }
    }


mcf3lmnfs

ahh found a mistake, this two lines must be written in one line
Code: [Select]

template <int DIR>
void IN_enc_fun(){


Thank you for reading this anyway. Sorry for bothering you guys.

pert

Note it's only necessary to put it all on one line in .ino files. You can write it in two lines in .h, etc. files.

Go Up