Fork of original mRotaryEncoder library supporting more types of encoders (ones with full rise and fall cycle per one rotations).
mRotaryEncoder.cpp@3:39c2fc4482be, 2011-02-01 (annotated)
- Committer:
- charly
- Date:
- Tue Feb 01 19:54:35 2011 +0000
- Revision:
- 3:39c2fc4482be
- Parent:
- 2:f99ac9745a2c
- Child:
- 5:75ddffaf3721
Updated PinDetect functions
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 | 2:f99ac9745a2c | 18 | m_pinSW = new PinDetect(pinSW); // interrupt on press switch |
charly | 0:562943b05e99 | 19 | m_pinSW->mode(pullMode); |
charly | 2:f99ac9745a2c | 20 | |
charly | 3:39c2fc4482be | 21 | m_pinSW->setSampleFrequency(debounceTime_us); // Start timers an Defaults debounce time. |
charly | 2:f99ac9745a2c | 22 | |
charly | 0:562943b05e99 | 23 | |
charly | 0:562943b05e99 | 24 | m_position = 0; |
charly | 0:562943b05e99 | 25 | |
charly | 0:562943b05e99 | 26 | m_debounceTime_us = debounceTime_us; |
charly | 0:562943b05e99 | 27 | } |
charly | 0:562943b05e99 | 28 | |
charly | 0:562943b05e99 | 29 | mRotaryEncoder::~mRotaryEncoder() { |
charly | 0:562943b05e99 | 30 | delete m_pinA; |
charly | 0:562943b05e99 | 31 | delete m_pinB; |
charly | 0:562943b05e99 | 32 | delete m_pinSW; |
charly | 0:562943b05e99 | 33 | } |
charly | 0:562943b05e99 | 34 | |
charly | 0:562943b05e99 | 35 | int mRotaryEncoder::Get(void) { |
charly | 0:562943b05e99 | 36 | return m_position; |
charly | 0:562943b05e99 | 37 | } |
charly | 0:562943b05e99 | 38 | |
charly | 0:562943b05e99 | 39 | |
charly | 0:562943b05e99 | 40 | |
charly | 0:562943b05e99 | 41 | void mRotaryEncoder::Set(int value) { |
charly | 0:562943b05e99 | 42 | m_position = value; |
charly | 0:562943b05e99 | 43 | } |
charly | 0:562943b05e99 | 44 | |
charly | 0:562943b05e99 | 45 | |
charly | 0:562943b05e99 | 46 | void mRotaryEncoder::fall(void) { |
charly | 0:562943b05e99 | 47 | //no interrupts |
charly | 0:562943b05e99 | 48 | m_pinA->rise(NULL); |
charly | 0:562943b05e99 | 49 | m_pinA->fall(NULL); |
charly | 0:562943b05e99 | 50 | wait_us(m_debounceTime_us); // wait while switch is bouncing |
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 | 0:562943b05e99 | 55 | } else { |
charly | 0:562943b05e99 | 56 | m_position--; |
charly | 0:562943b05e99 | 57 | } |
charly | 0:562943b05e99 | 58 | } |
charly | 0:562943b05e99 | 59 | //reenable interrupts |
charly | 0:562943b05e99 | 60 | m_pinA->rise(this, &mRotaryEncoder::rise); |
charly | 0:562943b05e99 | 61 | m_pinA->fall(this, &mRotaryEncoder::fall); |
charly | 0:562943b05e99 | 62 | rotIsr.call(); // call the isr for rotation |
charly | 0:562943b05e99 | 63 | } |
charly | 0:562943b05e99 | 64 | |
charly | 0:562943b05e99 | 65 | void mRotaryEncoder::rise(void) { |
charly | 0:562943b05e99 | 66 | //no interrupts |
charly | 0:562943b05e99 | 67 | m_pinA->rise(NULL); |
charly | 0:562943b05e99 | 68 | m_pinA->fall(NULL); |
charly | 0:562943b05e99 | 69 | wait_us(m_debounceTime_us); // wait while switch is bouncing |
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 | 0:562943b05e99 | 74 | } else { |
charly | 0:562943b05e99 | 75 | m_position++; |
charly | 0:562943b05e99 | 76 | } |
charly | 0:562943b05e99 | 77 | } |
charly | 0:562943b05e99 | 78 | //reenable interrupts |
charly | 0:562943b05e99 | 79 | m_pinA->rise(this, &mRotaryEncoder::rise); |
charly | 0:562943b05e99 | 80 | m_pinA->fall(this, &mRotaryEncoder::fall); |
charly | 0:562943b05e99 | 81 | rotIsr.call(); // call the isr for rotation |
charly | 0:562943b05e99 | 82 | } |
charly | 0:562943b05e99 | 83 |