delta

xod/core/delta

Calculates difference between current and past input values.
delta
@/delta
Calculates difference between current and past input values.
INnumber
Input value
UPDpulse
Update: trigger a calculation and store the current `IN` value as the new “past” value.
RSTpulse
Resets the stored past value to the current input value. Efficiently sets `OUT` to 0.
delta
OUT
IN
UPD
RST
OUTnumber
The last difference value.

C++ implementation

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_RST input_UPD
#pragma XOD dirtieness disable output_OUT

node {
    Number refValue = 0;

    void evaluate(Context ctx) {
        auto inValue = getValue<input_IN>(ctx);

        if (isInputDirty<input_RST>(ctx)) {
            refValue = inValue;
            emitValue<output_OUT>(ctx, 0);
            return;
        }

        if (!isInputDirty<input_UPD>(ctx)) {
          return;
        }

        auto outValue = inValue - refValue;
        emitValue<output_OUT>(ctx, outValue);
        refValue = inValue;
    }
}

Tabular tests

INUPDRSTOUT
0no-pulseno-pulse0
10pulseno-pulse10
30no-pulseno-pulse10
20no-pulsepulse0
30pulseno-pulse10
50pulseno-pulse20
10pulseno-pulse-40
10pulsepulse0
5pulseno-pulse-5