Fork of mRotaryEncoder for mdeb-os. uses newer version of PinDetect. Testprogram: https://os.mbed.com/users/charly/code/mRotaryEncoder_HelloWorld-os/

Dependencies:   PinDetect

Dependents:   mRotaryEncoder_HelloWorld-os TMC2209-Test2

Files at this revision

API Documentation at this revision

Comitter:
charly
Date:
Tue Mar 16 20:19:04 2021 +0000
Parent:
11:24b34deae975
Child:
13:d22167ec460c
Commit message:
Version of mRotaryEncoder for mbed-os

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 Mar 03 12:20:55 2020 +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 Mar 03 12:20:55 2020 +0000
+++ b/mRotaryEncoder.cpp	Tue Mar 16 20:19:04 2021 +0000
@@ -12,10 +12,10 @@
 
     // attach interrrupts on pinA
     if (detectRise != 0){
-        m_pinA->attach_asserted(this, &mRotaryEncoder::rise);
+        m_pinA->attach_asserted(callback(this, &mRotaryEncoder::rise));
     }
     if (detectFall != 0){
-        m_pinA->attach_deasserted(this, &mRotaryEncoder::fall);
+        m_pinA->attach_deasserted(callback(this, &mRotaryEncoder::fall));
     }
     
     //start sampling pinA
@@ -56,12 +56,18 @@
     if (*m_pinA == 0) {
         if (*m_pinB == 1) {
             m_position++;
-            rotCWIsr.call();
+            if (rotCWIsr) {
+                rotCWIsr();
+            }
         } else {
             m_position--;
-            rotCCWIsr.call();
+            if (rotCWIsr){
+                rotCCWIsr();
+            }
         }
-    rotIsr.call();                        // call the isr for rotation
+        if (rotIsr){
+            rotIsr();                        // call the isr for rotation
+        }
     }
 }
 
@@ -71,12 +77,18 @@
     if (*m_pinA == 1) {
         if (*m_pinB == 1) {
             m_position--;
-            rotCCWIsr.call();
+            if (rotCCWIsr){
+                rotCCWIsr();
+            }
         } else {
             m_position++;
-            rotCWIsr.call();
+            if (rotCWIsr){
+                rotCWIsr();
+            }
         }
-    rotIsr.call();                        // call the isr for rotation
+        if (rotIsr){
+            rotIsr();                        // call the isr for rotation
+        }
     }
 }
 
--- a/mRotaryEncoder.h	Tue Mar 03 12:20:55 2020 +0000
+++ b/mRotaryEncoder.h	Tue Mar 16 20:19:04 2021 +0000
@@ -46,8 +46,8 @@
     * @param pinSW Pin for push-button switch
     * @param pullmode mode for pinA pinB and pinSW like DigitalIn.mode
     * @param debounceTime_us time in micro-seconds to wait for bouncing of mechanical switches to end
-    * @param detectRise Detect rise event as new rotation
-    * @param detectFall Detect fall event as new rotation
+    * @param detectRise Detect rise event as new rotation. default 1
+    * @param detectFall Detect fall event as new rotation. default 1 
     */
     mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000, int detectRise=1, int detectFall=1);
 
@@ -80,74 +80,75 @@
     *
     * keep this function short, as no interrrupts can occour within
     *
-    * @param fptr Pointer to callback-function
+    * @param cb callback-function
     */
-    void attachSW(void (*fptr)(void)) {
-        m_pinSW->attach_deasserted(fptr);
+    void attachSW(Callback<void()> cb) {
+        m_pinSW->attach_deasserted(cb);
     }
 
-    template<typename T>
+//    template<typename T>
     /** attach an object member function to be called when switch is pressed
     *
     * @param tptr pointer to object
     * @param mprt pointer ro member function
     *
     */
-    void attachSW(T* tptr, void (T::*mptr)(void)) {
+/*    void attachSW(T* tptr, void (T::*mptr)(void)) {
         if ((mptr != NULL) && (tptr != NULL)) {
             m_pinSW->attach_deasserted(tptr, mptr);
         }
     }
-
+*/
     /**  callback-System for rotation of shaft
     *
     *  attach a function to be called when the shaft is rotated
     *  keep this function short, as no interrrupts can occour within
     *
-    * @param fprt Pointer to callback-function
+    * @param cb callback-function
     */
-    void attachROT(void (*fptr)(void)) {
-        rotIsr.attach(fptr);
+    void attachROT(Callback<void()> cb) {
+        rotIsr = cb;
     }
 
 
-    template<typename T>
+//    template<typename T>
     /** attach an object member function to be called when shaft is rotated
     *
     * @param tptr pointer to object
     * @param mprt pointer ro member function
     *
     */
-    void attachROT(T* tptr, void (T::*mptr)(void)) {
+/*    void attachROT(T* tptr, void (T::*mptr)(void)) {
         if ((mptr != NULL) && (tptr != NULL)) {
             rotIsr.attach(tptr, mptr);
         }
     }
-
+*/
    /**  callback-System for rotation of shaft CW
     *
     *  attach a function to be called when the shaft is rotated clockwise
     *  keep this function short, as no interrrupts can occour within
     *
-    * @param fprt Pointer to callback-function
+    * @param cb callback-function
     */
-    void attachROTCW(void (*fptr)(void)) {
-        rotCWIsr.attach(fptr);
+    void attachROTCW(Callback<void()> cb) {
+        rotCWIsr = cb;
     }
 
 
-    template<typename T>
+//    template<typename T>
     /** attach an object member function to be called when shaft is rotated clockwise
     *
     * @param tptr pointer to object
     * @param mprt pointer ro member function
     *
     */
-    void attachROTCW(T* tptr, void (T::*mptr)(void)) {
+/*    void attachROTCW(T* tptr, void (T::*mptr)(void)) {
         if ((mptr != NULL) && (tptr != NULL)) {
             rotCWIsr.attach(tptr, mptr);
         }
     }
+*/
 
    /**  callback-System for rotation of shaft CCW
     *
@@ -156,23 +157,24 @@
     *
     * @param fprt Pointer to callback-function
     */
-    void attachROTCCW(void (*fptr)(void)) {
-        rotCCWIsr.attach(fptr);
+    void attachROTCCW(Callback<void()> cb) {
+        rotCCWIsr = cb;
     }
 
 
-    template<typename T>
+//    template<typename T>
     /** attach an object member function to be called when shaft is rotated CCW
     *
     * @param tptr pointer to object
     * @param mprt pointer ro member function
     *
     */
-    void attachROTCCW(T* tptr, void (T::*mptr)(void)) {
+/*    void attachROTCCW(T* tptr, void (T::*mptr)(void)) {
         if ((mptr != NULL) && (tptr != NULL)) {
             rotCCWIsr.attach(tptr, mptr);
         }
     }    
+*/
 
 private:
     PinDetect       *m_pinA;
@@ -195,16 +197,16 @@
     /**
      * rotated any direction
     */
-    FunctionPointer rotIsr;
+    Callback<void()> rotIsr;
     /**
      * clockwise rotated
     */
-    FunctionPointer rotCWIsr;
+    Callback<void()> rotCWIsr;
 
     /**
      * counterclockwise rotated
     */    
-    FunctionPointer rotCCWIsr;
+    Callback<void()> rotCCWIsr;
 
 
 };