So far I've made the following changes to Encoder.h and Encoder.cpp. I'm going on the assumption that I need to perform the same debounce and edge checking operations on input b
as I do on input a
. I've commented around the code I've added.
In Encoder.h I added four new variables to private
private:
bool validEdge(bool a);
unsigned int debouncing_;
unsigned int debounce_;
int position_;
Polarity polarity_;
bool a_;
bool lastA_;
bool primed_;
//TIM 3-2-22 New
unsigned int debouncingB_;
unsigned int debounceB_;
bool b_;
bool lastB_;
//end NEW
In Encoder.cpp I made the following changes
void Encoder::setup(unsigned int debounce, Polarity polarity)
{
debounce_ = debounce;
reset();
polarity_ = polarity;
debouncing_ = 0;
primed_ = false;
//TIM 3-2-22 New
debounceB_ = debounce;
debouncingB_ = 0;
//end NEW
}
and
Encoder::Rotation Encoder::process(bool a, bool b)
{
Rotation ret = NONE;
if(!primed_) {
lastA_ = a_ = a;
primed_ = true;
}
if(a != lastA_) {
if(validEdge(a))
debouncing_ = debounce_ + 1;
else
a_ = a;
}
//TIM 3-2-22 New
if(b != lastB_) {
if(validEdge(b))
debouncingB_ = debounceB_ + 1;
else
b_ = b;
}
//end NEW
// wait for the data to be stable long enough
// before checking for an updated value
if(1 == debouncing_ && a_ != a)
{
a_ = a;
if(validEdge(a))
{
if(b == a)
ret = CCW;
else
ret = CW;
if(ACTIVE_LOW == polarity_)
ret = CCW == ret ? CW : CCW;
position_ += ret;
}
}
if(debouncing_)
--debouncing_;
lastA_ = a;
//TIM 3-2-22 NEW
if(debouncingB_)
--debouncingB_;
lastB_ = b;
//end NEW
return ret;
}
That leaves the checking and counting operation in the if(1 == debouncing_ && a_ != a)
conditional statement to handle.
Am I on track, and what are your thoughts on performing checking and counting input b
?