A patch to decode a rotary encoder INC and DEC pulses.
rotary-encoder-pulses
@/rotary-encoder-pulses
A patch to decode a rotary encoder INC and DEC pulses.
DTport
Board port to read from
CLKport
Board port to read from
UPDpulse
Triggers new read
DONEpulse
Fires on reading complete
DECpulse
DEC pulse out.
INCpulse
INC pulse out.
To use the node in your project you should have the bjbaylon/utilities-for-menus 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
#pragma XOD error_raise enable
struct State {
bool state = false;
};
{{ GENERATED_CODE }}
void evaluate(Context ctx) {
State* state = getState(ctx);
if (!isInputDirty<input_UPD>(ctx))
return;
const uint8_t dtport = getValue<input_DT>(ctx);
if (!isValidDigitalPort(dtport)) {
raiseError(ctx);
return;
}
const uint8_t clkport = getValue<input_CLK>(ctx);
if (!isValidDigitalPort(clkport)) {
raiseError(ctx);
return;
}
::pinMode(dtport, INPUT);
::pinMode(clkport, INPUT);
bool Dat = (ctx, ::digitalRead(dtport));
bool Clk = (ctx, ::digitalRead(clkport));
bool newValue = (!(Dat && Clk));
if (newValue == true && state->state == false) {
if (Dat) {
emitValue<output_INC>(ctx, 1);
emitValue<output_DONE>(ctx, 1);
}
else if (Clk) {
emitValue<output_DEC>(ctx, 1);
emitValue<output_DONE>(ctx, 1);
}
}
state->state = newValue;
}