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

Committer:
jirrick
Date:
Tue Mar 03 12:20:55 2020 +0000
Revision:
11:24b34deae975
Parent:
8:41c44b127443
configure detection of rise and fall events

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charly 0:562943b05e99 1 #ifndef MROTENC_H_INCLUDED
charly 0:562943b05e99 2 #define MROTENC_H_INCLUDED
charly 0:562943b05e99 3
charly 0:562943b05e99 4 #include "mbed.h"
charly 2:f99ac9745a2c 5 #include "PinDetect.h"
charly 0:562943b05e99 6
charly 0:562943b05e99 7
charly 1:bb7b4e631dff 8 /** This Class handles a rotary encoder with mechanical switches and an integrated pushbutton
charly 0:562943b05e99 9 * It uses two pins, one creating an interrupt on change.
charly 0:562943b05e99 10 * Rotation direction is determined by checking the state of the other pin.
charly 0:562943b05e99 11 * Additionally a pushbutton switch is detected
charly 0:562943b05e99 12 *
charly 0:562943b05e99 13 * Operating the encoder changes an internal integer value that can be read
charly 0:562943b05e99 14 * by Get() or the operator int() functions.
charly 0:562943b05e99 15 * A new value can be set by Set(value) or opperator=.
charly 0:562943b05e99 16 *
charly 0:562943b05e99 17 * Autor: Thomas Raab (Raabinator)
charly 1:bb7b4e631dff 18 * Extended by Karl Zweimueller (charly)
charly 0:562943b05e99 19 *
charly 0:562943b05e99 20 * Dent steady point ! ! !
charly 0:562943b05e99 21 * +-----+ +-----+
charly 0:562943b05e99 22 * pinA (interrupt) | | | |
charly 0:562943b05e99 23 * --+ +-----+ +---
charly 0:562943b05e99 24 * +-----+ +-----+
charly 0:562943b05e99 25 * pinB | | | |
charly 0:562943b05e99 26 * ----+ +-----+ +-
charly 0:562943b05e99 27 * --> C.W
charly 0:562943b05e99 28 * CW: increases position value
charly 0:562943b05e99 29 * CCW: decreases position value
charly 0:562943b05e99 30 *
charly 0:562943b05e99 31 * changelog:
charly 0:562943b05e99 32 *
charly 0:562943b05e99 33 * 09. Nov. 2010
charly 0:562943b05e99 34 * First version published Thomas Raab raabinator
charly 0:562943b05e99 35 * 26.11.2010 extended by charly - pushbutton, pullmode, debounce, callback-system
charly 5:75ddffaf3721 36 * Feb2011 Changes InterruptIn to PinDetect which does the debounce of mechanical switches
jirrick 11:24b34deae975 37 * Mar2020 Configurable detection of rise/fall events to account for different types of encoders (half as much dent points)
charly 0:562943b05e99 38 *
charly 0:562943b05e99 39 */
charly 0:562943b05e99 40 class mRotaryEncoder {
charly 0:562943b05e99 41 public:
charly 0:562943b05e99 42 /** Create a mechanical rotary encoder object connected to the specified pins
charly 0:562943b05e99 43 *
charly 0:562943b05e99 44 * @param pinA Switch A of quadrature encoder
charly 0:562943b05e99 45 * @param pinB Switch B of quadrature encoder
charly 0:562943b05e99 46 * @param pinSW Pin for push-button switch
charly 0:562943b05e99 47 * @param pullmode mode for pinA pinB and pinSW like DigitalIn.mode
charly 0:562943b05e99 48 * @param debounceTime_us time in micro-seconds to wait for bouncing of mechanical switches to end
jirrick 11:24b34deae975 49 * @param detectRise Detect rise event as new rotation
jirrick 11:24b34deae975 50 * @param detectFall Detect fall event as new rotation
charly 0:562943b05e99 51 */
jirrick 11:24b34deae975 52 mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000, int detectRise=1, int detectFall=1);
charly 0:562943b05e99 53
charly 0:562943b05e99 54 /** destroy object
charly 0:562943b05e99 55 *
charly 0:562943b05e99 56 */
charly 0:562943b05e99 57 ~mRotaryEncoder();
charly 0:562943b05e99 58
charly 0:562943b05e99 59 /** Get the actual value of the rotary position
charly 0:562943b05e99 60 *
charly 0:562943b05e99 61 * @return position int value of position
charly 0:562943b05e99 62 */
charly 0:562943b05e99 63 int Get(void);
charly 0:562943b05e99 64 inline operator int() {
charly 0:562943b05e99 65 return Get();
charly 0:562943b05e99 66 }
charly 0:562943b05e99 67
charly 0:562943b05e99 68 /** Set the current position value
charly 0:562943b05e99 69 *
charly 0:562943b05e99 70 * @param value the new position to set
charly 0:562943b05e99 71 *
charly 0:562943b05e99 72 */
charly 0:562943b05e99 73 void Set(int value);
charly 0:562943b05e99 74 inline mRotaryEncoder& operator= ( int value ) {
charly 0:562943b05e99 75 Set(value);
charly 0:562943b05e99 76 return *this;
charly 0:562943b05e99 77 }
charly 0:562943b05e99 78
charly 0:562943b05e99 79 /** attach a function to be called when switch is pressed
charly 7:ec80fd9c0c08 80 *
charly 0:562943b05e99 81 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 82 *
charly 0:562943b05e99 83 * @param fptr Pointer to callback-function
charly 0:562943b05e99 84 */
charly 0:562943b05e99 85 void attachSW(void (*fptr)(void)) {
charly 3:39c2fc4482be 86 m_pinSW->attach_deasserted(fptr);
charly 0:562943b05e99 87 }
charly 0:562943b05e99 88
charly 0:562943b05e99 89 template<typename T>
charly 0:562943b05e99 90 /** attach an object member function to be called when switch is pressed
charly 0:562943b05e99 91 *
charly 0:562943b05e99 92 * @param tptr pointer to object
charly 0:562943b05e99 93 * @param mprt pointer ro member function
charly 0:562943b05e99 94 *
charly 0:562943b05e99 95 */
charly 0:562943b05e99 96 void attachSW(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 97 if ((mptr != NULL) && (tptr != NULL)) {
charly 3:39c2fc4482be 98 m_pinSW->attach_deasserted(tptr, mptr);
charly 0:562943b05e99 99 }
charly 0:562943b05e99 100 }
charly 0:562943b05e99 101
charly 0:562943b05e99 102 /** callback-System for rotation of shaft
charly 7:ec80fd9c0c08 103 *
charly 8:41c44b127443 104 * attach a function to be called when the shaft is rotated
charly 0:562943b05e99 105 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 106 *
charly 0:562943b05e99 107 * @param fprt Pointer to callback-function
charly 0:562943b05e99 108 */
charly 0:562943b05e99 109 void attachROT(void (*fptr)(void)) {
charly 0:562943b05e99 110 rotIsr.attach(fptr);
charly 0:562943b05e99 111 }
charly 0:562943b05e99 112
charly 0:562943b05e99 113
charly 0:562943b05e99 114 template<typename T>
charly 8:41c44b127443 115 /** attach an object member function to be called when shaft is rotated
charly 0:562943b05e99 116 *
charly 0:562943b05e99 117 * @param tptr pointer to object
charly 0:562943b05e99 118 * @param mprt pointer ro member function
charly 0:562943b05e99 119 *
charly 0:562943b05e99 120 */
charly 0:562943b05e99 121 void attachROT(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 122 if ((mptr != NULL) && (tptr != NULL)) {
charly 6:854c349157b0 123 rotIsr.attach(tptr, mptr);
charly 0:562943b05e99 124 }
charly 0:562943b05e99 125 }
charly 0:562943b05e99 126
charly 7:ec80fd9c0c08 127 /** callback-System for rotation of shaft CW
charly 7:ec80fd9c0c08 128 *
charly 8:41c44b127443 129 * attach a function to be called when the shaft is rotated clockwise
charly 7:ec80fd9c0c08 130 * keep this function short, as no interrrupts can occour within
charly 7:ec80fd9c0c08 131 *
charly 7:ec80fd9c0c08 132 * @param fprt Pointer to callback-function
charly 7:ec80fd9c0c08 133 */
charly 7:ec80fd9c0c08 134 void attachROTCW(void (*fptr)(void)) {
charly 7:ec80fd9c0c08 135 rotCWIsr.attach(fptr);
charly 7:ec80fd9c0c08 136 }
charly 7:ec80fd9c0c08 137
charly 7:ec80fd9c0c08 138
charly 7:ec80fd9c0c08 139 template<typename T>
charly 8:41c44b127443 140 /** attach an object member function to be called when shaft is rotated clockwise
charly 7:ec80fd9c0c08 141 *
charly 7:ec80fd9c0c08 142 * @param tptr pointer to object
charly 7:ec80fd9c0c08 143 * @param mprt pointer ro member function
charly 7:ec80fd9c0c08 144 *
charly 7:ec80fd9c0c08 145 */
charly 7:ec80fd9c0c08 146 void attachROTCW(T* tptr, void (T::*mptr)(void)) {
charly 7:ec80fd9c0c08 147 if ((mptr != NULL) && (tptr != NULL)) {
charly 7:ec80fd9c0c08 148 rotCWIsr.attach(tptr, mptr);
charly 7:ec80fd9c0c08 149 }
charly 7:ec80fd9c0c08 150 }
charly 7:ec80fd9c0c08 151
charly 7:ec80fd9c0c08 152 /** callback-System for rotation of shaft CCW
charly 7:ec80fd9c0c08 153 *
charly 8:41c44b127443 154 * attach a function to be called when the shaft is rotated counterclockwise
charly 7:ec80fd9c0c08 155 * keep this function short, as no interrrupts can occour within
charly 7:ec80fd9c0c08 156 *
charly 7:ec80fd9c0c08 157 * @param fprt Pointer to callback-function
charly 7:ec80fd9c0c08 158 */
charly 7:ec80fd9c0c08 159 void attachROTCCW(void (*fptr)(void)) {
charly 7:ec80fd9c0c08 160 rotCCWIsr.attach(fptr);
charly 7:ec80fd9c0c08 161 }
charly 7:ec80fd9c0c08 162
charly 7:ec80fd9c0c08 163
charly 7:ec80fd9c0c08 164 template<typename T>
charly 8:41c44b127443 165 /** attach an object member function to be called when shaft is rotated CCW
charly 7:ec80fd9c0c08 166 *
charly 7:ec80fd9c0c08 167 * @param tptr pointer to object
charly 7:ec80fd9c0c08 168 * @param mprt pointer ro member function
charly 7:ec80fd9c0c08 169 *
charly 7:ec80fd9c0c08 170 */
charly 7:ec80fd9c0c08 171 void attachROTCCW(T* tptr, void (T::*mptr)(void)) {
charly 7:ec80fd9c0c08 172 if ((mptr != NULL) && (tptr != NULL)) {
charly 7:ec80fd9c0c08 173 rotCCWIsr.attach(tptr, mptr);
charly 7:ec80fd9c0c08 174 }
charly 7:ec80fd9c0c08 175 }
charly 0:562943b05e99 176
charly 0:562943b05e99 177 private:
charly 5:75ddffaf3721 178 PinDetect *m_pinA;
charly 0:562943b05e99 179 DigitalIn *m_pinB;
charly 0:562943b05e99 180 volatile int m_position;
charly 0:562943b05e99 181
charly 0:562943b05e99 182 int m_debounceTime_us;
charly 0:562943b05e99 183
charly 5:75ddffaf3721 184
charly 2:f99ac9745a2c 185 PinDetect *m_pinSW;
charly 0:562943b05e99 186
charly 0:562943b05e99 187 void rise(void);
charly 0:562943b05e99 188 void fall(void);
charly 0:562943b05e99 189
charly 0:562943b05e99 190 protected:
charly 0:562943b05e99 191 /**
charly 0:562943b05e99 192 * Callback system.
charly 0:562943b05e99 193 * @ingroup INTERNALS
charly 0:562943b05e99 194 */
charly 8:41c44b127443 195 /**
charly 8:41c44b127443 196 * rotated any direction
charly 8:41c44b127443 197 */
charly 0:562943b05e99 198 FunctionPointer rotIsr;
charly 8:41c44b127443 199 /**
charly 8:41c44b127443 200 * clockwise rotated
charly 8:41c44b127443 201 */
charly 7:ec80fd9c0c08 202 FunctionPointer rotCWIsr;
charly 8:41c44b127443 203
charly 8:41c44b127443 204 /**
charly 8:41c44b127443 205 * counterclockwise rotated
charly 8:41c44b127443 206 */
charly 7:ec80fd9c0c08 207 FunctionPointer rotCCWIsr;
charly 0:562943b05e99 208
charly 0:562943b05e99 209
charly 0:562943b05e99 210 };
charly 0:562943b05e99 211
charly 0:562943b05e99 212
charly 0:562943b05e99 213 #endif