i2c: update mpu6050 for bus interface

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-02-09 13:26:53 +01:00
parent 5790cb502a
commit 9a93d3bbe4
2 changed files with 18 additions and 7 deletions

View File

@ -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

View File

@ -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