Skip to content

Commit

Permalink
i2c: update mpu6050 for bus interface
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 9, 2017
1 parent 5790cb5 commit 9a93d3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 12 additions & 5 deletions drivers/i2c/mpu6050_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ type MPU6050Driver struct {
}

// NewMPU6050Driver creates a new driver with specified i2c interface
func NewMPU6050Driver(a I2cConnector, v ...time.Duration) *MPU6050Driver {
func NewMPU6050Driver(a I2cConnector, options ...func(I2cBusser)) *MPU6050Driver {
m := &MPU6050Driver{
name: gobot.DefaultName("MPM6050"),
name: gobot.DefaultName("MPU6050"),
connector: a,
I2cBusser: NewI2cBusser(),
interval: 10 * time.Millisecond,
Eventer: gobot.NewEventer(),
}

if len(v) > 0 {
m.interval = v[0]
for _, option := range options {
option(m)
}

// TODO: add commands to API

m.AddEvent(Error)
return m
}
Expand Down Expand Up @@ -100,7 +103,11 @@ func (h *MPU6050Driver) Start() (err error) {
func (h *MPU6050Driver) Halt() (err error) { return }

func (h *MPU6050Driver) initialize() (err error) {
bus := h.connector.I2cGetDefaultBus()
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()

h.connection, err = h.connector.I2cGetConnection(mpu6050Address, bus)
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions drivers/i2c/mpu6050_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package i2c

import (
"strings"
"testing"
"time"

Expand Down Expand Up @@ -37,8 +38,11 @@ func TestMPU6050Driver(t *testing.T) {
gobottest.Refute(t, mpu.Connection(), nil)
gobottest.Assert(t, mpu.interval, 10*time.Millisecond)

mpu = NewMPU6050Driver(newI2cTestAdaptor(), 100*time.Millisecond)
gobottest.Assert(t, mpu.interval, 100*time.Millisecond)
mpu = NewMPU6050Driver(newI2cTestAdaptor(), Bus(2))
gobottest.Assert(t, mpu.GetBus(), 2)

gobottest.Refute(t, mpu.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(mpu.Name(), "MPU6050"), true)
}

// Methods
Expand Down

0 comments on commit 9a93d3b

Please sign in to comment.