Rotary encoder library. Uses interrupts for reading rotation,
Fork of RPG by
Diff: RPG.cpp
- Revision:
- 2:94d27805abda
- Parent:
- 0:4a7e5f6a59a4
- Child:
- 3:e041653b7338
diff -r 0b389c2c21b5 -r 94d27805abda RPG.cpp --- a/RPG.cpp Fri Oct 12 14:04:17 2012 +0000 +++ b/RPG.cpp Sun May 21 09:59:35 2017 +0000 @@ -1,8 +1,9 @@ /** * ============================================================================= -* Rotary Pulse Generator class (Version 0.0.1) +* Rotary Pulse Generator class (Version 1.0.0) * ============================================================================= * Copyright (c) 2012 Christopher Anderson +* Copyright (c) 2017 Dmitry Makarenko * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,23 +32,30 @@ * Constructor(Channel A input pin, Channel B input pin, Pushbutton input pin) */ RPG::RPG(PinName pA, PinName pB, PinName pPB) - : chRPG(pA, pB), PB(pPB) + : IA(pA), B(pB), PB(pPB), m_dir(0) { - chRPG.input(); - chRPG.mode(PullUp); + IA.mode(PullUp); + IA.fall(this, &RPG::onA); + B.mode(PullUp); PB.mode(PullUp); wait_us(10); - RPGO = chRPG; } -/** - *Destructor +/** + * Interrupt handler for channel A. + * Increments or decrements m_dir depending on channel B state. */ -RPG::~RPG() -{} +void RPG::onA() { + if(B == 1) { + m_dir ++; + } + else { + m_dir --; + } +} /** - *reads and debounces push button returns bool result + * Reads and debounces push button returns bool result */ bool RPG::pb() { @@ -61,23 +69,14 @@ } /** - *Determines direction of rotation returns: - *1 for clockwise - *-1 for counter-clockwise - *0 for no rotation + * Direction and amount of rotation since last read: + * +1 for clockwise + * -1 for counter-clockwise + * 0 for no rotation */ int RPG::dir() { - int dir = 0; - RPGO = chRPG; - wait(.005); - RPGN = chRPG; - if(RPGN != RPGO) - { - if((RPGN & 1) != (RPGO >> 1)) dir = 1; - else dir = -1; - } - else dir = 0; - RPGO = RPGN; - return dir; + int tmp = m_dir; + m_dir = 0; + return tmp; } \ No newline at end of file