Simple RPG Library

Dependents:   accuBlast_display

Committer:
canderson199
Date:
Fri Oct 12 14:01:48 2012 +0000
Revision:
0:4a7e5f6a59a4
Child:
2:02a5238c3a47
Simple RPG library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
canderson199 0:4a7e5f6a59a4 1 /**
canderson199 0:4a7e5f6a59a4 2 * =============================================================================
canderson199 0:4a7e5f6a59a4 3 * Rotary Pulse Generator class (Version 0.0.1)
canderson199 0:4a7e5f6a59a4 4 * =============================================================================
canderson199 0:4a7e5f6a59a4 5 * Copyright (c) 2012 Christopher Anderson
canderson199 0:4a7e5f6a59a4 6 *
canderson199 0:4a7e5f6a59a4 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
canderson199 0:4a7e5f6a59a4 8 * of this software and associated documentation files (the "Software"), to deal
canderson199 0:4a7e5f6a59a4 9 * in the Software without restriction, including without limitation the rights
canderson199 0:4a7e5f6a59a4 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
canderson199 0:4a7e5f6a59a4 11 * copies of the Software, and to permit persons to whom the Software is
canderson199 0:4a7e5f6a59a4 12 * furnished to do so, subject to the following conditions:
canderson199 0:4a7e5f6a59a4 13 *
canderson199 0:4a7e5f6a59a4 14 * The above copyright notice and this permission notice shall be included in
canderson199 0:4a7e5f6a59a4 15 * all copies or substantial portions of the Software.
canderson199 0:4a7e5f6a59a4 16 *
canderson199 0:4a7e5f6a59a4 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
canderson199 0:4a7e5f6a59a4 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
canderson199 0:4a7e5f6a59a4 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
canderson199 0:4a7e5f6a59a4 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
canderson199 0:4a7e5f6a59a4 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
canderson199 0:4a7e5f6a59a4 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
canderson199 0:4a7e5f6a59a4 23 * THE SOFTWARE.
canderson199 0:4a7e5f6a59a4 24 * =============================================================================
canderson199 0:4a7e5f6a59a4 25 */
canderson199 0:4a7e5f6a59a4 26
canderson199 0:4a7e5f6a59a4 27 #include "RPG.h"
canderson199 0:4a7e5f6a59a4 28 #include "mbed.h"
canderson199 0:4a7e5f6a59a4 29
canderson199 0:4a7e5f6a59a4 30 /**
canderson199 0:4a7e5f6a59a4 31 * Constructor(Channel A input pin, Channel B input pin, Pushbutton input pin)
canderson199 0:4a7e5f6a59a4 32 */
canderson199 0:4a7e5f6a59a4 33 RPG::RPG(PinName pA, PinName pB, PinName pPB)
canderson199 0:4a7e5f6a59a4 34 : chRPG(pA, pB), PB(pPB)
canderson199 0:4a7e5f6a59a4 35 {
canderson199 0:4a7e5f6a59a4 36 chRPG.input();
canderson199 0:4a7e5f6a59a4 37 chRPG.mode(PullUp);
canderson199 0:4a7e5f6a59a4 38 PB.mode(PullUp);
canderson199 0:4a7e5f6a59a4 39 wait_us(10);
canderson199 0:4a7e5f6a59a4 40 RPGO = chRPG;
canderson199 0:4a7e5f6a59a4 41 }
canderson199 0:4a7e5f6a59a4 42
canderson199 0:4a7e5f6a59a4 43 /**
canderson199 0:4a7e5f6a59a4 44 *Destructor
canderson199 0:4a7e5f6a59a4 45 */
canderson199 0:4a7e5f6a59a4 46 RPG::~RPG()
canderson199 0:4a7e5f6a59a4 47 {}
canderson199 0:4a7e5f6a59a4 48
canderson199 0:4a7e5f6a59a4 49 /**
canderson199 0:4a7e5f6a59a4 50 *reads and debounces push button returns bool result
canderson199 0:4a7e5f6a59a4 51 */
canderson199 0:4a7e5f6a59a4 52 bool RPG::pb()
canderson199 0:4a7e5f6a59a4 53 {
canderson199 0:4a7e5f6a59a4 54 int check = PB;
canderson199 0:4a7e5f6a59a4 55 wait_us(5);
canderson199 0:4a7e5f6a59a4 56 if((!check) && !PB)
canderson199 0:4a7e5f6a59a4 57 {
canderson199 0:4a7e5f6a59a4 58 return true;
canderson199 0:4a7e5f6a59a4 59 }
canderson199 0:4a7e5f6a59a4 60 else return false;
canderson199 0:4a7e5f6a59a4 61 }
canderson199 0:4a7e5f6a59a4 62
canderson199 0:4a7e5f6a59a4 63 /**
canderson199 0:4a7e5f6a59a4 64 *Determines direction of rotation returns:
canderson199 0:4a7e5f6a59a4 65 *1 for clockwise
canderson199 0:4a7e5f6a59a4 66 *-1 for counter-clockwise
canderson199 0:4a7e5f6a59a4 67 *0 for no rotation
canderson199 0:4a7e5f6a59a4 68 */
canderson199 0:4a7e5f6a59a4 69 int RPG::dir()
canderson199 0:4a7e5f6a59a4 70 {
canderson199 0:4a7e5f6a59a4 71 int dir = 0;
canderson199 0:4a7e5f6a59a4 72 RPGO = chRPG;
canderson199 0:4a7e5f6a59a4 73 wait(.005);
canderson199 0:4a7e5f6a59a4 74 RPGN = chRPG;
canderson199 0:4a7e5f6a59a4 75 if(RPGN != RPGO)
canderson199 0:4a7e5f6a59a4 76 {
canderson199 0:4a7e5f6a59a4 77 if((RPGN & 1) != (RPGO >> 1)) dir = 1;
canderson199 0:4a7e5f6a59a4 78 else dir = -1;
canderson199 0:4a7e5f6a59a4 79 }
canderson199 0:4a7e5f6a59a4 80 else dir = 0;
canderson199 0:4a7e5f6a59a4 81 RPGO = RPGN;
canderson199 0:4a7e5f6a59a4 82 return dir;
canderson199 0:4a7e5f6a59a4 83 }