Reiko Randoja
/
ut_bbr_2018
Firmware for UT Robotex 2018 basketball robot
Diff: QED/qed.cpp
- Revision:
- 0:ef6268629f0b
diff -r 000000000000 -r ef6268629f0b QED/qed.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QED/qed.cpp Fri Sep 28 10:46:57 2018 +0000 @@ -0,0 +1,33 @@ +#include "qed.h" + +QED::QED(PinName channelA, PinName channelB) + : interruptA(channelA), interruptB(channelB) { + + pulses = 0; + currState = (interruptA.read() << 1) | (interruptB.read()); + prevState = currState; + + interruptA.rise(this, &QED::decode); + interruptA.fall(this, &QED::decode); + interruptB.rise(this, &QED::decode); + interruptB.fall(this, &QED::decode); +} + +int QED::read() { + int pulseCount = pulses; + pulses = 0; + return pulseCount; +} + +void QED::decode() { + currState = (interruptA.read() << 1) | (interruptB.read()); + + int change = (prevState & PREV_MASK) ^ ((currState & CURR_MASK) >> 1); + + if (change == 0) { + change = -1; + } + + pulses -= change; + prevState = currState; +} \ No newline at end of file