Skip to content

Commit

Permalink
i2c: refactoring of the interface/implementations based on feedback a…
Browse files Browse the repository at this point in the history
…nd golinter

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 10, 2017
1 parent 03eba38 commit 51f6aba
Show file tree
Hide file tree
Showing 31 changed files with 315 additions and 276 deletions.
36 changes: 18 additions & 18 deletions drivers/i2c/adafruit_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ type adaFruitStepperMotor struct {
// control both motor direction and speed over I2C.
type AdafruitMotorHatDriver struct {
name string
connector I2cConnector
motorHatConnection I2cConnection
servoHatConnection I2cConnection
I2cConfig
connector Connector
motorHatConnection Connection
servoHatConnection Connection
Config
gobot.Commander
dcMotors []adaFruitDCMotor
stepperMotors []adaFruitStepperMotor
Expand Down Expand Up @@ -130,13 +130,13 @@ 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.
// Params:
// conn I2cConnector - the Adaptor to use with this Driver
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.Bus(int): bus to use with this driver
// i2c.Address(int): address to use with this driver
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewAdafruitMotorHatDriver(conn I2cConnector, options ...func(I2cConfig)) *AdafruitMotorHatDriver {
func NewAdafruitMotorHatDriver(conn Connector, options ...func(Config)) *AdafruitMotorHatDriver {
var dc []adaFruitDCMotor
var st []adaFruitStepperMotor
for i := 0; i < 4; i++ {
Expand All @@ -159,7 +159,7 @@ func NewAdafruitMotorHatDriver(conn I2cConnector, options ...func(I2cConfig)) *A
driver := &AdafruitMotorHatDriver{
name: gobot.DefaultName("AdafruitMotorHat"),
connector: conn,
I2cConfig: NewI2cConfig(),
Config: NewConfig(),
Commander: gobot.NewCommander(),
dcMotors: dc,
stepperMotors: st,
Expand All @@ -169,11 +169,11 @@ func NewAdafruitMotorHatDriver(conn I2cConnector, options ...func(I2cConfig)) *A
option(driver)
}

// TODO: add API funcs?
// TODO: add API funcs
return driver
}

func (a *AdafruitMotorHatDriver) startDriver(connection I2cConnection) (err error) {
func (a *AdafruitMotorHatDriver) startDriver(connection Connection) (err error) {
if err = a.setAllPWM(connection, 0, 0); err != nil {
return
}
Expand Down Expand Up @@ -209,17 +209,17 @@ 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.GetBus(a.connector.I2cGetDefaultBus())
bus := a.GetBusOrDefault(a.connector.GetDefaultBus())

if a.servoHatConnection, err = a.connector.I2cGetConnection(servoHatAddress, bus); err != nil {
if a.servoHatConnection, err = a.connector.GetConnection(servoHatAddress, bus); err != nil {
return
}

if err = a.startDriver(a.servoHatConnection); err != nil {
return
}

if a.motorHatConnection, err = a.connector.I2cGetConnection(motorHatAddress, bus); err != nil {
if a.motorHatConnection, err = a.connector.GetConnection(motorHatAddress, bus); err != nil {
return
}

Expand All @@ -235,7 +235,7 @@ func (a *AdafruitMotorHatDriver) Halt() (err error) { return }

// setPWM sets the start (on) and end (off) of the high-segment of the PWM pulse
// on the specific channel (pin).
func (a *AdafruitMotorHatDriver) setPWM(conn I2cConnection, pin byte, on, off int32) (err error) {
func (a *AdafruitMotorHatDriver) setPWM(conn Connection, pin byte, on, off int32) (err error) {
// register and values to be written to that register
regVals := make(map[int][]byte)
regVals[0] = []byte{byte(_LedZeroOnL + 4*pin), byte(on & 0xff)}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (a *AdafruitMotorHatDriver) SetServoMotorPulse(channel byte, on, off int32)
// pulses per second are generated by the integrated circuit. The frequency
// determines how "long" each pulse is in duration from start to finish,
// taking into account the high and low segments of the pulse.
func (a *AdafruitMotorHatDriver) setPWMFreq(conn I2cConnection, freq float64) (err error) {
func (a *AdafruitMotorHatDriver) setPWMFreq(conn Connection, freq float64) (err error) {
// 25MHz
preScaleVal := 25000000.0
// 12-bit
Expand Down Expand Up @@ -315,7 +315,7 @@ func (a *AdafruitMotorHatDriver) setPWMFreq(conn I2cConnection, freq float64) (e
}

// setAllPWM sets all PWM channels for the given address
func (a *AdafruitMotorHatDriver) setAllPWM(conn I2cConnection, on, off int32) (err error) {
func (a *AdafruitMotorHatDriver) setAllPWM(conn Connection, on, off int32) (err error) {
// register and values to be written to that register
regVals := make(map[int][]byte)
regVals[0] = []byte{byte(_AllLedOnL), byte(on & 0xff)}
Expand All @@ -330,7 +330,7 @@ func (a *AdafruitMotorHatDriver) setAllPWM(conn I2cConnection, on, off int32) (e
return
}

func (a *AdafruitMotorHatDriver) setPin(conn I2cConnection, pin byte, value int32) (err error) {
func (a *AdafruitMotorHatDriver) setPin(conn Connection, pin byte, value int32) (err error) {
if value == 0 {
return a.setPWM(conn, pin, 0, 4096)
}
Expand Down
25 changes: 14 additions & 11 deletions drivers/i2c/blinkm_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ const blinkmAddress = 0x09
// BlinkMDriver is a Gobot Driver for a BlinkM LED
type BlinkMDriver struct {
name string
connector I2cConnector
connection I2cConnection
I2cConfig
connector Connector
connection Connection
Config
gobot.Commander
}

// NewBlinkMDriver creates a new BlinkMDriver.
//
// Params:
// conn I2cConnector - the Adaptor to use with this Driver
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.Bus(int): bus to use with this driver
// i2c.Address(int): address to use with this driver
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewBlinkMDriver(a I2cConnector, options ...func(I2cConfig)) *BlinkMDriver {
func NewBlinkMDriver(a Connector, options ...func(Config)) *BlinkMDriver {
b := &BlinkMDriver{
name: gobot.DefaultName("BlinkM"),
Commander: gobot.NewCommander(),
connector: a,
I2cConfig: NewI2cConfig(),
Config: NewConfig(),
}

for _, option := range options {
Expand All @@ -44,16 +44,19 @@ func NewBlinkMDriver(a I2cConnector, options ...func(I2cConfig)) *BlinkMDriver {
blue := byte(params["blue"].(float64))
return b.Rgb(red, green, blue)
})

b.AddCommand("Fade", func(params map[string]interface{}) interface{} {
red := byte(params["red"].(float64))
green := byte(params["green"].(float64))
blue := byte(params["blue"].(float64))
return b.Fade(red, green, blue)
})

b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
version, err := b.FirmwareVersion()
return map[string]interface{}{"version": version, "err": err}
})

b.AddCommand("Color", func(params map[string]interface{}) interface{} {
color, err := b.Color()
return map[string]interface{}{"color": color, "err": err}
Expand All @@ -73,10 +76,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) {
bus := b.GetBus(b.connector.I2cGetDefaultBus())
address := b.GetAddress(blinkmAddress)
bus := b.GetBusOrDefault(b.connector.GetDefaultBus())
address := b.GetAddressOrDefault(blinkmAddress)

b.connection, err = b.connector.I2cGetConnection(address, bus)
b.connection, err = b.connector.GetConnection(address, bus)
if err != nil {
return
}
Expand Down
22 changes: 11 additions & 11 deletions drivers/i2c/bmp180_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const bmp180RegisterPressureMSB = 0xF6
// Device datasheet: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
type BMP180Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cConfig
connector Connector
connection Connection
Config
calibrationCoefficients *calibrationCoefficients
}

Expand Down Expand Up @@ -58,17 +58,17 @@ type calibrationCoefficients struct {

// NewBMP180Driver creates a new driver with the i2c interface for the BMP180 device.
// Params:
// conn I2cConnector - the Adaptor to use with this Driver
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.Bus(int): bus to use with this driver
// i2c.Address(int): address to use with this driver
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewBMP180Driver(c I2cConnector, options ...func(I2cConfig)) *BMP180Driver {
func NewBMP180Driver(c Connector, options ...func(Config)) *BMP180Driver {
b := &BMP180Driver{
name: gobot.DefaultName("BMP180"),
connector: c,
I2cConfig: NewI2cConfig(),
Config: NewConfig(),
calibrationCoefficients: &calibrationCoefficients{},
}

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

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

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

// NewGroveLcdDriver creates a new driver with specified i2c interface.
// Params:
// conn I2cConnector - the Adaptor to use with this Driver
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.Bus(int): bus to use with this driver
// i2c.Address(int): address to use with this driver
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewGroveLcdDriver(a I2cConnector, options ...func(I2cConfig)) *GroveLcdDriver {
func NewGroveLcdDriver(a Connector, options ...func(Config)) *GroveLcdDriver {
lcd := &GroveLcdDriver{
JHD1313M1Driver: NewJHD1313M1Driver(a),
}
Expand All @@ -33,7 +33,14 @@ type GroveAccelerometerDriver struct {
}

// NewGroveAccelerometerDriver creates a new driver with specified i2c interface
func NewGroveAccelerometerDriver(a I2cConnector, options ...func(I2cConfig)) *GroveAccelerometerDriver {
// Params:
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewGroveAccelerometerDriver(a Connector, options ...func(Config)) *GroveAccelerometerDriver {
mma := &GroveAccelerometerDriver{
MMA7660Driver: NewMMA7660Driver(a),
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/i2c/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func (t *i2cTestAdaptor) WriteBlockData(reg uint8, b []byte) (err error) {
return
}

func (t *i2cTestAdaptor) I2cGetConnection( /* address */ int /* bus */, int) (connection I2cConnection, err error) {
func (t *i2cTestAdaptor) GetConnection( /* address */ int /* bus */, int) (connection Connection, err error) {
return t, nil
}

func (t *i2cTestAdaptor) I2cGetDefaultBus() int {
func (t *i2cTestAdaptor) GetDefaultBus() int {
return 0
}

Expand Down
22 changes: 11 additions & 11 deletions drivers/i2c/hmc6352_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ const hmc6352Address = 0x21
// HMC6352Driver is a Driver for a HMC6352 digital compass
type HMC6352Driver struct {
name string
connector I2cConnector
connection I2cConnection
I2cConfig
connector Connector
connection Connection
Config
}

// NewHMC6352Driver creates a new driver with specified i2c interface
// Params:
// conn I2cConnector - the Adaptor to use with this Driver
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.Bus(int): bus to use with this driver
// i2c.Address(int): address to use with this driver
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewHMC6352Driver(a I2cConnector, options ...func(I2cConfig)) *HMC6352Driver {
func NewHMC6352Driver(a Connector, options ...func(Config)) *HMC6352Driver {
hmc := &HMC6352Driver{
name: gobot.DefaultName("HMC6352"),
connector: a,
I2cConfig: NewI2cConfig(),
Config: NewConfig(),
}

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

// Start initializes the hmc6352
func (h *HMC6352Driver) Start() (err error) {
bus := h.GetBus(h.connector.I2cGetDefaultBus())
address := h.GetAddress(hmc6352Address)
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
address := h.GetAddressOrDefault(hmc6352Address)

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

0 comments on commit 51f6aba

Please sign in to comment.