Skip to content

Commit 21b2ff5

Browse files
committed
Add method to change encoder encoding type.
1 parent 721161c commit 21b2ff5

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

src/EncoderClass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,14 @@ int EncoderClass::getRevolutions(int channel) {
6363
}
6464
}
6565

66+
void EncoderClass::setEncoding(int channel, QEI::Encoding encoding) {
67+
switch (channel) {
68+
case 0:
69+
return _enc0.setEncoding(encoding);
70+
case 1:
71+
return _enc1.setEncoding(encoding);
72+
}
73+
}
74+
6675
EncoderClass MachineControl_Encoders;
6776
/**** END OF FILE ****/

src/EncoderClass.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ class EncoderClass {
9494
*/
9595
int getRevolutions(int channel);
9696

97+
/**
98+
* @brief Set the encoding type for the specified encoder channel.
99+
*
100+
* This method changes the encoding type from the default X2_ENCODING.
101+
*
102+
* @param channel The encoder channel (0 or 1) to be changed.
103+
* @param encoding The encoding type.
104+
*/
105+
void setEncoding(int channel, QEI::Encoding encoding);
106+
97107
private:
98108
QEI _enc0; // QEI object for encoder 0
99109
QEI _enc1; // QEI object for encoder 1

src/utility/QEI/QEI.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,7 @@ QEI::QEI(PinName channelA,
148148
currState_ = (chanA << 1) | (chanB);
149149
prevState_ = currState_;
150150

151-
//X2 encoding uses interrupts on only channel A.
152-
//X4 encoding uses interrupts on channel A,
153-
//and on channel B.
154-
channelA_.rise(mbed::callback(this, &QEI::encode));
155-
if(encoding != X1_ENCODING){
156-
channelA_.fall(mbed::callback(this, &QEI::encode));
157-
}
158-
159-
//If we're using X4 encoding, then attach interrupts to channel B too.
160-
if (encoding == X4_ENCODING) {
161-
channelB_.rise(mbed::callback(this, &QEI::encode));
162-
channelB_.fall(mbed::callback(this, &QEI::encode));
163-
}
151+
setEncoding(encoding);
164152
//Index is optional.
165153
if (index != NC) {
166154
index_.rise(mbed::callback(this, &QEI::index));
@@ -193,6 +181,24 @@ int QEI::getRevolutions(void) {
193181

194182
}
195183

184+
void QEI::setEncoding(Encoding encoding) {
185+
//X2 encoding uses interrupts on only channel A.
186+
//X4 encoding uses interrupts on channel A,
187+
//and on channel B.
188+
channelA_.rise(mbed::callback(this, &QEI::encode));
189+
if(encoding != X1_ENCODING){
190+
channelA_.fall(mbed::callback(this, &QEI::encode));
191+
} else {
192+
channelA_.fall(0);
193+
}
194+
195+
//If we're using X4 encoding, then attach interrupts to channel B too.
196+
if (encoding == X4_ENCODING) {
197+
channelB_.rise(mbed::callback(this, &QEI::encode));
198+
channelB_.fall(mbed::callback(this, &QEI::encode));
199+
}
200+
}
201+
196202
// +-------------+
197203
// | X1 Encoding |
198204
// +-------------+

src/utility/QEI/QEI.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class QEI {
176176
* of only channel A where as X4 uses them on both
177177
* channels.
178178
*/
179-
QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X2_ENCODING);
179+
QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X4_ENCODING);
180180

181181
/**
182182
* Reset the encoder.
@@ -208,6 +208,13 @@ class QEI {
208208
*/
209209
int getRevolutions(void);
210210

211+
/**
212+
* Set the ecoding type of the encoder.
213+
*
214+
* Changes the type of encoding used by the encoder from the default X2_ENCODING.
215+
*/
216+
void setEncoding(Encoding encoding);
217+
211218
private:
212219

213220
/**

0 commit comments

Comments
 (0)