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:
charly
Date:
Wed Feb 09 20:18:34 2011 +0000
Revision:
5:75ddffaf3721
Parent:
3:39c2fc4482be
Child:
6:854c349157b0
Changed InterruptIn to Pindetect for Rotary pinA

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 0:562943b05e99 77 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 78 *
charly 0:562943b05e99 79 * @param fptr Pointer to callback-function
charly 0:562943b05e99 80 */
charly 0:562943b05e99 81 void attachSW(void (*fptr)(void)) {
charly 3:39c2fc4482be 82 m_pinSW->attach_deasserted(fptr);
charly 0:562943b05e99 83 }
charly 0:562943b05e99 84
charly 0:562943b05e99 85 template<typename T>
charly 0:562943b05e99 86 /** attach an object member function to be called when switch is pressed
charly 0:562943b05e99 87 *
charly 0:562943b05e99 88 * @param tptr pointer to object
charly 0:562943b05e99 89 * @param mprt pointer ro member function
charly 0:562943b05e99 90 *
charly 0:562943b05e99 91 */
charly 0:562943b05e99 92 void attachSW(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 93 if ((mptr != NULL) && (tptr != NULL)) {
charly 3:39c2fc4482be 94 m_pinSW->attach_deasserted(tptr, mptr);
charly 0:562943b05e99 95 }
charly 0:562943b05e99 96 }
charly 0:562943b05e99 97
charly 0:562943b05e99 98 /** callback-System for rotation of shaft
charly 0:562943b05e99 99 * attach a function to be called when the shaft is rotaded
charly 0:562943b05e99 100 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 101 *
charly 0:562943b05e99 102 * @param fprt Pointer to callback-function
charly 0:562943b05e99 103 */
charly 0:562943b05e99 104 void attachROT(void (*fptr)(void)) {
charly 0:562943b05e99 105 rotIsr.attach(fptr);
charly 0:562943b05e99 106 }
charly 0:562943b05e99 107
charly 0:562943b05e99 108
charly 0:562943b05e99 109 template<typename T>
charly 0:562943b05e99 110 /** attach an object member function to be called when shaft is rotaded
charly 0:562943b05e99 111 *
charly 0:562943b05e99 112 * @param tptr pointer to object
charly 0:562943b05e99 113 * @param mprt pointer ro member function
charly 0:562943b05e99 114 *
charly 0:562943b05e99 115 */
charly 0:562943b05e99 116 void attachROT(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 117 if ((mptr != NULL) && (tptr != NULL)) {
charly 0:562943b05e99 118 rotISR.attach(tptr, mptr);
charly 0:562943b05e99 119 }
charly 0:562943b05e99 120 }
charly 0:562943b05e99 121
charly 0:562943b05e99 122
charly 0:562943b05e99 123 private:
charly 5:75ddffaf3721 124 PinDetect *m_pinA;
charly 0:562943b05e99 125 DigitalIn *m_pinB;
charly 0:562943b05e99 126 volatile int m_position;
charly 0:562943b05e99 127
charly 0:562943b05e99 128 int m_debounceTime_us;
charly 0:562943b05e99 129
charly 5:75ddffaf3721 130
charly 2:f99ac9745a2c 131 PinDetect *m_pinSW;
charly 0:562943b05e99 132
charly 0:562943b05e99 133 void rise(void);
charly 0:562943b05e99 134 void fall(void);
charly 0:562943b05e99 135
charly 0:562943b05e99 136 protected:
charly 0:562943b05e99 137 /**
charly 0:562943b05e99 138 * Callback system.
charly 0:562943b05e99 139 * @ingroup INTERNALS
charly 0:562943b05e99 140 */
charly 0:562943b05e99 141 FunctionPointer rotIsr;
charly 0:562943b05e99 142
charly 0:562943b05e99 143
charly 0:562943b05e99 144 };
charly 0:562943b05e99 145
charly 0:562943b05e99 146
charly 0:562943b05e99 147 #endif