rotary encoder with push button based on work I found elswhere.
QEI.cpp@0:ef2f1bbeb8e1, 2017-12-18 (annotated)
- Committer:
- bomilkar
- Date:
- Mon Dec 18 16:54:00 2017 +0000
- Revision:
- 0:ef2f1bbeb8e1
Initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bomilkar | 0:ef2f1bbeb8e1 | 1 | /** |
bomilkar | 0:ef2f1bbeb8e1 | 2 | * @author Aaron Berk |
bomilkar | 0:ef2f1bbeb8e1 | 3 | * |
bomilkar | 0:ef2f1bbeb8e1 | 4 | * @section LICENSE |
bomilkar | 0:ef2f1bbeb8e1 | 5 | * |
bomilkar | 0:ef2f1bbeb8e1 | 6 | * Copyright (c) 2010 ARM Limited |
bomilkar | 0:ef2f1bbeb8e1 | 7 | * |
bomilkar | 0:ef2f1bbeb8e1 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
bomilkar | 0:ef2f1bbeb8e1 | 9 | * of this software and associated documentation files (the "Software"), to deal |
bomilkar | 0:ef2f1bbeb8e1 | 10 | * in the Software without restriction, including without limitation the rights |
bomilkar | 0:ef2f1bbeb8e1 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
bomilkar | 0:ef2f1bbeb8e1 | 12 | * copies of the Software, and to permit persons to whom the Software is |
bomilkar | 0:ef2f1bbeb8e1 | 13 | * furnished to do so, subject to the following conditions: |
bomilkar | 0:ef2f1bbeb8e1 | 14 | * |
bomilkar | 0:ef2f1bbeb8e1 | 15 | * The above copyright notice and this permission notice shall be included in |
bomilkar | 0:ef2f1bbeb8e1 | 16 | * all copies or substantial portions of the Software. |
bomilkar | 0:ef2f1bbeb8e1 | 17 | * |
bomilkar | 0:ef2f1bbeb8e1 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
bomilkar | 0:ef2f1bbeb8e1 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
bomilkar | 0:ef2f1bbeb8e1 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
bomilkar | 0:ef2f1bbeb8e1 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
bomilkar | 0:ef2f1bbeb8e1 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bomilkar | 0:ef2f1bbeb8e1 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
bomilkar | 0:ef2f1bbeb8e1 | 24 | * THE SOFTWARE. |
bomilkar | 0:ef2f1bbeb8e1 | 25 | * |
bomilkar | 0:ef2f1bbeb8e1 | 26 | * @section DESCRIPTION |
bomilkar | 0:ef2f1bbeb8e1 | 27 | * |
bomilkar | 0:ef2f1bbeb8e1 | 28 | * Quadrature Encoder Interface. |
bomilkar | 0:ef2f1bbeb8e1 | 29 | * |
bomilkar | 0:ef2f1bbeb8e1 | 30 | * A quadrature encoder consists of two code tracks on a disc which are 90 |
bomilkar | 0:ef2f1bbeb8e1 | 31 | * degrees out of phase. It can be used to determine how far a wheel has |
bomilkar | 0:ef2f1bbeb8e1 | 32 | * rotated, relative to a known starting position. |
bomilkar | 0:ef2f1bbeb8e1 | 33 | * |
bomilkar | 0:ef2f1bbeb8e1 | 34 | * Only one code track changes at a time leading to a more robust system than |
bomilkar | 0:ef2f1bbeb8e1 | 35 | * a single track, because any jitter around any edge won't cause a state |
bomilkar | 0:ef2f1bbeb8e1 | 36 | * change as the other track will remain constant. |
bomilkar | 0:ef2f1bbeb8e1 | 37 | * |
bomilkar | 0:ef2f1bbeb8e1 | 38 | * Encoders can be a homebrew affair, consisting of infrared emitters/receivers |
bomilkar | 0:ef2f1bbeb8e1 | 39 | * and paper code tracks consisting of alternating black and white sections; |
bomilkar | 0:ef2f1bbeb8e1 | 40 | * alternatively, complete disk and PCB emitter/receiver encoder systems can |
bomilkar | 0:ef2f1bbeb8e1 | 41 | * be bought, but the interface, regardless of implementation is the same. |
bomilkar | 0:ef2f1bbeb8e1 | 42 | * |
bomilkar | 0:ef2f1bbeb8e1 | 43 | * +-----+ +-----+ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 44 | * Channel A | ^ | | | | | |
bomilkar | 0:ef2f1bbeb8e1 | 45 | * ---+ ^ +-----+ +-----+ +----- |
bomilkar | 0:ef2f1bbeb8e1 | 46 | * ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 47 | * ^ +-----+ +-----+ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 48 | * Channel B ^ | | | | | | |
bomilkar | 0:ef2f1bbeb8e1 | 49 | * ------+ +-----+ +-----+ +----- |
bomilkar | 0:ef2f1bbeb8e1 | 50 | * ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 51 | * ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 52 | * 90deg |
bomilkar | 0:ef2f1bbeb8e1 | 53 | * |
bomilkar | 0:ef2f1bbeb8e1 | 54 | * The interface uses X2 encoding by default which calculates the pulse count |
bomilkar | 0:ef2f1bbeb8e1 | 55 | * based on reading the current state after each rising and falling edge of |
bomilkar | 0:ef2f1bbeb8e1 | 56 | * channel A. |
bomilkar | 0:ef2f1bbeb8e1 | 57 | * |
bomilkar | 0:ef2f1bbeb8e1 | 58 | * +-----+ +-----+ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 59 | * Channel A | | | | | | |
bomilkar | 0:ef2f1bbeb8e1 | 60 | * ---+ +-----+ +-----+ +----- |
bomilkar | 0:ef2f1bbeb8e1 | 61 | * ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 62 | * ^ +-----+ ^ +-----+ ^ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 63 | * Channel B ^ | ^ | ^ | ^ | ^ | | |
bomilkar | 0:ef2f1bbeb8e1 | 64 | * ------+ ^ +-----+ ^ +-----+ +-- |
bomilkar | 0:ef2f1bbeb8e1 | 65 | * ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 66 | * ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 67 | * Pulse count 0 1 2 3 4 5 ... |
bomilkar | 0:ef2f1bbeb8e1 | 68 | * |
bomilkar | 0:ef2f1bbeb8e1 | 69 | * This interface can also use X4 encoding which calculates the pulse count |
bomilkar | 0:ef2f1bbeb8e1 | 70 | * based on reading the current state after each rising and falling edge of |
bomilkar | 0:ef2f1bbeb8e1 | 71 | * either channel. |
bomilkar | 0:ef2f1bbeb8e1 | 72 | * |
bomilkar | 0:ef2f1bbeb8e1 | 73 | * +-----+ +-----+ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 74 | * Channel A | | | | | | |
bomilkar | 0:ef2f1bbeb8e1 | 75 | * ---+ +-----+ +-----+ +----- |
bomilkar | 0:ef2f1bbeb8e1 | 76 | * ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 77 | * ^ +-----+ ^ +-----+ ^ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 78 | * Channel B ^ | ^ | ^ | ^ | ^ | | |
bomilkar | 0:ef2f1bbeb8e1 | 79 | * ------+ ^ +-----+ ^ +-----+ +-- |
bomilkar | 0:ef2f1bbeb8e1 | 80 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 81 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 82 | * Pulse count 0 1 2 3 4 5 6 7 8 9 ... |
bomilkar | 0:ef2f1bbeb8e1 | 83 | * |
bomilkar | 0:ef2f1bbeb8e1 | 84 | * It defaults |
bomilkar | 0:ef2f1bbeb8e1 | 85 | * |
bomilkar | 0:ef2f1bbeb8e1 | 86 | * An optional index channel can be used which determines when a full |
bomilkar | 0:ef2f1bbeb8e1 | 87 | * revolution has occured. |
bomilkar | 0:ef2f1bbeb8e1 | 88 | * |
bomilkar | 0:ef2f1bbeb8e1 | 89 | * If a 4 pules per revolution encoder was used, with X4 encoding, |
bomilkar | 0:ef2f1bbeb8e1 | 90 | * the following would be observed. |
bomilkar | 0:ef2f1bbeb8e1 | 91 | * |
bomilkar | 0:ef2f1bbeb8e1 | 92 | * +-----+ +-----+ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 93 | * Channel A | | | | | | |
bomilkar | 0:ef2f1bbeb8e1 | 94 | * ---+ +-----+ +-----+ +----- |
bomilkar | 0:ef2f1bbeb8e1 | 95 | * ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 96 | * ^ +-----+ ^ +-----+ ^ +-----+ |
bomilkar | 0:ef2f1bbeb8e1 | 97 | * Channel B ^ | ^ | ^ | ^ | ^ | | |
bomilkar | 0:ef2f1bbeb8e1 | 98 | * ------+ ^ +-----+ ^ +-----+ +-- |
bomilkar | 0:ef2f1bbeb8e1 | 99 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 100 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 101 | * ^ ^ ^ +--+ ^ ^ +--+ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 102 | * ^ ^ ^ | | ^ ^ | | ^ |
bomilkar | 0:ef2f1bbeb8e1 | 103 | * Index ------------+ +--------+ +----------- |
bomilkar | 0:ef2f1bbeb8e1 | 104 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
bomilkar | 0:ef2f1bbeb8e1 | 105 | * Pulse count 0 1 2 3 4 5 6 7 8 9 ... |
bomilkar | 0:ef2f1bbeb8e1 | 106 | * Rev. count 0 1 2 |
bomilkar | 0:ef2f1bbeb8e1 | 107 | * |
bomilkar | 0:ef2f1bbeb8e1 | 108 | * Rotational position in degrees can be calculated by: |
bomilkar | 0:ef2f1bbeb8e1 | 109 | * |
bomilkar | 0:ef2f1bbeb8e1 | 110 | * (pulse count / X * N) * 360 |
bomilkar | 0:ef2f1bbeb8e1 | 111 | * |
bomilkar | 0:ef2f1bbeb8e1 | 112 | * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number |
bomilkar | 0:ef2f1bbeb8e1 | 113 | * of pulses per revolution. |
bomilkar | 0:ef2f1bbeb8e1 | 114 | * |
bomilkar | 0:ef2f1bbeb8e1 | 115 | * Linear position can be calculated by: |
bomilkar | 0:ef2f1bbeb8e1 | 116 | * |
bomilkar | 0:ef2f1bbeb8e1 | 117 | * (pulse count / X * N) * (1 / PPI) |
bomilkar | 0:ef2f1bbeb8e1 | 118 | * |
bomilkar | 0:ef2f1bbeb8e1 | 119 | * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of |
bomilkar | 0:ef2f1bbeb8e1 | 120 | * pulses per revolution, and PPI is pulses per inch, or the equivalent for |
bomilkar | 0:ef2f1bbeb8e1 | 121 | * any other unit of displacement. PPI can be calculated by taking the |
bomilkar | 0:ef2f1bbeb8e1 | 122 | * circumference of the wheel or encoder disk and dividing it by the number |
bomilkar | 0:ef2f1bbeb8e1 | 123 | * of pulses per revolution. |
bomilkar | 0:ef2f1bbeb8e1 | 124 | */ |
bomilkar | 0:ef2f1bbeb8e1 | 125 | |
bomilkar | 0:ef2f1bbeb8e1 | 126 | /** |
bomilkar | 0:ef2f1bbeb8e1 | 127 | * Includes |
bomilkar | 0:ef2f1bbeb8e1 | 128 | */ |
bomilkar | 0:ef2f1bbeb8e1 | 129 | #include "QEI.h" |
bomilkar | 0:ef2f1bbeb8e1 | 130 | |
bomilkar | 0:ef2f1bbeb8e1 | 131 | QEI::QEI(PinName channelA, |
bomilkar | 0:ef2f1bbeb8e1 | 132 | PinName channelB, |
bomilkar | 0:ef2f1bbeb8e1 | 133 | int pulsesPerRev, |
bomilkar | 0:ef2f1bbeb8e1 | 134 | Encoding encoding) : channelA_(channelA), channelB_(channelB) |
bomilkar | 0:ef2f1bbeb8e1 | 135 | { |
bomilkar | 0:ef2f1bbeb8e1 | 136 | |
bomilkar | 0:ef2f1bbeb8e1 | 137 | // channelA_.mode(PullUp); |
bomilkar | 0:ef2f1bbeb8e1 | 138 | // channelB_.mode(PullUp); |
bomilkar | 0:ef2f1bbeb8e1 | 139 | |
bomilkar | 0:ef2f1bbeb8e1 | 140 | pulses_ = 0; |
bomilkar | 0:ef2f1bbeb8e1 | 141 | pulsesPerRev_ = pulsesPerRev; |
bomilkar | 0:ef2f1bbeb8e1 | 142 | encoding_ = encoding; |
bomilkar | 0:ef2f1bbeb8e1 | 143 | |
bomilkar | 0:ef2f1bbeb8e1 | 144 | //Workout what the current state is. |
bomilkar | 0:ef2f1bbeb8e1 | 145 | int chanA = channelA_.read(); |
bomilkar | 0:ef2f1bbeb8e1 | 146 | int chanB = channelB_.read(); |
bomilkar | 0:ef2f1bbeb8e1 | 147 | |
bomilkar | 0:ef2f1bbeb8e1 | 148 | //2-bit state. |
bomilkar | 0:ef2f1bbeb8e1 | 149 | currState_ = (chanA << 1) | (chanB); |
bomilkar | 0:ef2f1bbeb8e1 | 150 | prevState_ = currState_; |
bomilkar | 0:ef2f1bbeb8e1 | 151 | |
bomilkar | 0:ef2f1bbeb8e1 | 152 | //X2 encoding uses interrupts on only channel A. |
bomilkar | 0:ef2f1bbeb8e1 | 153 | //X4 encoding uses interrupts on channel A, |
bomilkar | 0:ef2f1bbeb8e1 | 154 | //and on channel B. |
bomilkar | 0:ef2f1bbeb8e1 | 155 | |
bomilkar | 0:ef2f1bbeb8e1 | 156 | channelA_.rise(callback(this, &QEI::encode)); |
bomilkar | 0:ef2f1bbeb8e1 | 157 | channelA_.fall(callback(this, &QEI::encode)); |
bomilkar | 0:ef2f1bbeb8e1 | 158 | |
bomilkar | 0:ef2f1bbeb8e1 | 159 | //If we're using X4 encoding, then attach interrupts to channel B too. |
bomilkar | 0:ef2f1bbeb8e1 | 160 | if (encoding == X4_ENCODING) { |
bomilkar | 0:ef2f1bbeb8e1 | 161 | channelB_.rise(callback(this, &QEI::encode)); |
bomilkar | 0:ef2f1bbeb8e1 | 162 | channelB_.fall(callback(this, &QEI::encode)); |
bomilkar | 0:ef2f1bbeb8e1 | 163 | } |
bomilkar | 0:ef2f1bbeb8e1 | 164 | |
bomilkar | 0:ef2f1bbeb8e1 | 165 | /* |
bomilkar | 0:ef2f1bbeb8e1 | 166 | //Index is optional. |
bomilkar | 0:ef2f1bbeb8e1 | 167 | if (index != NC) { |
bomilkar | 0:ef2f1bbeb8e1 | 168 | index_.rise(this, &QEI::index); |
bomilkar | 0:ef2f1bbeb8e1 | 169 | } |
bomilkar | 0:ef2f1bbeb8e1 | 170 | */ |
bomilkar | 0:ef2f1bbeb8e1 | 171 | } |
bomilkar | 0:ef2f1bbeb8e1 | 172 | |
bomilkar | 0:ef2f1bbeb8e1 | 173 | void QEI::reset(void) { |
bomilkar | 0:ef2f1bbeb8e1 | 174 | |
bomilkar | 0:ef2f1bbeb8e1 | 175 | pulses_ = 0; |
bomilkar | 0:ef2f1bbeb8e1 | 176 | |
bomilkar | 0:ef2f1bbeb8e1 | 177 | } |
bomilkar | 0:ef2f1bbeb8e1 | 178 | |
bomilkar | 0:ef2f1bbeb8e1 | 179 | int QEI::getCurrentState(void) { |
bomilkar | 0:ef2f1bbeb8e1 | 180 | |
bomilkar | 0:ef2f1bbeb8e1 | 181 | return currState_; |
bomilkar | 0:ef2f1bbeb8e1 | 182 | |
bomilkar | 0:ef2f1bbeb8e1 | 183 | } |
bomilkar | 0:ef2f1bbeb8e1 | 184 | |
bomilkar | 0:ef2f1bbeb8e1 | 185 | int QEI::getPulses(void) { |
bomilkar | 0:ef2f1bbeb8e1 | 186 | |
bomilkar | 0:ef2f1bbeb8e1 | 187 | return pulses_; |
bomilkar | 0:ef2f1bbeb8e1 | 188 | |
bomilkar | 0:ef2f1bbeb8e1 | 189 | } |
bomilkar | 0:ef2f1bbeb8e1 | 190 | |
bomilkar | 0:ef2f1bbeb8e1 | 191 | |
bomilkar | 0:ef2f1bbeb8e1 | 192 | // +-------------+ |
bomilkar | 0:ef2f1bbeb8e1 | 193 | // | X2 Encoding | |
bomilkar | 0:ef2f1bbeb8e1 | 194 | // +-------------+ |
bomilkar | 0:ef2f1bbeb8e1 | 195 | // |
bomilkar | 0:ef2f1bbeb8e1 | 196 | // When observing states two patterns will appear: |
bomilkar | 0:ef2f1bbeb8e1 | 197 | // |
bomilkar | 0:ef2f1bbeb8e1 | 198 | // Counter clockwise rotation: |
bomilkar | 0:ef2f1bbeb8e1 | 199 | // |
bomilkar | 0:ef2f1bbeb8e1 | 200 | // 10 -> 01 -> 10 -> 01 -> ... |
bomilkar | 0:ef2f1bbeb8e1 | 201 | // |
bomilkar | 0:ef2f1bbeb8e1 | 202 | // Clockwise rotation: |
bomilkar | 0:ef2f1bbeb8e1 | 203 | // |
bomilkar | 0:ef2f1bbeb8e1 | 204 | // 11 -> 00 -> 11 -> 00 -> ... |
bomilkar | 0:ef2f1bbeb8e1 | 205 | // |
bomilkar | 0:ef2f1bbeb8e1 | 206 | // We consider counter clockwise rotation to be "forward" and |
bomilkar | 0:ef2f1bbeb8e1 | 207 | // counter clockwise to be "backward". Therefore pulse count will increase |
bomilkar | 0:ef2f1bbeb8e1 | 208 | // during counter clockwise rotation and decrease during clockwise rotation. |
bomilkar | 0:ef2f1bbeb8e1 | 209 | // |
bomilkar | 0:ef2f1bbeb8e1 | 210 | // +-------------+ |
bomilkar | 0:ef2f1bbeb8e1 | 211 | // | X4 Encoding | |
bomilkar | 0:ef2f1bbeb8e1 | 212 | // +-------------+ |
bomilkar | 0:ef2f1bbeb8e1 | 213 | // |
bomilkar | 0:ef2f1bbeb8e1 | 214 | // There are four possible states for a quadrature encoder which correspond to |
bomilkar | 0:ef2f1bbeb8e1 | 215 | // 2-bit gray code. |
bomilkar | 0:ef2f1bbeb8e1 | 216 | // |
bomilkar | 0:ef2f1bbeb8e1 | 217 | // A state change is only valid if of only one bit has changed. |
bomilkar | 0:ef2f1bbeb8e1 | 218 | // A state change is invalid if both bits have changed. |
bomilkar | 0:ef2f1bbeb8e1 | 219 | // |
bomilkar | 0:ef2f1bbeb8e1 | 220 | // Clockwise Rotation -> |
bomilkar | 0:ef2f1bbeb8e1 | 221 | // |
bomilkar | 0:ef2f1bbeb8e1 | 222 | // 00 01 11 10 00 |
bomilkar | 0:ef2f1bbeb8e1 | 223 | // |
bomilkar | 0:ef2f1bbeb8e1 | 224 | // <- Counter Clockwise Rotation |
bomilkar | 0:ef2f1bbeb8e1 | 225 | // |
bomilkar | 0:ef2f1bbeb8e1 | 226 | // If we observe any valid state changes going from left to right, we have |
bomilkar | 0:ef2f1bbeb8e1 | 227 | // moved one pulse clockwise [we will consider this "backward" or "negative"]. |
bomilkar | 0:ef2f1bbeb8e1 | 228 | // |
bomilkar | 0:ef2f1bbeb8e1 | 229 | // If we observe any valid state changes going from right to left we have |
bomilkar | 0:ef2f1bbeb8e1 | 230 | // moved one pulse counter clockwise [we will consider this "forward" or |
bomilkar | 0:ef2f1bbeb8e1 | 231 | // "positive"]. |
bomilkar | 0:ef2f1bbeb8e1 | 232 | // |
bomilkar | 0:ef2f1bbeb8e1 | 233 | // We might enter an invalid state for a number of reasons which are hard to |
bomilkar | 0:ef2f1bbeb8e1 | 234 | // predict - if this is the case, it is generally safe to ignore it, update |
bomilkar | 0:ef2f1bbeb8e1 | 235 | // the state and carry on, with the error correcting itself shortly after. |
bomilkar | 0:ef2f1bbeb8e1 | 236 | void QEI::encode(void) { |
bomilkar | 0:ef2f1bbeb8e1 | 237 | |
bomilkar | 0:ef2f1bbeb8e1 | 238 | int change = 0; |
bomilkar | 0:ef2f1bbeb8e1 | 239 | int chanA = channelA_.read(); |
bomilkar | 0:ef2f1bbeb8e1 | 240 | int chanB = channelB_.read(); |
bomilkar | 0:ef2f1bbeb8e1 | 241 | |
bomilkar | 0:ef2f1bbeb8e1 | 242 | //2-bit state. |
bomilkar | 0:ef2f1bbeb8e1 | 243 | currState_ = (chanA << 1) | (chanB); |
bomilkar | 0:ef2f1bbeb8e1 | 244 | |
bomilkar | 0:ef2f1bbeb8e1 | 245 | if (encoding_ == X2_ENCODING) { |
bomilkar | 0:ef2f1bbeb8e1 | 246 | |
bomilkar | 0:ef2f1bbeb8e1 | 247 | //11->00->11->00 is counter clockwise rotation or "forward". |
bomilkar | 0:ef2f1bbeb8e1 | 248 | if ((prevState_ == 0x3 && currState_ == 0x0) || |
bomilkar | 0:ef2f1bbeb8e1 | 249 | (prevState_ == 0x0 && currState_ == 0x3)) { |
bomilkar | 0:ef2f1bbeb8e1 | 250 | |
bomilkar | 0:ef2f1bbeb8e1 | 251 | pulses_++; |
bomilkar | 0:ef2f1bbeb8e1 | 252 | |
bomilkar | 0:ef2f1bbeb8e1 | 253 | } |
bomilkar | 0:ef2f1bbeb8e1 | 254 | //10->01->10->01 is clockwise rotation or "backward". |
bomilkar | 0:ef2f1bbeb8e1 | 255 | else if ((prevState_ == 0x2 && currState_ == 0x1) || |
bomilkar | 0:ef2f1bbeb8e1 | 256 | (prevState_ == 0x1 && currState_ == 0x2)) { |
bomilkar | 0:ef2f1bbeb8e1 | 257 | |
bomilkar | 0:ef2f1bbeb8e1 | 258 | pulses_--; |
bomilkar | 0:ef2f1bbeb8e1 | 259 | |
bomilkar | 0:ef2f1bbeb8e1 | 260 | } |
bomilkar | 0:ef2f1bbeb8e1 | 261 | |
bomilkar | 0:ef2f1bbeb8e1 | 262 | } else if (encoding_ == X4_ENCODING) { |
bomilkar | 0:ef2f1bbeb8e1 | 263 | |
bomilkar | 0:ef2f1bbeb8e1 | 264 | //Entered a new valid state. |
bomilkar | 0:ef2f1bbeb8e1 | 265 | if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) { |
bomilkar | 0:ef2f1bbeb8e1 | 266 | //2 bit state. Right hand bit of prev XOR left hand bit of current |
bomilkar | 0:ef2f1bbeb8e1 | 267 | //gives 0 if clockwise rotation and 1 if counter clockwise rotation. |
bomilkar | 0:ef2f1bbeb8e1 | 268 | change = (prevState_ & PREV_MASK) ^ ((currState_ & CURR_MASK) >> 1); |
bomilkar | 0:ef2f1bbeb8e1 | 269 | |
bomilkar | 0:ef2f1bbeb8e1 | 270 | if (change == 0) { |
bomilkar | 0:ef2f1bbeb8e1 | 271 | change = -1; |
bomilkar | 0:ef2f1bbeb8e1 | 272 | } |
bomilkar | 0:ef2f1bbeb8e1 | 273 | |
bomilkar | 0:ef2f1bbeb8e1 | 274 | pulses_ -= change; |
bomilkar | 0:ef2f1bbeb8e1 | 275 | } |
bomilkar | 0:ef2f1bbeb8e1 | 276 | |
bomilkar | 0:ef2f1bbeb8e1 | 277 | } |
bomilkar | 0:ef2f1bbeb8e1 | 278 | |
bomilkar | 0:ef2f1bbeb8e1 | 279 | prevState_ = currState_; |
bomilkar | 0:ef2f1bbeb8e1 | 280 | |
bomilkar | 0:ef2f1bbeb8e1 | 281 | } |
bomilkar | 0:ef2f1bbeb8e1 | 282 | |
bomilkar | 0:ef2f1bbeb8e1 | 283 |