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.

Encoder.cpp

Committer:
eduardoG26
Date:
2014-11-25
Revision:
1:158a3886aced
Parent:
UI_Encoder.cpp@ 0:fc87dcec05fd
Child:
2:603b47d3755e

File content as of revision 1:158a3886aced:

// Encoder lib
// Extremely simple time driven state machin with inherent debouncing.
// No irq-jamming because of switch bouncing
// No counting on first edges when encoder leaves idle state (both switches open).
// Rock solid

#include "mbed.h"
#include "Encoder.h"


// Chose encoder pins, default, nucleo-XXX
#ifndef ENCODER_PIN_A
#define ENCODER_PIN_A D6
#endif
#ifndef ENCODER_PIN_B
#define ENCODER_PIN_B D7
#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.
// 250µs is a good value for fast 24+ ppr encoders (Sparkfun, Qubig Panel1 type...).
#ifndef ENCODER_SAMPLE_PERIOD_US
#define ENCODER_SAMPLE_PERIOD_US (250)
#endif

static volatile uint16_t Pulses;
static bool UpDown, Ready;

DigitalIn  PinA(ENCODER_PIN_A), PinB(ENCODER_PIN_B);

static  Ticker EncoderTick;
static void EncoderTickIRQ();

// Call once in main.
void EncoderStart ()
{
    PinA.mode(PullUp);
    PinB.mode(PullUp);
    EncoderTick.attach_us (&EncoderTickIRQ, ENCODER_SAMPLE_PERIOD_US);
}

// Switch off Pullups and IRQ to save power f. e.
void EncoderStop ()
{
    PinA.mode(PullNone);
    PinB.mode(PullNone);
    EncoderTick.detach();
}
// Get counting variable.
uint16_t EncoderGet ()
{
    return Pulses;
}

// State machine. Runs in IRQ. Static.
static void EncoderTickIRQ()
{
    // If-then-else structure is good because every pin is read max. once.
    if(PinB) {
        if(PinA) { // clockwise?
            Ready = true;
        } else {
            UpDown = true;
        }
    } else {
        if(PinA) { // counterclockwise?
            UpDown = false;
        } else {
            if(Ready) {
                if (UpDown) {
                    Pulses++; //moving forward
                } else {
                    Pulses--; //moving reverse
                }
                Ready = false;
            }
        }
    }
}

// End of file