jandelgado / jled

Non-blocking LED controlling library for Arduino and friends.
MIT License
331 stars 55 forks source link

User provided functions never get to max `t` #85

Closed TheLastGimbus closed 2 years ago

TheLastGimbus commented 2 years ago

Hi!

I wanted to write my own function that would just fade led linearly (btw this should be added to this lib).

So I wrote:


class LinearFadeOff : public jled::BrightnessEvaluator {
private:
    uint32_t _duration;
public:
    LinearFadeOff(uint32_t duration) { _duration = duration; }

    uint8_t Eval(uint32_t t) const override {
        return map(t, 0, _duration, 255, 0);
    }

    uint16_t Period() const override { return _duration; }
};

...but the led never fully turned off :(

So I added some prints:

uint8_t Eval(uint32_t t) const override {
    auto v = map(t, 0, _duration, 255, 0);
    Serial.print(t); Serial.print(" "); Serial.println(v);
    return v;
}

And for LinearFadeOff usrEffOff(500);, it prints:

491 6
492 5
493 5
494 4
495 4
496 3
497 3
498 2
499 2  <= 499 is laready here
499 2  <= 499 instead of 500

:confused:

The problem: Last value doubles instead of reaching the end :rainbow:

jandelgado commented 2 years ago

@TheLastGimbus: If the period is e.g. 500 like in your example, the value for t goes from 0 to 499, not from 0 to 500, t=500 will not be reached. The value is set twice because JLed makes sure that when Update is called for after the effects time elapsed, then the last value set, is that for period-1 or 499 in this example. This is necessary to handle edge cases like e.g.

led.Update();
delay(600);
led.Update();

Then JLed will make sure to set the LED's value to the value of t=499. Perphaps the addtional call could be optimized out, I'll have a look at it, but I don't see a problem here.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 5 days