mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-01 13:48:57 +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:
parent
f8f92af056
commit
90bd2d594f
@ -98,6 +98,7 @@ type AdafruitMotorHatDriver struct {
|
|||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
motorHatConnection I2cConnection
|
motorHatConnection I2cConnection
|
||||||
servoHatConnection I2cConnection
|
servoHatConnection I2cConnection
|
||||||
|
I2cBusser
|
||||||
gobot.Commander
|
gobot.Commander
|
||||||
dcMotors []adaFruitDCMotor
|
dcMotors []adaFruitDCMotor
|
||||||
stepperMotors []adaFruitStepperMotor
|
stepperMotors []adaFruitStepperMotor
|
||||||
@ -128,7 +129,7 @@ func (a *AdafruitMotorHatDriver) Connection() gobot.Connection { return a.connec
|
|||||||
|
|
||||||
// NewAdafruitMotorHatDriver initializes the internal DCMotor and StepperMotor types.
|
// 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.
|
// 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 dc []adaFruitDCMotor
|
||||||
var st []adaFruitStepperMotor
|
var st []adaFruitStepperMotor
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
@ -151,10 +152,16 @@ func NewAdafruitMotorHatDriver(conn I2cConnector) *AdafruitMotorHatDriver {
|
|||||||
driver := &AdafruitMotorHatDriver{
|
driver := &AdafruitMotorHatDriver{
|
||||||
name: gobot.DefaultName("AdafruitMotorHat"),
|
name: gobot.DefaultName("AdafruitMotorHat"),
|
||||||
connector: conn,
|
connector: conn,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
Commander: gobot.NewCommander(),
|
Commander: gobot.NewCommander(),
|
||||||
dcMotors: dc,
|
dcMotors: dc,
|
||||||
stepperMotors: st,
|
stepperMotors: st,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(driver)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: add API funcs?
|
// TODO: add API funcs?
|
||||||
return driver
|
return driver
|
||||||
}
|
}
|
||||||
@ -195,7 +202,11 @@ func (a *AdafruitMotorHatDriver) startDriver(connection I2cConnection) (err erro
|
|||||||
|
|
||||||
// Start initializes both I2C-addressable Adafruit Motor HAT drivers
|
// Start initializes both I2C-addressable Adafruit Motor HAT drivers
|
||||||
func (a *AdafruitMotorHatDriver) Start() (err error) {
|
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 {
|
if a.servoHatConnection, err = a.connector.I2cGetConnection(servoHatAddress, bus); err != nil {
|
||||||
return
|
return
|
||||||
|
@ -74,8 +74,9 @@ func (b *BlinkMDriver) Start() (err error) {
|
|||||||
if b.GetBus() == BusNotInitialized {
|
if b.GetBus() == BusNotInitialized {
|
||||||
b.Bus(b.connector.I2cGetDefaultBus())
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,10 @@ const bmp180RegisterPressureMSB = 0xF6
|
|||||||
// BMP180Driver is the gobot driver for the Bosch pressure sensor BMP180.
|
// BMP180Driver is the gobot driver for the Bosch pressure sensor BMP180.
|
||||||
// Device datasheet: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
|
// Device datasheet: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
|
||||||
type BMP180Driver struct {
|
type BMP180Driver struct {
|
||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
calibrationCoefficients *calibrationCoefficients
|
calibrationCoefficients *calibrationCoefficients
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,12 +57,20 @@ type calibrationCoefficients struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewBMP180Driver creates a new driver with the i2c interface for the BMP180 device.
|
// NewBMP180Driver creates a new driver with the i2c interface for the BMP180 device.
|
||||||
func NewBMP180Driver(c I2cConnector) *BMP180Driver {
|
func NewBMP180Driver(c I2cConnector, options ...func(I2cBusser)) *BMP180Driver {
|
||||||
return &BMP180Driver{
|
b := &BMP180Driver{
|
||||||
name: gobot.DefaultName("BMP180"),
|
name: gobot.DefaultName("BMP180"),
|
||||||
connector: c,
|
connector: c,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
calibrationCoefficients: &calibrationCoefficients{},
|
calibrationCoefficients: &calibrationCoefficients{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: expose commands to API
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name of the device.
|
// 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.
|
// Start initializes the BMP180 and loads the calibration coefficients.
|
||||||
func (d *BMP180Driver) Start() (err error) {
|
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 {
|
if d.connection, err = d.connector.I2cGetConnection(bmp180Address, bus); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,16 @@ type GroveLcdDriver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewGroveLcdDriver creates a new driver with specified i2c interface.
|
// NewGroveLcdDriver creates a new driver with specified i2c interface.
|
||||||
func NewGroveLcdDriver(a I2cConnector) *GroveLcdDriver {
|
func NewGroveLcdDriver(a I2cConnector, options ...func(I2cBusser)) *GroveLcdDriver {
|
||||||
return &GroveLcdDriver{
|
lcd := &GroveLcdDriver{
|
||||||
JHD1313M1Driver: NewJHD1313M1Driver(a),
|
JHD1313M1Driver: NewJHD1313M1Driver(a),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(lcd)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lcd
|
||||||
}
|
}
|
||||||
|
|
||||||
type GroveAccelerometerDriver struct {
|
type GroveAccelerometerDriver struct {
|
||||||
@ -20,8 +26,14 @@ type GroveAccelerometerDriver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewGroveAccelerometerDriver creates a new driver with specified i2c interface
|
// NewGroveAccelerometerDriver creates a new driver with specified i2c interface
|
||||||
func NewGroveAccelerometerDriver(a I2cConnector) *GroveAccelerometerDriver {
|
func NewGroveAccelerometerDriver(a I2cConnector, options ...func(I2cBusser)) *GroveAccelerometerDriver {
|
||||||
return &GroveAccelerometerDriver{
|
mma := &GroveAccelerometerDriver{
|
||||||
MMA7660Driver: NewMMA7660Driver(a),
|
MMA7660Driver: NewMMA7660Driver(a),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(mma)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mma
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,22 @@ type HMC6352Driver struct {
|
|||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHMC6352Driver creates a new driver with specified i2c interface
|
// NewHMC6352Driver creates a new driver with specified i2c interface
|
||||||
func NewHMC6352Driver(a I2cConnector) *HMC6352Driver {
|
func NewHMC6352Driver(a I2cConnector, options ...func(I2cBusser)) *HMC6352Driver {
|
||||||
return &HMC6352Driver{
|
hmc := &HMC6352Driver{
|
||||||
name: gobot.DefaultName("HMC6352"),
|
name: gobot.DefaultName("HMC6352"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(hmc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hmc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name for this Driver
|
// 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
|
// Start initializes the hmc6352
|
||||||
func (h *HMC6352Driver) Start() (err error) {
|
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)
|
h.connection, err = h.connector.I2cGetConnection(hmc6352Address, bus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -41,8 +41,6 @@ const (
|
|||||||
LCD_2NDLINEOFFSET = 0x40
|
LCD_2NDLINEOFFSET = 0x40
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.Driver = (*JHD1313M1Driver)(nil)
|
|
||||||
|
|
||||||
// CustomLCDChars is a map of CGRAM characters that can be loaded
|
// CustomLCDChars is a map of CGRAM characters that can be loaded
|
||||||
// into a LCD screen to display custom characters. Some LCD screens such
|
// into a LCD screen to display custom characters. Some LCD screens such
|
||||||
// as the Grove screen (jhd1313m1) isn't loaded with latin 1 characters.
|
// 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.
|
// 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
|
// http://www.seeedstudio.com/wiki/Grove_-_LCD_RGB_Backlight
|
||||||
type JHD1313M1Driver struct {
|
type JHD1313M1Driver struct {
|
||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
|
I2cBusser
|
||||||
lcdAddress int
|
lcdAddress int
|
||||||
lcdConnection I2cConnection
|
lcdConnection I2cConnection
|
||||||
rgbAddress int
|
rgbAddress int
|
||||||
@ -82,13 +81,20 @@ type JHD1313M1Driver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewJHD1313M1Driver creates a new driver with specified i2c interface.
|
// NewJHD1313M1Driver creates a new driver with specified i2c interface.
|
||||||
func NewJHD1313M1Driver(a I2cConnector) *JHD1313M1Driver {
|
func NewJHD1313M1Driver(a I2cConnector, options ...func(I2cBusser)) *JHD1313M1Driver {
|
||||||
return &JHD1313M1Driver{
|
j := &JHD1313M1Driver{
|
||||||
name: gobot.DefaultName("JHD1313M1"),
|
name: gobot.DefaultName("JHD1313M1"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
lcdAddress: 0x3E,
|
lcdAddress: 0x3E,
|
||||||
rgbAddress: 0x62,
|
rgbAddress: 0x62,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(j)
|
||||||
|
}
|
||||||
|
|
||||||
|
return j
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name the JHD1313M1 Driver was given when created.
|
// 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.
|
// Start starts the backlit and the screen and initializes the states.
|
||||||
func (h *JHD1313M1Driver) Start() (err error) {
|
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 {
|
if h.lcdConnection, err = h.connector.I2cGetConnection(h.lcdAddress, bus); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
41
drivers/i2c/jhd1313m1_driver_test.go
Normal file
41
drivers/i2c/jhd1313m1_driver_test.go
Normal 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)
|
||||||
|
}
|
@ -27,7 +27,8 @@ type L3GD20HDriver struct {
|
|||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
scale L3GD20HScale
|
I2cBusser
|
||||||
|
scale L3GD20HScale
|
||||||
}
|
}
|
||||||
|
|
||||||
// L3GD20HScale is the scale sensitivity of degrees-per-second.
|
// 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.
|
// NewL3GD20HDriver creates a new driver with the i2c interface for the L3GD20H device.
|
||||||
func NewL3GD20HDriver(c I2cConnector) *L3GD20HDriver {
|
func NewL3GD20HDriver(c I2cConnector, options ...func(I2cBusser)) *L3GD20HDriver {
|
||||||
return &L3GD20HDriver{
|
l := &L3GD20HDriver{
|
||||||
name: gobot.DefaultName("L3GD20H"),
|
name: gobot.DefaultName("L3GD20H"),
|
||||||
connector: c,
|
connector: c,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
scale: L3GD20HScale250dps,
|
scale: L3GD20HScale250dps,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: add commands to API
|
||||||
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name of the device.
|
// Name returns the name of the device.
|
||||||
@ -85,7 +94,11 @@ func (d *L3GD20HDriver) Start() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *L3GD20HDriver) initialization() (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)
|
d.connection, err = d.connector.I2cGetConnection(l3gd20hAddress, bus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -12,14 +12,23 @@ type LIDARLiteDriver struct {
|
|||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLIDARLiteDriver creates a new driver with specified i2c interface
|
// NewLIDARLiteDriver creates a new driver with specified i2c interface
|
||||||
func NewLIDARLiteDriver(a I2cConnector) *LIDARLiteDriver {
|
func NewLIDARLiteDriver(a I2cConnector, options ...func(I2cBusser)) *LIDARLiteDriver {
|
||||||
return &LIDARLiteDriver{
|
l := &LIDARLiteDriver{
|
||||||
name: gobot.DefaultName("LIDARLite"),
|
name: gobot.DefaultName("LIDARLite"),
|
||||||
connector: a,
|
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 }
|
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
|
// Start initialized the LIDAR
|
||||||
func (h *LIDARLiteDriver) Start() (err error) {
|
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)
|
h.connection, err = h.connector.I2cGetConnection(lidarliteAddress, bus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -20,16 +20,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gobot.io/x/gobot"
|
"gobot.io/x/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var debug = false // Set this to true to see debugging information
|
||||||
debug = false // Set this to true to see debugging information
|
|
||||||
// Register this Driver
|
|
||||||
_ gobot.Driver = (*MCP23017Driver)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Port contains all the registers for the device.
|
// Port contains all the registers for the device.
|
||||||
type port struct {
|
type port struct {
|
||||||
@ -80,27 +75,32 @@ func (mc *MCP23017Config) GetUint8Value() uint8 {
|
|||||||
|
|
||||||
// MCP23017Driver contains the driver configuration parameters.
|
// MCP23017Driver contains the driver configuration parameters.
|
||||||
type MCP23017Driver struct {
|
type MCP23017Driver struct {
|
||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
conf MCP23017Config
|
conf MCP23017Config
|
||||||
mcp23017Address int
|
mcp23017Address int
|
||||||
interval time.Duration
|
|
||||||
gobot.Commander
|
gobot.Commander
|
||||||
gobot.Eventer
|
gobot.Eventer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMCP23017Driver creates a new driver with specified i2c interface.
|
// 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{
|
m := &MCP23017Driver{
|
||||||
name: gobot.DefaultName("MCP23017"),
|
name: gobot.DefaultName("MCP23017"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
conf: conf,
|
conf: conf,
|
||||||
mcp23017Address: deviceAddress,
|
mcp23017Address: deviceAddress,
|
||||||
Commander: gobot.NewCommander(),
|
Commander: gobot.NewCommander(),
|
||||||
Eventer: gobot.NewEventer(),
|
Eventer: gobot.NewEventer(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(m)
|
||||||
|
}
|
||||||
|
|
||||||
m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} {
|
m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} {
|
||||||
pin := params["pin"].(uint8)
|
pin := params["pin"].(uint8)
|
||||||
val := params["val"].(uint8)
|
val := params["val"].(uint8)
|
||||||
@ -133,7 +133,11 @@ func (m *MCP23017Driver) Halt() (err error) { return }
|
|||||||
|
|
||||||
// Start writes the device configuration.
|
// Start writes the device configuration.
|
||||||
func (m *MCP23017Driver) Start() (err error) {
|
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)
|
m.connection, err = m.connector.I2cGetConnection(m.mcp23017Address, bus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -7,9 +7,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gobot.io/x/gobot"
|
||||||
"gobot.io/x/gobot/gobottest"
|
"gobot.io/x/gobot/gobottest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ gobot.Driver = (*MCP23017Driver)(nil)
|
||||||
|
|
||||||
var pinValPort = map[string]interface{}{
|
var pinValPort = map[string]interface{}{
|
||||||
"pin": uint8(7),
|
"pin": uint8(7),
|
||||||
"val": uint8(0),
|
"val": uint8(0),
|
||||||
|
@ -2,8 +2,6 @@ package i2c
|
|||||||
|
|
||||||
import "gobot.io/x/gobot"
|
import "gobot.io/x/gobot"
|
||||||
|
|
||||||
var _ gobot.Driver = (*MMA7660Driver)(nil)
|
|
||||||
|
|
||||||
const mma7660Address = 0x4c
|
const mma7660Address = 0x4c
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -34,14 +32,23 @@ type MMA7660Driver struct {
|
|||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMMA7660Driver creates a new driver with specified i2c interface
|
// NewMMA7660Driver creates a new driver with specified i2c interface
|
||||||
func NewMMA7660Driver(a I2cConnector) *MMA7660Driver {
|
func NewMMA7660Driver(a I2cConnector, options ...func(I2cBusser)) *MMA7660Driver {
|
||||||
return &MMA7660Driver{
|
m := &MMA7660Driver{
|
||||||
name: gobot.DefaultName("MMA7660"),
|
name: gobot.DefaultName("MMA7660"),
|
||||||
connector: a,
|
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 }
|
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
|
// Start initialized the mma7660
|
||||||
func (h *MMA7660Driver) Start() (err error) {
|
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)
|
h.connection, err = h.connector.I2cGetConnection(mma7660Address, bus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
41
drivers/i2c/mma7660_driver_test.go
Normal file
41
drivers/i2c/mma7660_driver_test.go
Normal 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)
|
||||||
|
}
|
@ -28,7 +28,8 @@ type MPL115A2Driver struct {
|
|||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
interval time.Duration
|
I2cBusser
|
||||||
|
interval time.Duration
|
||||||
gobot.Eventer
|
gobot.Eventer
|
||||||
A0 float32
|
A0 float32
|
||||||
B1 float32
|
B1 float32
|
||||||
@ -39,18 +40,22 @@ type MPL115A2Driver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewMPL115A2Driver creates a new driver with specified i2c interface
|
// 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{
|
m := &MPL115A2Driver{
|
||||||
name: gobot.DefaultName("MPL115A2"),
|
name: gobot.DefaultName("MPL115A2"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
Eventer: gobot.NewEventer(),
|
Eventer: gobot.NewEventer(),
|
||||||
interval: 10 * time.Millisecond,
|
interval: 10 * time.Millisecond,
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +121,11 @@ func (h *MPL115A2Driver) initialization() (err error) {
|
|||||||
var coB2 int16
|
var coB2 int16
|
||||||
var coC12 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)
|
h.connection, err = h.connector.I2cGetConnection(mpl115a2Address, 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,10 +38,7 @@ func TestMPL115A2Driver(t *testing.T) {
|
|||||||
mpl := initTestMPL115A2Driver()
|
mpl := initTestMPL115A2Driver()
|
||||||
|
|
||||||
gobottest.Refute(t, mpl.Connection(), nil)
|
gobottest.Refute(t, mpl.Connection(), nil)
|
||||||
gobottest.Assert(t, mpl.interval, 10*time.Millisecond)
|
gobottest.Assert(t, strings.HasPrefix(mpl.Name(), "MPL115A2"), true)
|
||||||
|
|
||||||
mpl = NewMPL115A2Driver(newI2cTestAdaptor(), 100*time.Millisecond)
|
|
||||||
gobottest.Assert(t, mpl.interval, 100*time.Millisecond)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMPL115A2DriverStart(t *testing.T) {
|
func TestMPL115A2DriverStart(t *testing.T) {
|
||||||
|
@ -33,9 +33,10 @@ type ThreeDData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MPU6050Driver struct {
|
type MPU6050Driver struct {
|
||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
Accelerometer ThreeDData
|
Accelerometer ThreeDData
|
||||||
Gyroscope ThreeDData
|
Gyroscope ThreeDData
|
||||||
|
@ -55,9 +55,10 @@ var (
|
|||||||
type SHT3xDriver struct {
|
type SHT3xDriver struct {
|
||||||
Units string
|
Units string
|
||||||
|
|
||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
|
I2cBusser
|
||||||
sht3xAddress int
|
sht3xAddress int
|
||||||
accuracy byte
|
accuracy byte
|
||||||
delay time.Duration
|
delay time.Duration
|
||||||
@ -65,15 +66,21 @@ type SHT3xDriver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewSHT3xDriver creates a new driver with specified i2c interface
|
// NewSHT3xDriver creates a new driver with specified i2c interface
|
||||||
func NewSHT3xDriver(a I2cConnector) *SHT3xDriver {
|
func NewSHT3xDriver(a I2cConnector, options ...func(I2cBusser)) *SHT3xDriver {
|
||||||
s := &SHT3xDriver{
|
s := &SHT3xDriver{
|
||||||
Units: "C",
|
Units: "C",
|
||||||
name: gobot.DefaultName("SHT3x"),
|
name: gobot.DefaultName("SHT3x"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
sht3xAddress: SHT3xAddressA,
|
sht3xAddress: SHT3xAddressA,
|
||||||
crcTable: crc8.MakeTable(crc8Params),
|
crcTable: crc8.MakeTable(crc8Params),
|
||||||
}
|
}
|
||||||
s.SetAccuracy(SHT3xAccuracyHigh)
|
s.SetAccuracy(SHT3xAccuracyHigh)
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(s)
|
||||||
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +95,11 @@ func (s *SHT3xDriver) Connection() gobot.Connection { return s.connector.(gobot.
|
|||||||
|
|
||||||
// Start initializes the SHT3x
|
// Start initializes the SHT3x
|
||||||
func (s *SHT3xDriver) Start() (err error) {
|
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)
|
s.connection, err = s.connector.I2cGetConnection(s.sht3xAddress, bus)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,9 @@ type WiichuckDriver struct {
|
|||||||
name string
|
name string
|
||||||
connector I2cConnector
|
connector I2cConnector
|
||||||
connection I2cConnection
|
connection I2cConnection
|
||||||
interval time.Duration
|
I2cBusser
|
||||||
pauseTime time.Duration
|
interval time.Duration
|
||||||
|
pauseTime time.Duration
|
||||||
gobot.Eventer
|
gobot.Eventer
|
||||||
joystick map[string]float64
|
joystick map[string]float64
|
||||||
data 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
|
// "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
|
// "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
|
// "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{
|
w := &WiichuckDriver{
|
||||||
name: gobot.DefaultName("Wiichuck"),
|
name: gobot.DefaultName("Wiichuck"),
|
||||||
connector: a,
|
connector: a,
|
||||||
|
I2cBusser: NewI2cBusser(),
|
||||||
interval: 10 * time.Millisecond,
|
interval: 10 * time.Millisecond,
|
||||||
pauseTime: 1 * time.Millisecond,
|
pauseTime: 1 * time.Millisecond,
|
||||||
Eventer: gobot.NewEventer(),
|
Eventer: gobot.NewEventer(),
|
||||||
@ -45,14 +47,15 @@ func NewWiichuckDriver(a I2cConnector, v ...time.Duration) *WiichuckDriver {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v) > 0 {
|
for _, option := range options {
|
||||||
w.interval = v[0]
|
option(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.AddEvent(Z)
|
w.AddEvent(Z)
|
||||||
w.AddEvent(C)
|
w.AddEvent(C)
|
||||||
w.AddEvent(Joystick)
|
w.AddEvent(Joystick)
|
||||||
w.AddEvent(Error)
|
w.AddEvent(Error)
|
||||||
|
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
func (w *WiichuckDriver) Name() string { return w.name }
|
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
|
// Start initilizes i2c and reads from adaptor
|
||||||
// using specified interval to update with new value
|
// using specified interval to update with new value
|
||||||
func (w *WiichuckDriver) Start() (err error) {
|
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)
|
w.connection, err = w.connector.I2cGetConnection(wiichuckAddress, 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,8 @@ func TestWiichuckDriver(t *testing.T) {
|
|||||||
gobottest.Refute(t, wii.Connection(), nil)
|
gobottest.Refute(t, wii.Connection(), nil)
|
||||||
gobottest.Assert(t, wii.interval, 10*time.Millisecond)
|
gobottest.Assert(t, wii.interval, 10*time.Millisecond)
|
||||||
|
|
||||||
wii = NewWiichuckDriver(newI2cTestAdaptor(), 100*time.Millisecond)
|
wii = NewWiichuckDriver(newI2cTestAdaptor())
|
||||||
gobottest.Assert(t, wii.interval, 100*time.Millisecond)
|
gobottest.Assert(t, strings.HasPrefix(wii.Name(), "Wiichuck"), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWiichuckDriverStart(t *testing.T) {
|
func TestWiichuckDriverStart(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user