Library for manual Encoders as used in user interfaces. Very simple, reduced and rock solid encoder library. Counts full pulses only. Inherent debouncing through state machine. Running on a regular timer IRQ. No IRQ jamming through bouncing. Immune to false edges giving unwanted counts when moving partial steps. Not depending on PinDetect or anything else. May be enhanced by adding acceleration and push button debouncing in the future.

Library for manual Encoders as used in user interfaces. Very simple, reduced and rock solid. Counts full pulses/steps only. Inherent debouncing through state machine. No time dependency for debouncing. Very tight code running in a regular timer IRQ. No IRQ jamming through bouncing edges. Immune to false edges giving unwanted counts when moving partial steps back and forth. Not depending on PinDetect or any other debouncing library. May be enhanced in the near future by adding acceleration and push button debouncing.

Revision:
6:c2af98aa7d2b
Parent:
5:70534b19d8bf
Child:
7:c7daa056b152
--- a/Encoder.cpp	Fri Jan 16 15:33:21 2015 +0000
+++ b/Encoder.cpp	Wed Mar 18 16:41:59 2015 +0000
@@ -56,12 +56,12 @@
 #define PB_DEBOUNCE_RELOAD (((uint32_t)ENCODER_PB_DEBOUNCE_MS * 1000uL) / ENCODER_SAMPLE_PERIOD_US)
 
 // Counts steps := pulses
-static volatile uint16_t Pulses;
+static volatile uint16_t Pulses_u16;
 
 // State variables
-static volatile bool UpDown, Ready, PB_RawState_b, PB_State_b;
+static volatile bool Up, Down, Ready, PB_RawState_b, PB_State_b;
 
-// Counter for bebouncing
+// Counter for debouncing
 static volatile uint16_t PB_DebounceCounter_u16;
 
 DigitalIn  PinA(ENCODER_PIN_A), PinB(ENCODER_PIN_B), PinPB(ENCODER_PIN_PB);
@@ -69,6 +69,10 @@
 static  Ticker EncoderTick;
 static void EncoderTickIRQ();
 
+#if DEBUG_ENC
+static DigitalOut DebugPin(D4);
+#endif
+
 // Call once in main.
 void EncoderStart ()
 {
@@ -91,7 +95,7 @@
 // Get counting variable.
 uint16_t EncoderGetPulses ()
 {
-    return Pulses;
+    return Pulses_u16;
 }
 
 // Get pushbutton state.
@@ -103,27 +107,41 @@
 // State machine. Runs in IRQ. Static.
 static void EncoderTickIRQ()
 {
+    // Encoder section
     // If-then-else structure is good because every pin is read max. once.
     if(PinB) {
         if(PinA) { // clockwise?
-            Ready = true;
+#if DEBUG_ENC
+            DebugPin = 0;
+#endif
+            //no! Skips pulses at low speed! Ready = (!Up && !Down); (one extra cicle in idle mode)
+            Ready = true;  // Reset all
+            Up = false;
+            Down = false;
         } else {
-            UpDown = true;
+            Up = Ready;  // clockwise.
         }
-    } else {
         if(PinA) { // counterclockwise?
-            UpDown = false;
+            Down = Ready; // counterclockwise.
         } else {
             if(Ready) {
-                if (UpDown) {
-                    Pulses++; //moving forward
-                } else {
-                    Pulses--; //moving reverse
+#if DEBUG_ENC
+                DebugPin = 1;
+#endif
+                if (Up) {
+                    Pulses_u16++; //moving forward
                 }
-                Ready = false;
+                // Do not change to  ...else... construct!
+                if (Down) {
+                    Pulses_u16--; //moving reverse
+                }
             }
+            Ready = false;
         }
     }
+
+
+// Pushbutton section
     if(PinPB == PB_RawState_b) {
         if(--PB_DebounceCounter_u16 == 0) {
             PB_State_b = PB_RawState_b;