Rotary encoder handler.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RotaryEncoder.cpp Source File

RotaryEncoder.cpp

00001 #include "RotaryEncoder.h"
00002 
00003 RotaryEncoder::RotaryEncoder(void (*cb)(void), PinName channelA_, PinName channelB_, PinName pushPin_, float period)
00004  : channelA(channelA_), channelB(channelB_), pushPin(pushPin_)
00005 {
00006     direction = 0;
00007     counter = 0;
00008     lastDirection = 0;
00009 
00010     encoderTicker.attach(this, &RotaryEncoder::pulse, period);
00011     pushPin.rise(cb);
00012 }
00013 
00014 RotaryEncoder::~RotaryEncoder() {};
00015 
00016 /** Public methods **/
00017 
00018 int8_t RotaryEncoder::getDirection() const
00019 {
00020     return direction;   
00021 }
00022 
00023 int RotaryEncoder::getCounter() const
00024 {
00025     return counter;
00026 }
00027 
00028 bool RotaryEncoder::getPushButton() const
00029 {
00030     return false;   
00031 }
00032 
00033 /** Private methods **/
00034 
00035 void RotaryEncoder::pulse()
00036 {    
00037     lastDirection <<= 2;
00038     if (channelB) lastDirection |= 1;
00039     if (channelA) lastDirection |= 2;
00040     
00041     direction = encoderTable[lastDirection & 0x0F];
00042     counter += direction;
00043 }
00044 
00045 const int8_t RotaryEncoder::encoderTable[] = {0, 0, 0, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 0, 0, 0};