Fork of mRotaryEncoder for mdeb-os. uses newer version of PinDetect. Testprogram: https://os.mbed.com/users/charly/code/mRotaryEncoder_HelloWorld-os/

Dependencies:   PinDetect

Dependents:   mRotaryEncoder_HelloWorld-os TMC2209-Test2

Committer:
charly
Date:
Tue Mar 16 20:19:04 2021 +0000
Revision:
12:1925aac090b7
Parent:
11:24b34deae975
Version of mRotaryEncoder for mbed-os

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){
charly 12:1925aac090b7 15 m_pinA->attach_asserted(callback(this, &mRotaryEncoder::rise));
jirrick 11:24b34deae975 16 }
jirrick 11:24b34deae975 17 if (detectFall != 0){
charly 12:1925aac090b7 18 m_pinA->attach_deasserted(callback(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 12:1925aac090b7 59 if (rotCWIsr) {
charly 12:1925aac090b7 60 rotCWIsr();
charly 12:1925aac090b7 61 }
charly 0:562943b05e99 62 } else {
charly 0:562943b05e99 63 m_position--;
charly 12:1925aac090b7 64 if (rotCWIsr){
charly 12:1925aac090b7 65 rotCCWIsr();
charly 12:1925aac090b7 66 }
charly 0:562943b05e99 67 }
charly 12:1925aac090b7 68 if (rotIsr){
charly 12:1925aac090b7 69 rotIsr(); // call the isr for rotation
charly 12:1925aac090b7 70 }
charly 0:562943b05e99 71 }
charly 0:562943b05e99 72 }
charly 0:562943b05e99 73
charly 0:562943b05e99 74 void mRotaryEncoder::rise(void) {
charly 5:75ddffaf3721 75 //PinDetect does debouncing
charly 0:562943b05e99 76 //pinA still high?
charly 0:562943b05e99 77 if (*m_pinA == 1) {
charly 0:562943b05e99 78 if (*m_pinB == 1) {
charly 0:562943b05e99 79 m_position--;
charly 12:1925aac090b7 80 if (rotCCWIsr){
charly 12:1925aac090b7 81 rotCCWIsr();
charly 12:1925aac090b7 82 }
charly 0:562943b05e99 83 } else {
charly 0:562943b05e99 84 m_position++;
charly 12:1925aac090b7 85 if (rotCWIsr){
charly 12:1925aac090b7 86 rotCWIsr();
charly 12:1925aac090b7 87 }
charly 0:562943b05e99 88 }
charly 12:1925aac090b7 89 if (rotIsr){
charly 12:1925aac090b7 90 rotIsr(); // call the isr for rotation
charly 12:1925aac090b7 91 }
charly 0:562943b05e99 92 }
charly 0:562943b05e99 93 }
charly 0:562943b05e99 94