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