Fork of mRotaryEncoder for mdeb-os. uses newer version of PinDetect. Testprogram: https://os.mbed.com/users/charly/code/mRotaryEncoder_HelloWorld-os/
Dependents: mRotaryEncoder_HelloWorld-os TMC2209-Test2
mRotaryEncoder.h
00001 #ifndef MROTENC_H_INCLUDED 00002 #define MROTENC_H_INCLUDED 00003 00004 #include "mbed.h" 00005 #include "PinDetect.h" 00006 00007 00008 /** This Class handles a rotary encoder with mechanical switches and an integrated pushbutton 00009 * It uses two pins, one creating an interrupt on change. 00010 * Rotation direction is determined by checking the state of the other pin. 00011 * Additionally a pushbutton switch is detected 00012 * 00013 * Operating the encoder changes an internal integer value that can be read 00014 * by Get() or the operator int() functions. 00015 * A new value can be set by Set(value) or opperator=. 00016 * 00017 * Autor: Thomas Raab (Raabinator) 00018 * Extended by Karl Zweimueller (charly) 00019 * 00020 * Dent steady point ! ! ! 00021 * +-----+ +-----+ 00022 * pinA (interrupt) | | | | 00023 * --+ +-----+ +--- 00024 * +-----+ +-----+ 00025 * pinB | | | | 00026 * ----+ +-----+ +- 00027 * --> C.W 00028 * CW: increases position value 00029 * CCW: decreases position value 00030 * 00031 * changelog: 00032 * 00033 * 09. Nov. 2010 00034 * First version published Thomas Raab raabinator 00035 * 26.11.2010 extended by charly - pushbutton, pullmode, debounce, callback-system 00036 * Feb2011 Changes InterruptIn to PinDetect which does the debounce of mechanical switches 00037 * Mar2020 Configurable detection of rise/fall events to account for different types of encoders (half as much dent points) 00038 * 00039 */ 00040 class mRotaryEncoder { 00041 public: 00042 /** Create a mechanical rotary encoder object connected to the specified pins 00043 * 00044 * @param pinA Switch A of quadrature encoder 00045 * @param pinB Switch B of quadrature encoder 00046 * @param pinSW Pin for push-button switch 00047 * @param pullmode mode for pinA pinB and pinSW like DigitalIn.mode 00048 * @param debounceTime_us time in micro-seconds to wait for bouncing of mechanical switches to end 00049 * @param detectRise Detect rise event as new rotation. default 1 00050 * @param detectFall Detect fall event as new rotation. default 1 00051 */ 00052 mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000, int detectRise=1, int detectFall=1); 00053 00054 /** destroy object 00055 * 00056 */ 00057 ~mRotaryEncoder(); 00058 00059 /** Get the actual value of the rotary position 00060 * 00061 * @return position int value of position 00062 */ 00063 int Get(void); 00064 inline operator int() { 00065 return Get(); 00066 } 00067 00068 /** Set the current position value 00069 * 00070 * @param value the new position to set 00071 * 00072 */ 00073 void Set(int value); 00074 inline mRotaryEncoder& operator= ( int value ) { 00075 Set(value); 00076 return *this; 00077 } 00078 00079 /** attach a function to be called when switch is pressed 00080 * 00081 * keep this function short, as no interrrupts can occour within 00082 * 00083 * @param cb callback-function 00084 */ 00085 void attachSW(Callback<void()> cb) { 00086 m_pinSW->attach_deasserted(cb); 00087 } 00088 00089 // template<typename T> 00090 /** attach an object member function to be called when switch is pressed 00091 * 00092 * @param tptr pointer to object 00093 * @param mprt pointer ro member function 00094 * 00095 */ 00096 /* void attachSW(T* tptr, void (T::*mptr)(void)) { 00097 if ((mptr != NULL) && (tptr != NULL)) { 00098 m_pinSW->attach_deasserted(tptr, mptr); 00099 } 00100 } 00101 */ 00102 /** callback-System for rotation of shaft 00103 * 00104 * attach a function to be called when the shaft is rotated 00105 * keep this function short, as no interrrupts can occour within 00106 * 00107 * @param cb callback-function 00108 */ 00109 void attachROT(Callback<void()> cb) { 00110 rotIsr = cb; 00111 } 00112 00113 00114 // template<typename T> 00115 /** attach an object member function to be called when shaft is rotated 00116 * 00117 * @param tptr pointer to object 00118 * @param mprt pointer ro member function 00119 * 00120 */ 00121 /* void attachROT(T* tptr, void (T::*mptr)(void)) { 00122 if ((mptr != NULL) && (tptr != NULL)) { 00123 rotIsr.attach(tptr, mptr); 00124 } 00125 } 00126 */ 00127 /** callback-System for rotation of shaft CW 00128 * 00129 * attach a function to be called when the shaft is rotated clockwise 00130 * keep this function short, as no interrrupts can occour within 00131 * 00132 * @param cb callback-function 00133 */ 00134 void attachROTCW(Callback<void()> cb) { 00135 rotCWIsr = cb; 00136 } 00137 00138 00139 // template<typename T> 00140 /** attach an object member function to be called when shaft is rotated clockwise 00141 * 00142 * @param tptr pointer to object 00143 * @param mprt pointer ro member function 00144 * 00145 */ 00146 /* void attachROTCW(T* tptr, void (T::*mptr)(void)) { 00147 if ((mptr != NULL) && (tptr != NULL)) { 00148 rotCWIsr.attach(tptr, mptr); 00149 } 00150 } 00151 */ 00152 00153 /** callback-System for rotation of shaft CCW 00154 * 00155 * attach a function to be called when the shaft is rotated counterclockwise 00156 * keep this function short, as no interrrupts can occour within 00157 * 00158 * @param fprt Pointer to callback-function 00159 */ 00160 void attachROTCCW(Callback<void()> cb) { 00161 rotCCWIsr = cb; 00162 } 00163 00164 00165 // template<typename T> 00166 /** attach an object member function to be called when shaft is rotated CCW 00167 * 00168 * @param tptr pointer to object 00169 * @param mprt pointer ro member function 00170 * 00171 */ 00172 /* void attachROTCCW(T* tptr, void (T::*mptr)(void)) { 00173 if ((mptr != NULL) && (tptr != NULL)) { 00174 rotCCWIsr.attach(tptr, mptr); 00175 } 00176 } 00177 */ 00178 00179 private: 00180 PinDetect *m_pinA; 00181 DigitalIn *m_pinB; 00182 volatile int m_position; 00183 00184 int m_debounceTime_us; 00185 00186 00187 PinDetect *m_pinSW; 00188 00189 void rise(void); 00190 void fall(void); 00191 00192 protected: 00193 /** 00194 * Callback system. 00195 * @ingroup INTERNALS 00196 */ 00197 /** 00198 * rotated any direction 00199 */ 00200 Callback<void()> rotIsr; 00201 /** 00202 * clockwise rotated 00203 */ 00204 Callback<void()> rotCWIsr; 00205 00206 /** 00207 * counterclockwise rotated 00208 */ 00209 Callback<void()> rotCCWIsr; 00210 00211 00212 }; 00213 00214 00215 #endif
Generated on Sat Jul 16 2022 08:13:14 by
1.7.2