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:
jirrick
Date:
Tue Mar 03 12:20:55 2020 +0000
Revision:
11:24b34deae975
Parent:
10:2502b829d452
configure detection of rise and fall events

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
jirrick 11:24b34deae975 5 mRotaryEncoder::mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode, int debounceTime_us, int detectRise, int detectFall) {
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
jirrick 11:24b34deae975 14 if (detectRise != 0){
jirrick 11:24b34deae975 15 m_pinA->attach_asserted(this, &mRotaryEncoder::rise);
jirrick 11:24b34deae975 16 }
jirrick 11:24b34deae975 17 if (detectFall != 0){
jirrick 11:24b34deae975 18 m_pinA->attach_deasserted(this, &mRotaryEncoder::fall);
jirrick 11:24b34deae975 19 }
charly 5:75ddffaf3721 20
charly 5:75ddffaf3721 21 //start sampling pinA
charly 5:75ddffaf3721 22 m_pinA->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time.
charly 0:562943b05e99 23
charly 0:562943b05e99 24 // Switch on pinSW
charly 2:f99ac9745a2c 25 m_pinSW = new PinDetect(pinSW); // interrupt on press switch
charly 0:562943b05e99 26 m_pinSW->mode(pullMode);
charly 2:f99ac9745a2c 27
charly 3:39c2fc4482be 28 m_pinSW->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time.
charly 2:f99ac9745a2c 29
charly 0:562943b05e99 30
charly 0:562943b05e99 31 m_position = 0;
charly 0:562943b05e99 32
charly 0:562943b05e99 33 m_debounceTime_us = debounceTime_us;
charly 0:562943b05e99 34 }
charly 0:562943b05e99 35
charly 0:562943b05e99 36 mRotaryEncoder::~mRotaryEncoder() {
charly 0:562943b05e99 37 delete m_pinA;
charly 0:562943b05e99 38 delete m_pinB;
charly 0:562943b05e99 39 delete m_pinSW;
charly 0:562943b05e99 40 }
charly 0:562943b05e99 41
charly 0:562943b05e99 42 int mRotaryEncoder::Get(void) {
charly 0:562943b05e99 43 return m_position;
charly 0:562943b05e99 44 }
charly 0:562943b05e99 45
charly 0:562943b05e99 46
charly 0:562943b05e99 47
charly 0:562943b05e99 48 void mRotaryEncoder::Set(int value) {
charly 0:562943b05e99 49 m_position = value;
charly 0:562943b05e99 50 }
charly 0:562943b05e99 51
charly 0:562943b05e99 52
charly 0:562943b05e99 53 void mRotaryEncoder::fall(void) {
charly 5:75ddffaf3721 54 // debouncing does PinDetect for us
charly 0:562943b05e99 55 //pinA still low?
charly 0:562943b05e99 56 if (*m_pinA == 0) {
charly 0:562943b05e99 57 if (*m_pinB == 1) {
charly 0:562943b05e99 58 m_position++;
charly 7:ec80fd9c0c08 59 rotCWIsr.call();
charly 0:562943b05e99 60 } else {
charly 0:562943b05e99 61 m_position--;
charly 7:ec80fd9c0c08 62 rotCCWIsr.call();
charly 0:562943b05e99 63 }
charly 10:2502b829d452 64 rotIsr.call(); // call the isr for rotation
charly 0:562943b05e99 65 }
charly 0:562943b05e99 66 }
charly 0:562943b05e99 67
charly 0:562943b05e99 68 void mRotaryEncoder::rise(void) {
charly 5:75ddffaf3721 69 //PinDetect does debouncing
charly 0:562943b05e99 70 //pinA still high?
charly 0:562943b05e99 71 if (*m_pinA == 1) {
charly 0:562943b05e99 72 if (*m_pinB == 1) {
charly 0:562943b05e99 73 m_position--;
charly 7:ec80fd9c0c08 74 rotCCWIsr.call();
charly 0:562943b05e99 75 } else {
charly 0:562943b05e99 76 m_position++;
charly 7:ec80fd9c0c08 77 rotCWIsr.call();
charly 0:562943b05e99 78 }
charly 10:2502b829d452 79 rotIsr.call(); // call the isr for rotation
charly 0:562943b05e99 80 }
charly 0:562943b05e99 81 }
charly 0:562943b05e99 82