Rotary Encoder handling library for mbed

Dependents:   MIDI_CW Gemphet8

Committer:
ChuckTimber
Date:
Sat Jul 26 22:49:27 2014 +0000
Revision:
0:c905f9e6866f
Child:
1:2732adb68dad
Newly added library for rotary encoder class

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 0:c905f9e6866f 9 * @endcode
ChuckTimber 0:c905f9e6866f 10 */
ChuckTimber 0:c905f9e6866f 11
ChuckTimber 0:c905f9e6866f 12 using namespace mbed;
ChuckTimber 0:c905f9e6866f 13 // constructor
ChuckTimber 0:c905f9e6866f 14 /** Create a Beep object connected to the specified PwmOut pin
ChuckTimber 0:c905f9e6866f 15 *
ChuckTimber 0:c905f9e6866f 16 * @param pin PwmOut pin to connect to
ChuckTimber 0:c905f9e6866f 17 */
ChuckTimber 0:c905f9e6866f 18
ChuckTimber 0:c905f9e6866f 19 void REnc::sample_encoder(void)
ChuckTimber 0:c905f9e6866f 20 {
ChuckTimber 0:c905f9e6866f 21 static unsigned char i;
ChuckTimber 0:c905f9e6866f 22
ChuckTimber 0:c905f9e6866f 23 i = (i << 2) + ((~_pinb & 0x01)<< 1) + (~_pina & 0x1);
ChuckTimber 0:c905f9e6866f 24 i &= 0xf;
ChuckTimber 0:c905f9e6866f 25
ChuckTimber 0:c905f9e6866f 26 switch (i) {
ChuckTimber 0:c905f9e6866f 27 case 0x7:
ChuckTimber 0:c905f9e6866f 28 case 0xe:
ChuckTimber 0:c905f9e6866f 29 CMD = FORWARD;
ChuckTimber 0:c905f9e6866f 30 if (mRightCallback != NULL) { mRightCallback(); CMD = IDLE; }
ChuckTimber 0:c905f9e6866f 31 break;
ChuckTimber 0:c905f9e6866f 32 case 0xb:
ChuckTimber 0:c905f9e6866f 33 case 0xd:
ChuckTimber 0:c905f9e6866f 34 CMD = BACKWARD;
ChuckTimber 0:c905f9e6866f 35 if (mLeftCallback != NULL) { mLeftCallback(); CMD = IDLE; }
ChuckTimber 0:c905f9e6866f 36 break;
ChuckTimber 0:c905f9e6866f 37 default:
ChuckTimber 0:c905f9e6866f 38 CMD = IDLE;
ChuckTimber 0:c905f9e6866f 39 }
ChuckTimber 0:c905f9e6866f 40 }
ChuckTimber 0:c905f9e6866f 41
ChuckTimber 0:c905f9e6866f 42 void REnc::setHandleRight(void (*fptr)(void)) { mRightCallback = fptr; }
ChuckTimber 0:c905f9e6866f 43 void REnc::setHandleLeft(void (*fptr)(void)) { mLeftCallback = fptr; }
ChuckTimber 0:c905f9e6866f 44
ChuckTimber 0:c905f9e6866f 45 REnc::REnc(PinName pina, PinName pinb) : _pina(pina), _pinb(pinb)
ChuckTimber 0:c905f9e6866f 46 {
ChuckTimber 0:c905f9e6866f 47 CMD = IDLE;
ChuckTimber 0:c905f9e6866f 48 mRightCallback = NULL;
ChuckTimber 0:c905f9e6866f 49 mLeftCallback = NULL;
ChuckTimber 0:c905f9e6866f 50 _tick.attach(this, &REnc::sample_encoder, 0.01);
ChuckTimber 0:c905f9e6866f 51 }