Skip to content

Commit

Permalink
i2c: add commands to PCA9685Driver
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Aug 15, 2018
1 parent 80dccb3 commit eb7c7c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
28 changes: 25 additions & 3 deletions drivers/i2c/pca9685_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type PCA9685Driver struct {
connector Connector
connection Connection
Config
gobot.Commander
}

// NewPCA9685Driver creates a new driver with specified i2c interface
Expand All @@ -50,13 +51,34 @@ func NewPCA9685Driver(a Connector, options ...func(Config)) *PCA9685Driver {
name: gobot.DefaultName("PCA9685"),
connector: a,
Config: NewConfig(),
Commander: gobot.NewCommander(),
}

for _, option := range options {
option(p)
}

// TODO: add commands for API
p.AddCommand("PwmWrite", func(params map[string]interface{}) interface{} {
pin := params["pin"].(string)
val, _ := strconv.Atoi(params["val"].(string))
return p.PwmWrite(pin, byte(val))
})
p.AddCommand("ServoWrite", func(params map[string]interface{}) interface{} {
pin := params["pin"].(string)
val, _ := strconv.Atoi(params["val"].(string))
return p.ServoWrite(pin, byte(val))
})
p.AddCommand("SetPWM", func(params map[string]interface{}) interface{} {
channel, _ := strconv.Atoi(params["channel"].(string))
on, _ := strconv.Atoi(params["on"].(string))
off, _ := strconv.Atoi(params["off"].(string))
return p.SetPWM(channel, uint16(on), uint16(off))
})
p.AddCommand("SetPWMFreq", func(params map[string]interface{}) interface{} {
freq, _ := strconv.ParseFloat(params["freq"].(string), 32)
return p.SetPWMFreq(float32(freq))
})

return p
}

Expand Down Expand Up @@ -157,7 +179,7 @@ func (p *PCA9685Driver) SetPWMFreq(freq float32) error {
return nil
}

// PwmWrite writes a PWM signal to the specified pin.
// PwmWrite writes a PWM signal to the specified channel aka "pin".
// Value values are from 0-255, to conform to the PwmWriter interface.
// If you need finer control, please look at SetPWM().
//
Expand All @@ -170,7 +192,7 @@ func (p *PCA9685Driver) PwmWrite(pin string, val byte) (err error) {
return p.SetPWM(i, 0, uint16(v))
}

// ServoWrite writes a servo signal to the specified pin.
// ServoWrite writes a servo signal to the specified channel aka "pin".
// Valid values are from 0-180, to conform to the ServoWriter interface.
// If you need finer control, please look at SetPWM().
//
Expand Down
17 changes: 17 additions & 0 deletions drivers/i2c/pca9685_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,20 @@ func TestPCA9685DriverSetName(t *testing.T) {
pca.SetName("TESTME")
gobottest.Assert(t, pca.Name(), "TESTME")
}

func TestPCA9685DriverCommands(t *testing.T) {
pca := initTestPCA9685Driver()
pca.Start()

err := pca.Command("PwmWrite")(map[string]interface{}{"pin": "1", "val": "1"})
gobottest.Assert(t, err, nil)

err = pca.Command("ServoWrite")(map[string]interface{}{"pin": "1", "val": "1"})
gobottest.Assert(t, err, nil)

err = pca.Command("SetPWM")(map[string]interface{}{"channel": "1", "on": "0", "off": "1024"})
gobottest.Assert(t, err, nil)

err = pca.Command("SetPWMFreq")(map[string]interface{}{"freq": "60"})
gobottest.Assert(t, err, nil)
}

0 comments on commit eb7c7c5

Please sign in to comment.