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/

Dependencies:   PinDetect

Dependents:   SimplePIDBot FinalProgram VS1053Player SPK-DVIMXR ... more

Files at this revision

API Documentation at this revision

Comitter:
charly
Date:
Wed Feb 09 20:18:34 2011 +0000
Parent:
4:82be633acd05
Child:
6:854c349157b0
Commit message:
Changed InterruptIn to Pindetect for Rotary pinA

Changed in this revision

PinDetect.lib Show diff for this revision Revisions of this file
mRotaryEncoder.cpp Show annotated file Show diff for this revision Revisions of this file
mRotaryEncoder.h Show annotated file Show diff for this revision Revisions of this file
--- a/PinDetect.lib	Tue Feb 01 20:06:48 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- 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
 }
 
--- a/mRotaryEncoder.h	Tue Feb 01 20:06:48 2011 +0000
+++ b/mRotaryEncoder.h	Wed Feb 09 20:18:34 2011 +0000
@@ -33,6 +33,7 @@
  * 09. Nov. 2010
  *     First version published Thomas Raab raabinator
  * 26.11.2010 extended by charly - pushbutton, pullmode, debounce, callback-system
+ * Feb2011 Changes InterruptIn to PinDetect which does the debounce of mechanical switches
  *
  */
 class mRotaryEncoder {
@@ -120,13 +121,13 @@
 
 
 private:
-    InterruptIn     *m_pinA;
+    PinDetect       *m_pinA;
     DigitalIn       *m_pinB;
     volatile int    m_position;
 
     int             m_debounceTime_us;
 
-    //InterruptIn     *m_pinSW;
+
     PinDetect       *m_pinSW;
 
     void rise(void);