Skip to content

Proposal: PWM abstraction changes to match hardware #932

@syoder

Description

@syoder

Currently, the machine package implements the methods Configure() and Set(). We need additional functionality in order to control the frequency and possibly other PWM settings per device. But because most devices have multiple channels per PWM, we really need a slightly different abstraction that would allow changing PWM settings separately from setting PWM channel outputs. See #855

Functionality we probably need:

  • any initialization needed, depending on chip / board
  • set frequency (possibly prescaler / bit rate settings?)
  • configure an individual channel's output Pin
  • set an individual channel's value
  • ability to stop the PWM

Option A:

PWM could have the following functions:

  • Configure()
  • SetFrequency(f int) error
  • ConfigureChannel(num int, pin machine.Pin)
  • SetChannel(num int, value uint16)
  • Stop()

One variation would be to have configure look like this: Configure(pins []machine.Pin) error and not have a ConfigureChannel().

Option B:

Have separate PWM and PWMChannel structs. PWM could have:

  • Configure()
  • SetFrequency(f int) error
  • Channel(num int) (PWMChannel, error)
  • Stop()

and PWMChannel could have:

  • Configure(pin machine.Pin)
  • Set(value uint16)

It would be somewhat painful to rewrite this for all the currently supported machines, but I think we need to do something in order to support servos and other hardware that uses PWMs. I would be happy to work on this if we can reach consensus. Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions