yashirou shimogamo / Mbed 2 deprecated BIRD2019

Dependencies:   mbed mbed-rtos SPI_MX25R

Committer:
shimogamo
Date:
Sat Dec 01 15:10:42 2018 +0000
Revision:
0:d6402bcd58f7
BIRD2018????????????

Who changed what in which revision?

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