Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User provided functions never get to max t #85

Closed
TheLastGimbus opened this issue Jan 7, 2022 · 3 comments
Closed

User provided functions never get to max t #85

TheLastGimbus opened this issue Jan 7, 2022 · 3 comments

Comments

@TheLastGimbus
Copy link

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

😕

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

@jandelgado
Copy link
Owner

jandelgado commented Jan 7, 2022

@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
Copy link

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 github-actions bot added Stale and removed Stale labels Jun 21, 2022
@github-actions
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants