Rotary encoder library. Uses interrupts for reading rotation,
Fork of RPG by
RPG.h@2:94d27805abda, 2017-05-21 (annotated)
- Committer:
- kryksyh
- Date:
- Sun May 21 09:59:35 2017 +0000
- Revision:
- 2:94d27805abda
- Parent:
- 1:0b389c2c21b5
- Child:
- 3:e041653b7338
Rotation is more reliable now, and does not depend on reading frequency.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
canderson199 | 0:4a7e5f6a59a4 | 1 | /** |
canderson199 | 0:4a7e5f6a59a4 | 2 | * ============================================================================= |
kryksyh | 2:94d27805abda | 3 | * Rotary Pulse Generator class (Version 1.0.0) |
canderson199 | 0:4a7e5f6a59a4 | 4 | * ============================================================================= |
canderson199 | 0:4a7e5f6a59a4 | 5 | * Copyright (c) 2012 Christopher Anderson |
kryksyh | 2:94d27805abda | 6 | * Copyright (c) 2017 Dmitry Makarenko |
canderson199 | 0:4a7e5f6a59a4 | 7 | * |
canderson199 | 0:4a7e5f6a59a4 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
canderson199 | 0:4a7e5f6a59a4 | 9 | * of this software and associated documentation files (the "Software"), to deal |
canderson199 | 0:4a7e5f6a59a4 | 10 | * in the Software without restriction, including without limitation the rights |
canderson199 | 0:4a7e5f6a59a4 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
canderson199 | 0:4a7e5f6a59a4 | 12 | * copies of the Software, and to permit persons to whom the Software is |
canderson199 | 0:4a7e5f6a59a4 | 13 | * furnished to do so, subject to the following conditions: |
canderson199 | 0:4a7e5f6a59a4 | 14 | * |
canderson199 | 0:4a7e5f6a59a4 | 15 | * The above copyright notice and this permission notice shall be included in |
canderson199 | 0:4a7e5f6a59a4 | 16 | * all copies or substantial portions of the Software. |
canderson199 | 0:4a7e5f6a59a4 | 17 | * |
canderson199 | 0:4a7e5f6a59a4 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
canderson199 | 0:4a7e5f6a59a4 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
canderson199 | 0:4a7e5f6a59a4 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
canderson199 | 0:4a7e5f6a59a4 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
canderson199 | 0:4a7e5f6a59a4 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
canderson199 | 0:4a7e5f6a59a4 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
canderson199 | 0:4a7e5f6a59a4 | 24 | * THE SOFTWARE. |
canderson199 | 0:4a7e5f6a59a4 | 25 | * ============================================================================= |
canderson199 | 0:4a7e5f6a59a4 | 26 | */ |
canderson199 | 0:4a7e5f6a59a4 | 27 | |
canderson199 | 0:4a7e5f6a59a4 | 28 | #ifndef MBED_RPG_H |
canderson199 | 0:4a7e5f6a59a4 | 29 | #define MBED_RPG_H |
canderson199 | 0:4a7e5f6a59a4 | 30 | |
canderson199 | 0:4a7e5f6a59a4 | 31 | #include "mbed.h" |
canderson199 | 0:4a7e5f6a59a4 | 32 | |
canderson199 | 1:0b389c2c21b5 | 33 | /** |
canderson199 | 1:0b389c2c21b5 | 34 | * Constructor(Channel A input pin, Channel B input pin, Pushbutton input pin) |
canderson199 | 1:0b389c2c21b5 | 35 | */ |
canderson199 | 0:4a7e5f6a59a4 | 36 | class RPG{ |
kryksyh | 2:94d27805abda | 37 | |
canderson199 | 0:4a7e5f6a59a4 | 38 | public: |
canderson199 | 0:4a7e5f6a59a4 | 39 | RPG(PinName pA, PinName pB, PinName pPB); |
canderson199 | 0:4a7e5f6a59a4 | 40 | |
canderson199 | 0:4a7e5f6a59a4 | 41 | //Get Direction |
canderson199 | 0:4a7e5f6a59a4 | 42 | int dir(); |
kryksyh | 2:94d27805abda | 43 | |
canderson199 | 0:4a7e5f6a59a4 | 44 | //Check Push Button |
canderson199 | 0:4a7e5f6a59a4 | 45 | bool pb(); |
canderson199 | 0:4a7e5f6a59a4 | 46 | |
canderson199 | 0:4a7e5f6a59a4 | 47 | |
canderson199 | 0:4a7e5f6a59a4 | 48 | private: |
kryksyh | 2:94d27805abda | 49 | |
kryksyh | 2:94d27805abda | 50 | // Channel A |
kryksyh | 2:94d27805abda | 51 | InterruptIn IA; |
kryksyh | 2:94d27805abda | 52 | |
kryksyh | 2:94d27805abda | 53 | // Channel B |
kryksyh | 2:94d27805abda | 54 | DigitalIn B; |
kryksyh | 2:94d27805abda | 55 | |
kryksyh | 2:94d27805abda | 56 | //Push button |
canderson199 | 0:4a7e5f6a59a4 | 57 | DigitalIn PB; |
kryksyh | 2:94d27805abda | 58 | |
kryksyh | 2:94d27805abda | 59 | // Rotation counter (positive value - cw rotation, negative - ccw) |
kryksyh | 2:94d27805abda | 60 | int m_dir; |
kryksyh | 2:94d27805abda | 61 | |
kryksyh | 2:94d27805abda | 62 | // Interrupt handler for channel A, IH for CB is not required |
kryksyh | 2:94d27805abda | 63 | void onA(); |
canderson199 | 0:4a7e5f6a59a4 | 64 | }; |
canderson199 | 0:4a7e5f6a59a4 | 65 | #endif |