Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
decoder.cpp@0:c3a05fe15b84, 2019-01-08 (annotated)
- Committer:
- ianaherne
- Date:
- Tue Jan 08 17:10:13 2019 +0000
- Revision:
- 0:c3a05fe15b84
- Child:
- 1:017efde7ab29
working counting up and down and directional information
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 |