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:
5:70534b19d8bf
Parent:
4:ee384561bfa4
Child:
6:c2af98aa7d2b
--- a/Encoder.cpp	Wed Dec 17 19:35:25 2014 +0000
+++ b/Encoder.cpp	Fri Jan 16 15:33:21 2015 +0000
@@ -35,10 +35,15 @@
 #ifndef ENCODER_PIN_A
 #define ENCODER_PIN_A D6
 #endif
+
 #ifndef ENCODER_PIN_B
 #define ENCODER_PIN_B D7
 #endif
 
+#ifndef ENCODER_PIN_PB
+#define ENCODER_PIN_PB D5
+#endif
+
 // Sample every ... microseconds, default
 // 250µs is a good value for cheap 24 ppr encoders (ALPS EC12E24...) with strong bouncing.
 // 1000µs is a good value for quality 24 ppr encoders (ALPS EC12E24...) with low bouncing.
@@ -47,13 +52,19 @@
 #define ENCODER_SAMPLE_PERIOD_US (250)
 #endif
 
+// Calc debounce cpounter reload
+#define PB_DEBOUNCE_RELOAD (((uint32_t)ENCODER_PB_DEBOUNCE_MS * 1000uL) / ENCODER_SAMPLE_PERIOD_US)
+
 // Counts steps := pulses
 static volatile uint16_t Pulses;
 
 // State variables
-static volatile bool UpDown, Ready;
+static volatile bool UpDown, Ready, PB_RawState_b, PB_State_b;
 
-DigitalIn  PinA(ENCODER_PIN_A), PinB(ENCODER_PIN_B);
+// Counter for bebouncing
+static volatile uint16_t PB_DebounceCounter_u16;
+
+DigitalIn  PinA(ENCODER_PIN_A), PinB(ENCODER_PIN_B), PinPB(ENCODER_PIN_PB);
 
 static  Ticker EncoderTick;
 static void EncoderTickIRQ();
@@ -61,9 +72,11 @@
 // Call once in main.
 void EncoderStart ()
 {
-    PinA.mode(PullUp);
-    PinB.mode(PullUp);
+    PinA.mode(PullUp);  // Encoder pin A
+    PinB.mode(PullUp);  // Encoder pin B
     EncoderTick.attach_us (&EncoderTickIRQ, ENCODER_SAMPLE_PERIOD_US);
+    PinPB.mode(PullUp); // Pushbutton pin
+    PB_DebounceCounter_u16 = PB_DEBOUNCE_RELOAD;    // Start
 }
 
 // Switch off IRQ and change Pullups to PullDowns to save power
@@ -71,14 +84,22 @@
 {
     PinA.mode(PullDown);
     PinB.mode(PullDown);
+    PinPB.mode(PullDown);
     EncoderTick.detach();
 }
+
 // Get counting variable.
-uint16_t EncoderGet ()
+uint16_t EncoderGetPulses ()
 {
     return Pulses;
 }
 
+// Get pushbutton state.
+bool EncoderGetPB ()
+{
+    return not PB_State_b;  // invert, pressed == true
+}
+
 // State machine. Runs in IRQ. Static.
 static void EncoderTickIRQ()
 {
@@ -103,6 +124,15 @@
             }
         }
     }
+    if(PinPB == PB_RawState_b) {
+        if(--PB_DebounceCounter_u16 == 0) {
+            PB_State_b = PB_RawState_b;
+            PB_DebounceCounter_u16 = PB_DEBOUNCE_RELOAD;    // Restart
+        }
+    } else {
+        PB_RawState_b = PinPB;
+        PB_DebounceCounter_u16 = PB_DEBOUNCE_RELOAD;    // Restart
+    }
 }
 
 // End of file