2017-06-24 02:13:28 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
const pca9685Address = 0x40
|
|
|
|
|
|
|
|
const (
|
|
|
|
PCA9685_MODE1 = 0x00
|
|
|
|
PCA9685_PRESCALE = 0xFE
|
|
|
|
PCA9685_SUBADR1 = 0x02
|
|
|
|
PCA9685_SUBADR2 = 0x03
|
|
|
|
PCA9685_SUBADR3 = 0x04
|
|
|
|
PCA9685_LED0_ON_L = 0x06
|
|
|
|
PCA9685_LED0_ON_H = 0x07
|
|
|
|
PCA9685_LED0_OFF_L = 0x08
|
|
|
|
PCA9685_LED0_OFF_H = 0x09
|
|
|
|
PCA9685_ALLLED_ON_L = 0xFA
|
|
|
|
PCA9685_ALLLED_ON_H = 0xFB
|
|
|
|
PCA9685_ALLLED_OFF_L = 0xFC
|
|
|
|
PCA9685_ALLLED_OFF_H = 0xFD
|
|
|
|
)
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
// PCA9685Driver is a Gobot Driver for the PCA9685 16-channel 12-bit
|
|
|
|
// PWM/Servo controller.
|
2017-06-24 02:13:28 +08:00
|
|
|
type PCA9685Driver struct {
|
|
|
|
name string
|
|
|
|
connector Connector
|
|
|
|
connection Connection
|
|
|
|
Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPCA9685Driver creates a new driver with specified i2c interface
|
|
|
|
// Params:
|
|
|
|
// conn Connector - the Adaptor to use with this Driver
|
|
|
|
//
|
|
|
|
// Optional params:
|
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
|
|
|
//
|
|
|
|
func NewPCA9685Driver(a Connector, options ...func(Config)) *PCA9685Driver {
|
2017-06-24 02:55:28 +08:00
|
|
|
p := &PCA9685Driver{
|
2017-06-24 02:13:28 +08:00
|
|
|
name: gobot.DefaultName("PCA9685"),
|
|
|
|
connector: a,
|
|
|
|
Config: NewConfig(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
2017-06-24 02:55:28 +08:00
|
|
|
option(p)
|
2017-06-24 02:13:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add commands for API
|
2017-06-24 02:55:28 +08:00
|
|
|
return p
|
2017-06-24 02:13:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the Name for the Driver
|
2017-06-24 02:55:28 +08:00
|
|
|
func (p *PCA9685Driver) Name() string { return p.name }
|
2017-06-24 02:13:28 +08:00
|
|
|
|
|
|
|
// SetName sets the Name for the Driver
|
2017-06-24 02:55:28 +08:00
|
|
|
func (p *PCA9685Driver) SetName(n string) { p.name = n }
|
2017-06-24 02:13:28 +08:00
|
|
|
|
|
|
|
// Connection returns the connection for the Driver
|
2017-06-24 02:55:28 +08:00
|
|
|
func (p *PCA9685Driver) Connection() gobot.Connection { return p.connector.(gobot.Connection) }
|
2017-06-24 02:13:28 +08:00
|
|
|
|
|
|
|
// Start initializes the pca9685
|
2017-06-24 02:55:28 +08:00
|
|
|
func (p *PCA9685Driver) Start() (err error) {
|
|
|
|
bus := p.GetBusOrDefault(p.connector.GetDefaultBus())
|
|
|
|
address := p.GetAddressOrDefault(pca9685Address)
|
2017-06-24 02:13:28 +08:00
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
p.connection, err = p.connector.GetConnection(address, bus)
|
2017-06-24 02:13:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
if _, err := p.connection.Write([]byte{PCA9685_MODE1, 0x00}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
if _, err := p.connection.Write([]byte{PCA9685_ALLLED_OFF_H, 0x10}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
// Halt stops the device
|
|
|
|
func (p *PCA9685Driver) Halt() (err error) {
|
|
|
|
_, err = p.connection.Write([]byte{PCA9685_ALLLED_OFF_H, 0x10})
|
|
|
|
return
|
|
|
|
}
|
2017-06-24 02:13:28 +08:00
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
// SetPWM sets a specific channel to a pwm value from 0-4096
|
|
|
|
func (p *PCA9685Driver) SetPWM(channel int, on uint16, off uint16) (err error) {
|
|
|
|
if _, err := p.connection.Write([]byte{byte(PCA9685_LED0_ON_L + 4*channel), byte(on), byte(on >> 8), byte(off), byte(off >> 8)}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
// SetPWMFreq sets the PWM frequency in Hz
|
|
|
|
func (p *PCA9685Driver) SetPWMFreq(freq float32) error {
|
2017-06-24 02:13:28 +08:00
|
|
|
freq *= 0.9
|
|
|
|
|
|
|
|
var prescalevel float32 = 25000000
|
|
|
|
prescalevel /= 4096
|
|
|
|
prescalevel /= freq
|
|
|
|
prescalevel -= 1
|
|
|
|
prescale := byte(prescalevel + 0.5)
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1)}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
data := make([]byte, 1)
|
2017-06-24 02:55:28 +08:00
|
|
|
oldmode, err := p.connection.Read(data)
|
2017-06-24 02:13:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
newmode := (oldmode & 0x7F) | 0x10
|
|
|
|
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(newmode)}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
if _, err := p.connection.Write([]byte{byte(PCA9685_PRESCALE), prescale}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode)}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2017-06-24 02:55:28 +08:00
|
|
|
if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode | 0xa1)}); err != nil {
|
2017-06-24 02:13:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
// PwmWrite writes a PWM signal to the specified pin
|
|
|
|
func (p *PCA9685Driver) PwmWrite(pin string, val byte) (err error) {
|
2017-06-24 02:13:28 +08:00
|
|
|
i, err := strconv.Atoi(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := gobot.ToScale(gobot.FromScale(float64(val), 0, 255), 0, 4096)
|
2017-06-24 02:55:28 +08:00
|
|
|
return p.SetPWM(i, 0, uint16(v))
|
2017-06-24 02:13:28 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 02:55:28 +08:00
|
|
|
// ServoWrite writes a servo signal to the specified pin.
|
|
|
|
// Valid values are from 0-180.
|
|
|
|
func (p *PCA9685Driver) ServoWrite(pin string, val byte) (err error) {
|
2017-06-24 02:13:28 +08:00
|
|
|
i, err := strconv.Atoi(pin)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v := gobot.ToScale(gobot.FromScale(float64(val), 0, 180), 200, 500)
|
2017-06-24 02:55:28 +08:00
|
|
|
return p.SetPWM(i, 0, uint16(v))
|
2017-06-24 02:13:28 +08:00
|
|
|
}
|