Fork of original mRotaryEncoder library supporting more types of encoders (ones with full rise and fall cycle per one rotations).

Dependencies:   PinDetect

Committer:
charly
Date:
Wed Mar 11 21:51:10 2015 +0000
Revision:
8:41c44b127443
Parent:
7:ec80fd9c0c08
Child:
11:24b34deae975
fixed typos

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
charly 0:562943b05e99 37 *
charly 0:562943b05e99 38 */
charly 0:562943b05e99 39 class mRotaryEncoder {
charly 0:562943b05e99 40 public:
charly 0:562943b05e99 41 /** Create a mechanical rotary encoder object connected to the specified pins
charly 0:562943b05e99 42 *
charly 0:562943b05e99 43 * @param pinA Switch A of quadrature encoder
charly 0:562943b05e99 44 * @param pinB Switch B of quadrature encoder
charly 0:562943b05e99 45 * @param pinSW Pin for push-button switch
charly 0:562943b05e99 46 * @param pullmode mode for pinA pinB and pinSW like DigitalIn.mode
charly 0:562943b05e99 47 * @param debounceTime_us time in micro-seconds to wait for bouncing of mechanical switches to end
charly 0:562943b05e99 48 */
charly 0:562943b05e99 49 mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000);
charly 0:562943b05e99 50
charly 0:562943b05e99 51 /** destroy object
charly 0:562943b05e99 52 *
charly 0:562943b05e99 53 */
charly 0:562943b05e99 54 ~mRotaryEncoder();
charly 0:562943b05e99 55
charly 0:562943b05e99 56 /** Get the actual value of the rotary position
charly 0:562943b05e99 57 *
charly 0:562943b05e99 58 * @return position int value of position
charly 0:562943b05e99 59 */
charly 0:562943b05e99 60 int Get(void);
charly 0:562943b05e99 61 inline operator int() {
charly 0:562943b05e99 62 return Get();
charly 0:562943b05e99 63 }
charly 0:562943b05e99 64
charly 0:562943b05e99 65 /** Set the current position value
charly 0:562943b05e99 66 *
charly 0:562943b05e99 67 * @param value the new position to set
charly 0:562943b05e99 68 *
charly 0:562943b05e99 69 */
charly 0:562943b05e99 70 void Set(int value);
charly 0:562943b05e99 71 inline mRotaryEncoder& operator= ( int value ) {
charly 0:562943b05e99 72 Set(value);
charly 0:562943b05e99 73 return *this;
charly 0:562943b05e99 74 }
charly 0:562943b05e99 75
charly 0:562943b05e99 76 /** attach a function to be called when switch is pressed
charly 7:ec80fd9c0c08 77 *
charly 0:562943b05e99 78 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 79 *
charly 0:562943b05e99 80 * @param fptr Pointer to callback-function
charly 0:562943b05e99 81 */
charly 0:562943b05e99 82 void attachSW(void (*fptr)(void)) {
charly 3:39c2fc4482be 83 m_pinSW->attach_deasserted(fptr);
charly 0:562943b05e99 84 }
charly 0:562943b05e99 85
charly 0:562943b05e99 86 template<typename T>
charly 0:562943b05e99 87 /** attach an object member function to be called when switch is pressed
charly 0:562943b05e99 88 *
charly 0:562943b05e99 89 * @param tptr pointer to object
charly 0:562943b05e99 90 * @param mprt pointer ro member function
charly 0:562943b05e99 91 *
charly 0:562943b05e99 92 */
charly 0:562943b05e99 93 void attachSW(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 94 if ((mptr != NULL) && (tptr != NULL)) {
charly 3:39c2fc4482be 95 m_pinSW->attach_deasserted(tptr, mptr);
charly 0:562943b05e99 96 }
charly 0:562943b05e99 97 }
charly 0:562943b05e99 98
charly 0:562943b05e99 99 /** callback-System for rotation of shaft
charly 7:ec80fd9c0c08 100 *
charly 8:41c44b127443 101 * attach a function to be called when the shaft is rotated
charly 0:562943b05e99 102 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 103 *
charly 0:562943b05e99 104 * @param fprt Pointer to callback-function
charly 0:562943b05e99 105 */
charly 0:562943b05e99 106 void attachROT(void (*fptr)(void)) {
charly 0:562943b05e99 107 rotIsr.attach(fptr);
charly 0:562943b05e99 108 }
charly 0:562943b05e99 109
charly 0:562943b05e99 110
charly 0:562943b05e99 111 template<typename T>
charly 8:41c44b127443 112 /** attach an object member function to be called when shaft is rotated
charly 0:562943b05e99 113 *
charly 0:562943b05e99 114 * @param tptr pointer to object
charly 0:562943b05e99 115 * @param mprt pointer ro member function
charly 0:562943b05e99 116 *
charly 0:562943b05e99 117 */
charly 0:562943b05e99 118 void attachROT(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 119 if ((mptr != NULL) && (tptr != NULL)) {
charly 6:854c349157b0 120 rotIsr.attach(tptr, mptr);
charly 0:562943b05e99 121 }
charly 0:562943b05e99 122 }
charly 0:562943b05e99 123
charly 7:ec80fd9c0c08 124 /** callback-System for rotation of shaft CW
charly 7:ec80fd9c0c08 125 *
charly 8:41c44b127443 126 * attach a function to be called when the shaft is rotated clockwise
charly 7:ec80fd9c0c08 127 * keep this function short, as no interrrupts can occour within
charly 7:ec80fd9c0c08 128 *
charly 7:ec80fd9c0c08 129 * @param fprt Pointer to callback-function
charly 7:ec80fd9c0c08 130 */
charly 7:ec80fd9c0c08 131 void attachROTCW(void (*fptr)(void)) {
charly 7:ec80fd9c0c08 132 rotCWIsr.attach(fptr);
charly 7:ec80fd9c0c08 133 }
charly 7:ec80fd9c0c08 134
charly 7:ec80fd9c0c08 135
charly 7:ec80fd9c0c08 136 template<typename T>
charly 8:41c44b127443 137 /** attach an object member function to be called when shaft is rotated clockwise
charly 7:ec80fd9c0c08 138 *
charly 7:ec80fd9c0c08 139 * @param tptr pointer to object
charly 7:ec80fd9c0c08 140 * @param mprt pointer ro member function
charly 7:ec80fd9c0c08 141 *
charly 7:ec80fd9c0c08 142 */
charly 7:ec80fd9c0c08 143 void attachROTCW(T* tptr, void (T::*mptr)(void)) {
charly 7:ec80fd9c0c08 144 if ((mptr != NULL) && (tptr != NULL)) {
charly 7:ec80fd9c0c08 145 rotCWIsr.attach(tptr, mptr);
charly 7:ec80fd9c0c08 146 }
charly 7:ec80fd9c0c08 147 }
charly 7:ec80fd9c0c08 148
charly 7:ec80fd9c0c08 149 /** callback-System for rotation of shaft CCW
charly 7:ec80fd9c0c08 150 *
charly 8:41c44b127443 151 * attach a function to be called when the shaft is rotated counterclockwise
charly 7:ec80fd9c0c08 152 * keep this function short, as no interrrupts can occour within
charly 7:ec80fd9c0c08 153 *
charly 7:ec80fd9c0c08 154 * @param fprt Pointer to callback-function
charly 7:ec80fd9c0c08 155 */
charly 7:ec80fd9c0c08 156 void attachROTCCW(void (*fptr)(void)) {
charly 7:ec80fd9c0c08 157 rotCCWIsr.attach(fptr);
charly 7:ec80fd9c0c08 158 }
charly 7:ec80fd9c0c08 159
charly 7:ec80fd9c0c08 160
charly 7:ec80fd9c0c08 161 template<typename T>
charly 8:41c44b127443 162 /** attach an object member function to be called when shaft is rotated CCW
charly 7:ec80fd9c0c08 163 *
charly 7:ec80fd9c0c08 164 * @param tptr pointer to object
charly 7:ec80fd9c0c08 165 * @param mprt pointer ro member function
charly 7:ec80fd9c0c08 166 *
charly 7:ec80fd9c0c08 167 */
charly 7:ec80fd9c0c08 168 void attachROTCCW(T* tptr, void (T::*mptr)(void)) {
charly 7:ec80fd9c0c08 169 if ((mptr != NULL) && (tptr != NULL)) {
charly 7:ec80fd9c0c08 170 rotCCWIsr.attach(tptr, mptr);
charly 7:ec80fd9c0c08 171 }
charly 7:ec80fd9c0c08 172 }
charly 0:562943b05e99 173
charly 0:562943b05e99 174 private:
charly 5:75ddffaf3721 175 PinDetect *m_pinA;
charly 0:562943b05e99 176 DigitalIn *m_pinB;
charly 0:562943b05e99 177 volatile int m_position;
charly 0:562943b05e99 178
charly 0:562943b05e99 179 int m_debounceTime_us;
charly 0:562943b05e99 180
charly 5:75ddffaf3721 181
charly 2:f99ac9745a2c 182 PinDetect *m_pinSW;
charly 0:562943b05e99 183
charly 0:562943b05e99 184 void rise(void);
charly 0:562943b05e99 185 void fall(void);
charly 0:562943b05e99 186
charly 0:562943b05e99 187 protected:
charly 0:562943b05e99 188 /**
charly 0:562943b05e99 189 * Callback system.
charly 0:562943b05e99 190 * @ingroup INTERNALS
charly 0:562943b05e99 191 */
charly 8:41c44b127443 192 /**
charly 8:41c44b127443 193 * rotated any direction
charly 8:41c44b127443 194 */
charly 0:562943b05e99 195 FunctionPointer rotIsr;
charly 8:41c44b127443 196 /**
charly 8:41c44b127443 197 * clockwise rotated
charly 8:41c44b127443 198 */
charly 7:ec80fd9c0c08 199 FunctionPointer rotCWIsr;
charly 8:41c44b127443 200
charly 8:41c44b127443 201 /**
charly 8:41c44b127443 202 * counterclockwise rotated
charly 8:41c44b127443 203 */
charly 7:ec80fd9c0c08 204 FunctionPointer rotCCWIsr;
charly 0:562943b05e99 205
charly 0:562943b05e99 206
charly 0:562943b05e99 207 };
charly 0:562943b05e99 208
charly 0:562943b05e99 209
charly 0:562943b05e99 210 #endif