fade

xod/core/fade

Lineary animates an internal value toward target value `TARG` with a rate `RATE`. Use the node to smoothen LED switching, motor starting, or servo angular position update.
fade
@/fade
Lineary animates an internal value toward target value `TARG` with a rate `RATE`. Use the node to smoothen LED switching, motor starting, or servo angular position update.
TARGnumber
The target value to strive for.
RATEnumber
Speed rate in units per second.
UPDpulse
Triggers an update (i.e. recalculation) of `OUT` value. Keep the value set to `Continuously` to achieve the most smooth animation possible.
fade
OUT
TARG
RATE
UPD
OUTnumber
The current animation value.

C++ implementation

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_UPD

node {
    TimeMs lastUpdateTime;

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

        TimeMs now = transactionTime();
        Number target = getValue<input_TARG>(ctx);
        Number position = getValue<output_OUT>(ctx);

        if (target == position) {
            // Already done. Store timestamp anyway so that an animation to a new
            // value would not jump at the first update
            lastUpdateTime = now;
            return;
        }

        Number rate = getValue<input_RATE>(ctx);
        TimeMs dtMs = now - lastUpdateTime;
        Number step = (Number)dtMs / 1000. * rate;

        if (target > position) {
            position = min(target, position + step);
        } else {
            position = max(target, position - step);
        }

        emitValue<output_OUT>(ctx, position);
        lastUpdateTime = now;
    }
}

Tabular tests

__time(ms)TARGRATEUPDOUT
011pulse0
10011pulse0.1
35011pulse0.35~
90011pulse0.9
100011pulse1
150011pulse1
160011no-pulse1
199900.1no-pulse1
200000.1pulse1~
250000.1pulse0.95~
3000NaN1pulseNaN
40001NaNpulse1
50001Infpulse1
6000-1Infpulse-1
7000-100-InfpulseInf
8000Inf-InfpulseInf