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