Class mRotaryEncoder for mechanical incremental rotary encoders with pushbuttons. Use debouncing and callback-functions for rotation and pressing of button. This version is for old mbed. New version for mbed-os see https://os.mbed.com/users/charly/code/mRotaryEncoder-os/
Dependents: SimplePIDBot FinalProgram VS1053Player SPK-DVIMXR ... more
Diff: mRotaryEncoder.cpp
- Revision:
- 5:75ddffaf3721
- Parent:
- 3:39c2fc4482be
- Child:
- 7:ec80fd9c0c08
--- a/mRotaryEncoder.cpp Tue Feb 01 20:06:48 2011 +0000
+++ b/mRotaryEncoder.cpp Wed Feb 09 20:18:34 2011 +0000
@@ -3,7 +3,7 @@
mRotaryEncoder::mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode, int debounceTime_us) {
- m_pinA = new InterruptIn(pinA); // interrrupts on pinA
+ m_pinA = new PinDetect(pinA); // interrrupts on pinA
m_pinB = new DigitalIn(pinB); // only digitalIn for pinB
//set pins with internal PullUP-default
@@ -11,8 +11,11 @@
m_pinB->mode(pullMode);
// attach interrrupts on pinA
- m_pinA->rise(this, &mRotaryEncoder::rise);
- m_pinA->fall(this, &mRotaryEncoder::fall);
+ m_pinA->attach_asserted(this, &mRotaryEncoder::rise);
+ m_pinA->attach_deasserted(this, &mRotaryEncoder::fall);
+
+ //start sampling pinA
+ m_pinA->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time.
// Switch on pinSW
m_pinSW = new PinDetect(pinSW); // interrupt on press switch
@@ -44,10 +47,7 @@
void mRotaryEncoder::fall(void) {
- //no interrupts
- m_pinA->rise(NULL);
- m_pinA->fall(NULL);
- wait_us(m_debounceTime_us); // wait while switch is bouncing
+ // debouncing does PinDetect for us
//pinA still low?
if (*m_pinA == 0) {
if (*m_pinB == 1) {
@@ -56,17 +56,11 @@
m_position--;
}
}
- //reenable interrupts
- m_pinA->rise(this, &mRotaryEncoder::rise);
- m_pinA->fall(this, &mRotaryEncoder::fall);
rotIsr.call(); // call the isr for rotation
}
void mRotaryEncoder::rise(void) {
- //no interrupts
- m_pinA->rise(NULL);
- m_pinA->fall(NULL);
- wait_us(m_debounceTime_us); // wait while switch is bouncing
+ //PinDetect does debouncing
//pinA still high?
if (*m_pinA == 1) {
if (*m_pinB == 1) {
@@ -75,9 +69,6 @@
m_position++;
}
}
- //reenable interrupts
- m_pinA->rise(this, &mRotaryEncoder::rise);
- m_pinA->fall(this, &mRotaryEncoder::fall);
rotIsr.call(); // call the isr for rotation
}