Rotary Encoder handling library for mbed

Dependents:   MIDI_CW Gemphet8

REnc.cpp

Committer:
ChuckTimber
Date:
2014-07-26
Revision:
0:c905f9e6866f
Child:
1:2732adb68dad

File content as of revision 0:c905f9e6866f:

#include "REnc.h"
#include "mbed.h"

/** class to make sound with a buzzer, based on a PwmOut
 *   The class use a timeout to switch off the sound  - it is not blocking while making noise
 *
 * Example:
 * @code
 * @endcode
 */

using namespace mbed;
 // constructor
 /** Create a Beep object connected to the specified PwmOut pin
  *
  * @param pin PwmOut pin to connect to 
  */

void REnc::sample_encoder(void)
{
    static unsigned char i;

    i = (i << 2) + ((~_pinb & 0x01)<< 1) + (~_pina & 0x1);
    i &= 0xf;
    
    switch (i) {
        case 0x7:
        case 0xe:
            CMD = FORWARD;
            if (mRightCallback != NULL)  { mRightCallback();  CMD = IDLE; }
            break;
        case 0xb:
        case 0xd:
            CMD = BACKWARD;
            if (mLeftCallback != NULL)   { mLeftCallback();   CMD = IDLE; } 
            break;
        default:
            CMD = IDLE;
    }
}

void REnc::setHandleRight(void (*fptr)(void))  { mRightCallback = fptr; }
void REnc::setHandleLeft(void (*fptr)(void))   { mLeftCallback  = fptr; }

REnc::REnc(PinName pina, PinName pinb) : _pina(pina), _pinb(pinb)
{
    CMD = IDLE;
    mRightCallback  = NULL;
    mLeftCallback   = NULL;
    _tick.attach(this, &REnc::sample_encoder, 0.01);
}