diff --git a/drivers/i2c/mpu6050_driver.go b/drivers/i2c/mpu6050_driver.go index 19045da1..7f903e6c 100644 --- a/drivers/i2c/mpu6050_driver.go +++ b/drivers/i2c/mpu6050_driver.go @@ -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 } @@ -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 diff --git a/drivers/i2c/mpu6050_driver_test.go b/drivers/i2c/mpu6050_driver_test.go index ed447715..290d4b09 100644 --- a/drivers/i2c/mpu6050_driver_test.go +++ b/drivers/i2c/mpu6050_driver_test.go @@ -1,6 +1,7 @@ package i2c import ( + "strings" "testing" "time" @@ -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