Alvaro Cassinelli
/
skinGames_II
save loops
hardwareIO/CRotaryEncoder.cpp@1:3be7b7d050f4, 2014-12-02 (annotated)
- Committer:
- mbedalvaro
- Date:
- Tue Dec 02 08:29:59 2014 +0000
- Revision:
- 1:3be7b7d050f4
- Parent:
- 0:df6fdd9b99f0
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:df6fdd9b99f0 | 1 | #include "mbed.h" |
mbedalvaro | 0:df6fdd9b99f0 | 2 | #include "CRotaryEncoder.h" |
mbedalvaro | 0:df6fdd9b99f0 | 3 | |
mbedalvaro | 0:df6fdd9b99f0 | 4 | |
mbedalvaro | 0:df6fdd9b99f0 | 5 | CRotaryEncoder rotaryEncoder1=CRotaryEncoder(ROTARY_ENCODER1_PINA, ROTARY_ENCODER1_PINB);//pre-instanciation of object lockin with inter-file scope (declared extern in .h file) |
mbedalvaro | 0:df6fdd9b99f0 | 6 | CRotaryEncoder rotaryEncoder2=CRotaryEncoder(ROTARY_ENCODER2_PINA, ROTARY_ENCODER2_PINB);//pre-instanciation of object lockin with inter-file scope (declared extern in .h file) |
mbedalvaro | 0:df6fdd9b99f0 | 7 | |
mbedalvaro | 0:df6fdd9b99f0 | 8 | CRotaryEncoder::CRotaryEncoder(PinName pinA, PinName pinB) |
mbedalvaro | 0:df6fdd9b99f0 | 9 | { |
mbedalvaro | 0:df6fdd9b99f0 | 10 | m_pinA = new InterruptIn(pinA); |
mbedalvaro | 0:df6fdd9b99f0 | 11 | m_pinA->mode(PullUp); // this is for use in the case of the simple contact based rotary encoder (common is tied to ground) |
mbedalvaro | 0:df6fdd9b99f0 | 12 | m_pinA->rise(this, &CRotaryEncoder::rise); |
mbedalvaro | 0:df6fdd9b99f0 | 13 | m_pinA->fall(this, &CRotaryEncoder::fall); |
mbedalvaro | 0:df6fdd9b99f0 | 14 | |
mbedalvaro | 0:df6fdd9b99f0 | 15 | m_pinB = new DigitalIn(pinB); |
mbedalvaro | 0:df6fdd9b99f0 | 16 | m_pinB->mode(PullUp); |
mbedalvaro | 0:df6fdd9b99f0 | 17 | m_position = 0; |
mbedalvaro | 0:df6fdd9b99f0 | 18 | |
mbedalvaro | 0:df6fdd9b99f0 | 19 | newValue=true; |
mbedalvaro | 0:df6fdd9b99f0 | 20 | minValue=0; maxValue=360; |
mbedalvaro | 0:df6fdd9b99f0 | 21 | } |
mbedalvaro | 0:df6fdd9b99f0 | 22 | |
mbedalvaro | 0:df6fdd9b99f0 | 23 | CRotaryEncoder::~CRotaryEncoder() |
mbedalvaro | 0:df6fdd9b99f0 | 24 | { |
mbedalvaro | 0:df6fdd9b99f0 | 25 | delete m_pinA; |
mbedalvaro | 0:df6fdd9b99f0 | 26 | delete m_pinB; |
mbedalvaro | 0:df6fdd9b99f0 | 27 | } |
mbedalvaro | 0:df6fdd9b99f0 | 28 | |
mbedalvaro | 0:df6fdd9b99f0 | 29 | void CRotaryEncoder::SetMinMax(int min, int max) { |
mbedalvaro | 0:df6fdd9b99f0 | 30 | minValue=min; |
mbedalvaro | 0:df6fdd9b99f0 | 31 | maxValue=max; |
mbedalvaro | 0:df6fdd9b99f0 | 32 | } |
mbedalvaro | 0:df6fdd9b99f0 | 33 | |
mbedalvaro | 0:df6fdd9b99f0 | 34 | int CRotaryEncoder::Get(void) |
mbedalvaro | 0:df6fdd9b99f0 | 35 | { |
mbedalvaro | 0:df6fdd9b99f0 | 36 | return m_position; |
mbedalvaro | 0:df6fdd9b99f0 | 37 | } |
mbedalvaro | 0:df6fdd9b99f0 | 38 | |
mbedalvaro | 0:df6fdd9b99f0 | 39 | void CRotaryEncoder::Set(int value) |
mbedalvaro | 0:df6fdd9b99f0 | 40 | { |
mbedalvaro | 0:df6fdd9b99f0 | 41 | if (value>maxValue) m_position = maxValue; |
mbedalvaro | 0:df6fdd9b99f0 | 42 | if (value<minValue) m_position = minValue; |
mbedalvaro | 0:df6fdd9b99f0 | 43 | m_position = value; |
mbedalvaro | 0:df6fdd9b99f0 | 44 | } |
mbedalvaro | 0:df6fdd9b99f0 | 45 | |
mbedalvaro | 0:df6fdd9b99f0 | 46 | bool CRotaryEncoder::CheckNew() |
mbedalvaro | 0:df6fdd9b99f0 | 47 | { |
mbedalvaro | 0:df6fdd9b99f0 | 48 | bool auxValue=newValue; |
mbedalvaro | 0:df6fdd9b99f0 | 49 | newValue=0; |
mbedalvaro | 0:df6fdd9b99f0 | 50 | return(auxValue); |
mbedalvaro | 0:df6fdd9b99f0 | 51 | } |
mbedalvaro | 0:df6fdd9b99f0 | 52 | |
mbedalvaro | 0:df6fdd9b99f0 | 53 | void CRotaryEncoder::wrapValue() { |
mbedalvaro | 0:df6fdd9b99f0 | 54 | if (m_position>maxValue) m_position = minValue; |
mbedalvaro | 0:df6fdd9b99f0 | 55 | if (m_position<minValue) m_position = maxValue; |
mbedalvaro | 0:df6fdd9b99f0 | 56 | } |
mbedalvaro | 0:df6fdd9b99f0 | 57 | |
mbedalvaro | 0:df6fdd9b99f0 | 58 | void CRotaryEncoder::fall(void) |
mbedalvaro | 0:df6fdd9b99f0 | 59 | { |
mbedalvaro | 0:df6fdd9b99f0 | 60 | if(*m_pinB == 1) |
mbedalvaro | 0:df6fdd9b99f0 | 61 | { |
mbedalvaro | 0:df6fdd9b99f0 | 62 | m_position++; |
mbedalvaro | 0:df6fdd9b99f0 | 63 | } |
mbedalvaro | 0:df6fdd9b99f0 | 64 | else |
mbedalvaro | 0:df6fdd9b99f0 | 65 | { |
mbedalvaro | 0:df6fdd9b99f0 | 66 | m_position--; |
mbedalvaro | 0:df6fdd9b99f0 | 67 | } |
mbedalvaro | 0:df6fdd9b99f0 | 68 | |
mbedalvaro | 0:df6fdd9b99f0 | 69 | wrapValue(); |
mbedalvaro | 0:df6fdd9b99f0 | 70 | |
mbedalvaro | 0:df6fdd9b99f0 | 71 | newValue=true; |
mbedalvaro | 0:df6fdd9b99f0 | 72 | } |
mbedalvaro | 0:df6fdd9b99f0 | 73 | |
mbedalvaro | 0:df6fdd9b99f0 | 74 | void CRotaryEncoder::rise(void) |
mbedalvaro | 0:df6fdd9b99f0 | 75 | { |
mbedalvaro | 0:df6fdd9b99f0 | 76 | if(*m_pinB == 1) |
mbedalvaro | 0:df6fdd9b99f0 | 77 | { |
mbedalvaro | 0:df6fdd9b99f0 | 78 | m_position--; |
mbedalvaro | 0:df6fdd9b99f0 | 79 | } |
mbedalvaro | 0:df6fdd9b99f0 | 80 | else |
mbedalvaro | 0:df6fdd9b99f0 | 81 | { |
mbedalvaro | 0:df6fdd9b99f0 | 82 | m_position++; |
mbedalvaro | 0:df6fdd9b99f0 | 83 | } |
mbedalvaro | 0:df6fdd9b99f0 | 84 | |
mbedalvaro | 0:df6fdd9b99f0 | 85 | wrapValue(); |
mbedalvaro | 0:df6fdd9b99f0 | 86 | |
mbedalvaro | 0:df6fdd9b99f0 | 87 | newValue=true; |
mbedalvaro | 0:df6fdd9b99f0 | 88 | } |
mbedalvaro | 0:df6fdd9b99f0 | 89 |