mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-04 22:17:39 +08:00
i2c: update mpu6050 for bus interface
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
5790cb502a
commit
9a93d3bbe4
@ -45,18 +45,21 @@ type MPU6050Driver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewMPU6050Driver creates a new driver with specified i2c interface
|
// 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{
|
m := &MPU6050Driver{
|
||||||
name: gobot.DefaultName("MPM6050"),
|
name: gobot.DefaultName("MPU6050"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
interval: 10 * time.Millisecond,
|
interval: 10 * time.Millisecond,
|
||||||
Eventer: gobot.NewEventer(),
|
Eventer: gobot.NewEventer(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v) > 0 {
|
for _, option := range options {
|
||||||
m.interval = v[0]
|
option(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add commands to API
|
||||||
|
|
||||||
m.AddEvent(Error)
|
m.AddEvent(Error)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
@ -100,7 +103,11 @@ func (h *MPU6050Driver) Start() (err error) {
|
|||||||
func (h *MPU6050Driver) Halt() (err error) { return }
|
func (h *MPU6050Driver) Halt() (err error) { return }
|
||||||
|
|
||||||
func (h *MPU6050Driver) initialize() (err error) {
|
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)
|
h.connection, err = h.connector.I2cGetConnection(mpu6050Address, bus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package i2c
|
package i2c
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -37,8 +38,11 @@ func TestMPU6050Driver(t *testing.T) {
|
|||||||
gobottest.Refute(t, mpu.Connection(), nil)
|
gobottest.Refute(t, mpu.Connection(), nil)
|
||||||
gobottest.Assert(t, mpu.interval, 10*time.Millisecond)
|
gobottest.Assert(t, mpu.interval, 10*time.Millisecond)
|
||||||
|
|
||||||
mpu = NewMPU6050Driver(newI2cTestAdaptor(), 100*time.Millisecond)
|
mpu = NewMPU6050Driver(newI2cTestAdaptor(), Bus(2))
|
||||||
gobottest.Assert(t, mpu.interval, 100*time.Millisecond)
|
gobottest.Assert(t, mpu.GetBus(), 2)
|
||||||
|
|
||||||
|
gobottest.Refute(t, mpu.Connection(), nil)
|
||||||
|
gobottest.Assert(t, strings.HasPrefix(mpu.Name(), "MPU6050"), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
|
Loading…
x
Reference in New Issue
Block a user