Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mRotaryEncoder_HelloWorld-os TMC2209-Test2
mRotaryEncoder.cpp
00001 #include "mbed.h" 00002 #include "mRotaryEncoder.h" 00003 00004 00005 mRotaryEncoder::mRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode, int debounceTime_us, int detectRise, int detectFall) { 00006 m_pinA = new PinDetect(pinA); // interrrupts on pinA 00007 m_pinB = new DigitalIn(pinB); // only digitalIn for pinB 00008 00009 //set pins with internal PullUP-default 00010 m_pinA->mode(pullMode); 00011 m_pinB->mode(pullMode); 00012 00013 // attach interrrupts on pinA 00014 if (detectRise != 0){ 00015 m_pinA->attach_asserted(callback(this, &mRotaryEncoder::rise)); 00016 } 00017 if (detectFall != 0){ 00018 m_pinA->attach_deasserted(callback(this, &mRotaryEncoder::fall)); 00019 } 00020 00021 //start sampling pinA 00022 m_pinA->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time. 00023 00024 // Switch on pinSW 00025 m_pinSW = new PinDetect(pinSW); // interrupt on press switch 00026 m_pinSW->mode(pullMode); 00027 00028 m_pinSW->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time. 00029 00030 00031 m_position = 0; 00032 00033 m_debounceTime_us = debounceTime_us; 00034 } 00035 00036 mRotaryEncoder::~mRotaryEncoder() { 00037 delete m_pinA; 00038 delete m_pinB; 00039 delete m_pinSW; 00040 } 00041 00042 int mRotaryEncoder::Get(void) { 00043 return m_position; 00044 } 00045 00046 00047 00048 void mRotaryEncoder::Set(int value) { 00049 m_position = value; 00050 } 00051 00052 00053 void mRotaryEncoder::fall(void) { 00054 // debouncing does PinDetect for us 00055 //pinA still low? 00056 if (*m_pinA == 0) { 00057 if (*m_pinB == 1) { 00058 m_position++; 00059 if (rotCWIsr) { 00060 rotCWIsr(); 00061 } 00062 } else { 00063 m_position--; 00064 if (rotCWIsr){ 00065 rotCCWIsr(); 00066 } 00067 } 00068 if (rotIsr){ 00069 rotIsr(); // call the isr for rotation 00070 } 00071 } 00072 } 00073 00074 void mRotaryEncoder::rise(void) { 00075 //PinDetect does debouncing 00076 //pinA still high? 00077 if (*m_pinA == 1) { 00078 if (*m_pinB == 1) { 00079 m_position--; 00080 if (rotCCWIsr){ 00081 rotCCWIsr(); 00082 } 00083 } else { 00084 m_position++; 00085 if (rotCWIsr){ 00086 rotCWIsr(); 00087 } 00088 } 00089 if (rotIsr){ 00090 rotIsr(); // call the isr for rotation 00091 } 00092 } 00093 } 00094
Generated on Sat Jul 16 2022 08:13:14 by
1.7.2