mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-26 13:48:49 +08:00
core: Refactor I2C drivers for new Driver creation signatures
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
93772e1c37
commit
80cc3139d0
@ -1,6 +1,6 @@
|
||||
# I2C
|
||||
|
||||
This package provides drivers for [i2c](https://en.wikipedia.org/wiki/I%C2%B2C)devices . It is normally not used directly, but instead is registered by an adaptor such as [firmata](https://github.com/hybridgroup/gobot/platforms/firmata) that supports the needed interfaces for i2c devices.
|
||||
This package provides drivers for [i2c](https://en.wikipedia.org/wiki/I%C2%B2C)devices. It must be used along with an adaptor such as [firmata](https://github.com/hybridgroup/gobot/platforms/firmata) that supports the needed interfaces for i2c devices.
|
||||
|
||||
## Getting Started
|
||||
|
||||
@ -13,8 +13,14 @@ go get -d -u github.com/hybridgroup/gobot/... && go install github.com/hybridgro
|
||||
Gobot has a extensible system for connecting to hardware devices. The following i2c devices are currently supported:
|
||||
|
||||
- BlinkM
|
||||
- HMC6352 Digital Compass
|
||||
- MPL115A2 Barometer/Temperature Sensor
|
||||
- Grove Digital Accelerometer
|
||||
- Grove RGB LCD
|
||||
- HMC6352 Compass
|
||||
- JHD1313M1 RGB LCD Display
|
||||
- LIDAR-Lite
|
||||
- MCP23017 Port Expander
|
||||
- MMA7660 3-Axis Accelerometer
|
||||
- MPL115A2 Barometer
|
||||
- MPU6050 Accelerometer/Gyroscope
|
||||
- Wii Nunchuck Controller
|
||||
|
||||
|
@ -16,16 +16,15 @@ type BlinkMDriver struct {
|
||||
gobot.Commander
|
||||
}
|
||||
|
||||
// NewBlinkMDriver creates a new BlinkMDriver with specified name.
|
||||
// NewBlinkMDriver creates a new BlinkMDriver.
|
||||
//
|
||||
// Adds the following API commands:
|
||||
// Rgb - sets RGB color
|
||||
// Fade - fades the RGB color
|
||||
// FirmwareVersion - returns the version of the current Frimware
|
||||
// Color - returns the color of the LED.
|
||||
func NewBlinkMDriver(a I2c, name string) *BlinkMDriver {
|
||||
func NewBlinkMDriver(a I2c) *BlinkMDriver {
|
||||
b := &BlinkMDriver{
|
||||
name: name,
|
||||
connection: a,
|
||||
Commander: gobot.NewCommander(),
|
||||
}
|
||||
@ -54,6 +53,7 @@ func NewBlinkMDriver(a I2c, name string) *BlinkMDriver {
|
||||
return b
|
||||
}
|
||||
func (b *BlinkMDriver) Name() string { return b.name }
|
||||
func (b *BlinkMDriver) SetName(n string) { b.name = n }
|
||||
func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }
|
||||
|
||||
// adaptor returns I2C adaptor
|
||||
|
@ -14,15 +14,15 @@ func initTestBlinkMDriver() (driver *BlinkMDriver) {
|
||||
}
|
||||
|
||||
func initTestBlinkDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
|
||||
adaptor := newI2cTestAdaptor("adaptor")
|
||||
return NewBlinkMDriver(adaptor, "bot"), adaptor
|
||||
adaptor := newI2cTestAdaptor()
|
||||
return NewBlinkMDriver(adaptor), adaptor
|
||||
}
|
||||
|
||||
// --------- TESTS
|
||||
|
||||
func TestNewBlinkMDriver(t *testing.T) {
|
||||
// Does it return a pointer to an instance of BlinkMDriver?
|
||||
var bm interface{} = NewBlinkMDriver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
var bm interface{} = NewBlinkMDriver(newI2cTestAdaptor())
|
||||
_, ok := bm.(*BlinkMDriver)
|
||||
if !ok {
|
||||
t.Errorf("NewBlinkMDriver() should have returned a *BlinkMDriver")
|
||||
@ -84,8 +84,7 @@ func TestNewBlinkMDriverCommands_Color(t *testing.T) {
|
||||
func TestBlinkMDriver(t *testing.T) {
|
||||
blinkM := initTestBlinkMDriver()
|
||||
|
||||
gobottest.Assert(t, blinkM.Name(), "bot")
|
||||
gobottest.Assert(t, blinkM.Connection().Name(), "adaptor")
|
||||
gobottest.Refute(t, blinkM.Connection(), nil)
|
||||
}
|
||||
|
||||
func TestBlinkMDriverStart(t *testing.T) {
|
||||
|
@ -13,10 +13,10 @@ type GroveLcdDriver struct {
|
||||
*JHD1313M1Driver
|
||||
}
|
||||
|
||||
// NewGroveLcdDriver creates a new driver with specified name and i2c interface.
|
||||
func NewGroveLcdDriver(a I2c, name string) *GroveLcdDriver {
|
||||
// NewGroveLcdDriver creates a new driver with specified i2c interface.
|
||||
func NewGroveLcdDriver(a I2c) *GroveLcdDriver {
|
||||
return &GroveLcdDriver{
|
||||
JHD1313M1Driver: NewJHD1313M1Driver(a, name),
|
||||
JHD1313M1Driver: NewJHD1313M1Driver(a),
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,9 +24,9 @@ type GroveAccelerometerDriver struct {
|
||||
*MMA7660Driver
|
||||
}
|
||||
|
||||
// NewGroveAccelerometerDriver creates a new driver with specified name and i2c interface
|
||||
func NewGroveAccelerometerDriver(a I2c, name string) *GroveAccelerometerDriver {
|
||||
// NewGroveAccelerometerDriver creates a new driver with specified i2c interface
|
||||
func NewGroveAccelerometerDriver(a I2c) *GroveAccelerometerDriver {
|
||||
return &GroveAccelerometerDriver{
|
||||
MMA7660Driver: NewMMA7660Driver(a, name),
|
||||
MMA7660Driver: NewMMA7660Driver(a),
|
||||
}
|
||||
}
|
||||
|
@ -31,12 +31,12 @@ func (t *i2cTestAdaptor) I2cWrite(int, []byte) (err error) {
|
||||
return t.i2cWriteImpl()
|
||||
}
|
||||
func (t *i2cTestAdaptor) Name() string { return t.name }
|
||||
func (t *i2cTestAdaptor) SetName(n string) { t.name = n }
|
||||
func (t *i2cTestAdaptor) Connect() (errs []error) { return }
|
||||
func (t *i2cTestAdaptor) Finalize() (errs []error) { return }
|
||||
|
||||
func newI2cTestAdaptor(name string) *i2cTestAdaptor {
|
||||
func newI2cTestAdaptor() *i2cTestAdaptor {
|
||||
return &i2cTestAdaptor{
|
||||
name: name,
|
||||
i2cReadImpl: func() ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
},
|
||||
|
@ -11,15 +11,15 @@ type HMC6352Driver struct {
|
||||
connection I2c
|
||||
}
|
||||
|
||||
// NewHMC6352Driver creates a new driver with specified name and i2c interface
|
||||
func NewHMC6352Driver(a I2c, name string) *HMC6352Driver {
|
||||
// NewHMC6352Driver creates a new driver with specified i2c interface
|
||||
func NewHMC6352Driver(a I2c) *HMC6352Driver {
|
||||
return &HMC6352Driver{
|
||||
name: name,
|
||||
connection: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HMC6352Driver) Name() string { return h.name }
|
||||
func (h *HMC6352Driver) SetName(n string) { h.name = n }
|
||||
func (h *HMC6352Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
||||
|
||||
// Start initialized the hmc6352
|
||||
|
@ -14,23 +14,22 @@ func initTestHMC6352Driver() (driver *HMC6352Driver) {
|
||||
}
|
||||
|
||||
func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor) {
|
||||
adaptor := newI2cTestAdaptor("adaptor")
|
||||
return NewHMC6352Driver(adaptor, "bot"), adaptor
|
||||
adaptor := newI2cTestAdaptor()
|
||||
return NewHMC6352Driver(adaptor), adaptor
|
||||
}
|
||||
|
||||
// --------- TESTS
|
||||
|
||||
func TestNewHMC6352Driver(t *testing.T) {
|
||||
// Does it return a pointer to an instance of HMC6352Driver?
|
||||
var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor())
|
||||
_, ok := bm.(*HMC6352Driver)
|
||||
if !ok {
|
||||
t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
|
||||
}
|
||||
|
||||
b := NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
gobottest.Assert(t, b.Name(), "bot")
|
||||
gobottest.Assert(t, b.Connection().Name(), "adaptor")
|
||||
b := NewHMC6352Driver(newI2cTestAdaptor())
|
||||
gobottest.Refute(t, b.Connection(), nil)
|
||||
}
|
||||
|
||||
// Methods
|
||||
|
@ -79,10 +79,9 @@ type JHD1313M1Driver struct {
|
||||
rgbAddress int
|
||||
}
|
||||
|
||||
// NewJHD1313M1Driver creates a new driver with specified name and i2c interface.
|
||||
func NewJHD1313M1Driver(a I2c, name string) *JHD1313M1Driver {
|
||||
// NewJHD1313M1Driver creates a new driver with specified i2c interface.
|
||||
func NewJHD1313M1Driver(a I2c) *JHD1313M1Driver {
|
||||
return &JHD1313M1Driver{
|
||||
name: name,
|
||||
connection: a,
|
||||
lcdAddress: 0x3E,
|
||||
rgbAddress: 0x62,
|
||||
@ -92,6 +91,9 @@ func NewJHD1313M1Driver(a I2c, name string) *JHD1313M1Driver {
|
||||
// Name returns the name the JHD1313M1 Driver was given when created.
|
||||
func (h *JHD1313M1Driver) Name() string { return h.name }
|
||||
|
||||
// SetName sets the name for the JHD1313M1 Driver.
|
||||
func (h *JHD1313M1Driver) SetName(n string) { h.name = n }
|
||||
|
||||
// Connection returns the driver connection to the device.
|
||||
func (h *JHD1313M1Driver) Connection() gobot.Connection {
|
||||
return h.connection.(gobot.Connection)
|
||||
@ -225,7 +227,7 @@ func (h *JHD1313M1Driver) command(buf []byte) error {
|
||||
return h.connection.I2cWrite(h.lcdAddress, append([]byte{LCD_CMD}, buf...))
|
||||
}
|
||||
|
||||
// CustomChar sets one of the 8 CGRAM locations with a custom character.
|
||||
// SetCustomChar sets one of the 8 CGRAM locations with a custom character.
|
||||
// The custom character can be used by writing a byte of value 0 to 7.
|
||||
// When you are using LCD as 5x8 dots in function set then you can define a total of 8 user defined patterns
|
||||
// (1 Byte for each row and 8 rows for each pattern).
|
||||
|
@ -15,15 +15,15 @@ type LIDARLiteDriver struct {
|
||||
connection I2c
|
||||
}
|
||||
|
||||
// NewLIDARLiteDriver creates a new driver with specified name and i2c interface
|
||||
func NewLIDARLiteDriver(a I2c, name string) *LIDARLiteDriver {
|
||||
// NewLIDARLiteDriver creates a new driver with specified i2c interface
|
||||
func NewLIDARLiteDriver(a I2c) *LIDARLiteDriver {
|
||||
return &LIDARLiteDriver{
|
||||
name: name,
|
||||
connection: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *LIDARLiteDriver) Name() string { return h.name }
|
||||
func (h *LIDARLiteDriver) SetName(n string) { h.name = n }
|
||||
func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
||||
|
||||
// Start initialized the LIDAR
|
||||
|
@ -14,23 +14,22 @@ func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
|
||||
}
|
||||
|
||||
func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
|
||||
adaptor := newI2cTestAdaptor("adaptor")
|
||||
return NewLIDARLiteDriver(adaptor, "bot"), adaptor
|
||||
adaptor := newI2cTestAdaptor()
|
||||
return NewLIDARLiteDriver(adaptor), adaptor
|
||||
}
|
||||
|
||||
// --------- TESTS
|
||||
|
||||
func TestNewLIDARLiteDriver(t *testing.T) {
|
||||
// Does it return a pointer to an instance of LIDARLiteDriver?
|
||||
var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor())
|
||||
_, ok := bm.(*LIDARLiteDriver)
|
||||
if !ok {
|
||||
t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
|
||||
}
|
||||
|
||||
b := NewLIDARLiteDriver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
gobottest.Assert(t, b.Name(), "bot")
|
||||
gobottest.Assert(t, b.Connection().Name(), "adaptor")
|
||||
b := NewLIDARLiteDriver(newI2cTestAdaptor())
|
||||
gobottest.Refute(t, b.Connection(), nil)
|
||||
}
|
||||
|
||||
// Methods
|
||||
|
@ -89,10 +89,9 @@ type MCP23017Driver struct {
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// NewMCP23017Driver creates a new driver with specified name and i2c interface.
|
||||
func NewMCP23017Driver(a I2c, name string, conf MCP23017Config, deviceAddress int, v ...time.Duration) *MCP23017Driver {
|
||||
// NewMCP23017Driver creates a new driver with specified i2c interface.
|
||||
func NewMCP23017Driver(a I2c, conf MCP23017Config, deviceAddress int, v ...time.Duration) *MCP23017Driver {
|
||||
m := &MCP23017Driver{
|
||||
name: name,
|
||||
connection: a,
|
||||
conf: conf,
|
||||
mcp23017Address: deviceAddress,
|
||||
@ -121,6 +120,9 @@ func NewMCP23017Driver(a I2c, name string, conf MCP23017Config, deviceAddress in
|
||||
// Name return the driver name.
|
||||
func (m *MCP23017Driver) Name() string { return m.name }
|
||||
|
||||
// SetName set the driver name.
|
||||
func (m *MCP23017Driver) SetName(n string) { m.name = n }
|
||||
|
||||
// Connection returns the I2c connection.
|
||||
func (m *MCP23017Driver) Connection() gobot.Connection { return m.connection.(gobot.Connection) }
|
||||
|
||||
|
@ -27,12 +27,12 @@ func (t *i2cMcpTestAdaptor) I2cWrite(int, []byte) (err error) {
|
||||
return t.i2cMcpWriteImpl()
|
||||
}
|
||||
func (t *i2cMcpTestAdaptor) Name() string { return t.name }
|
||||
func (t *i2cMcpTestAdaptor) SetName(n string) { t.name = n }
|
||||
func (t *i2cMcpTestAdaptor) Connect() (errs []error) { return }
|
||||
func (t *i2cMcpTestAdaptor) Finalize() (errs []error) { return }
|
||||
|
||||
func newMcpI2cTestAdaptor(name string) *i2cMcpTestAdaptor {
|
||||
func newMcpI2cTestAdaptor() *i2cMcpTestAdaptor {
|
||||
return &i2cMcpTestAdaptor{
|
||||
name: name,
|
||||
i2cMcpReadImpl: func(address int, numBytes int) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
},
|
||||
@ -62,20 +62,19 @@ func initTestMCP23017Driver(b uint8) (driver *MCP23017Driver) {
|
||||
}
|
||||
|
||||
func initTestMCP23017DriverWithStubbedAdaptor(b uint8) (*MCP23017Driver, *i2cMcpTestAdaptor) {
|
||||
adaptor := newMcpI2cTestAdaptor("adaptor")
|
||||
return NewMCP23017Driver(adaptor, "bot", MCP23017Config{Bank: b}, 0x20), adaptor
|
||||
adaptor := newMcpI2cTestAdaptor()
|
||||
return NewMCP23017Driver(adaptor, MCP23017Config{Bank: b}, 0x20), adaptor
|
||||
}
|
||||
|
||||
func TestNewMCP23017Driver(t *testing.T) {
|
||||
var bm interface{} = NewMCP23017Driver(newMcpI2cTestAdaptor("adaptor"), "bot", MCP23017Config{}, 0x20)
|
||||
var bm interface{} = NewMCP23017Driver(newMcpI2cTestAdaptor(), MCP23017Config{}, 0x20)
|
||||
_, ok := bm.(*MCP23017Driver)
|
||||
if !ok {
|
||||
t.Errorf("NewMCP23017Driver() should have returned a *MCP23017Driver")
|
||||
}
|
||||
|
||||
b := NewMCP23017Driver(newMcpI2cTestAdaptor("adaptor"), "bot", MCP23017Config{}, 0x20)
|
||||
gobottest.Assert(t, b.Name(), "bot")
|
||||
gobottest.Assert(t, b.Connection().Name(), "adaptor")
|
||||
b := NewMCP23017Driver(newMcpI2cTestAdaptor(), MCP23017Config{}, 0x20)
|
||||
gobottest.Refute(t, b.Connection(), nil)
|
||||
}
|
||||
|
||||
func TestMCP23017DriverStart(t *testing.T) {
|
||||
@ -188,26 +187,26 @@ func TestMCP23017DriverReadGPIO(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMCP23017DriverPinMode(t *testing.T) {
|
||||
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
|
||||
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
|
||||
return make([]byte, b), nil
|
||||
}
|
||||
adaptor.i2cMcpWriteImpl = func() error {
|
||||
return nil
|
||||
}
|
||||
err := mcp.PinMode(7, 0, "A")
|
||||
gobottest.Assert(t, err, nil)
|
||||
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
|
||||
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
|
||||
return make([]byte, b), nil
|
||||
}
|
||||
adaptor.i2cMcpWriteImpl = func() error {
|
||||
return nil
|
||||
}
|
||||
err := mcp.PinMode(7, 0, "A")
|
||||
gobottest.Assert(t, err, nil)
|
||||
|
||||
// write error
|
||||
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
|
||||
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
|
||||
return make([]byte, b), nil
|
||||
}
|
||||
adaptor.i2cMcpWriteImpl = func() error {
|
||||
return errors.New("write error")
|
||||
}
|
||||
err = mcp.PinMode(7, 0, "A")
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
// write error
|
||||
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
|
||||
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
|
||||
return make([]byte, b), nil
|
||||
}
|
||||
adaptor.i2cMcpWriteImpl = func() error {
|
||||
return errors.New("write error")
|
||||
}
|
||||
err = mcp.PinMode(7, 0, "A")
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
||||
func TestMCP23017DriverSetPullUp(t *testing.T) {
|
||||
|
@ -35,15 +35,15 @@ type MMA7660Driver struct {
|
||||
connection I2c
|
||||
}
|
||||
|
||||
// NewMMA7660Driver creates a new driver with specified name and i2c interface
|
||||
func NewMMA7660Driver(a I2c, name string) *MMA7660Driver {
|
||||
// NewMMA7660Driver creates a new driver with specified i2c interface
|
||||
func NewMMA7660Driver(a I2c) *MMA7660Driver {
|
||||
return &MMA7660Driver{
|
||||
name: name,
|
||||
connection: a,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MMA7660Driver) Name() string { return h.name }
|
||||
func (h *MMA7660Driver) SetName(n string) { h.name = n }
|
||||
func (h *MMA7660Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
||||
|
||||
// Start initialized the mma7660
|
||||
|
@ -39,10 +39,9 @@ type MPL115A2Driver struct {
|
||||
Temperature float32
|
||||
}
|
||||
|
||||
// NewMPL115A2Driver creates a new driver with specified name and i2c interface
|
||||
func NewMPL115A2Driver(a I2c, name string, v ...time.Duration) *MPL115A2Driver {
|
||||
// NewMPL115A2Driver creates a new driver with specified i2c interface
|
||||
func NewMPL115A2Driver(a I2c, v ...time.Duration) *MPL115A2Driver {
|
||||
m := &MPL115A2Driver{
|
||||
name: name,
|
||||
connection: a,
|
||||
Eventer: gobot.NewEventer(),
|
||||
interval: 10 * time.Millisecond,
|
||||
@ -56,6 +55,7 @@ func NewMPL115A2Driver(a I2c, name string, v ...time.Duration) *MPL115A2Driver {
|
||||
}
|
||||
|
||||
func (h *MPL115A2Driver) Name() string { return h.name }
|
||||
func (h *MPL115A2Driver) SetName(n string) { h.name = n }
|
||||
func (h *MPL115A2Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
|
@ -14,15 +14,15 @@ func initTestMPL115A2Driver() (driver *MPL115A2Driver) {
|
||||
}
|
||||
|
||||
func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdaptor) {
|
||||
adaptor := newI2cTestAdaptor("adaptor")
|
||||
return NewMPL115A2Driver(adaptor, "bot"), adaptor
|
||||
adaptor := newI2cTestAdaptor()
|
||||
return NewMPL115A2Driver(adaptor), adaptor
|
||||
}
|
||||
|
||||
// --------- TESTS
|
||||
|
||||
func TestNewMPL115A2Driver(t *testing.T) {
|
||||
// Does it return a pointer to an instance of MPL115A2Driver?
|
||||
var mpl interface{} = NewMPL115A2Driver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
var mpl interface{} = NewMPL115A2Driver(newI2cTestAdaptor())
|
||||
_, ok := mpl.(*MPL115A2Driver)
|
||||
if !ok {
|
||||
t.Errorf("NewMPL115A2Driver() should have returned a *MPL115A2Driver")
|
||||
@ -33,11 +33,10 @@ func TestNewMPL115A2Driver(t *testing.T) {
|
||||
func TestMPL115A2Driver(t *testing.T) {
|
||||
mpl := initTestMPL115A2Driver()
|
||||
|
||||
gobottest.Assert(t, mpl.Name(), "bot")
|
||||
gobottest.Assert(t, mpl.Connection().Name(), "adaptor")
|
||||
gobottest.Refute(t, mpl.Connection(), nil)
|
||||
gobottest.Assert(t, mpl.interval, 10*time.Millisecond)
|
||||
|
||||
mpl = NewMPL115A2Driver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond)
|
||||
mpl = NewMPL115A2Driver(newI2cTestAdaptor(), 100*time.Millisecond)
|
||||
gobottest.Assert(t, mpl.interval, 100*time.Millisecond)
|
||||
}
|
||||
|
||||
|
@ -44,10 +44,9 @@ type MPU6050Driver struct {
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// NewMPU6050Driver creates a new driver with specified name and i2c interface
|
||||
func NewMPU6050Driver(a I2c, name string, v ...time.Duration) *MPU6050Driver {
|
||||
// NewMPU6050Driver creates a new driver with specified i2c interface
|
||||
func NewMPU6050Driver(a I2c, v ...time.Duration) *MPU6050Driver {
|
||||
m := &MPU6050Driver{
|
||||
name: name,
|
||||
connection: a,
|
||||
interval: 10 * time.Millisecond,
|
||||
Eventer: gobot.NewEventer(),
|
||||
@ -62,6 +61,7 @@ func NewMPU6050Driver(a I2c, name string, v ...time.Duration) *MPU6050Driver {
|
||||
}
|
||||
|
||||
func (h *MPU6050Driver) Name() string { return h.name }
|
||||
func (h *MPU6050Driver) SetName(n string) { h.name = n }
|
||||
func (h *MPU6050Driver) Connection() gobot.Connection { return h.connection.(gobot.Connection) }
|
||||
|
||||
// Start writes initialization bytes and reads from adaptor
|
||||
|
@ -14,15 +14,15 @@ func initTestMPU6050Driver() (driver *MPU6050Driver) {
|
||||
}
|
||||
|
||||
func initTestMPU6050DriverWithStubbedAdaptor() (*MPU6050Driver, *i2cTestAdaptor) {
|
||||
adaptor := newI2cTestAdaptor("adaptor")
|
||||
return NewMPU6050Driver(adaptor, "bot"), adaptor
|
||||
adaptor := newI2cTestAdaptor()
|
||||
return NewMPU6050Driver(adaptor), adaptor
|
||||
}
|
||||
|
||||
// --------- TESTS
|
||||
|
||||
func TestNewMPU6050Driver(t *testing.T) {
|
||||
// Does it return a pointer to an instance of MPU6050Driver?
|
||||
var bm interface{} = NewMPU6050Driver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
var bm interface{} = NewMPU6050Driver(newI2cTestAdaptor())
|
||||
_, ok := bm.(*MPU6050Driver)
|
||||
if !ok {
|
||||
t.Errorf("NewMPU6050Driver() should have returned a *MPU6050Driver")
|
||||
@ -31,11 +31,10 @@ func TestNewMPU6050Driver(t *testing.T) {
|
||||
|
||||
func TestMPU6050Driver(t *testing.T) {
|
||||
mpu := initTestMPU6050Driver()
|
||||
gobottest.Assert(t, mpu.Name(), "bot")
|
||||
gobottest.Assert(t, mpu.Connection().Name(), "adaptor")
|
||||
gobottest.Refute(t, mpu.Connection(), nil)
|
||||
gobottest.Assert(t, mpu.interval, 10*time.Millisecond)
|
||||
|
||||
mpu = NewMPU6050Driver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond)
|
||||
mpu = NewMPU6050Driver(newI2cTestAdaptor(), 100*time.Millisecond)
|
||||
gobottest.Assert(t, mpu.interval, 100*time.Millisecond)
|
||||
}
|
||||
|
||||
|
@ -20,16 +20,15 @@ type WiichuckDriver struct {
|
||||
data map[string]float64
|
||||
}
|
||||
|
||||
// NewWiichuckDriver creates a WiichuckDriver with specified i2c interface and name.
|
||||
// NewWiichuckDriver creates a WiichuckDriver with specified i2c interface.
|
||||
//
|
||||
// It adds the following events:
|
||||
// "z"- Gets triggered every interval amount of time if the z button is pressed
|
||||
// "c" - Gets triggered every interval amount of time if the c button is pressed
|
||||
// "joystick" - Gets triggered every "interval" amount of time if a joystick event occurred, you can access values x, y
|
||||
// "error" - Gets triggered whenever the WiichuckDriver encounters an error
|
||||
func NewWiichuckDriver(a I2c, name string, v ...time.Duration) *WiichuckDriver {
|
||||
func NewWiichuckDriver(a I2c, v ...time.Duration) *WiichuckDriver {
|
||||
w := &WiichuckDriver{
|
||||
name: name,
|
||||
connection: a,
|
||||
interval: 10 * time.Millisecond,
|
||||
pauseTime: 1 * time.Millisecond,
|
||||
@ -57,6 +56,7 @@ func NewWiichuckDriver(a I2c, name string, v ...time.Duration) *WiichuckDriver {
|
||||
return w
|
||||
}
|
||||
func (w *WiichuckDriver) Name() string { return w.name }
|
||||
func (w *WiichuckDriver) SetName(n string) { w.name = n }
|
||||
func (w *WiichuckDriver) Connection() gobot.Connection { return w.connection.(gobot.Connection) }
|
||||
|
||||
// Start initilizes i2c and reads from adaptor
|
||||
|
@ -14,15 +14,15 @@ func initTestWiichuckDriver() (driver *WiichuckDriver) {
|
||||
}
|
||||
|
||||
func initTestWiichuckDriverWithStubbedAdaptor() (*WiichuckDriver, *i2cTestAdaptor) {
|
||||
adaptor := newI2cTestAdaptor("adaptor")
|
||||
return NewWiichuckDriver(adaptor, "bot"), adaptor
|
||||
adaptor := newI2cTestAdaptor()
|
||||
return NewWiichuckDriver(adaptor), adaptor
|
||||
}
|
||||
|
||||
// --------- TESTS
|
||||
|
||||
func TestNewWiichuckDriver(t *testing.T) {
|
||||
// Does it return a pointer to an instance of WiichuckDriver?
|
||||
var bm interface{} = NewWiichuckDriver(newI2cTestAdaptor("adaptor"), "bot")
|
||||
var bm interface{} = NewWiichuckDriver(newI2cTestAdaptor())
|
||||
_, ok := bm.(*WiichuckDriver)
|
||||
if !ok {
|
||||
t.Errorf("NewWiichuckDriver() should have returned a *WiichuckDriver")
|
||||
@ -31,11 +31,10 @@ func TestNewWiichuckDriver(t *testing.T) {
|
||||
|
||||
func TestWiichuckDriver(t *testing.T) {
|
||||
wii := initTestWiichuckDriver()
|
||||
gobottest.Assert(t, wii.Name(), "bot")
|
||||
gobottest.Assert(t, wii.Connection().Name(), "adaptor")
|
||||
gobottest.Refute(t, wii.Connection(), nil)
|
||||
gobottest.Assert(t, wii.interval, 10*time.Millisecond)
|
||||
|
||||
wii = NewWiichuckDriver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond)
|
||||
wii = NewWiichuckDriver(newI2cTestAdaptor(), 100*time.Millisecond)
|
||||
gobottest.Assert(t, wii.interval, 100*time.Millisecond)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user