Class mRotaryEncoder for mechanical incremental rotary encoders with pushbuttons. Use debouncing and callback-functions for rotation and pressing of button. This version is for old mbed. New version for mbed-os see https://os.mbed.com/users/charly/code/mRotaryEncoder-os/

Dependencies:   PinDetect

Dependents:   SimplePIDBot FinalProgram VS1053Player SPK-DVIMXR ... more

Committer:
charly
Date:
Fri Feb 26 20:18:57 2016 +0000
Revision:
10:2502b829d452
Parent:
7:ec80fd9c0c08
Child:
11:24b34deae975
bugfix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charly 0:562943b05e99 1 #include "mbed.h"
charly 0:562943b05e99 2 #include "mRotaryEncoder.h"
charly 0:562943b05e99 3
charly 0:562943b05e99 4
charly 0:562943b05e99 5 mRotaryEncoder::mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode, int debounceTime_us) {
charly 5:75ddffaf3721 6 m_pinA = new PinDetect(pinA); // interrrupts on pinA
charly 0:562943b05e99 7 m_pinB = new DigitalIn(pinB); // only digitalIn for pinB
charly 0:562943b05e99 8
charly 0:562943b05e99 9 //set pins with internal PullUP-default
charly 0:562943b05e99 10 m_pinA->mode(pullMode);
charly 0:562943b05e99 11 m_pinB->mode(pullMode);
charly 0:562943b05e99 12
charly 0:562943b05e99 13 // attach interrrupts on pinA
charly 5:75ddffaf3721 14 m_pinA->attach_asserted(this, &mRotaryEncoder::rise);
charly 5:75ddffaf3721 15 m_pinA->attach_deasserted(this, &mRotaryEncoder::fall);
charly 5:75ddffaf3721 16
charly 5:75ddffaf3721 17 //start sampling pinA
charly 5:75ddffaf3721 18 m_pinA->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time.
charly 0:562943b05e99 19
charly 0:562943b05e99 20 // Switch on pinSW
charly 2:f99ac9745a2c 21 m_pinSW = new PinDetect(pinSW); // interrupt on press switch
charly 0:562943b05e99 22 m_pinSW->mode(pullMode);
charly 2:f99ac9745a2c 23
charly 3:39c2fc4482be 24 m_pinSW->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time.
charly 2:f99ac9745a2c 25
charly 0:562943b05e99 26
charly 0:562943b05e99 27 m_position = 0;
charly 0:562943b05e99 28
charly 0:562943b05e99 29 m_debounceTime_us = debounceTime_us;
charly 0:562943b05e99 30 }
charly 0:562943b05e99 31
charly 0:562943b05e99 32 mRotaryEncoder::~mRotaryEncoder() {
charly 0:562943b05e99 33 delete m_pinA;
charly 0:562943b05e99 34 delete m_pinB;
charly 0:562943b05e99 35 delete m_pinSW;
charly 0:562943b05e99 36 }
charly 0:562943b05e99 37
charly 0:562943b05e99 38 int mRotaryEncoder::Get(void) {
charly 0:562943b05e99 39 return m_position;
charly 0:562943b05e99 40 }
charly 0:562943b05e99 41
charly 0:562943b05e99 42
charly 0:562943b05e99 43
charly 0:562943b05e99 44 void mRotaryEncoder::Set(int value) {
charly 0:562943b05e99 45 m_position = value;
charly 0:562943b05e99 46 }
charly 0:562943b05e99 47
charly 0:562943b05e99 48
charly 0:562943b05e99 49 void mRotaryEncoder::fall(void) {
charly 5:75ddffaf3721 50 // debouncing does PinDetect for us
charly 0:562943b05e99 51 //pinA still low?
charly 0:562943b05e99 52 if (*m_pinA == 0) {
charly 0:562943b05e99 53 if (*m_pinB == 1) {
charly 0:562943b05e99 54 m_position++;
charly 7:ec80fd9c0c08 55 rotCWIsr.call();
charly 0:562943b05e99 56 } else {
charly 0:562943b05e99 57 m_position--;
charly 7:ec80fd9c0c08 58 rotCCWIsr.call();
charly 0:562943b05e99 59 }
charly 10:2502b829d452 60 rotIsr.call(); // call the isr for rotation
charly 0:562943b05e99 61 }
charly 0:562943b05e99 62 }
charly 0:562943b05e99 63
charly 0:562943b05e99 64 void mRotaryEncoder::rise(void) {
charly 5:75ddffaf3721 65 //PinDetect does debouncing
charly 0:562943b05e99 66 //pinA still high?
charly 0:562943b05e99 67 if (*m_pinA == 1) {
charly 0:562943b05e99 68 if (*m_pinB == 1) {
charly 0:562943b05e99 69 m_position--;
charly 7:ec80fd9c0c08 70 rotCCWIsr.call();
charly 0:562943b05e99 71 } else {
charly 0:562943b05e99 72 m_position++;
charly 7:ec80fd9c0c08 73 rotCWIsr.call();
charly 0:562943b05e99 74 }
charly 10:2502b829d452 75 rotIsr.call(); // call the isr for rotation
charly 0:562943b05e99 76 }
charly 0:562943b05e99 77 }
charly 0:562943b05e99 78