Skip to content

Add mruby-esp32-pwm. #49

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 2 commits into
base: master
Choose a base branch
from
Open

Add mruby-esp32-pwm. #49

wants to merge 2 commits into from

Conversation

yuuu
Copy link
Contributor

@yuuu yuuu commented Jul 29, 2023

Added PWM class.

This class uses MCPWM internally, unlike LEDC.

This class is implemented based on the "peripheral class common specifications" here.
https://github.com/HirohitoHigashi/mruby_io_class_study


@vickash
I am not sure if the name "PWM class" is appropriate since the role is similar to mruby-esp32-ledc. However, I would like to follow the "peripheral class common specifications."
Any good ideas would be appreciated.

@yuuu yuuu self-assigned this Jul 29, 2023
@yuuu yuuu requested a review from vickash July 29, 2023 21:30
@vickash
Copy link
Contributor

vickash commented Jul 30, 2023

Just tested this with a servo on my ESP32. Works great!

Wrt naming, I think the class should be MCPWM and the gem mruby-esp32-mcpwm. Your methods already follow the spec, and we can rework LEDC to match the spec too, so there's consistency. But I like that the class and gem names make it clear which resources on the chip are being used.

I didn't dig all the way into the MCPWM docs yet, but can each channel set its frequency independently?

For LEDC there are 4 timers shared among each group of 6 or 8 channels, so #frequency might get tricky. To abstract it away, it will need to optimize timer use, so motors all share one 50Hz timer for example, and timers no longer in use can be reused. Maybe an issue for MCPWM too?

@yuuu
Copy link
Contributor Author

yuuu commented Jul 30, 2023

@vickash
Thank you for your advices ! I also think this mrbgem should be renamed.

However, we believe that a class named PWM is necessary in case a program is ported from another platform (ex. mruby/c) or a beginner uses mruby-esp32.

I checked the MicroPython on ESP32 documentation and the PWM class was still present. And it uses LEDC internally.
https://docs.micropython.org/en/latest/esp32/quickref.html#pwm-pulse-width-modulation

I have two ideas.

  1. Extend the methods of the PWM class described in "peripheral class common specifications" to mruby-esp32-ledc and add an alias with the class name PWM
  2. Implement a new PWM class with LDEC internally

How about I create a Pull Request with idea 1, if you don't mind?

@yuuu
Copy link
Contributor Author

yuuu commented Jul 30, 2023

I didn't dig all the way into the MCPWM docs yet, but can each channel set its frequency independently?

Perhaps you could use more than one timer. However, the current mruby-esp32-pwm can only use one timer.

@vickash
Copy link
Contributor

vickash commented Jul 30, 2023

However, we believe that a class named PWM is necessary in case a program is ported from another platform (ex. mruby/c) or a beginner uses mruby-esp32.

Ah, I see. I think aliasing LEDC to PWM is a good choice then. I'll do a PR soon with those changes, and some logic to manage its timer allocation.

@yuuu
Copy link
Contributor Author

yuuu commented Jul 30, 2023

@vickash
Thanks. I pushed rename mrbgem commit.
Please review its code.

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