Basic library to interface a 2 channel quadrature encoder with mBed
Diff: QuadratureEncoder.cpp
- Revision:
- 0:8e8c33a797a9
- Child:
- 1:42d1e211e624
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QuadratureEncoder.cpp Tue Jul 18 16:06:21 2017 +0000 @@ -0,0 +1,31 @@ +#include "QuadratureEncoder.h" + +QuadratureEncoder::QuadratureEncoder(PinName pinA, PinName pinB): _channelA(pinA), _channelB(pinB) +{ + _ticks = 0; + _channelB.rise(this, &QuadratureEncoder::_Brising); + _channelB.fall(this, &QuadratureEncoder::_Bfalling); +} + +int QuadratureEncoder::getTicks() +{ + return _ticks; +} + +void QuadratureEncoder::_Brising() +{ + if (_channelA.read()) { + _ticks--; + } else { + _ticks++; + } +} + +void QuadratureEncoder::_Bfalling() +{ + if (_channelA.read()) { + _ticks++; + } else { + _ticks--; + } +} \ No newline at end of file