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:
Mon Nov 29 21:38:17 2010 +0000
Revision:
1:bb7b4e631dff
Parent:
0:562943b05e99
Child:
2:f99ac9745a2c
doc

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 0:562943b05e99 5
charly 0:562943b05e99 6
charly 1:bb7b4e631dff 7 /** This Class handles a rotary encoder with mechanical switches and an integrated pushbutton
charly 0:562943b05e99 8 * It uses two pins, one creating an interrupt on change.
charly 0:562943b05e99 9 * Rotation direction is determined by checking the state of the other pin.
charly 0:562943b05e99 10 * Additionally a pushbutton switch is detected
charly 0:562943b05e99 11 *
charly 0:562943b05e99 12 * Operating the encoder changes an internal integer value that can be read
charly 0:562943b05e99 13 * by Get() or the operator int() functions.
charly 0:562943b05e99 14 * A new value can be set by Set(value) or opperator=.
charly 0:562943b05e99 15 *
charly 0:562943b05e99 16 * Autor: Thomas Raab (Raabinator)
charly 1:bb7b4e631dff 17 * Extended by Karl Zweimueller (charly)
charly 0:562943b05e99 18 *
charly 0:562943b05e99 19 * Dent steady point ! ! !
charly 0:562943b05e99 20 * +-----+ +-----+
charly 0:562943b05e99 21 * pinA (interrupt) | | | |
charly 0:562943b05e99 22 * --+ +-----+ +---
charly 0:562943b05e99 23 * +-----+ +-----+
charly 0:562943b05e99 24 * pinB | | | |
charly 0:562943b05e99 25 * ----+ +-----+ +-
charly 0:562943b05e99 26 * --> C.W
charly 0:562943b05e99 27 * CW: increases position value
charly 0:562943b05e99 28 * CCW: decreases position value
charly 0:562943b05e99 29 *
charly 0:562943b05e99 30 * changelog:
charly 0:562943b05e99 31 *
charly 0:562943b05e99 32 * 09. Nov. 2010
charly 0:562943b05e99 33 * First version published Thomas Raab raabinator
charly 0:562943b05e99 34 * 26.11.2010 extended by charly - pushbutton, pullmode, debounce, callback-system
charly 0:562943b05e99 35 *
charly 0:562943b05e99 36 */
charly 0:562943b05e99 37 class mRotaryEncoder {
charly 0:562943b05e99 38 public:
charly 0:562943b05e99 39 /** Create a mechanical rotary encoder object connected to the specified pins
charly 0:562943b05e99 40 *
charly 0:562943b05e99 41 * @param pinA Switch A of quadrature encoder
charly 0:562943b05e99 42 * @param pinB Switch B of quadrature encoder
charly 0:562943b05e99 43 * @param pinSW Pin for push-button switch
charly 0:562943b05e99 44 * @param pullmode mode for pinA pinB and pinSW like DigitalIn.mode
charly 0:562943b05e99 45 * @param debounceTime_us time in micro-seconds to wait for bouncing of mechanical switches to end
charly 0:562943b05e99 46 */
charly 0:562943b05e99 47 mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000);
charly 0:562943b05e99 48
charly 0:562943b05e99 49 /** destroy object
charly 0:562943b05e99 50 *
charly 0:562943b05e99 51 */
charly 0:562943b05e99 52 ~mRotaryEncoder();
charly 0:562943b05e99 53
charly 0:562943b05e99 54 /** Get the actual value of the rotary position
charly 0:562943b05e99 55 *
charly 0:562943b05e99 56 * @return position int value of position
charly 0:562943b05e99 57 */
charly 0:562943b05e99 58 int Get(void);
charly 0:562943b05e99 59 inline operator int() {
charly 0:562943b05e99 60 return Get();
charly 0:562943b05e99 61 }
charly 0:562943b05e99 62
charly 0:562943b05e99 63 /** Set the current position value
charly 0:562943b05e99 64 *
charly 0:562943b05e99 65 * @param value the new position to set
charly 0:562943b05e99 66 *
charly 0:562943b05e99 67 */
charly 0:562943b05e99 68 void Set(int value);
charly 0:562943b05e99 69 inline mRotaryEncoder& operator= ( int value ) {
charly 0:562943b05e99 70 Set(value);
charly 0:562943b05e99 71 return *this;
charly 0:562943b05e99 72 }
charly 0:562943b05e99 73
charly 0:562943b05e99 74 /** attach a function to be called when switch is pressed
charly 0:562943b05e99 75 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 76 *
charly 0:562943b05e99 77 * @param fptr Pointer to callback-function
charly 0:562943b05e99 78 */
charly 0:562943b05e99 79 void attachSW(void (*fptr)(void)) {
charly 0:562943b05e99 80 m_pinSW->fall(fptr);
charly 0:562943b05e99 81 }
charly 0:562943b05e99 82
charly 0:562943b05e99 83 template<typename T>
charly 0:562943b05e99 84 /** attach an object member function to be called when switch is pressed
charly 0:562943b05e99 85 *
charly 0:562943b05e99 86 * @param tptr pointer to object
charly 0:562943b05e99 87 * @param mprt pointer ro member function
charly 0:562943b05e99 88 *
charly 0:562943b05e99 89 */
charly 0:562943b05e99 90 void attachSW(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 91 if ((mptr != NULL) && (tptr != NULL)) {
charly 0:562943b05e99 92 m_pinSW->fall(tptr, mptr);
charly 0:562943b05e99 93 }
charly 0:562943b05e99 94 }
charly 0:562943b05e99 95
charly 0:562943b05e99 96 /** callback-System for rotation of shaft
charly 0:562943b05e99 97 * attach a function to be called when the shaft is rotaded
charly 0:562943b05e99 98 * keep this function short, as no interrrupts can occour within
charly 0:562943b05e99 99 *
charly 0:562943b05e99 100 * @param fprt Pointer to callback-function
charly 0:562943b05e99 101 */
charly 0:562943b05e99 102 void attachROT(void (*fptr)(void)) {
charly 0:562943b05e99 103 rotIsr.attach(fptr);
charly 0:562943b05e99 104 }
charly 0:562943b05e99 105
charly 0:562943b05e99 106
charly 0:562943b05e99 107 template<typename T>
charly 0:562943b05e99 108 /** attach an object member function to be called when shaft is rotaded
charly 0:562943b05e99 109 *
charly 0:562943b05e99 110 * @param tptr pointer to object
charly 0:562943b05e99 111 * @param mprt pointer ro member function
charly 0:562943b05e99 112 *
charly 0:562943b05e99 113 */
charly 0:562943b05e99 114 void attachROT(T* tptr, void (T::*mptr)(void)) {
charly 0:562943b05e99 115 if ((mptr != NULL) && (tptr != NULL)) {
charly 0:562943b05e99 116 rotISR.attach(tptr, mptr);
charly 0:562943b05e99 117 }
charly 0:562943b05e99 118 }
charly 0:562943b05e99 119
charly 0:562943b05e99 120
charly 0:562943b05e99 121 private:
charly 0:562943b05e99 122 InterruptIn *m_pinA;
charly 0:562943b05e99 123 DigitalIn *m_pinB;
charly 0:562943b05e99 124 volatile int m_position;
charly 0:562943b05e99 125
charly 0:562943b05e99 126 int m_debounceTime_us;
charly 0:562943b05e99 127
charly 0:562943b05e99 128 InterruptIn *m_pinSW;
charly 0:562943b05e99 129
charly 0:562943b05e99 130 void rise(void);
charly 0:562943b05e99 131 void fall(void);
charly 0:562943b05e99 132
charly 0:562943b05e99 133 protected:
charly 0:562943b05e99 134 /**
charly 0:562943b05e99 135 * Callback system.
charly 0:562943b05e99 136 * @ingroup INTERNALS
charly 0:562943b05e99 137 */
charly 0:562943b05e99 138 FunctionPointer rotIsr;
charly 0:562943b05e99 139
charly 0:562943b05e99 140
charly 0:562943b05e99 141 };
charly 0:562943b05e99 142
charly 0:562943b05e99 143
charly 0:562943b05e99 144 #endif