Fork of mRotaryEncoder for mdeb-os. uses newer version of PinDetect. Testprogram: https://os.mbed.com/users/charly/code/mRotaryEncoder_HelloWorld-os/

Dependencies:   PinDetect

Dependents:   mRotaryEncoder_HelloWorld-os TMC2209-Test2

Committer:
charly
Date:
Thu Mar 18 19:31:29 2021 +0000
Revision:
13:d22167ec460c
Parent:
12:1925aac090b7
Moved PinDetect into Library

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