driver for incremental encoder
decoder.cpp@1:017efde7ab29, 2019-01-08 (annotated)
- Committer:
- ianaherne
- Date:
- Tue Jan 08 17:29:43 2019 +0000
- Revision:
- 1:017efde7ab29
- Parent:
- 0:c3a05fe15b84
- Child:
- 2:b82d31306c4b
added reset function, changed channel B to digitalIn as interruptIn was not needed on that channel
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:c3a05fe15b84 | 15 | _oldCount = 0; |
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 | 0:c3a05fe15b84 | 34 | uint8_t chA = _channelA.read(); |
ianaherne | 0:c3a05fe15b84 | 35 | uint8_t chB = _channelB.read(); |
ianaherne | 0:c3a05fe15b84 | 36 | |
ianaherne | 0:c3a05fe15b84 | 37 | if(chA == 1){ |
ianaherne | 0:c3a05fe15b84 | 38 | |
ianaherne | 0:c3a05fe15b84 | 39 | if(chB == 1){_count++;} |
ianaherne | 0:c3a05fe15b84 | 40 | if(chB == 0){_count--;} |
ianaherne | 0:c3a05fe15b84 | 41 | |
ianaherne | 0:c3a05fe15b84 | 42 | } |
ianaherne | 0:c3a05fe15b84 | 43 | |
ianaherne | 0:c3a05fe15b84 | 44 | if(_oldCount < _count){_direction = 1;} |
ianaherne | 0:c3a05fe15b84 | 45 | if(_oldCount > _count){_direction = -1;} |
ianaherne | 0:c3a05fe15b84 | 46 | if(_oldCount == _count){_direction = 0;} |
ianaherne | 0:c3a05fe15b84 | 47 | |
ianaherne | 0:c3a05fe15b84 | 48 | _oldCount = _count; |
ianaherne | 0:c3a05fe15b84 | 49 | |
ianaherne | 0:c3a05fe15b84 | 50 | } |
ianaherne | 0:c3a05fe15b84 | 51 | |
ianaherne | 0:c3a05fe15b84 | 52 | /** |
ianaherne | 0:c3a05fe15b84 | 53 | * @brief getDirection() |
ianaherne | 0:c3a05fe15b84 | 54 | * @details function to return the class private variable _direction |
ianaherne | 0:c3a05fe15b84 | 55 | * @param NA |
ianaherne | 0:c3a05fe15b84 | 56 | * @return the direction of encoder |
ianaherne | 0:c3a05fe15b84 | 57 | * @warning |
ianaherne | 0:c3a05fe15b84 | 58 | * |
ianaherne | 0:c3a05fe15b84 | 59 | */ |
ianaherne | 0:c3a05fe15b84 | 60 | |
ianaherne | 0:c3a05fe15b84 | 61 | int8_t decoder :: getDirection(){ |
ianaherne | 0:c3a05fe15b84 | 62 | |
ianaherne | 0:c3a05fe15b84 | 63 | return _direction; |
ianaherne | 0:c3a05fe15b84 | 64 | |
ianaherne | 0:c3a05fe15b84 | 65 | |
ianaherne | 0:c3a05fe15b84 | 66 | |
ianaherne | 0:c3a05fe15b84 | 67 | } |
ianaherne | 0:c3a05fe15b84 | 68 | |
ianaherne | 0:c3a05fe15b84 | 69 | /** |
ianaherne | 0:c3a05fe15b84 | 70 | * @brief getCount |
ianaherne | 0:c3a05fe15b84 | 71 | * @details function to return the class private variable _count |
ianaherne | 0:c3a05fe15b84 | 72 | * @param NA |
ianaherne | 0:c3a05fe15b84 | 73 | * @return the number of turns of encoder +/- |
ianaherne | 0:c3a05fe15b84 | 74 | * @warning |
ianaherne | 0:c3a05fe15b84 | 75 | * |
ianaherne | 0:c3a05fe15b84 | 76 | */ |
ianaherne | 0:c3a05fe15b84 | 77 | |
ianaherne | 0:c3a05fe15b84 | 78 | int16_t decoder :: getCount(){ |
ianaherne | 0:c3a05fe15b84 | 79 | |
ianaherne | 0:c3a05fe15b84 | 80 | |
ianaherne | 0:c3a05fe15b84 | 81 | return _count; |
ianaherne | 0:c3a05fe15b84 | 82 | |
ianaherne | 0:c3a05fe15b84 | 83 | } |
ianaherne | 0:c3a05fe15b84 | 84 | |
ianaherne | 1:017efde7ab29 | 85 | /** |
ianaherne | 1:017efde7ab29 | 86 | * @brief reset() |
ianaherne | 1:017efde7ab29 | 87 | * @details function to reset all internal counting and directional variables |
ianaherne | 1:017efde7ab29 | 88 | * @param NA |
ianaherne | 1:017efde7ab29 | 89 | * @return |
ianaherne | 1:017efde7ab29 | 90 | * @warning |
ianaherne | 1:017efde7ab29 | 91 | * |
ianaherne | 1:017efde7ab29 | 92 | */ |
ianaherne | 1:017efde7ab29 | 93 | |
ianaherne | 1:017efde7ab29 | 94 | void decoder :: reset(){ |
ianaherne | 1:017efde7ab29 | 95 | |
ianaherne | 1:017efde7ab29 | 96 | _oldCount = 0; |
ianaherne | 1:017efde7ab29 | 97 | _count = 0; |
ianaherne | 1:017efde7ab29 | 98 | _direction = 0; |
ianaherne | 1:017efde7ab29 | 99 | |
ianaherne | 1:017efde7ab29 | 100 | } |
ianaherne | 1:017efde7ab29 | 101 |