encoder-ky040

escape-ford/encoder-ky040/encoder-ky040

Encoder ky040 patch
encoder-ky040
@/encoder-ky040
Encoder ky040 patch
CLKboolean
Clock input node
DTboolean
Data input node
RESboolean
Reset input
encoder-ky040
CLK
DT
RES
OUT
CW
CCW
CCWboolean
Counterclockwise detection
CWboolean
Clockwise detection
OUTnumber
Increments output
To use the node in your project you should have the escape-ford/encoder-ky040 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

struct State {
    bool lastState=false;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    auto state = getState(ctx);

    auto CLK = (bool)getValue<input_CLK>(ctx);  // boolean digital inputs
    auto DT = (bool)getValue<input_DT>(ctx);
    auto RES = (bool)getValue<input_RES>(ctx);
    Number count = getValue<output_OUT>(ctx);   // count declaration
    auto CW=(bool)getValue<output_CW>(ctx);     // clockwise declaration
    auto CCW=(bool)getValue<output_CCW>(ctx);   // counterclockwise declaration

    auto currentState = CLK; // actual state storage
    if ((state->lastState== true) && (currentState == false)) {
        if (DT == true){
            count++; // clockwise increment
            CW=true;
            CCW=false;
       }else{
            count--; // counterclockwise decrement
            CW=false;
            CCW=true;
        }
        //if (count<0) {    // uncoment this to obtain only values above zero
        //count=0;
        //}
        emitValue<output_OUT>(ctx,count);
        emitValue<output_CW>(ctx,CW);
        emitValue<output_CCW>(ctx,CCW);
     }
    if (RES==true){
        count=0;
        emitValue<output_OUT>(ctx,count);
    }
    state->lastState = currentState;
}