Rotary Encoder handling library for mbed

Dependents:   MIDI_CW Gemphet8

Committer:
ChuckTimber
Date:
Wed Aug 06 23:46:02 2014 +0000
Revision:
2:46173a05338e
Parent:
1:2732adb68dad
Child:
3:9dfe441065e9
RENC_SAMPLING_PERIOD and RENC_EXECUTION_DELAY defined

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ChuckTimber 0:c905f9e6866f 1 #ifndef MBED_RENC_H
ChuckTimber 0:c905f9e6866f 2 #define MBED_RENC_H
ChuckTimber 0:c905f9e6866f 3
ChuckTimber 0:c905f9e6866f 4 #include "mbed.h"
ChuckTimber 0:c905f9e6866f 5
ChuckTimber 1:2732adb68dad 6 /** class to handle a rotary encoder
ChuckTimber 1:2732adb68dad 7 * The class use DigitalIn and Ticker
ChuckTimber 1:2732adb68dad 8 *
ChuckTimber 1:2732adb68dad 9 * Refered to: http://elm-chan.org/docs/tec/te04.html
ChuckTimber 0:c905f9e6866f 10 *
ChuckTimber 0:c905f9e6866f 11 * Example:
ChuckTimber 0:c905f9e6866f 12 * @code
ChuckTimber 1:2732adb68dad 13 * // REnc sample
ChuckTimber 1:2732adb68dad 14 * #include "mbed.h"
ChuckTimber 1:2732adb68dad 15 * #include "REnc.h"
ChuckTimber 1:2732adb68dad 16 * void proc_REnc_right(void);
ChuckTimber 1:2732adb68dad 17 * void proc_REnc_left(void);
ChuckTimber 1:2732adb68dad 18 *
ChuckTimber 1:2732adb68dad 19 * // void proc_REnc_right(void) { }
ChuckTimber 1:2732adb68dad 20 * // void proc_REnc_left(void) { }
ChuckTimber 1:2732adb68dad 21 *
ChuckTimber 1:2732adb68dad 22 * REnc renc(dp1, dp2);
ChuckTimber 1:2732adb68dad 23 *
ChuckTimber 1:2732adb68dad 24 * int main()
ChuckTimber 1:2732adb68dad 25 * {
ChuckTimber 1:2732adb68dad 26 *
ChuckTimber 1:2732adb68dad 27 * // renc.setHandleRight(&proc_REnc_right);
ChuckTimber 1:2732adb68dad 28 * // renc.setHandleLeft(&proc_REnc_left);
ChuckTimber 1:2732adb68dad 29 *
ChuckTimber 1:2732adb68dad 30 * while(1) {
ChuckTimber 1:2732adb68dad 31 * if (renc.CMD == FORWARD) {
ChuckTimber 1:2732adb68dad 32 * proc_REnc_right();
ChuckTimber 1:2732adb68dad 33 * renc.CMD = STOP;
ChuckTimber 1:2732adb68dad 34 * } else if (renc.CMD == REVERSE) {
ChuckTimber 1:2732adb68dad 35 * proc_REnc_left();
ChuckTimber 1:2732adb68dad 36 * renc.CMD = STOP;
ChuckTimber 1:2732adb68dad 37 * }
ChuckTimber 1:2732adb68dad 38 * }
ChuckTimber 1:2732adb68dad 39 * }
ChuckTimber 0:c905f9e6866f 40 * @endcode
ChuckTimber 0:c905f9e6866f 41 */
ChuckTimber 0:c905f9e6866f 42
ChuckTimber 2:46173a05338e 43 #define RENC_SAMPLING_PERIOD 0.002
ChuckTimber 2:46173a05338e 44 #define RENC_EXECUTION_DELAY 300
ChuckTimber 0:c905f9e6866f 45
ChuckTimber 0:c905f9e6866f 46 namespace mbed {
ChuckTimber 0:c905f9e6866f 47
ChuckTimber 1:2732adb68dad 48 enum { STOP, FORWARD, REVERSE };
ChuckTimber 0:c905f9e6866f 49
ChuckTimber 1:2732adb68dad 50 /** class to handle a rotary encoder
ChuckTimber 1:2732adb68dad 51 * The class uses DigitalIn and Ticker
ChuckTimber 0:c905f9e6866f 52 */
ChuckTimber 0:c905f9e6866f 53 class REnc {
ChuckTimber 0:c905f9e6866f 54
ChuckTimber 0:c905f9e6866f 55 public:
ChuckTimber 0:c905f9e6866f 56 unsigned char CMD;
ChuckTimber 1:2732adb68dad 57 unsigned char STABLE;
ChuckTimber 0:c905f9e6866f 58 void setHandleRight(void (*fptr)(void));
ChuckTimber 0:c905f9e6866f 59 void setHandleLeft(void (*fptr)(void));
ChuckTimber 0:c905f9e6866f 60
ChuckTimber 0:c905f9e6866f 61 REnc(PinName pina, PinName pinb);
ChuckTimber 0:c905f9e6866f 62
ChuckTimber 0:c905f9e6866f 63 private:
ChuckTimber 0:c905f9e6866f 64 void sample_encoder(void);
ChuckTimber 0:c905f9e6866f 65 void (*mRightCallback)(void);
ChuckTimber 0:c905f9e6866f 66 void (*mLeftCallback)(void);
ChuckTimber 0:c905f9e6866f 67
ChuckTimber 0:c905f9e6866f 68 DigitalIn _pina;
ChuckTimber 0:c905f9e6866f 69 DigitalIn _pinb;
ChuckTimber 0:c905f9e6866f 70 Ticker _tick;
ChuckTimber 0:c905f9e6866f 71
ChuckTimber 0:c905f9e6866f 72 }; /* end of REnc class definition */
ChuckTimber 0:c905f9e6866f 73
ChuckTimber 0:c905f9e6866f 74 } /* end of namespace mbed */
ChuckTimber 0:c905f9e6866f 75 #endif