Skip to content

Commit

Permalink
Adapted DRV2605L driver to latest i2c changes
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Agsjö <erik.agsjo@gmail.com>
  • Loading branch information
erkkah committed Feb 20, 2017
1 parent e9b05a9 commit c91465c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
50 changes: 34 additions & 16 deletions drivers/i2c/drv2605l_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,38 @@ const (
//
// Basic use:
//
// haptic := i2c.NewDRV2605Driver(adaptor)
// haptic.SetSequence([]byte{1, 13})
// haptic.Go()
// haptic := i2c.NewDRV2605Driver(adaptor)
// haptic.SetSequence([]byte{1, 13})
// haptic.Go()
//
type DRV2605LDriver struct {
name string
connector I2cConnector
connection I2cConnection
connector Connector
connection Connection
Config
}

// NewDRV2605LDriver creates a new driver with the i2c interface for the DRV2605L device.
func NewDRV2605LDriver(c I2cConnector) *DRV2605LDriver {
return &DRV2605LDriver{
name: "DRV2605L",
connector: c,
// NewDRV2605LDriver creates a new driver for the DRV2605L device.
//
// 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 NewDRV2605LDriver(conn Connector, options ...func(Config)) *DRV2605LDriver {
driver := &DRV2605LDriver{
name: gobot.DefaultName("DRV2605L"),
connector: conn,
Config: NewConfig(),
}

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

return driver
}

// Name returns the name of the device.
Expand Down Expand Up @@ -118,20 +134,22 @@ func (d *DRV2605LDriver) writeByteRegisters(regValPairs []struct{ reg, val uint8
}

func (d *DRV2605LDriver) initialize() (err error) {
bus := d.connector.I2cGetDefaultBus()
d.connection, err = d.connector.I2cGetConnection(drv2605Address, bus)
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
address := d.GetAddressOrDefault(drv2605Address)

d.connection, err = d.connector.GetConnection(address, bus)
if err != nil {
return err
return
}

feedback, err := d.connection.ReadByteData(drv2605RegFeedback)
if err != nil {
return err
return
}

control, err := d.connection.ReadByteData(drv2605RegControl3)
if err != nil {
return err
return
}

err = d.writeByteRegisters([]struct{ reg, val uint8 }{
Expand All @@ -151,7 +169,7 @@ func (d *DRV2605LDriver) initialize() (err error) {
{drv2605RegControl3, control | 0x20},
})

return err
return
}

// SetMode sets the device in one of the eight modes as described in the
Expand Down
11 changes: 11 additions & 0 deletions drivers/i2c/drv2605l_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ func TestDRV2605LDriverSequenceTruncation(t *testing.T) {
drv2605RegWaveSeq8, 8,
})
}

func TestDRV2605LDriverSetName(t *testing.T) {
d, _ := initTestDriverAndAdaptor()
d.SetName("TESTME")
gobottest.Assert(t, d.Name(), "TESTME")
}

func TestDRV2605DriverOptions(t *testing.T) {
d := NewDRV2605LDriver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
}

0 comments on commit c91465c

Please sign in to comment.