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:
parent
802820700a
commit
1f2c7fd6c7
@ -6,10 +6,12 @@ import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*BlinkMDriver)(nil)
|
||||
var _ gobot.Driver = (*BlinkMDriver)(nil)
|
||||
|
||||
type BlinkMDriver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
gobot.Commander
|
||||
}
|
||||
|
||||
// NewBlinkMDriver creates a new BlinkMDriver with specified name.
|
||||
@ -21,11 +23,9 @@ type BlinkMDriver struct {
|
||||
// Color - returns the color of the LED.
|
||||
func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
||||
b := &BlinkMDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"BlinkMDriver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
Commander: gobot.NewCommander(),
|
||||
}
|
||||
|
||||
b.AddCommand("Rgb", func(params map[string]interface{}) interface{} {
|
||||
@ -51,10 +51,12 @@ func NewBlinkMDriver(a I2cInterface, name string) *BlinkMDriver {
|
||||
|
||||
return b
|
||||
}
|
||||
func (b *BlinkMDriver) Name() string { return b.name }
|
||||
func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection }
|
||||
|
||||
// adaptor returns I2C adaptor
|
||||
func (b *BlinkMDriver) adaptor() I2cInterface {
|
||||
return b.Adaptor().(I2cInterface)
|
||||
return b.Connection().(I2cInterface)
|
||||
}
|
||||
|
||||
// Start writes start bytes
|
||||
|
@ -1,8 +1,9 @@
|
||||
package i2c
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
// --------- HELPERS
|
||||
@ -20,7 +21,7 @@ func initTestBlinkDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
|
||||
|
||||
func TestBlinkMDriver(t *testing.T) {
|
||||
// Does it implement gobot.DriverInterface?
|
||||
var _ gobot.DriverInterface = (*BlinkMDriver)(nil)
|
||||
var _ gobot.Driver = (*BlinkMDriver)(nil)
|
||||
|
||||
// Does its adaptor implements the I2cInterface?
|
||||
driver := initTestBlinkMDriver()
|
||||
@ -40,14 +41,14 @@ func TestNewBlinkMDriver(t *testing.T) {
|
||||
func TestNewBlinkMDriverCommands_Rgb(t *testing.T) {
|
||||
blinkM := initTestBlinkMDriver()
|
||||
|
||||
result := blinkM.Driver.Command("Rgb")(rgb)
|
||||
result := blinkM.Command("Rgb")(rgb)
|
||||
gobot.Assert(t, result, nil)
|
||||
}
|
||||
|
||||
func TestNewBlinkMDriverCommands_Fade(t *testing.T) {
|
||||
blinkM := initTestBlinkMDriver()
|
||||
|
||||
result := blinkM.Driver.Command("Fade")(rgb)
|
||||
result := blinkM.Command("Fade")(rgb)
|
||||
gobot.Assert(t, result, nil)
|
||||
}
|
||||
|
||||
@ -61,7 +62,7 @@ func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
|
||||
return []byte{99, 1}
|
||||
}
|
||||
|
||||
result := blinkM.Driver.Command("FirmwareVersion")(param)
|
||||
result := blinkM.Command("FirmwareVersion")(param)
|
||||
|
||||
version, _ := blinkM.FirmwareVersion()
|
||||
gobot.Assert(t, result.(map[string]interface{})["version"].(string), version)
|
||||
@ -70,7 +71,7 @@ func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
|
||||
adaptor.i2cReadImpl = func() []byte {
|
||||
return []byte{99}
|
||||
}
|
||||
result = blinkM.Driver.Command("FirmwareVersion")(param)
|
||||
result = blinkM.Command("FirmwareVersion")(param)
|
||||
|
||||
version, _ = blinkM.FirmwareVersion()
|
||||
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{})
|
||||
|
||||
result := blinkM.Driver.Command("Color")(param)
|
||||
result := blinkM.Command("Color")(param)
|
||||
|
||||
color, _ := blinkM.Color()
|
||||
gobot.Assert(t, result.(map[string]interface{})["color"].([]byte), color)
|
||||
|
@ -6,26 +6,27 @@ import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*HMC6352Driver)(nil)
|
||||
var _ gobot.Driver = (*HMC6352Driver)(nil)
|
||||
|
||||
type HMC6352Driver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
}
|
||||
|
||||
// NewHMC6352Driver creates a new driver with specified name and i2c interface
|
||||
func NewHMC6352Driver(a I2cInterface, name string) *HMC6352Driver {
|
||||
return &HMC6352Driver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"HMC6352Driver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HMC6352Driver) Name() string { return h.name }
|
||||
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection }
|
||||
|
||||
// adaptor returns HMC6352 adaptor
|
||||
func (h *HMC6352Driver) adaptor() I2cInterface {
|
||||
return h.Adaptor().(I2cInterface)
|
||||
return h.Connection().(I2cInterface)
|
||||
}
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
|
@ -22,7 +22,7 @@ func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor)
|
||||
|
||||
func TestHMC6352Driver(t *testing.T) {
|
||||
// Does it implement gobot.DriverInterface?
|
||||
var _ gobot.DriverInterface = (*HMC6352Driver)(nil)
|
||||
var _ gobot.Driver = (*HMC6352Driver)(nil)
|
||||
|
||||
// Does its adaptor implements the I2cInterface?
|
||||
driver := initTestHMC6352Driver()
|
||||
@ -50,7 +50,6 @@ func TestHMC6352DriverStart(t *testing.T) {
|
||||
|
||||
numberOfCyclesForEvery := 3
|
||||
|
||||
hmc.SetInterval(1 * time.Millisecond)
|
||||
gobot.Assert(t, len(hmc.Start()), 0)
|
||||
go func() {
|
||||
for {
|
||||
@ -75,7 +74,6 @@ func TestHMC6352DriverStart(t *testing.T) {
|
||||
return []byte{99}
|
||||
}
|
||||
|
||||
hmc.SetInterval(1 * time.Millisecond)
|
||||
gobot.Assert(t, len(hmc.Start()), 0)
|
||||
go func() {
|
||||
for {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*MPL115A2Driver)(nil)
|
||||
var _ gobot.Driver = (*MPL115A2Driver)(nil)
|
||||
|
||||
const MPL115A2_REGISTER_PRESSURE_MSB = 0x00
|
||||
const MPL115A2_REGISTER_PRESSURE_LSB = 0x01
|
||||
@ -25,7 +25,10 @@ const MPL115A2_REGISTER_C12_COEFF_LSB = 0x0B
|
||||
const MPL115A2_REGISTER_STARTCONVERSION = 0x12
|
||||
|
||||
type MPL115A2Driver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
interval time.Duration
|
||||
gobot.Eventer
|
||||
A0 float32
|
||||
B1 float32
|
||||
B2 float32
|
||||
@ -37,19 +40,21 @@ type MPL115A2Driver struct {
|
||||
// NewMPL115A2Driver creates a new driver with specified name and i2c interface
|
||||
func NewMPL115A2Driver(a I2cInterface, name string) *MPL115A2Driver {
|
||||
m := &MPL115A2Driver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"MPL115A2Driver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
Eventer: gobot.NewEventer(),
|
||||
interval: 10 * time.Millisecond,
|
||||
}
|
||||
m.AddEvent("error")
|
||||
return m
|
||||
}
|
||||
|
||||
func (h *MPL115A2Driver) Name() string { return h.name }
|
||||
func (h *MPL115A2Driver) Connection() gobot.Connection { return h.connection }
|
||||
|
||||
// adaptor returns MPL115A2 adaptor
|
||||
func (h *MPL115A2Driver) adaptor() I2cInterface {
|
||||
return h.Adaptor().(I2cInterface)
|
||||
return h.Connection().(I2cInterface)
|
||||
}
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
@ -63,7 +68,7 @@ func (h *MPL115A2Driver) Start() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
gobot.Every(h.Interval(), func() {
|
||||
gobot.Every(h.interval, func() {
|
||||
if err := h.adaptor().I2cWrite([]byte{MPL115A2_REGISTER_STARTCONVERSION, 0}); err != nil {
|
||||
gobot.Publish(h.Event("error"), err)
|
||||
return
|
||||
|
@ -21,7 +21,7 @@ func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdapto
|
||||
|
||||
func TestMPL115A2DriverDriver(t *testing.T) {
|
||||
// Does it implement gobot.DriverInterface?
|
||||
var _ gobot.DriverInterface = (*MPL115A2Driver)(nil)
|
||||
var _ gobot.Driver = (*MPL115A2Driver)(nil)
|
||||
|
||||
// Does its adaptor implements the I2cInterface?
|
||||
driver := initTestMPL115A2Driver()
|
||||
|
@ -3,10 +3,12 @@ package i2c
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*MPU6050Driver)(nil)
|
||||
var _ gobot.Driver = (*MPU6050Driver)(nil)
|
||||
|
||||
const MPU6050_RA_ACCEL_XOUT_H = 0x3B
|
||||
const MPU6050_RA_PWR_MGMT_1 = 0x6B
|
||||
@ -30,7 +32,10 @@ type ThreeDData struct {
|
||||
}
|
||||
|
||||
type MPU6050Driver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
interval time.Duration
|
||||
gobot.Eventer
|
||||
Accelerometer ThreeDData
|
||||
Gyroscope ThreeDData
|
||||
Temperature int16
|
||||
@ -39,19 +44,21 @@ type MPU6050Driver struct {
|
||||
// NewMPU6050Driver creates a new driver with specified name and i2c interface
|
||||
func NewMPU6050Driver(a I2cInterface, name string) *MPU6050Driver {
|
||||
m := &MPU6050Driver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"MPU6050Driver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
interval: 10 * time.Millisecond,
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
m.AddEvent("error")
|
||||
return m
|
||||
}
|
||||
|
||||
func (h *MPU6050Driver) Name() string { return h.name }
|
||||
func (h *MPU6050Driver) Connection() gobot.Connection { return h.connection }
|
||||
|
||||
// adaptor returns MPU6050 adaptor
|
||||
func (h *MPU6050Driver) adaptor() I2cInterface {
|
||||
return h.Adaptor().(I2cInterface)
|
||||
return h.Connection().(I2cInterface)
|
||||
}
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
@ -61,7 +68,7 @@ func (h *MPU6050Driver) Start() (errs []error) {
|
||||
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 {
|
||||
gobot.Publish(h.Event("error"), err)
|
||||
return
|
||||
|
@ -21,7 +21,7 @@ func initTestMPU6050DriverWithStubbedAdaptor() (*MPU6050Driver, *i2cTestAdaptor)
|
||||
|
||||
func TestMPU6050Driver(t *testing.T) {
|
||||
// Does it implement gobot.DriverInterface?
|
||||
var _ gobot.DriverInterface = (*MPU6050Driver)(nil)
|
||||
var _ gobot.Driver = (*MPU6050Driver)(nil)
|
||||
|
||||
// Does its adaptor implements the I2cInterface?
|
||||
driver := initTestMPU6050Driver()
|
||||
|
@ -1,9 +1,5 @@
|
||||
package i2c
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var rgb = map[string]interface{}{
|
||||
"red": 1.0,
|
||||
"green": 1.0,
|
||||
@ -19,7 +15,7 @@ var green = castColor("green")
|
||||
var blue = castColor("blue")
|
||||
|
||||
type i2cTestAdaptor struct {
|
||||
gobot.Adaptor
|
||||
name string
|
||||
i2cReadImpl func() []byte
|
||||
}
|
||||
|
||||
@ -28,15 +24,13 @@ func (t *i2cTestAdaptor) I2cRead(uint) (data []byte, err error) {
|
||||
return t.i2cReadImpl(), 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) Finalize() (errs []error) { return }
|
||||
|
||||
func newI2cTestAdaptor(name string) *i2cTestAdaptor {
|
||||
return &i2cTestAdaptor{
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
name,
|
||||
"I2cTestAdaptor",
|
||||
),
|
||||
name: name,
|
||||
i2cReadImpl: func() []byte {
|
||||
return []byte{}
|
||||
},
|
||||
|
@ -2,14 +2,18 @@ package i2c
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*WiichuckDriver)(nil)
|
||||
var _ gobot.Driver = (*WiichuckDriver)(nil)
|
||||
|
||||
type WiichuckDriver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
interval time.Duration
|
||||
gobot.Eventer
|
||||
joystick 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
|
||||
func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
||||
w := &WiichuckDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"WiichuckDriver",
|
||||
a.(gobot.AdaptorInterface),
|
||||
),
|
||||
name: name,
|
||||
connection: a.(gobot.Connection),
|
||||
interval: 10 * time.Millisecond,
|
||||
Eventer: gobot.NewEventer(),
|
||||
joystick: map[string]float64{
|
||||
"sy_origin": -1,
|
||||
"sx_origin": -1,
|
||||
@ -44,10 +47,12 @@ func NewWiichuckDriver(a I2cInterface, name string) *WiichuckDriver {
|
||||
w.AddEvent("joystick")
|
||||
return w
|
||||
}
|
||||
func (w *WiichuckDriver) Name() string { return w.name }
|
||||
func (w *WiichuckDriver) Connection() gobot.Connection { return w.connection }
|
||||
|
||||
// adaptor returns i2c interface adaptor
|
||||
func (w *WiichuckDriver) adaptor() I2cInterface {
|
||||
return w.Adaptor().(I2cInterface)
|
||||
return w.Connection().(I2cInterface)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return []error{err}
|
||||
}
|
||||
gobot.Every(w.Interval(), func() {
|
||||
gobot.Every(w.interval, func() {
|
||||
if err := w.adaptor().I2cWrite([]byte{0x40, 0x00}); err != nil {
|
||||
gobot.Publish(w.Event("error"), err)
|
||||
return
|
||||
|
@ -21,7 +21,7 @@ func initTestWiichuckDriverWithStubbedAdaptor() (*WiichuckDriver, *i2cTestAdapto
|
||||
// --------- TESTS
|
||||
func TestWiichuckDriver(t *testing.T) {
|
||||
// Does it implement gobot.DriverInterface?
|
||||
var _ gobot.DriverInterface = (*WiichuckDriver)(nil)
|
||||
var _ gobot.Driver = (*WiichuckDriver)(nil)
|
||||
|
||||
// Does its adaptor implements the I2cInterface?
|
||||
driver := initTestWiichuckDriver()
|
||||
@ -47,7 +47,7 @@ func TestWiichuckDriverStart(t *testing.T) {
|
||||
|
||||
numberOfCyclesForEvery := 3
|
||||
|
||||
wii.SetInterval(1 * time.Millisecond)
|
||||
wii.interval = 1 * time.Millisecond
|
||||
gobot.Assert(t, len(wii.Start()), 0)
|
||||
|
||||
go func() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user