Skip to content

Add mutable frequency supports for PWM #9

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hamlet-lee
Copy link
Contributor

This PR provides the capability to change the frequency of PWM (which is also discussed in #8 )
An example "example/pwm_mutable_frequency.rs" is also added.

I have tested on my hardware device, and it works.

As I'm still deepening my understanding of Rust and this project, there may be some inadequacies in this implementation. I would greatly appreciate your feedback and suggestions for improvement. Also, you can feel free to take over the code and do modifications you think necessary.

Thank you for your time and guidance!

@hamlet-lee
Copy link
Contributor Author

@gpgreen , would you take a look a this PR when convienient?

@gpgreen
Copy link
Contributor

gpgreen commented May 28, 2025

I'll take a look soon, thanks for the work

@gpgreen
Copy link
Contributor

gpgreen commented Jun 3, 2025

I had some time to look, sorry it took a while. I'm glad you figured out a solution to what you want. I have some suggestions or comments.

One of the objectives of a HAL is to make it easy to use the hardware, and hard to misuse. An existing problem with the pwm code was that the max duty was altered to make it non-zero. It looks like your new timer has an issue with that, I see your comment on getting a 0 if the frequency, which is an Option, is None. So the bad design gets a little worse.

BTW, there is a good document that explains best practices on how to design a library API in rust, which is what this is. I have read it many times. It takes a lot of time to absorb all that for me. https://rust-lang.github.io/api-guidelines/

A much better way of getting the max duty, would have been to create a 'NewType`, or in this case a NonZero type. That way getting a zero cannot happen, you get an error instead. We are making the HAL hard to use with a fault that is hidden. I never fixed that due to time, and also I wasn't sure why it was done that way.

Is there a reason that the frequency is an Option? Conceptually that doesn't make sense, a timer with no frequency is a timer that hasn't been setup, and can't be used.

I see you added a "stop_pwm" method, but it is not added to the WithPwm trait which seals it.

It was hard to review the code as a diff, I really need to checkout your version and compare it but didn't have time today.

@hamlet-lee
Copy link
Contributor Author

@gpgreen
"Sorry for the late reply—I've caught a cold recently.
I've received your feedback and reanalyzed the code, and proposed another PR #10

@gpgreen
Copy link
Contributor

gpgreen commented Jun 20, 2025

I commented on your #10, this PR is the better solution to me. It allows you to do what you wanted.

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