Rotary Encoder handling library for mbed

Dependents:   MIDI_CW Gemphet8

REnc.h

Committer:
ChuckTimber
Date:
2014-08-06
Revision:
3:9dfe441065e9
Parent:
2:46173a05338e
Child:
4:916992052518

File content as of revision 3:9dfe441065e9:

#ifndef MBED_RENC_H
#define MBED_RENC_H

#include "mbed.h"

/** class to handle a rotary encoder 
 *   The class use DigitalIn and Ticker
 *
 * Refered to: http://elm-chan.org/docs/tec/te04.html
 *
 * Example:
 * @code
 * // REnc sample
 * #include "mbed.h"
 * #include "REnc.h"
 * void proc_REnc_right(void);
 * void proc_REnc_left(void);
 * 
 * // void proc_REnc_right(void) { }
 * // void proc_REnc_left(void)  { }
 * 
 * REnc renc(dp1, dp2);
 * 
 * int main()
 * {
 * 
 *     // renc.setHandleRight(&proc_REnc_right);
 *     // renc.setHandleLeft(&proc_REnc_left);
 *  
 *     while(1) {
 *         if (renc.CMD == CLOCKWISE) {
 *             proc_REnc_right();
 *             renc.CMD = STOP;
 *         } else if (renc.CMD == COUNTERCLOCKWISE) {
 *             proc_REnc_left();
 *             renc.CMD = STOP;
 *         }
 *         if (renc.STABLE) {
 *             // do_something
 *         }
 *     }
 * }
 * @endcode
 */

#define RENC_SAMPLING_PERIOD 0.002
#define RENC_EXECUTION_DELAY 300

namespace mbed {

    enum { STOP, CLOCKWISE, COUNTERCLOCKWISE };

/** class to handle a rotary encoder 
 *   The class uses DigitalIn and Ticker
 */
class REnc {

public:
    unsigned char CMD;
    unsigned char STABLE;
    void setHandleRight(void (*fptr)(void));
    void setHandleLeft(void (*fptr)(void));

    REnc(PinName pina, PinName pinb);

private:
    void sample_encoder(void);
    void (*mRightCallback)(void);
    void (*mLeftCallback)(void);

    DigitalIn _pina;
    DigitalIn _pinb;
    Ticker _tick;

}; /* end of REnc class definition */

} /* end of namespace mbed */
#endif