brianlow / Rotary

Rotary encoder library for Arduino
233 stars 73 forks source link

support reactive callbacks #8

Open Andrei-Pozolotin opened 5 years ago

Andrei-Pozolotin commented 5 years ago

please add support for reactive callbacks, i.e.:

class RotaryReactor: public Rotary {
private:
    static void noop() {
    }
    void (*reactorPositive)() = &noop;
    void (*reactorNegative)() = &noop;
public:
    RotaryReactor(char pinA, char pinB) :
            Rotary(pinA, pinB) {
    }
    void attachReactorPositive(void (*reactor)()) {
        reactorPositive = reactor;
    }
    void attachReactorNegative(void (*reactor)()) {
        reactorNegative = reactor;
    }
    void react() {
        unsigned char result = this->process();
        if (result == DIR_CW) {
            reactorPositive();
        } else if (result == DIR_CCW) {
            reactorNegative();
        }
    }
};