Rotary encoder handler.
RotaryEncoder.cpp@0:db7f371c8530, 2016-07-20 (annotated)
- Committer:
- Sateg
- Date:
- Wed Jul 20 00:30:23 2016 +0000
- Revision:
- 0:db7f371c8530
Working, conservative rotary encoder handler.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sateg | 0:db7f371c8530 | 1 | #include "RotaryEncoder.h" |
Sateg | 0:db7f371c8530 | 2 | |
Sateg | 0:db7f371c8530 | 3 | RotaryEncoder::RotaryEncoder(void (*cb)(void), PinName channelA_, PinName channelB_, PinName pushPin_, float period) |
Sateg | 0:db7f371c8530 | 4 | : channelA(channelA_), channelB(channelB_), pushPin(pushPin_) |
Sateg | 0:db7f371c8530 | 5 | { |
Sateg | 0:db7f371c8530 | 6 | direction = 0; |
Sateg | 0:db7f371c8530 | 7 | counter = 0; |
Sateg | 0:db7f371c8530 | 8 | lastDirection = 0; |
Sateg | 0:db7f371c8530 | 9 | |
Sateg | 0:db7f371c8530 | 10 | encoderTicker.attach(this, &RotaryEncoder::pulse, period); |
Sateg | 0:db7f371c8530 | 11 | pushPin.rise(cb); |
Sateg | 0:db7f371c8530 | 12 | } |
Sateg | 0:db7f371c8530 | 13 | |
Sateg | 0:db7f371c8530 | 14 | RotaryEncoder::~RotaryEncoder() {}; |
Sateg | 0:db7f371c8530 | 15 | |
Sateg | 0:db7f371c8530 | 16 | /** Public methods **/ |
Sateg | 0:db7f371c8530 | 17 | |
Sateg | 0:db7f371c8530 | 18 | int8_t RotaryEncoder::getDirection() const |
Sateg | 0:db7f371c8530 | 19 | { |
Sateg | 0:db7f371c8530 | 20 | return direction; |
Sateg | 0:db7f371c8530 | 21 | } |
Sateg | 0:db7f371c8530 | 22 | |
Sateg | 0:db7f371c8530 | 23 | int RotaryEncoder::getCounter() const |
Sateg | 0:db7f371c8530 | 24 | { |
Sateg | 0:db7f371c8530 | 25 | return counter; |
Sateg | 0:db7f371c8530 | 26 | } |
Sateg | 0:db7f371c8530 | 27 | |
Sateg | 0:db7f371c8530 | 28 | bool RotaryEncoder::getPushButton() const |
Sateg | 0:db7f371c8530 | 29 | { |
Sateg | 0:db7f371c8530 | 30 | return false; |
Sateg | 0:db7f371c8530 | 31 | } |
Sateg | 0:db7f371c8530 | 32 | |
Sateg | 0:db7f371c8530 | 33 | /** Private methods **/ |
Sateg | 0:db7f371c8530 | 34 | |
Sateg | 0:db7f371c8530 | 35 | void RotaryEncoder::pulse() |
Sateg | 0:db7f371c8530 | 36 | { |
Sateg | 0:db7f371c8530 | 37 | lastDirection <<= 2; |
Sateg | 0:db7f371c8530 | 38 | if (channelB) lastDirection |= 1; |
Sateg | 0:db7f371c8530 | 39 | if (channelA) lastDirection |= 2; |
Sateg | 0:db7f371c8530 | 40 | |
Sateg | 0:db7f371c8530 | 41 | direction = encoderTable[lastDirection & 0x0F]; |
Sateg | 0:db7f371c8530 | 42 | counter += direction; |
Sateg | 0:db7f371c8530 | 43 | } |
Sateg | 0:db7f371c8530 | 44 | |
Sateg | 0:db7f371c8530 | 45 | const int8_t RotaryEncoder::encoderTable[] = {0, 0, 0, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 0, 0, 0}; |