Fork of original mRotaryEncoder library supporting more types of encoders (ones with full rise and fall cycle per one rotations).
mRotaryEncoder.cpp@7:ec80fd9c0c08, 2015-03-11 (annotated)
- Committer:
- charly
- Date:
- Wed Mar 11 21:45:46 2015 +0000
- Revision:
- 7:ec80fd9c0c08
- Parent:
- 5:75ddffaf3721
- Child:
- 10:2502b829d452
Added FunctionPointers and Methodes for clockwise(CW) rotation and counterclockwise(CCW) rotation
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:562943b05e99 | 60 | } |
charly | 0:562943b05e99 | 61 | rotIsr.call(); // call the isr for rotation |
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 | 0:562943b05e99 | 75 | } |
charly | 0:562943b05e99 | 76 | rotIsr.call(); // call the isr for rotation |
charly | 0:562943b05e99 | 77 | } |
charly | 0:562943b05e99 | 78 |