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

i2c: complete adding optional param with alternative i2c bus for all i2c drivers

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-02-09 11:23:36 +01:00
parent f8f92af056
commit 90bd2d594f
19 changed files with 283 additions and 72 deletions

View File

@ -98,6 +98,7 @@ type AdafruitMotorHatDriver struct {
connector I2cConnector
motorHatConnection I2cConnection
servoHatConnection I2cConnection
I2cBusser
gobot.Commander
dcMotors []adaFruitDCMotor
stepperMotors []adaFruitStepperMotor
@ -128,7 +129,7 @@ func (a *AdafruitMotorHatDriver) Connection() gobot.Connection { return a.connec
// NewAdafruitMotorHatDriver initializes the internal DCMotor and StepperMotor types.
// Again the Adafruit Motor Hat supports up to four DC motors and up to two stepper motors.
func NewAdafruitMotorHatDriver(conn I2cConnector) *AdafruitMotorHatDriver {
func NewAdafruitMotorHatDriver(conn I2cConnector, options ...func(I2cBusser)) *AdafruitMotorHatDriver {
var dc []adaFruitDCMotor
var st []adaFruitStepperMotor
for i := 0; i < 4; i++ {
@ -151,10 +152,16 @@ func NewAdafruitMotorHatDriver(conn I2cConnector) *AdafruitMotorHatDriver {
driver := &AdafruitMotorHatDriver{
name: gobot.DefaultName("AdafruitMotorHat"),
connector: conn,
I2cBusser: NewI2cBusser(),
Commander: gobot.NewCommander(),
dcMotors: dc,
stepperMotors: st,
}
for _, option := range options {
option(driver)
}
// TODO: add API funcs?
return driver
}
@ -195,7 +202,11 @@ func (a *AdafruitMotorHatDriver) startDriver(connection I2cConnection) (err erro
// Start initializes both I2C-addressable Adafruit Motor HAT drivers
func (a *AdafruitMotorHatDriver) Start() (err error) {
bus := a.connector.I2cGetDefaultBus()
if a.GetBus() == BusNotInitialized {
a.Bus(a.connector.I2cGetDefaultBus())
}
bus := a.GetBus()
if a.servoHatConnection, err = a.connector.I2cGetConnection(servoHatAddress, bus); err != nil {
return

View File

@ -74,8 +74,9 @@ func (b *BlinkMDriver) Start() (err error) {
if b.GetBus() == BusNotInitialized {
b.Bus(b.connector.I2cGetDefaultBus())
}
bus := b.GetBus()
b.connection, err = b.connector.I2cGetConnection(blinkmAddress, b.GetBus())
b.connection, err = b.connector.I2cGetConnection(blinkmAddress, bus)
if err != nil {
return
}

View File

@ -21,9 +21,10 @@ const bmp180RegisterPressureMSB = 0xF6
// BMP180Driver is the gobot driver for the Bosch pressure sensor BMP180.
// Device datasheet: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
type BMP180Driver struct {
name string
connector I2cConnector
connection I2cConnection
name string
connector I2cConnector
connection I2cConnection
I2cBusser
calibrationCoefficients *calibrationCoefficients
}
@ -56,12 +57,20 @@ type calibrationCoefficients struct {
}
// NewBMP180Driver creates a new driver with the i2c interface for the BMP180 device.
func NewBMP180Driver(c I2cConnector) *BMP180Driver {
return &BMP180Driver{
func NewBMP180Driver(c I2cConnector, options ...func(I2cBusser)) *BMP180Driver {
b := &BMP180Driver{
name: gobot.DefaultName("BMP180"),
connector: c,
I2cBusser: NewI2cBusser(),
calibrationCoefficients: &calibrationCoefficients{},
}
for _, option := range options {
option(b)
}
// TODO: expose commands to API
return b
}
// Name returns the name of the device.
@ -81,7 +90,11 @@ func (d *BMP180Driver) Connection() gobot.Connection {
// Start initializes the BMP180 and loads the calibration coefficients.
func (d *BMP180Driver) Start() (err error) {
bus := d.connector.I2cGetDefaultBus()
if d.GetBus() == BusNotInitialized {
d.Bus(d.connector.I2cGetDefaultBus())
}
bus := d.GetBus()
if d.connection, err = d.connector.I2cGetConnection(bmp180Address, bus); err != nil {
return err
}

View File

@ -9,10 +9,16 @@ type GroveLcdDriver struct {
}
// NewGroveLcdDriver creates a new driver with specified i2c interface.
func NewGroveLcdDriver(a I2cConnector) *GroveLcdDriver {
return &GroveLcdDriver{
func NewGroveLcdDriver(a I2cConnector, options ...func(I2cBusser)) *GroveLcdDriver {
lcd := &GroveLcdDriver{
JHD1313M1Driver: NewJHD1313M1Driver(a),
}
for _, option := range options {
option(lcd)
}
return lcd
}
type GroveAccelerometerDriver struct {
@ -20,8 +26,14 @@ type GroveAccelerometerDriver struct {
}
// NewGroveAccelerometerDriver creates a new driver with specified i2c interface
func NewGroveAccelerometerDriver(a I2cConnector) *GroveAccelerometerDriver {
return &GroveAccelerometerDriver{
func NewGroveAccelerometerDriver(a I2cConnector, options ...func(I2cBusser)) *GroveAccelerometerDriver {
mma := &GroveAccelerometerDriver{
MMA7660Driver: NewMMA7660Driver(a),
}
for _, option := range options {
option(mma)
}
return mma
}

View File

@ -9,14 +9,22 @@ type HMC6352Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
}
// NewHMC6352Driver creates a new driver with specified i2c interface
func NewHMC6352Driver(a I2cConnector) *HMC6352Driver {
return &HMC6352Driver{
func NewHMC6352Driver(a I2cConnector, options ...func(I2cBusser)) *HMC6352Driver {
hmc := &HMC6352Driver{
name: gobot.DefaultName("HMC6352"),
connector: a,
I2cBusser: NewI2cBusser(),
}
for _, option := range options {
option(hmc)
}
return hmc
}
// Name returns the name for this Driver
@ -30,7 +38,11 @@ func (h *HMC6352Driver) Connection() gobot.Connection { return h.connector.(gobo
// Start initializes the hmc6352
func (h *HMC6352Driver) Start() (err error) {
bus := h.connector.I2cGetDefaultBus()
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
h.connection, err = h.connector.I2cGetConnection(hmc6352Address, bus)
if err != nil {
return err

View File

@ -41,8 +41,6 @@ const (
LCD_2NDLINEOFFSET = 0x40
)
var _ gobot.Driver = (*JHD1313M1Driver)(nil)
// CustomLCDChars is a map of CGRAM characters that can be loaded
// into a LCD screen to display custom characters. Some LCD screens such
// as the Grove screen (jhd1313m1) isn't loaded with latin 1 characters.
@ -73,8 +71,9 @@ var CustomLCDChars = map[string][8]byte{
// This module was tested with the Seed Grove LCD RGB Backlight v2.0 display which requires 5V to operate.
// http://www.seeedstudio.com/wiki/Grove_-_LCD_RGB_Backlight
type JHD1313M1Driver struct {
name string
connector I2cConnector
name string
connector I2cConnector
I2cBusser
lcdAddress int
lcdConnection I2cConnection
rgbAddress int
@ -82,13 +81,20 @@ type JHD1313M1Driver struct {
}
// NewJHD1313M1Driver creates a new driver with specified i2c interface.
func NewJHD1313M1Driver(a I2cConnector) *JHD1313M1Driver {
return &JHD1313M1Driver{
func NewJHD1313M1Driver(a I2cConnector, options ...func(I2cBusser)) *JHD1313M1Driver {
j := &JHD1313M1Driver{
name: gobot.DefaultName("JHD1313M1"),
connector: a,
I2cBusser: NewI2cBusser(),
lcdAddress: 0x3E,
rgbAddress: 0x62,
}
for _, option := range options {
option(j)
}
return j
}
// Name returns the name the JHD1313M1 Driver was given when created.
@ -104,7 +110,11 @@ func (h *JHD1313M1Driver) Connection() gobot.Connection {
// Start starts the backlit and the screen and initializes the states.
func (h *JHD1313M1Driver) Start() (err error) {
bus := h.connector.I2cGetDefaultBus()
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
if h.lcdConnection, err = h.connector.I2cGetConnection(h.lcdAddress, bus); err != nil {
return err
}

View File

@ -0,0 +1,41 @@
package i2c
import (
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*JHD1313M1Driver)(nil)
// --------- HELPERS
func initTestJHD1313M1Driver() (driver *JHD1313M1Driver) {
driver, _ = initTestJHD1313M1DriverWithStubbedAdaptor()
return
}
func initTestJHD1313M1DriverWithStubbedAdaptor() (*JHD1313M1Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewJHD1313M1Driver(adaptor), adaptor
}
// --------- TESTS
func TestNewJHD1313M1Driver(t *testing.T) {
// Does it return a pointer to an instance of JHD1313M1Driver?
var mpl interface{} = NewJHD1313M1Driver(newI2cTestAdaptor())
_, ok := mpl.(*JHD1313M1Driver)
if !ok {
t.Errorf("NewJHD1313M1Driver() should have returned a *JHD1313M1Driver")
}
}
// Methods
func TestJHD1313M1Driver(t *testing.T) {
jhd := initTestJHD1313M1Driver()
gobottest.Refute(t, jhd.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(jhd.Name(), "JHD1313M1"), true)
}

View File

@ -27,7 +27,8 @@ type L3GD20HDriver struct {
name string
connector I2cConnector
connection I2cConnection
scale L3GD20HScale
I2cBusser
scale L3GD20HScale
}
// L3GD20HScale is the scale sensitivity of degrees-per-second.
@ -43,12 +44,20 @@ const (
)
// NewL3GD20HDriver creates a new driver with the i2c interface for the L3GD20H device.
func NewL3GD20HDriver(c I2cConnector) *L3GD20HDriver {
return &L3GD20HDriver{
func NewL3GD20HDriver(c I2cConnector, options ...func(I2cBusser)) *L3GD20HDriver {
l := &L3GD20HDriver{
name: gobot.DefaultName("L3GD20H"),
connector: c,
I2cBusser: NewI2cBusser(),
scale: L3GD20HScale250dps,
}
for _, option := range options {
option(l)
}
// TODO: add commands to API
return l
}
// Name returns the name of the device.
@ -85,7 +94,11 @@ func (d *L3GD20HDriver) Start() (err error) {
}
func (d *L3GD20HDriver) initialization() (err error) {
bus := d.connector.I2cGetDefaultBus()
if d.GetBus() == BusNotInitialized {
d.Bus(d.connector.I2cGetDefaultBus())
}
bus := d.GetBus()
d.connection, err = d.connector.I2cGetConnection(l3gd20hAddress, bus)
if err != nil {
return err

View File

@ -12,14 +12,23 @@ type LIDARLiteDriver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
}
// NewLIDARLiteDriver creates a new driver with specified i2c interface
func NewLIDARLiteDriver(a I2cConnector) *LIDARLiteDriver {
return &LIDARLiteDriver{
func NewLIDARLiteDriver(a I2cConnector, options ...func(I2cBusser)) *LIDARLiteDriver {
l := &LIDARLiteDriver{
name: gobot.DefaultName("LIDARLite"),
connector: a,
I2cBusser: NewI2cBusser(),
}
for _, option := range options {
option(l)
}
// TODO: add commands to API
return l
}
func (h *LIDARLiteDriver) Name() string { return h.name }
@ -28,7 +37,11 @@ func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connector.(go
// Start initialized the LIDAR
func (h *LIDARLiteDriver) Start() (err error) {
bus := h.connector.I2cGetDefaultBus()
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
h.connection, err = h.connector.I2cGetConnection(lidarliteAddress, bus)
if err != nil {
return err

View File

@ -20,16 +20,11 @@ import (
"fmt"
"log"
"strings"
"time"
"gobot.io/x/gobot"
)
var (
debug = false // Set this to true to see debugging information
// Register this Driver
_ gobot.Driver = (*MCP23017Driver)(nil)
)
var debug = false // Set this to true to see debugging information
// Port contains all the registers for the device.
type port struct {
@ -80,27 +75,32 @@ func (mc *MCP23017Config) GetUint8Value() uint8 {
// MCP23017Driver contains the driver configuration parameters.
type MCP23017Driver struct {
name string
connector I2cConnector
connection I2cConnection
name string
connector I2cConnector
connection I2cConnection
I2cBusser
conf MCP23017Config
mcp23017Address int
interval time.Duration
gobot.Commander
gobot.Eventer
}
// NewMCP23017Driver creates a new driver with specified i2c interface.
func NewMCP23017Driver(a I2cConnector, conf MCP23017Config, deviceAddress int, v ...time.Duration) *MCP23017Driver {
func NewMCP23017Driver(a I2cConnector, conf MCP23017Config, deviceAddress int, options ...func(I2cBusser)) *MCP23017Driver {
m := &MCP23017Driver{
name: gobot.DefaultName("MCP23017"),
connector: a,
I2cBusser: NewI2cBusser(),
conf: conf,
mcp23017Address: deviceAddress,
Commander: gobot.NewCommander(),
Eventer: gobot.NewEventer(),
}
for _, option := range options {
option(m)
}
m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} {
pin := params["pin"].(uint8)
val := params["val"].(uint8)
@ -133,7 +133,11 @@ func (m *MCP23017Driver) Halt() (err error) { return }
// Start writes the device configuration.
func (m *MCP23017Driver) Start() (err error) {
bus := m.connector.I2cGetDefaultBus()
if m.GetBus() == BusNotInitialized {
m.Bus(m.connector.I2cGetDefaultBus())
}
bus := m.GetBus()
m.connection, err = m.connector.I2cGetConnection(m.mcp23017Address, bus)
if err != nil {
return err

View File

@ -7,9 +7,12 @@ import (
"os"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*MCP23017Driver)(nil)
var pinValPort = map[string]interface{}{
"pin": uint8(7),
"val": uint8(0),

View File

@ -2,8 +2,6 @@ package i2c
import "gobot.io/x/gobot"
var _ gobot.Driver = (*MMA7660Driver)(nil)
const mma7660Address = 0x4c
const (
@ -34,14 +32,23 @@ type MMA7660Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
}
// NewMMA7660Driver creates a new driver with specified i2c interface
func NewMMA7660Driver(a I2cConnector) *MMA7660Driver {
return &MMA7660Driver{
func NewMMA7660Driver(a I2cConnector, options ...func(I2cBusser)) *MMA7660Driver {
m := &MMA7660Driver{
name: gobot.DefaultName("MMA7660"),
connector: a,
I2cBusser: NewI2cBusser(),
}
for _, option := range options {
option(m)
}
// TODO: add commands for API
return m
}
func (h *MMA7660Driver) Name() string { return h.name }
@ -50,7 +57,10 @@ func (h *MMA7660Driver) Connection() gobot.Connection { return h.connector.(gobo
// Start initialized the mma7660
func (h *MMA7660Driver) Start() (err error) {
bus := h.connector.I2cGetDefaultBus()
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
h.connection, err = h.connector.I2cGetConnection(mma7660Address, bus)
if err != nil {

View File

@ -0,0 +1,41 @@
package i2c
import (
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*MMA7660Driver)(nil)
// --------- HELPERS
func initTestMMA7660Driver() (driver *MMA7660Driver) {
driver, _ = initTestMMA7660DriverWithStubbedAdaptor()
return
}
func initTestMMA7660DriverWithStubbedAdaptor() (*MMA7660Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewMMA7660Driver(adaptor), adaptor
}
// --------- TESTS
func TestNewMMA7660Driver(t *testing.T) {
// Does it return a pointer to an instance of MMA7660Driver?
var mma interface{} = NewMMA7660Driver(newI2cTestAdaptor())
_, ok := mma.(*MMA7660Driver)
if !ok {
t.Errorf("NewMMA7660Driver() should have returned a *MMA7660Driver")
}
}
// Methods
func TestMMA7660Driver(t *testing.T) {
mma := initTestMMA7660Driver()
gobottest.Refute(t, mma.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(mma.Name(), "MMA7660"), true)
}

View File

@ -28,7 +28,8 @@ type MPL115A2Driver struct {
name string
connector I2cConnector
connection I2cConnection
interval time.Duration
I2cBusser
interval time.Duration
gobot.Eventer
A0 float32
B1 float32
@ -39,18 +40,22 @@ type MPL115A2Driver struct {
}
// NewMPL115A2Driver creates a new driver with specified i2c interface
func NewMPL115A2Driver(a I2cConnector, v ...time.Duration) *MPL115A2Driver {
func NewMPL115A2Driver(a I2cConnector, options ...func(I2cBusser)) *MPL115A2Driver {
m := &MPL115A2Driver{
name: gobot.DefaultName("MPL115A2"),
connector: a,
I2cBusser: NewI2cBusser(),
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
}
if len(v) > 0 {
m.interval = v[0]
for _, option := range options {
option(m)
}
// TODO: add commands to API
m.AddEvent(Error)
return m
}
@ -116,7 +121,11 @@ func (h *MPL115A2Driver) initialization() (err error) {
var coB2 int16
var coC12 int16
bus := h.connector.I2cGetDefaultBus()
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
h.connection, err = h.connector.I2cGetConnection(mpl115a2Address, bus)
if err != nil {
return err

View File

@ -1,6 +1,7 @@
package i2c
import (
"strings"
"testing"
"time"
@ -37,10 +38,7 @@ func TestMPL115A2Driver(t *testing.T) {
mpl := initTestMPL115A2Driver()
gobottest.Refute(t, mpl.Connection(), nil)
gobottest.Assert(t, mpl.interval, 10*time.Millisecond)
mpl = NewMPL115A2Driver(newI2cTestAdaptor(), 100*time.Millisecond)
gobottest.Assert(t, mpl.interval, 100*time.Millisecond)
gobottest.Assert(t, strings.HasPrefix(mpl.Name(), "MPL115A2"), true)
}
func TestMPL115A2DriverStart(t *testing.T) {

View File

@ -33,9 +33,10 @@ type ThreeDData struct {
}
type MPU6050Driver struct {
name string
connector I2cConnector
connection I2cConnection
name string
connector I2cConnector
connection I2cConnection
I2cBusser
interval time.Duration
Accelerometer ThreeDData
Gyroscope ThreeDData

View File

@ -55,9 +55,10 @@ var (
type SHT3xDriver struct {
Units string
name string
connector I2cConnector
connection I2cConnection
name string
connector I2cConnector
connection I2cConnection
I2cBusser
sht3xAddress int
accuracy byte
delay time.Duration
@ -65,15 +66,21 @@ type SHT3xDriver struct {
}
// NewSHT3xDriver creates a new driver with specified i2c interface
func NewSHT3xDriver(a I2cConnector) *SHT3xDriver {
func NewSHT3xDriver(a I2cConnector, options ...func(I2cBusser)) *SHT3xDriver {
s := &SHT3xDriver{
Units: "C",
name: gobot.DefaultName("SHT3x"),
connector: a,
I2cBusser: NewI2cBusser(),
sht3xAddress: SHT3xAddressA,
crcTable: crc8.MakeTable(crc8Params),
}
s.SetAccuracy(SHT3xAccuracyHigh)
for _, option := range options {
option(s)
}
return s
}
@ -88,7 +95,11 @@ func (s *SHT3xDriver) Connection() gobot.Connection { return s.connector.(gobot.
// Start initializes the SHT3x
func (s *SHT3xDriver) Start() (err error) {
bus := s.connector.I2cGetDefaultBus()
if s.GetBus() == BusNotInitialized {
s.Bus(s.connector.I2cGetDefaultBus())
}
bus := s.GetBus()
s.connection, err = s.connector.I2cGetConnection(s.sht3xAddress, bus)
return
}

View File

@ -12,8 +12,9 @@ type WiichuckDriver struct {
name string
connector I2cConnector
connection I2cConnection
interval time.Duration
pauseTime time.Duration
I2cBusser
interval time.Duration
pauseTime time.Duration
gobot.Eventer
joystick map[string]float64
data map[string]float64
@ -26,10 +27,11 @@ type WiichuckDriver struct {
// "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 I2cConnector, v ...time.Duration) *WiichuckDriver {
func NewWiichuckDriver(a I2cConnector, options ...func(I2cBusser)) *WiichuckDriver {
w := &WiichuckDriver{
name: gobot.DefaultName("Wiichuck"),
connector: a,
I2cBusser: NewI2cBusser(),
interval: 10 * time.Millisecond,
pauseTime: 1 * time.Millisecond,
Eventer: gobot.NewEventer(),
@ -45,14 +47,15 @@ func NewWiichuckDriver(a I2cConnector, v ...time.Duration) *WiichuckDriver {
},
}
if len(v) > 0 {
w.interval = v[0]
for _, option := range options {
option(w)
}
w.AddEvent(Z)
w.AddEvent(C)
w.AddEvent(Joystick)
w.AddEvent(Error)
return w
}
func (w *WiichuckDriver) Name() string { return w.name }
@ -62,7 +65,11 @@ func (w *WiichuckDriver) Connection() gobot.Connection { return w.connector.(gob
// Start initilizes i2c and reads from adaptor
// using specified interval to update with new value
func (w *WiichuckDriver) Start() (err error) {
bus := w.connector.I2cGetDefaultBus()
if w.GetBus() == BusNotInitialized {
w.Bus(w.connector.I2cGetDefaultBus())
}
bus := w.GetBus()
w.connection, err = w.connector.I2cGetConnection(wiichuckAddress, bus)
if err != nil {
return err

View File

@ -1,6 +1,7 @@
package i2c
import (
"strings"
"testing"
"time"
@ -37,8 +38,8 @@ func TestWiichuckDriver(t *testing.T) {
gobottest.Refute(t, wii.Connection(), nil)
gobottest.Assert(t, wii.interval, 10*time.Millisecond)
wii = NewWiichuckDriver(newI2cTestAdaptor(), 100*time.Millisecond)
gobottest.Assert(t, wii.interval, 100*time.Millisecond)
wii = NewWiichuckDriver(newI2cTestAdaptor())
gobottest.Assert(t, strings.HasPrefix(wii.Name(), "Wiichuck"), true)
}
func TestWiichuckDriverStart(t *testing.T) {