Six crescent shaped legs

Dependencies:   mbed

Encoder.cpp

Committer:
sim642
Date:
2016-06-21
Revision:
47:4f418a4b0051
Parent:
37:8021b3ce241a

File content as of revision 47:4f418a4b0051:

#include "Encoder.hpp"

/*
Forward:
A   0011001100
B   1001100110
pB  1100110011
A != pB

Backward:
A   0011001100
B   0110011001
pB  0011001100
A == pB
*/

Encoder::Encoder(EncoderData nData) :
    data(nData),
    intA(data.encAPin), intB(data.encBPin),
    valA(DigitalIn(data.encAPin).read()),
    valB(DigitalIn(data.encBPin).read())
{
    intA.rise(this, &Encoder::changeA);
    intA.fall(this, &Encoder::changeA);
    intB.rise(this, &Encoder::changeB);
    intB.fall(this, &Encoder::changeB);
    reset();
}

void Encoder::reset()
{
    count = 0;
}

long Encoder::getCount() const
{
    return count;
}

float Encoder::getTurn() const
{
    return float(getCount()) / data.turnCount;
}

void Encoder::changeA()
{
    valA = !valA;
    changeCount(false);
}

void Encoder::changeB()
{
    valB = !valB;
    changeCount(true);
}

void Encoder::changeCount(bool flipB)
{
    if (valA == (valB ^ flipB))
        count++;
    else
        count--;
}