save loops

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CRotaryEncoder.cpp Source File

CRotaryEncoder.cpp

00001 #include "mbed.h"
00002 #include "CRotaryEncoder.h"
00003 
00004 
00005 CRotaryEncoder rotaryEncoder1=CRotaryEncoder(ROTARY_ENCODER1_PINA, ROTARY_ENCODER1_PINB);//pre-instanciation of object lockin with inter-file scope (declared extern in .h file)
00006 CRotaryEncoder rotaryEncoder2=CRotaryEncoder(ROTARY_ENCODER2_PINA, ROTARY_ENCODER2_PINB);//pre-instanciation of object lockin with inter-file scope (declared extern in .h file)
00007 
00008 CRotaryEncoder::CRotaryEncoder(PinName pinA, PinName pinB)
00009 {
00010     m_pinA = new InterruptIn(pinA);
00011     m_pinA->mode(PullUp); // this is for use in the case of the simple contact based rotary encoder (common is tied to ground)
00012     m_pinA->rise(this, &CRotaryEncoder::rise);
00013     m_pinA->fall(this, &CRotaryEncoder::fall);
00014 
00015     m_pinB = new DigitalIn(pinB);
00016     m_pinB->mode(PullUp); 
00017     m_position = 0;
00018     
00019     newValue=true;
00020     minValue=0; maxValue=360;
00021 }
00022 
00023 CRotaryEncoder::~CRotaryEncoder()
00024 {
00025     delete m_pinA;
00026     delete m_pinB;
00027 }
00028  
00029  void CRotaryEncoder::SetMinMax(int min, int max) {
00030      minValue=min;
00031      maxValue=max;
00032      }
00033  
00034 int CRotaryEncoder::Get(void)
00035 {
00036     return m_position;
00037 }
00038 
00039 void CRotaryEncoder::Set(int value)
00040 {
00041     if (value>maxValue) m_position = maxValue;
00042     if (value<minValue) m_position = minValue;
00043     m_position = value;
00044 }
00045 
00046 bool CRotaryEncoder::CheckNew()
00047 {
00048     bool auxValue=newValue;
00049     newValue=0;
00050     return(auxValue);
00051 }
00052 
00053 void CRotaryEncoder::wrapValue() {
00054     if (m_position>maxValue) m_position = minValue;
00055     if (m_position<minValue) m_position = maxValue;
00056     }
00057 
00058 void CRotaryEncoder::fall(void)
00059 {
00060         if(*m_pinB == 1)
00061         {
00062             m_position++;
00063         }
00064         else
00065         {
00066             m_position--;
00067         }
00068         
00069         wrapValue();
00070         
00071         newValue=true;
00072 }
00073 
00074 void CRotaryEncoder::rise(void)
00075 {
00076         if(*m_pinB == 1)
00077         {
00078             m_position--;
00079         }
00080         else
00081         {
00082             m_position++;
00083         }
00084         
00085         wrapValue(); 
00086         
00087         newValue=true;
00088 }
00089