mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-14 20:03:40 +00:00
pwm: update drivers with PWM to use new interface
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+38
-16
@@ -56,49 +56,71 @@ func (d *Device) Stop() {
|
||||
d.en.Low()
|
||||
}
|
||||
|
||||
// PWM is the interface necessary for controlling the motor driver.
|
||||
type PWM interface {
|
||||
Configure(config machine.PWMConfig) error
|
||||
Channel(pin machine.Pin) (channel uint8, err error)
|
||||
Top() uint32
|
||||
Set(channel uint8, value uint32)
|
||||
SetPeriod(period uint64) error
|
||||
}
|
||||
|
||||
// PWMDevice is a motor with speed control.
|
||||
// a1 and a2 are the directional GPIO pins.
|
||||
// en is the PWM pin that controls the motor speed.
|
||||
type PWMDevice struct {
|
||||
a1, a2 machine.Pin
|
||||
en machine.PWM
|
||||
spc uint8
|
||||
pwm PWM
|
||||
}
|
||||
|
||||
// NewWithSpeed returns a new PWMMotor driver that uses a PWM pin to control speed.
|
||||
func NewWithSpeed(direction1, direction2 machine.Pin, speedPin machine.PWM) PWMDevice {
|
||||
// NewWithSpeed returns a new PWMMotor driver that uses an already configured PWM channel
|
||||
// to control speed.
|
||||
func NewWithSpeed(direction1, direction2 machine.Pin, spc uint8, pwm PWM) PWMDevice {
|
||||
return PWMDevice{
|
||||
a1: direction1,
|
||||
a2: direction2,
|
||||
en: speedPin,
|
||||
a1: direction1,
|
||||
a2: direction2,
|
||||
spc: spc,
|
||||
pwm: pwm,
|
||||
}
|
||||
}
|
||||
|
||||
// Configure configures the PWMDevice.
|
||||
func (d *PWMDevice) Configure() {
|
||||
// Configure configures the PWMDevice. Note that the PWM interface and
|
||||
// channel must already be configured, this function will not do it for you.
|
||||
func (d *PWMDevice) Configure() error {
|
||||
d.a1.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.a2.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.en.Configure()
|
||||
|
||||
d.Stop()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Forward turns motor on in forward direction at specific speed.
|
||||
func (d *PWMDevice) Forward(speed uint16) {
|
||||
// Forward turns motor on in forward direction at specific speed as a percentage.
|
||||
func (d *PWMDevice) Forward(speed uint32) {
|
||||
if speed > 100 {
|
||||
speed = 100
|
||||
}
|
||||
|
||||
d.a1.High()
|
||||
d.a2.Low()
|
||||
d.en.Set(speed)
|
||||
d.pwm.Set(d.spc, d.pwm.Top()*speed/100)
|
||||
}
|
||||
|
||||
// Backward turns motor on in backward direction at specific speed.
|
||||
func (d *PWMDevice) Backward(speed uint16) {
|
||||
// Backward turns motor on in backward direction at specific speed as a percentage.
|
||||
func (d *PWMDevice) Backward(speed uint32) {
|
||||
if speed > 100 {
|
||||
speed = 100
|
||||
}
|
||||
|
||||
d.a1.Low()
|
||||
d.a2.High()
|
||||
d.en.Set(speed)
|
||||
d.pwm.Set(d.spc, d.pwm.Top()*speed/100)
|
||||
}
|
||||
|
||||
// Stop turns motor off.
|
||||
func (d *PWMDevice) Stop() {
|
||||
d.a1.Low()
|
||||
d.a2.Low()
|
||||
d.en.Set(0)
|
||||
d.pwm.Set(d.spc, 0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user