tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/l9110x/l9110x.go (about)

     1  // Package l9110x provides a driver to the L9110/L9110S H-bridge chip
     2  // typically used to control DC motors.
     3  //
     4  // Datasheet: https://www.elecrow.com/download/datasheet-l9110.pdf
     5  package l9110x // import "tinygo.org/x/drivers/l9110x"
     6  
     7  import (
     8  	"machine"
     9  )
    10  
    11  // Device is a motor without speed control.
    12  // ia and ib are the directional pins.
    13  type Device struct {
    14  	ia, ib machine.Pin
    15  }
    16  
    17  // New returns a new Motor driver for GPIO-only operation.
    18  func New(direction1, direction2 machine.Pin) Device {
    19  	return Device{
    20  		ia: direction1,
    21  		ib: direction2,
    22  	}
    23  }
    24  
    25  // Configure configures the Device.
    26  func (d *Device) Configure() {
    27  	d.ia.Configure(machine.PinConfig{Mode: machine.PinOutput})
    28  	d.ib.Configure(machine.PinConfig{Mode: machine.PinOutput})
    29  
    30  	d.Stop()
    31  }
    32  
    33  // Forward turns motor on in forward direction.
    34  func (d *Device) Forward() {
    35  	d.ia.High()
    36  	d.ib.Low()
    37  }
    38  
    39  // Backward turns motor on in backward direction.
    40  func (d *Device) Backward() {
    41  	d.ia.Low()
    42  	d.ib.High()
    43  }
    44  
    45  // Stop turns motor off.
    46  func (d *Device) Stop() {
    47  	d.ia.Low()
    48  	d.ib.Low()
    49  }
    50  
    51  // PWM is the interface necessary for controlling the motor driver.
    52  type PWM interface {
    53  	Configure(config machine.PWMConfig) error
    54  	Channel(pin machine.Pin) (channel uint8, err error)
    55  	Top() uint32
    56  	Set(channel uint8, value uint32)
    57  	SetPeriod(period uint64) error
    58  }
    59  
    60  // PWMDevice is a motor with speed control.
    61  // ia and ib are the directional/speed PWM pins.
    62  type PWMDevice struct {
    63  	pwm    PWM
    64  	ca, cb uint8
    65  }
    66  
    67  // NewWithSpeed returns a new PWMMotor driver that uses 2 PWM pins to control both direction and speed.
    68  func NewWithSpeed(ca, cb uint8, pwm PWM) PWMDevice {
    69  	return PWMDevice{
    70  		pwm: pwm,
    71  		ca:  ca,
    72  		cb:  cb,
    73  	}
    74  }
    75  
    76  // Configure configures the PWMDevice. Note that the pins, PWM interface,
    77  // and channels must all already be configured.
    78  func (d *PWMDevice) Configure() (err error) {
    79  	d.Stop()
    80  	return
    81  }
    82  
    83  // Forward turns motor on in forward direction at specific speed as a percentage.
    84  func (d *PWMDevice) Forward(speed uint32) {
    85  	d.pwm.Set(d.ca, d.pwm.Top()*speed/100)
    86  	d.pwm.Set(d.cb, 0)
    87  }
    88  
    89  // Backward turns motor on in backward direction at specific speed as a percentage.
    90  func (d *PWMDevice) Backward(speed uint32) {
    91  	d.pwm.Set(d.ca, 0)
    92  	d.pwm.Set(d.cb, d.pwm.Top()*speed/100)
    93  }
    94  
    95  // Stop turns motor off.
    96  func (d *PWMDevice) Stop() {
    97  	d.pwm.Set(d.ca, 0)
    98  	d.pwm.Set(d.cb, 0)
    99  }