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:
7:c7daa056b152
Parent:
6:c2af98aa7d2b
Child:
8:3086ea6466d1
--- a/Encoder.cpp	Wed Mar 18 16:41:59 2015 +0000
+++ b/Encoder.cpp	Thu Mar 26 14:21:57 2015 +0000
@@ -52,7 +52,7 @@
 #define ENCODER_SAMPLE_PERIOD_US (250)
 #endif
 
-// Calc debounce cpounter reload
+// Calc debounce counter reload
 #define PB_DEBOUNCE_RELOAD (((uint32_t)ENCODER_PB_DEBOUNCE_MS * 1000uL) / ENCODER_SAMPLE_PERIOD_US)
 
 // Counts steps := pulses
@@ -110,10 +110,7 @@
     // Encoder section
     // If-then-else structure is good because every pin is read max. once.
     if(PinB) {
-        if(PinA) { // clockwise?
-#if DEBUG_ENC
-            DebugPin = 0;
-#endif
+        if(PinA) { // booth open?
             //no! Skips pulses at low speed! Ready = (!Up && !Down); (one extra cicle in idle mode)
             Ready = true;  // Reset all
             Up = false;
@@ -121,19 +118,23 @@
         } else {
             Up = Ready;  // clockwise.
         }
+    } else {
         if(PinA) { // counterclockwise?
-            Down = Ready; // counterclockwise.
-        } else {
+            Down = Ready;
+        } else {  // booth closed?
             if(Ready) {
-#if DEBUG_ENC
-                DebugPin = 1;
-#endif
                 if (Up) {
                     Pulses_u16++; //moving forward
+#if DEBUG_ENC
+                    DebugPin = 1;
+#endif
                 }
                 // Do not change to  ...else... construct!
                 if (Down) {
                     Pulses_u16--; //moving reverse
+#if DEBUG_ENC
+                    DebugPin = 0;
+#endif
                 }
             }
             Ready = false;