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

Add pulse method to RGB class #1803

Merged
merged 9 commits into from
Jan 23, 2023
Merged

Add pulse method to RGB class #1803

merged 9 commits into from
Jan 23, 2023

Conversation

fisher-alice
Copy link
Contributor

@fisher-alice fisher-alice commented Dec 2, 2022

Currently, the Led class contains both pulse and blink methods, but the Rgb class only contains the blink method.

This PR adds a pulse method to the Rgb class. Note that the pulse method was recently re-added to the forked branch code-dot-org/johnny-five after being updated to the latest release of upstream. The pulse method was first added to the Rgb class by @islemaster in this PR.

I added the state value isRunning which was already a defined property in order to keep track of whether an Animation is in progress, i.e., if pulse is being run. This is similar to how isRunning is used in lib/led.js.

The update method is now writeable to be able to reassign it in the unit test for Animation.render in test/rgb.js.

More details of implementation are included in embedded comments.

Testing

I modified unit tests in both rgb.collection.js and rgb.js as well as added new unit tests for the RGB pulse method.

Documentation

At the request of @dtex, I added the example file docs/led-rgb-pulse.md and here is documentation about the Rgb pulse method that can be added to the johnny-five wiki.

@fisher-alice fisher-alice changed the title Rgb pulse Add pulse method to RGB class Dec 2, 2022
/* istanbul ignore else */
if (this.isOn) {
state.prev = RGB.colors.reduce((current, color) => (current[color] = state[color], current), {});

Copy link
Contributor Author

@fisher-alice fisher-alice Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within the off() method, there is no need to reassign state.prev if the RGB is on since state.prev is assigned when the on() method is called. This also ensures that if off() is called when the RGB is pulsing that the RGB values are NOT assigned lesser and lesser values until reaching (0,0,0) which is the case when off() is repeatedly called after pulse() is called if the line above remains.

}


}

RGB.colors = ["red", "green", "blue"];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignment of RGB.colors was moved before Animation.keys is assigned to it. This small PR resolves this issue and adds a unit test for this fix.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1799 has been merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

lib/led/rgb.js Outdated
return this.color(frames[0]);
const state = priv.get(this);
state.value = frames[0];
return this.update();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Animation.render method here was calling color(), which does a lot of normalization and validation before itself calling update on line 270 above.
Now the render method behaves more like render in the led class which 'trusts' its animations and calls update directly .
This also bypasses the reassignment of state.prev to a 'tween' value when Animation.render is called for pulse function which resulted in the RGB values decreasing so that the RGB light would gradually decrease.

@dtex dtex self-requested a review December 3, 2022 00:47
Copy link
Collaborator

@dtex dtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @fisher-alice; This is an excellent addition to the RGB class.

Everything looks good, and I think the only thing that is missing is documentation. The wiki needs the RGB.pulse() method, and it warrants an example file too.

Can you add those things?

this.update(colors);
} else if (state.isRunning) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this else-if branch for case when on() is called while RGB is pulsing so that RGB displays full RGB values and not 'tween' values of pulse method.

@fisher-alice
Copy link
Contributor Author

Thank you, @fisher-alice; This is an excellent addition to the RGB class.

Everything looks good, and I think the only thing that is missing is documentation. The wiki needs the RGB.pulse() method, and it warrants an example file too.

Can you add those things?

Hello @dtex , thanks for taking time to look at my PR! Yes, I can definitely add documentation to the wiki and create an example file. FYI - I also added a commit since your comment.
I see you're based in Houston as well...go Coogs!

@dtex
Copy link
Collaborator

dtex commented Jan 23, 2023

Oh, you're in Houston too?! Cool! I haven't been getting out much lately (for like almost three years). Have you been to any local meetups or groups on IoT or robtics stuff? I see Houston Robotics Club has been re-animated but I haven't checked it out yet.

There are a few PR's here that I think are ready. I'll ping Rick and see if I can get them merged.

@dtex dtex merged commit bff6cdb into rwaldron:main Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants