Fork of original mRotaryEncoder library supporting more types of encoders (ones with full rise and fall cycle per one rotations).
mRotaryEncoder.cpp@0:562943b05e99, 2010-11-29 (annotated)
- Committer:
- charly
- Date:
- Mon Nov 29 21:25:13 2010 +0000
- Revision:
- 0:562943b05e99
- Child:
- 2:f99ac9745a2c
Initial version based on CRotaryEncoder from Thomas Raab
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 | 0:562943b05e99 | 6 | m_pinA = new InterruptIn(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 | 0:562943b05e99 | 14 | m_pinA->rise(this, &mRotaryEncoder::rise); |
charly | 0:562943b05e99 | 15 | m_pinA->fall(this, &mRotaryEncoder::fall); |
charly | 0:562943b05e99 | 16 | |
charly | 0:562943b05e99 | 17 | // Switch on pinSW |
charly | 0:562943b05e99 | 18 | m_pinSW = new InterruptIn(pinSW); // interrupt on press switch |
charly | 0:562943b05e99 | 19 | m_pinSW->mode(pullMode); |
charly | 0:562943b05e99 | 20 | |
charly | 0:562943b05e99 | 21 | m_position = 0; |
charly | 0:562943b05e99 | 22 | |
charly | 0:562943b05e99 | 23 | m_debounceTime_us = debounceTime_us; |
charly | 0:562943b05e99 | 24 | } |
charly | 0:562943b05e99 | 25 | |
charly | 0:562943b05e99 | 26 | mRotaryEncoder::~mRotaryEncoder() { |
charly | 0:562943b05e99 | 27 | delete m_pinA; |
charly | 0:562943b05e99 | 28 | delete m_pinB; |
charly | 0:562943b05e99 | 29 | delete m_pinSW; |
charly | 0:562943b05e99 | 30 | } |
charly | 0:562943b05e99 | 31 | |
charly | 0:562943b05e99 | 32 | int mRotaryEncoder::Get(void) { |
charly | 0:562943b05e99 | 33 | return m_position; |
charly | 0:562943b05e99 | 34 | } |
charly | 0:562943b05e99 | 35 | |
charly | 0:562943b05e99 | 36 | |
charly | 0:562943b05e99 | 37 | |
charly | 0:562943b05e99 | 38 | void mRotaryEncoder::Set(int value) { |
charly | 0:562943b05e99 | 39 | m_position = value; |
charly | 0:562943b05e99 | 40 | } |
charly | 0:562943b05e99 | 41 | |
charly | 0:562943b05e99 | 42 | |
charly | 0:562943b05e99 | 43 | void mRotaryEncoder::fall(void) { |
charly | 0:562943b05e99 | 44 | //no interrupts |
charly | 0:562943b05e99 | 45 | m_pinA->rise(NULL); |
charly | 0:562943b05e99 | 46 | m_pinA->fall(NULL); |
charly | 0:562943b05e99 | 47 | wait_us(m_debounceTime_us); // wait while switch is bouncing |
charly | 0:562943b05e99 | 48 | //pinA still low? |
charly | 0:562943b05e99 | 49 | if (*m_pinA == 0) { |
charly | 0:562943b05e99 | 50 | if (*m_pinB == 1) { |
charly | 0:562943b05e99 | 51 | m_position++; |
charly | 0:562943b05e99 | 52 | } else { |
charly | 0:562943b05e99 | 53 | m_position--; |
charly | 0:562943b05e99 | 54 | } |
charly | 0:562943b05e99 | 55 | } |
charly | 0:562943b05e99 | 56 | //reenable interrupts |
charly | 0:562943b05e99 | 57 | m_pinA->rise(this, &mRotaryEncoder::rise); |
charly | 0:562943b05e99 | 58 | m_pinA->fall(this, &mRotaryEncoder::fall); |
charly | 0:562943b05e99 | 59 | rotIsr.call(); // call the isr for rotation |
charly | 0:562943b05e99 | 60 | } |
charly | 0:562943b05e99 | 61 | |
charly | 0:562943b05e99 | 62 | void mRotaryEncoder::rise(void) { |
charly | 0:562943b05e99 | 63 | //no interrupts |
charly | 0:562943b05e99 | 64 | m_pinA->rise(NULL); |
charly | 0:562943b05e99 | 65 | m_pinA->fall(NULL); |
charly | 0:562943b05e99 | 66 | wait_us(m_debounceTime_us); // wait while switch is bouncing |
charly | 0:562943b05e99 | 67 | //pinA still high? |
charly | 0:562943b05e99 | 68 | if (*m_pinA == 1) { |
charly | 0:562943b05e99 | 69 | if (*m_pinB == 1) { |
charly | 0:562943b05e99 | 70 | m_position--; |
charly | 0:562943b05e99 | 71 | } else { |
charly | 0:562943b05e99 | 72 | m_position++; |
charly | 0:562943b05e99 | 73 | } |
charly | 0:562943b05e99 | 74 | } |
charly | 0:562943b05e99 | 75 | //reenable interrupts |
charly | 0:562943b05e99 | 76 | m_pinA->rise(this, &mRotaryEncoder::rise); |
charly | 0:562943b05e99 | 77 | m_pinA->fall(this, &mRotaryEncoder::fall); |
charly | 0:562943b05e99 | 78 | rotIsr.call(); // call the isr for rotation |
charly | 0:562943b05e99 | 79 | } |
charly | 0:562943b05e99 | 80 |