Rotary encoder library. Uses interrupts for reading rotation,
Fork of RPG by
RPG.cpp@3:e041653b7338, 2017-05-21 (annotated)
- Committer:
- kryksyh
- Date:
- Sun May 21 15:20:46 2017 +0000
- Revision:
- 3:e041653b7338
- Parent:
- 2:94d27805abda
Docs fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kryksyh | 3:e041653b7338 | 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 | #include "RPG.h" |
canderson199 | 0:4a7e5f6a59a4 | 29 | #include "mbed.h" |
canderson199 | 0:4a7e5f6a59a4 | 30 | |
canderson199 | 0:4a7e5f6a59a4 | 31 | /** |
canderson199 | 0:4a7e5f6a59a4 | 32 | * Constructor(Channel A input pin, Channel B input pin, Pushbutton input pin) |
canderson199 | 0:4a7e5f6a59a4 | 33 | */ |
canderson199 | 0:4a7e5f6a59a4 | 34 | RPG::RPG(PinName pA, PinName pB, PinName pPB) |
kryksyh | 2:94d27805abda | 35 | : IA(pA), B(pB), PB(pPB), m_dir(0) |
canderson199 | 0:4a7e5f6a59a4 | 36 | { |
kryksyh | 2:94d27805abda | 37 | IA.mode(PullUp); |
kryksyh | 2:94d27805abda | 38 | IA.fall(this, &RPG::onA); |
kryksyh | 2:94d27805abda | 39 | B.mode(PullUp); |
canderson199 | 0:4a7e5f6a59a4 | 40 | PB.mode(PullUp); |
canderson199 | 0:4a7e5f6a59a4 | 41 | wait_us(10); |
canderson199 | 0:4a7e5f6a59a4 | 42 | } |
canderson199 | 0:4a7e5f6a59a4 | 43 | |
kryksyh | 2:94d27805abda | 44 | /** |
kryksyh | 2:94d27805abda | 45 | * Interrupt handler for channel A. |
kryksyh | 2:94d27805abda | 46 | * Increments or decrements m_dir depending on channel B state. |
canderson199 | 0:4a7e5f6a59a4 | 47 | */ |
kryksyh | 2:94d27805abda | 48 | void RPG::onA() { |
kryksyh | 2:94d27805abda | 49 | if(B == 1) { |
kryksyh | 2:94d27805abda | 50 | m_dir ++; |
kryksyh | 2:94d27805abda | 51 | } |
kryksyh | 2:94d27805abda | 52 | else { |
kryksyh | 2:94d27805abda | 53 | m_dir --; |
kryksyh | 2:94d27805abda | 54 | } |
kryksyh | 2:94d27805abda | 55 | } |
canderson199 | 0:4a7e5f6a59a4 | 56 | |
canderson199 | 0:4a7e5f6a59a4 | 57 | /** |
kryksyh | 2:94d27805abda | 58 | * Reads and debounces push button returns bool result |
canderson199 | 0:4a7e5f6a59a4 | 59 | */ |
canderson199 | 0:4a7e5f6a59a4 | 60 | bool RPG::pb() |
canderson199 | 0:4a7e5f6a59a4 | 61 | { |
canderson199 | 0:4a7e5f6a59a4 | 62 | int check = PB; |
canderson199 | 0:4a7e5f6a59a4 | 63 | wait_us(5); |
canderson199 | 0:4a7e5f6a59a4 | 64 | if((!check) && !PB) |
canderson199 | 0:4a7e5f6a59a4 | 65 | { |
canderson199 | 0:4a7e5f6a59a4 | 66 | return true; |
canderson199 | 0:4a7e5f6a59a4 | 67 | } |
canderson199 | 0:4a7e5f6a59a4 | 68 | else return false; |
canderson199 | 0:4a7e5f6a59a4 | 69 | } |
canderson199 | 0:4a7e5f6a59a4 | 70 | |
canderson199 | 0:4a7e5f6a59a4 | 71 | /** |
kryksyh | 2:94d27805abda | 72 | * Direction and amount of rotation since last read: |
kryksyh | 2:94d27805abda | 73 | * +1 for clockwise |
kryksyh | 2:94d27805abda | 74 | * -1 for counter-clockwise |
kryksyh | 2:94d27805abda | 75 | * 0 for no rotation |
canderson199 | 0:4a7e5f6a59a4 | 76 | */ |
canderson199 | 0:4a7e5f6a59a4 | 77 | int RPG::dir() |
canderson199 | 0:4a7e5f6a59a4 | 78 | { |
kryksyh | 2:94d27805abda | 79 | int tmp = m_dir; |
kryksyh | 2:94d27805abda | 80 | m_dir = 0; |
kryksyh | 2:94d27805abda | 81 | return tmp; |
canderson199 | 0:4a7e5f6a59a4 | 82 | } |