driver for incremental encoder

Committer:
ianaherne
Date:
Tue Mar 12 18:12:04 2019 +0000
Revision:
4:9913990cfd78
Parent:
2:b82d31306c4b
Child:
5:1a5772466668
streamlined encoder reading

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ianaherne 0:c3a05fe15b84 1 #include "decoder.h"
ianaherne 0:c3a05fe15b84 2
ianaherne 0:c3a05fe15b84 3 /**
ianaherne 0:c3a05fe15b84 4 * @brief constructor
ianaherne 0:c3a05fe15b84 5 * @details initializes interrupt on channel A, and initialises internal variables
ianaherne 0:c3a05fe15b84 6 * @param NA
ianaherne 0:c3a05fe15b84 7 * @return NA
ianaherne 0:c3a05fe15b84 8 * @warning interrupt functionallity on channel B is not used as it is not required.
ianaherne 0:c3a05fe15b84 9 *
ianaherne 0:c3a05fe15b84 10 */
ianaherne 0:c3a05fe15b84 11
ianaherne 0:c3a05fe15b84 12 decoder :: decoder(PinName channelA, PinName channelB):_channelA(channelA),_channelB(channelB) {
ianaherne 0:c3a05fe15b84 13
ianaherne 0:c3a05fe15b84 14 _channelA.rise(this, &decoder::isr);
ianaherne 4:9913990cfd78 15
ianaherne 0:c3a05fe15b84 16 _count = 0;
ianaherne 0:c3a05fe15b84 17 _direction = 0;
ianaherne 0:c3a05fe15b84 18
ianaherne 0:c3a05fe15b84 19 }
ianaherne 0:c3a05fe15b84 20
ianaherne 0:c3a05fe15b84 21 /**
ianaherne 0:c3a05fe15b84 22 * @brief isr
ianaherne 0:c3a05fe15b84 23 * @details interrupt servive routine to decode the encoder output
ianaherne 0:c3a05fe15b84 24 * @param NA
ianaherne 0:c3a05fe15b84 25 * @return NA
ianaherne 0:c3a05fe15b84 26 * @warning
ianaherne 0:c3a05fe15b84 27 *
ianaherne 0:c3a05fe15b84 28 */
ianaherne 0:c3a05fe15b84 29
ianaherne 0:c3a05fe15b84 30
ianaherne 0:c3a05fe15b84 31
ianaherne 0:c3a05fe15b84 32 void decoder :: isr(){
ianaherne 0:c3a05fe15b84 33
ianaherne 4:9913990cfd78 34
ianaherne 0:c3a05fe15b84 35 uint8_t chA = _channelA.read();
ianaherne 4:9913990cfd78 36
ianaherne 0:c3a05fe15b84 37 uint8_t chB = _channelB.read();
ianaherne 0:c3a05fe15b84 38
ianaherne 0:c3a05fe15b84 39 if(chA == 1){
ianaherne 0:c3a05fe15b84 40
ianaherne 4:9913990cfd78 41 if(chB == 1){
ianaherne 4:9913990cfd78 42
ianaherne 4:9913990cfd78 43 if( _count != UPPER_LIMIT){
ianaherne 4:9913990cfd78 44 _count++;
ianaherne 4:9913990cfd78 45 }
ianaherne 4:9913990cfd78 46 _direction = 1;
ianaherne 4:9913990cfd78 47
ianaherne 4:9913990cfd78 48 }
ianaherne 4:9913990cfd78 49
ianaherne 4:9913990cfd78 50
ianaherne 0:c3a05fe15b84 51
ianaherne 4:9913990cfd78 52 if(chB == 0){
ianaherne 4:9913990cfd78 53
ianaherne 4:9913990cfd78 54 if(_count != LOWER_LIMIT){
ianaherne 4:9913990cfd78 55 _count--;
ianaherne 4:9913990cfd78 56 }
ianaherne 4:9913990cfd78 57 _direction = 0;
ianaherne 4:9913990cfd78 58
ianaherne 4:9913990cfd78 59 }
ianaherne 4:9913990cfd78 60
ianaherne 4:9913990cfd78 61 }
ianaherne 0:c3a05fe15b84 62
ianaherne 0:c3a05fe15b84 63 }
ianaherne 0:c3a05fe15b84 64
ianaherne 0:c3a05fe15b84 65 /**
ianaherne 0:c3a05fe15b84 66 * @brief getDirection()
ianaherne 0:c3a05fe15b84 67 * @details function to return the class private variable _direction
ianaherne 0:c3a05fe15b84 68 * @param NA
ianaherne 0:c3a05fe15b84 69 * @return the direction of encoder
ianaherne 0:c3a05fe15b84 70 * @warning
ianaherne 0:c3a05fe15b84 71 *
ianaherne 0:c3a05fe15b84 72 */
ianaherne 0:c3a05fe15b84 73
ianaherne 0:c3a05fe15b84 74 int8_t decoder :: getDirection(){
ianaherne 0:c3a05fe15b84 75
ianaherne 0:c3a05fe15b84 76 return _direction;
ianaherne 0:c3a05fe15b84 77
ianaherne 0:c3a05fe15b84 78
ianaherne 0:c3a05fe15b84 79
ianaherne 0:c3a05fe15b84 80 }
ianaherne 0:c3a05fe15b84 81
ianaherne 0:c3a05fe15b84 82 /**
ianaherne 0:c3a05fe15b84 83 * @brief getCount
ianaherne 0:c3a05fe15b84 84 * @details function to return the class private variable _count
ianaherne 0:c3a05fe15b84 85 * @param NA
ianaherne 0:c3a05fe15b84 86 * @return the number of turns of encoder +/-
ianaherne 0:c3a05fe15b84 87 * @warning
ianaherne 0:c3a05fe15b84 88 *
ianaherne 0:c3a05fe15b84 89 */
ianaherne 0:c3a05fe15b84 90
ianaherne 0:c3a05fe15b84 91 int16_t decoder :: getCount(){
ianaherne 0:c3a05fe15b84 92
ianaherne 0:c3a05fe15b84 93
ianaherne 0:c3a05fe15b84 94 return _count;
ianaherne 0:c3a05fe15b84 95
ianaherne 0:c3a05fe15b84 96 }
ianaherne 0:c3a05fe15b84 97
ianaherne 1:017efde7ab29 98 /**
ianaherne 1:017efde7ab29 99 * @brief reset()
ianaherne 1:017efde7ab29 100 * @details function to reset all internal counting and directional variables
ianaherne 1:017efde7ab29 101 * @param NA
ianaherne 1:017efde7ab29 102 * @return
ianaherne 1:017efde7ab29 103 * @warning
ianaherne 1:017efde7ab29 104 *
ianaherne 1:017efde7ab29 105 */
ianaherne 1:017efde7ab29 106
ianaherne 1:017efde7ab29 107 void decoder :: reset(){
ianaherne 1:017efde7ab29 108
ianaherne 4:9913990cfd78 109
ianaherne 1:017efde7ab29 110 _count = 0;
ianaherne 1:017efde7ab29 111 _direction = 0;
ianaherne 1:017efde7ab29 112
ianaherne 1:017efde7ab29 113 }
ianaherne 1:017efde7ab29 114