Hello, I'm trying to implement 2 magnetic encoders into my project. In order to do so I've created a script that takes two input pins, adds an interrupt to each of the pins and counts pulses. That works fine when doing inside the main function. The problem is whenever I try to fit all of this inside a class. Right now I have the following code:
class EncReader
{
private:
int EncA;
int EncB;
int Pulse;
public:
EncReader(int new_EncA, int new_EncB, double new_ts);
void cb_fcnA(const string &channel);
void cb_fcnB(const string &channel);
void pulse_callback_encA();
void pulse_callback_encB();
void encoder_setup();
int get_pulse();
};
EncReader::EncReader(int new_EncA, int new_EncB, double new_ts):
EncA(new_EncA), EncB(new_EncB), Pulse(0), ts(new_ts)
{
encoder_setup();
}
void EncReader::encoder_setup(){
setmode(BOARD);
setup(EncA,IN);
setup(EncB,IN);
add_event_detect(EncA,BOTH, cb_fcnA);
add_event_detect(EncB,BOTH, cb_fcnB);
}
void EncReader::cb_fcnA(const string &channel){
pulse_callback_encA();
}
void EncReader::cb_fcnB(const string &channel){
pulse_callback_encB();
}
void EncReader::pulse_callback_encA(){ //Interrupt attached to encoder A
if(input(EncB) == input(EncA)){
Pulse--;
}
else{
Pulse++;
}
}
void EncReader::pulse_callback_encB(){ //Interrupt attached to encoder B
if(input(EncA) != input(EncB)){
Pulse--;
}
else{
Pulse++;
}
}
I need to use these functions inside of the class for my project, so I don't know what else to do. Any help is appreciated.