Skip to content

Commit

Permalink
i2c: completed implemetation of I2cConfig interface
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 9, 2017
1 parent 9a93d3b commit 21bdf5a
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 122 deletions.
12 changes: 4 additions & 8 deletions drivers/i2c/adafruit_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type AdafruitMotorHatDriver struct {
connector I2cConnector
motorHatConnection I2cConnection
servoHatConnection I2cConnection
I2cBusser
I2cConfig
gobot.Commander
dcMotors []adaFruitDCMotor
stepperMotors []adaFruitStepperMotor
Expand Down Expand Up @@ -129,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, options ...func(I2cBusser)) *AdafruitMotorHatDriver {
func NewAdafruitMotorHatDriver(conn I2cConnector, options ...func(I2cConfig)) *AdafruitMotorHatDriver {
var dc []adaFruitDCMotor
var st []adaFruitStepperMotor
for i := 0; i < 4; i++ {
Expand All @@ -152,7 +152,7 @@ func NewAdafruitMotorHatDriver(conn I2cConnector, options ...func(I2cBusser)) *A
driver := &AdafruitMotorHatDriver{
name: gobot.DefaultName("AdafruitMotorHat"),
connector: conn,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
Commander: gobot.NewCommander(),
dcMotors: dc,
stepperMotors: st,
Expand Down Expand Up @@ -202,11 +202,7 @@ func (a *AdafruitMotorHatDriver) startDriver(connection I2cConnection) (err erro

// Start initializes both I2C-addressable Adafruit Motor HAT drivers
func (a *AdafruitMotorHatDriver) Start() (err error) {
if a.GetBus() == BusNotInitialized {
a.Bus(a.connector.I2cGetDefaultBus())
}

bus := a.GetBus()
bus := a.GetBus(a.connector.I2cGetDefaultBus())

if a.servoHatConnection, err = a.connector.I2cGetConnection(servoHatAddress, bus); err != nil {
return
Expand Down
14 changes: 6 additions & 8 deletions drivers/i2c/blinkm_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type BlinkMDriver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
I2cConfig
gobot.Commander
}

Expand All @@ -24,12 +24,12 @@ type BlinkMDriver struct {
// Fade - fades the RGB color
// FirmwareVersion - returns the version of the current Frimware
// Color - returns the color of the LED.
func NewBlinkMDriver(a I2cConnector, options ...func(I2cBusser)) *BlinkMDriver {
func NewBlinkMDriver(a I2cConnector, options ...func(I2cConfig)) *BlinkMDriver {
b := &BlinkMDriver{
name: gobot.DefaultName("BlinkM"),
Commander: gobot.NewCommander(),
connector: a,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
}

for _, option := range options {
Expand Down Expand Up @@ -71,12 +71,10 @@ func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection.(gobo

// Start starts the Driver up, and writes start command
func (b *BlinkMDriver) Start() (err error) {
if b.GetBus() == BusNotInitialized {
b.Bus(b.connector.I2cGetDefaultBus())
}
bus := b.GetBus()
bus := b.GetBus(b.connector.I2cGetDefaultBus())
address := b.GetAddress(blinkmAddress)

b.connection, err = b.connector.I2cGetConnection(blinkmAddress, bus)
b.connection, err = b.connector.I2cGetConnection(address, bus)
if err != nil {
return
}
Expand Down
14 changes: 6 additions & 8 deletions drivers/i2c/bmp180_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type BMP180Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
I2cConfig
calibrationCoefficients *calibrationCoefficients
}

Expand Down Expand Up @@ -57,11 +57,11 @@ type calibrationCoefficients struct {
}

// NewBMP180Driver creates a new driver with the i2c interface for the BMP180 device.
func NewBMP180Driver(c I2cConnector, options ...func(I2cBusser)) *BMP180Driver {
func NewBMP180Driver(c I2cConnector, options ...func(I2cConfig)) *BMP180Driver {
b := &BMP180Driver{
name: gobot.DefaultName("BMP180"),
connector: c,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
calibrationCoefficients: &calibrationCoefficients{},
}

Expand Down Expand Up @@ -90,12 +90,10 @@ func (d *BMP180Driver) Connection() gobot.Connection {

// Start initializes the BMP180 and loads the calibration coefficients.
func (d *BMP180Driver) Start() (err error) {
if d.GetBus() == BusNotInitialized {
d.Bus(d.connector.I2cGetDefaultBus())
}
bus := d.GetBus()
bus := d.GetBus(d.connector.I2cGetDefaultBus())
address := d.GetAddress(bmp180Address)

if d.connection, err = d.connector.I2cGetConnection(bmp180Address, bus); err != nil {
if d.connection, err = d.connector.I2cGetConnection(address, bus); err != nil {
return err
}
if err := d.initialization(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions drivers/i2c/grove_drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type GroveLcdDriver struct {
}

// NewGroveLcdDriver creates a new driver with specified i2c interface.
func NewGroveLcdDriver(a I2cConnector, options ...func(I2cBusser)) *GroveLcdDriver {
func NewGroveLcdDriver(a I2cConnector, options ...func(I2cConfig)) *GroveLcdDriver {
lcd := &GroveLcdDriver{
JHD1313M1Driver: NewJHD1313M1Driver(a),
}
Expand All @@ -26,7 +26,7 @@ type GroveAccelerometerDriver struct {
}

// NewGroveAccelerometerDriver creates a new driver with specified i2c interface
func NewGroveAccelerometerDriver(a I2cConnector, options ...func(I2cBusser)) *GroveAccelerometerDriver {
func NewGroveAccelerometerDriver(a I2cConnector, options ...func(I2cConfig)) *GroveAccelerometerDriver {
mma := &GroveAccelerometerDriver{
MMA7660Driver: NewMMA7660Driver(a),
}
Expand Down
14 changes: 6 additions & 8 deletions drivers/i2c/hmc6352_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ type HMC6352Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
I2cConfig
}

// NewHMC6352Driver creates a new driver with specified i2c interface
func NewHMC6352Driver(a I2cConnector, options ...func(I2cBusser)) *HMC6352Driver {
func NewHMC6352Driver(a I2cConnector, options ...func(I2cConfig)) *HMC6352Driver {
hmc := &HMC6352Driver{
name: gobot.DefaultName("HMC6352"),
connector: a,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
}

for _, option := range options {
Expand All @@ -38,12 +38,10 @@ func (h *HMC6352Driver) Connection() gobot.Connection { return h.connector.(gobo

// Start initializes the hmc6352
func (h *HMC6352Driver) Start() (err error) {
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
bus := h.GetBus(h.connector.I2cGetDefaultBus())
address := h.GetAddress(hmc6352Address)

h.connection, err = h.connector.I2cGetConnection(hmc6352Address, bus)
h.connection, err = h.connector.I2cGetConnection(address, bus)
if err != nil {
return err
}
Expand Down
63 changes: 48 additions & 15 deletions drivers/i2c/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const (
)

const (
BusNotInitialized = -1
BusNotInitialized = -1
AddressNotInitialized = -1
)

// I2cConnection is a connection to an I2C device with a specified address
Expand Down Expand Up @@ -140,38 +141,70 @@ type I2cConnector interface {
I2cGetDefaultBus() int
}

type i2cBusser struct {
bus int
type i2cConfig struct {
bus int
address int
}

// I2cBusser is the interface which describes how a Driver can specify
// which I2C bus it wants to use
type I2cBusser interface {
// I2cConfig is the interface which describes how a Driver can specify
// optional I2C params such as which I2C bus it wants to use
type I2cConfig interface {
// Bus sets which bus to use
Bus(bus int)

// GetBus gets which bus to use
GetBus() int
GetBus(def int) int

// Address sets which address to use
Address(address int)

// GetAddress gets which address to use
GetAddress(def int) int
}

// NewI2cBusser returns a new I2cBusser.
func NewI2cBusser() I2cBusser {
return &i2cBusser{}
// NewI2cConfig returns a new I2cConfig.
func NewI2cConfig() I2cConfig {
return &i2cConfig{bus: BusNotInitialized, address: AddressNotInitialized}
}

// Bus sets which bus to use
func (i *i2cBusser) Bus(bus int) {
// Bus sets preferred bus to use
func (i *i2cConfig) Bus(bus int) {
i.bus = bus
}

// GetBus gets which bus to use
func (i *i2cBusser) GetBus() int {
func (i *i2cConfig) GetBus(d int) int {
if i.bus == BusNotInitialized {
return d
}

return i.bus
}

// Bus sets which bus to use as a optional param
func Bus(bus int) func(I2cBusser) {
return func(i I2cBusser) {
func Bus(bus int) func(I2cConfig) {
return func(i I2cConfig) {
i.Bus(bus)
}
}

// Address sets which address to use
func (i *i2cConfig) Address(address int) {
i.address = address
}

// GetAddress gets which address to use
func (i *i2cConfig) GetAddress(a int) int {
if i.address == AddressNotInitialized {
return a
}

return i.address
}

// Address sets which address to use as a optional param
func Address(address int) func(I2cConfig) {
return func(i I2cConfig) {
i.Address(address)
}
}
11 changes: 4 additions & 7 deletions drivers/i2c/jhd1313m1_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ var CustomLCDChars = map[string][8]byte{
type JHD1313M1Driver struct {
name string
connector I2cConnector
I2cBusser
I2cConfig
lcdAddress int
lcdConnection I2cConnection
rgbAddress int
rgbConnection I2cConnection
}

// NewJHD1313M1Driver creates a new driver with specified i2c interface.
func NewJHD1313M1Driver(a I2cConnector, options ...func(I2cBusser)) *JHD1313M1Driver {
func NewJHD1313M1Driver(a I2cConnector, options ...func(I2cConfig)) *JHD1313M1Driver {
j := &JHD1313M1Driver{
name: gobot.DefaultName("JHD1313M1"),
connector: a,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
lcdAddress: 0x3E,
rgbAddress: 0x62,
}
Expand All @@ -110,10 +110,7 @@ func (h *JHD1313M1Driver) Connection() gobot.Connection {

// Start starts the backlit and the screen and initializes the states.
func (h *JHD1313M1Driver) Start() (err error) {
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
bus := h.GetBus(h.connector.I2cGetDefaultBus())

if h.lcdConnection, err = h.connector.I2cGetConnection(h.lcdAddress, bus); err != nil {
return err
Expand Down
14 changes: 6 additions & 8 deletions drivers/i2c/l3gd20h_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type L3GD20HDriver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
I2cConfig
scale L3GD20HScale
}

Expand All @@ -44,11 +44,11 @@ const (
)

// NewL3GD20HDriver creates a new driver with the i2c interface for the L3GD20H device.
func NewL3GD20HDriver(c I2cConnector, options ...func(I2cBusser)) *L3GD20HDriver {
func NewL3GD20HDriver(c I2cConnector, options ...func(I2cConfig)) *L3GD20HDriver {
l := &L3GD20HDriver{
name: gobot.DefaultName("L3GD20H"),
connector: c,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
scale: L3GD20HScale250dps,
}

Expand Down Expand Up @@ -94,12 +94,10 @@ func (d *L3GD20HDriver) Start() (err error) {
}

func (d *L3GD20HDriver) initialization() (err error) {
if d.GetBus() == BusNotInitialized {
d.Bus(d.connector.I2cGetDefaultBus())
}
bus := d.GetBus()
bus := d.GetBus(d.connector.I2cGetDefaultBus())
address := d.GetAddress(l3gd20hAddress)

d.connection, err = d.connector.I2cGetConnection(l3gd20hAddress, bus)
d.connection, err = d.connector.I2cGetConnection(address, bus)
if err != nil {
return err
}
Expand Down
14 changes: 6 additions & 8 deletions drivers/i2c/lidarlite_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type LIDARLiteDriver struct {
name string
connector I2cConnector
connection I2cConnection
I2cBusser
I2cConfig
}

// NewLIDARLiteDriver creates a new driver with specified i2c interface
func NewLIDARLiteDriver(a I2cConnector, options ...func(I2cBusser)) *LIDARLiteDriver {
func NewLIDARLiteDriver(a I2cConnector, options ...func(I2cConfig)) *LIDARLiteDriver {
l := &LIDARLiteDriver{
name: gobot.DefaultName("LIDARLite"),
connector: a,
I2cBusser: NewI2cBusser(),
I2cConfig: NewI2cConfig(),
}

for _, option := range options {
Expand All @@ -37,12 +37,10 @@ func (h *LIDARLiteDriver) Connection() gobot.Connection { return h.connector.(go

// Start initialized the LIDAR
func (h *LIDARLiteDriver) Start() (err error) {
if h.GetBus() == BusNotInitialized {
h.Bus(h.connector.I2cGetDefaultBus())
}
bus := h.GetBus()
bus := h.GetBus(h.connector.I2cGetDefaultBus())
address := h.GetAddress(lidarliteAddress)

h.connection, err = h.connector.I2cGetConnection(lidarliteAddress, bus)
h.connection, err = h.connector.I2cGetConnection(address, bus)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 21bdf5a

Please sign in to comment.