fade-down

vegunes/keypad-shield/fade-down

No description
fade-down
@/fade-down
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-down
OUT
TARG
RATE
UPD
OUTnumber
The current animation value.
To use the node in your project you should have the vegunes/keypad-shield 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 evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_UPD

struct State {
    TimeMs lastUpdateTime;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    State* state = getState(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
        state->lastUpdateTime = now;
        return;
    }

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

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

    emitValue<output_OUT>(ctx, position);
    state->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