Rotary Encoder handling library for mbed

Dependents:   MIDI_CW Gemphet8

Committer:
ChuckTimber
Date:
Wed Aug 06 23:54:19 2014 +0000
Revision:
3:9dfe441065e9
Parent:
2:46173a05338e
Child:
4:916992052518
changed parameter name

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ChuckTimber 0:c905f9e6866f 1 #include "REnc.h"
ChuckTimber 0:c905f9e6866f 2 #include "mbed.h"
ChuckTimber 0:c905f9e6866f 3
ChuckTimber 0:c905f9e6866f 4 /** class to make sound with a buzzer, based on a PwmOut
ChuckTimber 0:c905f9e6866f 5 * The class use a timeout to switch off the sound - it is not blocking while making noise
ChuckTimber 0:c905f9e6866f 6 *
ChuckTimber 0:c905f9e6866f 7 * Example:
ChuckTimber 0:c905f9e6866f 8 * @code
ChuckTimber 1:2732adb68dad 9 * // REnc sample
ChuckTimber 1:2732adb68dad 10 * #include "mbed.h"
ChuckTimber 1:2732adb68dad 11 * #include "REnc.h"
ChuckTimber 1:2732adb68dad 12 * void proc_REnc_right(void);
ChuckTimber 1:2732adb68dad 13 * void proc_REnc_left(void);
ChuckTimber 1:2732adb68dad 14 *
ChuckTimber 1:2732adb68dad 15 * // void proc_REnc_right(void) { }
ChuckTimber 1:2732adb68dad 16 * // void proc_REnc_left(void) { }
ChuckTimber 1:2732adb68dad 17 *
ChuckTimber 1:2732adb68dad 18 * REnc renc(dp1, dp2);
ChuckTimber 1:2732adb68dad 19 *
ChuckTimber 1:2732adb68dad 20 * int main()
ChuckTimber 1:2732adb68dad 21 * {
ChuckTimber 1:2732adb68dad 22 *
ChuckTimber 1:2732adb68dad 23 * // renc.setHandleRight(&proc_REnc_right);
ChuckTimber 1:2732adb68dad 24 * // renc.setHandleLeft(&proc_REnc_left);
ChuckTimber 1:2732adb68dad 25 *
ChuckTimber 1:2732adb68dad 26 * while(1) {
ChuckTimber 3:9dfe441065e9 27 * if (renc.CMD == CLOCKWISE) {
ChuckTimber 1:2732adb68dad 28 * proc_REnc_right();
ChuckTimber 1:2732adb68dad 29 * renc.CMD = STOP;
ChuckTimber 3:9dfe441065e9 30 * } else if (renc.CMD == COUNTERCLOCKWISE) {
ChuckTimber 1:2732adb68dad 31 * proc_REnc_left();
ChuckTimber 1:2732adb68dad 32 * renc.CMD = STOP;
ChuckTimber 1:2732adb68dad 33 * }
ChuckTimber 3:9dfe441065e9 34 * if (renc.STABLE) {
ChuckTimber 3:9dfe441065e9 35 * // do_something
ChuckTimber 3:9dfe441065e9 36 * }
ChuckTimber 1:2732adb68dad 37 * }
ChuckTimber 1:2732adb68dad 38 * }
ChuckTimber 0:c905f9e6866f 39 * @endcode
ChuckTimber 0:c905f9e6866f 40 */
ChuckTimber 0:c905f9e6866f 41
ChuckTimber 0:c905f9e6866f 42 using namespace mbed;
ChuckTimber 0:c905f9e6866f 43
ChuckTimber 1:2732adb68dad 44 // private function
ChuckTimber 1:2732adb68dad 45 /** sample_encoder input and process
ChuckTimber 1:2732adb68dad 46 *
ChuckTimber 1:2732adb68dad 47 */
ChuckTimber 0:c905f9e6866f 48 void REnc::sample_encoder(void)
ChuckTimber 0:c905f9e6866f 49 {
ChuckTimber 0:c905f9e6866f 50 static unsigned char i;
ChuckTimber 1:2732adb68dad 51 static unsigned int cnt;
ChuckTimber 1:2732adb68dad 52
ChuckTimber 0:c905f9e6866f 53 i = (i << 2) + ((~_pinb & 0x01)<< 1) + (~_pina & 0x1);
ChuckTimber 0:c905f9e6866f 54 i &= 0xf;
ChuckTimber 0:c905f9e6866f 55
ChuckTimber 0:c905f9e6866f 56 switch (i) {
ChuckTimber 0:c905f9e6866f 57 case 0x7:
ChuckTimber 0:c905f9e6866f 58 case 0xe:
ChuckTimber 3:9dfe441065e9 59 CMD = CLOCKWISE;
ChuckTimber 2:46173a05338e 60 cnt = RENC_EXECUTION_DELAY;
ChuckTimber 1:2732adb68dad 61 STABLE = 0;
ChuckTimber 1:2732adb68dad 62 if (mRightCallback != NULL) { mRightCallback(); CMD = STOP; }
ChuckTimber 0:c905f9e6866f 63 break;
ChuckTimber 0:c905f9e6866f 64 case 0xb:
ChuckTimber 0:c905f9e6866f 65 case 0xd:
ChuckTimber 3:9dfe441065e9 66 CMD = COUNTERCLOCKWISE;
ChuckTimber 2:46173a05338e 67 cnt = RENC_EXECUTION_DELAY;
ChuckTimber 1:2732adb68dad 68 STABLE = 0;
ChuckTimber 1:2732adb68dad 69 if (mLeftCallback != NULL) { mLeftCallback(); CMD = STOP; }
ChuckTimber 0:c905f9e6866f 70 break;
ChuckTimber 0:c905f9e6866f 71 default:
ChuckTimber 1:2732adb68dad 72 CMD = STOP;
ChuckTimber 1:2732adb68dad 73 if (cnt)
ChuckTimber 1:2732adb68dad 74 cnt--;
ChuckTimber 1:2732adb68dad 75 else
ChuckTimber 1:2732adb68dad 76 STABLE = 1;
ChuckTimber 0:c905f9e6866f 77 }
ChuckTimber 0:c905f9e6866f 78 }
ChuckTimber 0:c905f9e6866f 79
ChuckTimber 0:c905f9e6866f 80 void REnc::setHandleRight(void (*fptr)(void)) { mRightCallback = fptr; }
ChuckTimber 0:c905f9e6866f 81 void REnc::setHandleLeft(void (*fptr)(void)) { mLeftCallback = fptr; }
ChuckTimber 0:c905f9e6866f 82
ChuckTimber 1:2732adb68dad 83 // constructor
ChuckTimber 1:2732adb68dad 84 /** Create an REnc object connected to DigitalIn pins
ChuckTimber 1:2732adb68dad 85 *
ChuckTimber 1:2732adb68dad 86 * @param pina - Digital Input to A-phase of the rotary encoder
ChuckTimber 1:2732adb68dad 87 * @param pinb - Digital Input to B-phase of the rotary encoder
ChuckTimber 1:2732adb68dad 88 */
ChuckTimber 0:c905f9e6866f 89 REnc::REnc(PinName pina, PinName pinb) : _pina(pina), _pinb(pinb)
ChuckTimber 0:c905f9e6866f 90 {
ChuckTimber 1:2732adb68dad 91 CMD = STOP;
ChuckTimber 2:46173a05338e 92 STABLE = 1;
ChuckTimber 0:c905f9e6866f 93 mRightCallback = NULL;
ChuckTimber 0:c905f9e6866f 94 mLeftCallback = NULL;
ChuckTimber 2:46173a05338e 95 _tick.attach(this, &REnc::sample_encoder, RENC_SAMPLING_PERIOD);
ChuckTimber 0:c905f9e6866f 96 }