@@ -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// +-------------+
0 commit comments