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@2:f99ac9745a2c, 2011-02-01 (annotated)
- Committer:
- charly
- Date:
- Tue Feb 01 19:43:46 2011 +0000
- Revision:
- 2:f99ac9745a2c
- Parent:
- 1:bb7b4e631dff
- Child:
- 3:39c2fc4482be
Changed InterruptIn for switch to PinDetect
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:562943b05e99 | 36 | * |
charly | 0:562943b05e99 | 37 | */ |
charly | 0:562943b05e99 | 38 | class mRotaryEncoder { |
charly | 0:562943b05e99 | 39 | public: |
charly | 0:562943b05e99 | 40 | /** Create a mechanical rotary encoder object connected to the specified pins |
charly | 0:562943b05e99 | 41 | * |
charly | 0:562943b05e99 | 42 | * @param pinA Switch A of quadrature encoder |
charly | 0:562943b05e99 | 43 | * @param pinB Switch B of quadrature encoder |
charly | 0:562943b05e99 | 44 | * @param pinSW Pin for push-button switch |
charly | 0:562943b05e99 | 45 | * @param pullmode mode for pinA pinB and pinSW like DigitalIn.mode |
charly | 0:562943b05e99 | 46 | * @param debounceTime_us time in micro-seconds to wait for bouncing of mechanical switches to end |
charly | 0:562943b05e99 | 47 | */ |
charly | 0:562943b05e99 | 48 | mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000); |
charly | 0:562943b05e99 | 49 | |
charly | 0:562943b05e99 | 50 | /** destroy object |
charly | 0:562943b05e99 | 51 | * |
charly | 0:562943b05e99 | 52 | */ |
charly | 0:562943b05e99 | 53 | ~mRotaryEncoder(); |
charly | 0:562943b05e99 | 54 | |
charly | 0:562943b05e99 | 55 | /** Get the actual value of the rotary position |
charly | 0:562943b05e99 | 56 | * |
charly | 0:562943b05e99 | 57 | * @return position int value of position |
charly | 0:562943b05e99 | 58 | */ |
charly | 0:562943b05e99 | 59 | int Get(void); |
charly | 0:562943b05e99 | 60 | inline operator int() { |
charly | 0:562943b05e99 | 61 | return Get(); |
charly | 0:562943b05e99 | 62 | } |
charly | 0:562943b05e99 | 63 | |
charly | 0:562943b05e99 | 64 | /** Set the current position value |
charly | 0:562943b05e99 | 65 | * |
charly | 0:562943b05e99 | 66 | * @param value the new position to set |
charly | 0:562943b05e99 | 67 | * |
charly | 0:562943b05e99 | 68 | */ |
charly | 0:562943b05e99 | 69 | void Set(int value); |
charly | 0:562943b05e99 | 70 | inline mRotaryEncoder& operator= ( int value ) { |
charly | 0:562943b05e99 | 71 | Set(value); |
charly | 0:562943b05e99 | 72 | return *this; |
charly | 0:562943b05e99 | 73 | } |
charly | 0:562943b05e99 | 74 | |
charly | 0:562943b05e99 | 75 | /** attach a function to be called when switch is pressed |
charly | 0:562943b05e99 | 76 | * keep this function short, as no interrrupts can occour within |
charly | 0:562943b05e99 | 77 | * |
charly | 0:562943b05e99 | 78 | * @param fptr Pointer to callback-function |
charly | 0:562943b05e99 | 79 | */ |
charly | 0:562943b05e99 | 80 | void attachSW(void (*fptr)(void)) { |
charly | 2:f99ac9745a2c | 81 | m_pinSW->attach_asserted(fptr); |
charly | 0:562943b05e99 | 82 | } |
charly | 0:562943b05e99 | 83 | |
charly | 0:562943b05e99 | 84 | template<typename T> |
charly | 0:562943b05e99 | 85 | /** attach an object member function to be called when switch is pressed |
charly | 0:562943b05e99 | 86 | * |
charly | 0:562943b05e99 | 87 | * @param tptr pointer to object |
charly | 0:562943b05e99 | 88 | * @param mprt pointer ro member function |
charly | 0:562943b05e99 | 89 | * |
charly | 0:562943b05e99 | 90 | */ |
charly | 0:562943b05e99 | 91 | void attachSW(T* tptr, void (T::*mptr)(void)) { |
charly | 0:562943b05e99 | 92 | if ((mptr != NULL) && (tptr != NULL)) { |
charly | 0:562943b05e99 | 93 | m_pinSW->fall(tptr, mptr); |
charly | 0:562943b05e99 | 94 | } |
charly | 0:562943b05e99 | 95 | } |
charly | 0:562943b05e99 | 96 | |
charly | 0:562943b05e99 | 97 | /** callback-System for rotation of shaft |
charly | 0:562943b05e99 | 98 | * attach a function to be called when the shaft is rotaded |
charly | 0:562943b05e99 | 99 | * keep this function short, as no interrrupts can occour within |
charly | 0:562943b05e99 | 100 | * |
charly | 0:562943b05e99 | 101 | * @param fprt Pointer to callback-function |
charly | 0:562943b05e99 | 102 | */ |
charly | 0:562943b05e99 | 103 | void attachROT(void (*fptr)(void)) { |
charly | 0:562943b05e99 | 104 | rotIsr.attach(fptr); |
charly | 0:562943b05e99 | 105 | } |
charly | 0:562943b05e99 | 106 | |
charly | 0:562943b05e99 | 107 | |
charly | 0:562943b05e99 | 108 | template<typename T> |
charly | 0:562943b05e99 | 109 | /** attach an object member function to be called when shaft is rotaded |
charly | 0:562943b05e99 | 110 | * |
charly | 0:562943b05e99 | 111 | * @param tptr pointer to object |
charly | 0:562943b05e99 | 112 | * @param mprt pointer ro member function |
charly | 0:562943b05e99 | 113 | * |
charly | 0:562943b05e99 | 114 | */ |
charly | 0:562943b05e99 | 115 | void attachROT(T* tptr, void (T::*mptr)(void)) { |
charly | 0:562943b05e99 | 116 | if ((mptr != NULL) && (tptr != NULL)) { |
charly | 0:562943b05e99 | 117 | rotISR.attach(tptr, mptr); |
charly | 0:562943b05e99 | 118 | } |
charly | 0:562943b05e99 | 119 | } |
charly | 0:562943b05e99 | 120 | |
charly | 0:562943b05e99 | 121 | |
charly | 0:562943b05e99 | 122 | private: |
charly | 0:562943b05e99 | 123 | InterruptIn *m_pinA; |
charly | 0:562943b05e99 | 124 | DigitalIn *m_pinB; |
charly | 0:562943b05e99 | 125 | volatile int m_position; |
charly | 0:562943b05e99 | 126 | |
charly | 0:562943b05e99 | 127 | int m_debounceTime_us; |
charly | 0:562943b05e99 | 128 | |
charly | 2:f99ac9745a2c | 129 | //InterruptIn *m_pinSW; |
charly | 2:f99ac9745a2c | 130 | PinDetect *m_pinSW; |
charly | 0:562943b05e99 | 131 | |
charly | 0:562943b05e99 | 132 | void rise(void); |
charly | 0:562943b05e99 | 133 | void fall(void); |
charly | 0:562943b05e99 | 134 | |
charly | 0:562943b05e99 | 135 | protected: |
charly | 0:562943b05e99 | 136 | /** |
charly | 0:562943b05e99 | 137 | * Callback system. |
charly | 0:562943b05e99 | 138 | * @ingroup INTERNALS |
charly | 0:562943b05e99 | 139 | */ |
charly | 0:562943b05e99 | 140 | FunctionPointer rotIsr; |
charly | 0:562943b05e99 | 141 | |
charly | 0:562943b05e99 | 142 | |
charly | 0:562943b05e99 | 143 | }; |
charly | 0:562943b05e99 | 144 | |
charly | 0:562943b05e99 | 145 | |
charly | 0:562943b05e99 | 146 | #endif |