1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

Refactor i2c to use new driver interface

This commit is contained in:
Adrian Zankich 2014-11-22 19:38:39 -08:00
parent 802820700a
commit 1f2c7fd6c7
11 changed files with 79 additions and 66 deletions

View File

@ -6,10 +6,12 @@ import (
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
) )
var _ gobot.DriverInterface = (*BlinkMDriver)(nil) var _ gobot.Driver = (*BlinkMDriver)(nil)
type BlinkMDriver struct { type BlinkMDriver struct {
gobot.Driver name string
connection gobot.Connection
gobot.Commander
} }
// NewBlinkMDriver creates a new BlinkMDriver with specified name. // NewBlinkMDriver creates a new BlinkMDriver with specified name.
@ -21,11 +23,9 @@ type BlinkMDriver struct {
// Color - returns the color of the LED. // Color - returns the color of the LED.
func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver { func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
b := &BlinkMDriver{ b := &BlinkMDriver{
Driver: *gobot.NewDriver( name: name,
name, connection: a.(gobot.Connection),
"BlinkMDriver", Commander: gobot.NewCommander(),
a.(gobot.AdaptorInterface),
),
} }
b.AddCommand("Rgb", func(params map[string]interface{}) interface{} { b.AddCommand("Rgb", func(params map[string]interface{}) interface{} {
@ -51,10 +51,12 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
return b return b
} }
func (b *BlinkMDriver) Name() string { return b.name }
func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection }
// adaptor returns I2C adaptor // adaptor returns I2C adaptor
func (b *BlinkMDriver) adaptor() I2cInterface { func (b *BlinkMDriver) adaptor() I2cInterface {
return b.Adaptor().(I2cInterface) return b.Connection().(I2cInterface)
} }
// Start writes start bytes // Start writes start bytes

View File

@ -1,8 +1,9 @@
package i2c package i2c
import ( import (
"github.com/hybridgroup/gobot"
"testing" "testing"
"github.com/hybridgroup/gobot"
) )
// --------- HELPERS // --------- HELPERS
@ -20,7 +21,7 @@ func initTestBlinkDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
func TestBlinkMDriver(t *testing.T) { func TestBlinkMDriver(t *testing.T) {
// Does it implement gobot.DriverInterface? // Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*BlinkMDriver)(nil) var _ gobot.Driver = (*BlinkMDriver)(nil)
// Does its adaptor implements the I2cInterface? // Does its adaptor implements the I2cInterface?
driver := initTestBlinkMDriver() driver := initTestBlinkMDriver()
@ -40,14 +41,14 @@ func TestNewBlinkMDriver(t *testing.T) {
func TestNewBlinkMDriverCommands_Rgb(t *testing.T) { func TestNewBlinkMDriverCommands_Rgb(t *testing.T) {
blinkM := initTestBlinkMDriver() blinkM := initTestBlinkMDriver()
result := blinkM.Driver.Command("Rgb")(rgb) result := blinkM.Command("Rgb")(rgb)
gobot.Assert(t, result, nil) gobot.Assert(t, result, nil)
} }
func TestNewBlinkMDriverCommands_Fade(t *testing.T) { func TestNewBlinkMDriverCommands_Fade(t *testing.T) {
blinkM := initTestBlinkMDriver() blinkM := initTestBlinkMDriver()
result := blinkM.Driver.Command("Fade")(rgb) result := blinkM.Command("Fade")(rgb)
gobot.Assert(t, result, nil) gobot.Assert(t, result, nil)
} }
@ -61,7 +62,7 @@ func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
return []byte{99, 1} return []byte{99, 1}
} }
result := blinkM.Driver.Command("FirmwareVersion")(param) result := blinkM.Command("FirmwareVersion")(param)
version, _ := blinkM.FirmwareVersion() version, _ := blinkM.FirmwareVersion()
gobot.Assert(t, result.(map[string]interface{})["version"].(string), version) gobot.Assert(t, result.(map[string]interface{})["version"].(string), version)
@ -70,7 +71,7 @@ func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
adaptor.i2cReadImpl = func() []byte { adaptor.i2cReadImpl = func() []byte {
return []byte{99} return []byte{99}
} }
result = blinkM.Driver.Command("FirmwareVersion")(param) result = blinkM.Command("FirmwareVersion")(param)
version, _ = blinkM.FirmwareVersion() version, _ = blinkM.FirmwareVersion()
gobot.Assert(t, result.(map[string]interface{})["version"].(string), version) gobot.Assert(t, result.(map[string]interface{})["version"].(string), version)
@ -81,7 +82,7 @@ func TestNewBlinkMDriverCommands_Color(t *testing.T) {
param := make(map[string]interface{}) param := make(map[string]interface{})
result := blinkM.Driver.Command("Color")(param) result := blinkM.Command("Color")(param)
color, _ := blinkM.Color() color, _ := blinkM.Color()
gobot.Assert(t, result.(map[string]interface{})["color"].([]byte), color) gobot.Assert(t, result.(map[string]interface{})["color"].([]byte), color)

View File

@ -6,26 +6,27 @@ import (
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
) )
var _ gobot.DriverInterface = (*HMC6352Driver)(nil) var _ gobot.Driver = (*HMC6352Driver)(nil)
type HMC6352Driver struct { type HMC6352Driver struct {
gobot.Driver name string
connection gobot.Connection
} }
// NewHMC6352Driver creates a new driver with specified name and i2c interface // NewHMC6352Driver creates a new driver with specified name and i2c interface
func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver { func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
return &HMC6352Driver{ return &HMC6352Driver{
Driver: *gobot.NewDriver( name: name,
name, connection: a.(gobot.Connection),
"HMC6352Driver",
a.(gobot.AdaptorInterface),
),
} }
} }
func (h *HMC6352Driver) Name() string { return h.name }
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection }
// adaptor returns HMC6352 adaptor // adaptor returns HMC6352 adaptor
func (h *HMC6352Driver) adaptor() I2cInterface { func (h *HMC6352Driver) adaptor() I2cInterface {
return h.Adaptor().(I2cInterface) return h.Connection().(I2cInterface)
} }
// Start writes initialization bytes and reads from adaptor // Start writes initialization bytes and reads from adaptor

View File

@ -22,7 +22,7 @@ func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor)
func TestHMC6352Driver(t *testing.T) { func TestHMC6352Driver(t *testing.T) {
// Does it implement gobot.DriverInterface? // Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*HMC6352Driver)(nil) var _ gobot.Driver = (*HMC6352Driver)(nil)
// Does its adaptor implements the I2cInterface? // Does its adaptor implements the I2cInterface?
driver := initTestHMC6352Driver() driver := initTestHMC6352Driver()
@ -50,7 +50,6 @@ func TestHMC6352DriverStart(t *testing.T) {
numberOfCyclesForEvery := 3 numberOfCyclesForEvery := 3
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, len(hmc.Start()), 0) gobot.Assert(t, len(hmc.Start()), 0)
go func() { go func() {
for { for {
@ -75,7 +74,6 @@ func TestHMC6352DriverStart(t *testing.T) {
return []byte{99} return []byte{99}
} }
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, len(hmc.Start()), 0) gobot.Assert(t, len(hmc.Start()), 0)
go func() { go func() {
for { for {

View File

@ -8,7 +8,7 @@ import (
"time" "time"
) )
var _ gobot.DriverInterface = (*MPL115A2Driver)(nil) var _ gobot.Driver = (*MPL115A2Driver)(nil)
const MPL115A2_REGISTER_PRESSURE_MSB = 0x00 const MPL115A2_REGISTER_PRESSURE_MSB = 0x00
const MPL115A2_REGISTER_PRESSURE_LSB = 0x01 const MPL115A2_REGISTER_PRESSURE_LSB = 0x01
@ -25,7 +25,10 @@ const MPL115A2_REGISTER_C12_COEFF_LSB = 0x0B
const MPL115A2_REGISTER_STARTCONVERSION = 0x12 const MPL115A2_REGISTER_STARTCONVERSION = 0x12
type MPL115A2Driver struct { type MPL115A2Driver struct {
gobot.Driver name string
connection gobot.Connection
interval time.Duration
gobot.Eventer
A0 float32 A0 float32
B1 float32 B1 float32
B2 float32 B2 float32
@ -37,19 +40,21 @@ type MPL115A2Driver struct {
// NewMPL115A2Driver creates a new driver with specified name and i2c interface // NewMPL115A2Driver creates a new driver with specified name and i2c interface
func NewMPL115A2Driver(a I2cInterface, name string) *MPL115A2Driver { func NewMPL115A2Driver(a I2cInterface, name string) *MPL115A2Driver {
m := &MPL115A2Driver{ m := &MPL115A2Driver{
Driver: *gobot.NewDriver( name: name,
name, connection: a.(gobot.Connection),
"MPL115A2Driver", Eventer: gobot.NewEventer(),
a.(gobot.AdaptorInterface), interval: 10 * time.Millisecond,
),
} }
m.AddEvent("error") m.AddEvent("error")
return m return m
} }
func (h *MPL115A2Driver) Name() string { return h.name }
func (h *MPL115A2Driver) Connection() gobot.Connection { return h.connection }
// adaptor returns MPL115A2 adaptor // adaptor returns MPL115A2 adaptor
func (h *MPL115A2Driver) adaptor() I2cInterface { func (h *MPL115A2Driver) adaptor() I2cInterface {
return h.Adaptor().(I2cInterface) return h.Connection().(I2cInterface)
} }
// Start writes initialization bytes and reads from adaptor // Start writes initialization bytes and reads from adaptor
@ -63,7 +68,7 @@ func (h *MPL115A2Driver) Start() (errs []error) {
return return
} }
gobot.Every(h.Interval(), func() { gobot.Every(h.interval, func() {
if err := h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil { if err := h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil {
gobot.Publish(h.Event("error"), err) gobot.Publish(h.Event("error"), err)
return return

View File

@ -21,7 +21,7 @@ func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdapto
func TestMPL115A2DriverDriver(t *testing.T) { func TestMPL115A2DriverDriver(t *testing.T) {
// Does it implement gobot.DriverInterface? // Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*MPL115A2Driver)(nil) var _ gobot.Driver = (*MPL115A2Driver)(nil)
// Does its adaptor implements the I2cInterface? // Does its adaptor implements the I2cInterface?
driver := initTestMPL115A2Driver() driver := initTestMPL115A2Driver()

View File

@ -3,10 +3,12 @@ package i2c
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"time"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
) )
var _ gobot.DriverInterface = (*MPU6050Driver)(nil) var _ gobot.Driver = (*MPU6050Driver)(nil)
const MPU6050_RA_ACCEL_XOUT_H = 0x3B const MPU6050_RA_ACCEL_XOUT_H = 0x3B
const MPU6050_RA_PWR_MGMT_1 = 0x6B const MPU6050_RA_PWR_MGMT_1 = 0x6B
@ -30,7 +32,10 @@ type ThreeDData struct {
} }
type MPU6050Driver struct { type MPU6050Driver struct {
gobot.Driver name string
connection gobot.Connection
interval time.Duration
gobot.Eventer
Accelerometer ThreeDData Accelerometer ThreeDData
Gyroscope ThreeDData Gyroscope ThreeDData
Temperature int16 Temperature int16
@ -39,19 +44,21 @@ type MPU6050Driver struct {
// NewMPU6050Driver creates a new driver with specified name and i2c interface // NewMPU6050Driver creates a new driver with specified name and i2c interface
func NewMPU6050Driver(a I2cInterface, name string) *MPU6050Driver { func NewMPU6050Driver(a I2cInterface, name string) *MPU6050Driver {
m := &MPU6050Driver{ m := &MPU6050Driver{
Driver: *gobot.NewDriver( name: name,
name, connection: a.(gobot.Connection),
"MPU6050Driver", interval: 10 * time.Millisecond,
a.(gobot.AdaptorInterface), Eventer: gobot.NewEventer(),
),
} }
m.AddEvent("error") m.AddEvent("error")
return m return m
} }
func (h *MPU6050Driver) Name() string { return h.name }
func (h *MPU6050Driver) Connection() gobot.Connection { return h.connection }
// adaptor returns MPU6050 adaptor // adaptor returns MPU6050 adaptor
func (h *MPU6050Driver) adaptor() I2cInterface { func (h *MPU6050Driver) adaptor() I2cInterface {
return h.Adaptor().(I2cInterface) return h.Connection().(I2cInterface)
} }
// Start writes initialization bytes and reads from adaptor // Start writes initialization bytes and reads from adaptor
@ -61,7 +68,7 @@ func (h *MPU6050Driver) Start() (errs []error) {
return []error{err} return []error{err}
} }
gobot.Every(h.Interval(), func() { gobot.Every(h.interval, func() {
if err := h.adaptor().I2cWrite([]byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil { if err := h.adaptor().I2cWrite([]byte{MPU6050_RA_ACCEL_XOUT_H}); err != nil {
gobot.Publish(h.Event("error"), err) gobot.Publish(h.Event("error"), err)
return return

View File

@ -21,7 +21,7 @@ func initTestMPU6050DriverWithStubbedAdaptor() (*MPU6050Driver, *i2cTestAdaptor)
func TestMPU6050Driver(t *testing.T) { func TestMPU6050Driver(t *testing.T) {
// Does it implement gobot.DriverInterface? // Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*MPU6050Driver)(nil) var _ gobot.Driver = (*MPU6050Driver)(nil)
// Does its adaptor implements the I2cInterface? // Does its adaptor implements the I2cInterface?
driver := initTestMPU6050Driver() driver := initTestMPU6050Driver()

View File

@ -1,9 +1,5 @@
package i2c package i2c
import (
"github.com/hybridgroup/gobot"
)
var rgb = map[string]interface{}{ var rgb = map[string]interface{}{
"red": 1.0, "red": 1.0,
"green": 1.0, "green": 1.0,
@ -19,7 +15,7 @@ var green = castColor("green")
var blue = castColor("blue") var blue = castColor("blue")
type i2cTestAdaptor struct { type i2cTestAdaptor struct {
gobot.Adaptor name string
i2cReadImpl func() []byte i2cReadImpl func() []byte
} }
@ -28,15 +24,13 @@ func (t *i2cTestAdaptor) I2cRead(uint) (data []byte, err error) {
return t.i2cReadImpl(), nil return t.i2cReadImpl(), nil
} }
func (t *i2cTestAdaptor) I2cWrite([]byte) (err error) { return nil } func (t *i2cTestAdaptor) I2cWrite([]byte) (err error) { return nil }
func (t *i2cTestAdaptor) Name() string { return t.name }
func (t *i2cTestAdaptor) Connect() (errs []error) { return } func (t *i2cTestAdaptor) Connect() (errs []error) { return }
func (t *i2cTestAdaptor) Finalize() (errs []error) { return } func (t *i2cTestAdaptor) Finalize() (errs []error) { return }
func newI2cTestAdaptor(name string) *i2cTestAdaptor { func newI2cTestAdaptor(name string) *i2cTestAdaptor {
return &i2cTestAdaptor{ return &i2cTestAdaptor{
Adaptor: *gobot.NewAdaptor( name: name,
name,
"I2cTestAdaptor",
),
i2cReadImpl: func() []byte { i2cReadImpl: func() []byte {
return []byte{} return []byte{}
}, },

View File

@ -2,14 +2,18 @@ package i2c
import ( import (
"errors" "errors"
"time"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
) )
var _ gobot.DriverInterface = (*WiichuckDriver)(nil) var _ gobot.Driver = (*WiichuckDriver)(nil)
type WiichuckDriver struct { type WiichuckDriver struct {
gobot.Driver name string
connection gobot.Connection
interval time.Duration
gobot.Eventer
joystick map[string]float64 joystick map[string]float64
data map[string]float64 data map[string]float64
} }
@ -22,11 +26,10 @@ type WiichuckDriver struct {
// "joystick" - Get's triggered every "interval" amount of time if a joystick event occured, you can access values x, y // "joystick" - Get's triggered every "interval" amount of time if a joystick event occured, you can access values x, y
func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver { func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
w := &WiichuckDriver{ w := &WiichuckDriver{
Driver: *gobot.NewDriver( name: name,
name, connection: a.(gobot.Connection),
"WiichuckDriver", interval: 10 * time.Millisecond,
a.(gobot.AdaptorInterface), Eventer: gobot.NewEventer(),
),
joystick: map[string]float64{ joystick: map[string]float64{
"sy_origin": -1, "sy_origin": -1,
"sx_origin": -1, "sx_origin": -1,
@ -44,10 +47,12 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
w.AddEvent("joystick") w.AddEvent("joystick")
return w return w
} }
func (w *WiichuckDriver) Name() string { return w.name }
func (w *WiichuckDriver) Connection() gobot.Connection { return w.connection }
// adaptor returns i2c interface adaptor // adaptor returns i2c interface adaptor
func (w *WiichuckDriver) adaptor() I2cInterface { func (w *WiichuckDriver) adaptor() I2cInterface {
return w.Adaptor().(I2cInterface) return w.Connection().(I2cInterface)
} }
// Start initilizes i2c and reads from adaptor // Start initilizes i2c and reads from adaptor
@ -56,7 +61,7 @@ func (w *WiichuckDriver) Start() (errs []error) {
if err := w.adaptor().I2cStart(0x52); err != nil { if err := w.adaptor().I2cStart(0x52); err != nil {
return []error{err} return []error{err}
} }
gobot.Every(w.Interval(), func() { gobot.Every(w.interval, func() {
if err := w.adaptor().I2cWrite([]byte{0x40, 0x00}); err != nil { if err := w.adaptor().I2cWrite([]byte{0x40, 0x00}); err != nil {
gobot.Publish(w.Event("error"), err) gobot.Publish(w.Event("error"), err)
return return

View File

@ -21,7 +21,7 @@ func initTestWiichuckDriverWithStubbedAdaptor() (*WiichuckDriver, *i2cTestAdapto
// --------- TESTS // --------- TESTS
func TestWiichuckDriver(t *testing.T) { func TestWiichuckDriver(t *testing.T) {
// Does it implement gobot.DriverInterface? // Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*WiichuckDriver)(nil) var _ gobot.Driver = (*WiichuckDriver)(nil)
// Does its adaptor implements the I2cInterface? // Does its adaptor implements the I2cInterface?
driver := initTestWiichuckDriver() driver := initTestWiichuckDriver()
@ -47,7 +47,7 @@ func TestWiichuckDriverStart(t *testing.T) {
numberOfCyclesForEvery := 3 numberOfCyclesForEvery := 3
wii.SetInterval(1 * time.Millisecond) wii.interval = 1 * time.Millisecond
gobot.Assert(t, len(wii.Start()), 0) gobot.Assert(t, len(wii.Start()), 0)
go func() { go func() {