Handle rotary encoder connected to two pins. With interrupt. Operating encoder changes an int position value that can be polled.

Dependents:   encoder_test distance_measure speed_measure straight_speed_control ... more

Files at this revision

API Documentation at this revision

Comitter:
Raabinator
Date:
Tue Nov 09 20:14:32 2010 +0000
Commit message:
First version

Changed in this revision

CRotaryEncoder.cpp Show annotated file Show diff for this revision Revisions of this file
CRotaryEncoder.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 56f636261771 CRotaryEncoder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CRotaryEncoder.cpp	Tue Nov 09 20:14:32 2010 +0000
@@ -0,0 +1,54 @@
+#include "mbed.h"
+#include "CRotaryEncoder.h"
+
+CRotaryEncoder::CRotaryEncoder(PinName pinA, PinName pinB)
+{
+    m_pinA = new InterruptIn(pinA);
+    m_pinA->rise(this, &CRotaryEncoder::rise);
+    m_pinA->fall(this, &CRotaryEncoder::fall);
+
+    m_pinB = new DigitalIn(pinB);
+    m_position = 0;
+}
+
+CRotaryEncoder::~CRotaryEncoder()
+{
+    delete m_pinA;
+    delete m_pinB;
+}
+ 
+int CRotaryEncoder::Get(void)
+{
+    return m_position;
+}
+
+void CRotaryEncoder::Set(int value)
+{
+    m_position = value;
+}
+
+    
+void CRotaryEncoder::fall(void)
+{
+        if(*m_pinB == 1)
+        {
+            m_position++;
+        }
+        else
+        {
+            m_position--;
+        }
+}
+
+void CRotaryEncoder::rise(void)
+{
+        if(*m_pinB == 1)
+        {
+            m_position--;
+        }
+        else
+        {
+            m_position++;
+        }
+}
+
diff -r 000000000000 -r 56f636261771 CRotaryEncoder.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CRotaryEncoder.h	Tue Nov 09 20:14:32 2010 +0000
@@ -0,0 +1,61 @@
+#ifndef CROTENC_H_INCLUDED
+#define CROTENC_H_INCLUDED
+
+#include "mbed.h"
+
+
+/* This Class handles a rotary encoder like the one from Pollin electronic (Panasonic EVEP...).
+ * It uses two pins, one creating an interrupt on change.
+ * Rotation direction is determined by checking the state of the other pin.
+ *
+ * Operating the encoder changes an internal integer value that can be read
+ * by Get() or the operator int() functions.
+ * A new value can be set by Set(value) or opperator=.
+ *
+ * Autor: Thomas Raab (Raabinator)
+ *
+ * Dent steady point     !     !     !
+ *                    +-----+     +-----+
+ * pinA (interrupt)   |     |     |     |
+ *                  --+     +-----+     +---
+ *                      +-----+     +-----+
+ * pinB                 |     |     |     |
+ *                  ----+     +-----+     +-
+ *                           --> C.W
+ * CW:  increases position value
+ * CCW: decreases position value
+ *
+ * changelog:
+ *
+ * 09. Nov. 2010
+ *     First version published.
+ *
+ */
+
+
+
+class CRotaryEncoder
+{
+    public:
+    CRotaryEncoder(PinName pinA, PinName pinB);
+    ~CRotaryEncoder();
+    
+    int Get(void);
+    inline operator int() { return Get(); }
+
+    void Set(int value);
+    inline CRotaryEncoder& operator= ( int  value ) { Set(value); return *this; }
+
+    private:
+    InterruptIn     *m_pinA;
+    DigitalIn       *m_pinB;
+    volatile int    m_position;
+    
+    void rise(void);
+    void fall(void);
+    
+
+};
+
+
+#endif