1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-04 22:17:39 +08:00

Code cleanup, improve go report card

This commit is contained in:
Ulises Flynn 2016-03-07 23:51:45 -07:00
parent 88fd93fbaf
commit 30a8aa211b
2 changed files with 230 additions and 111 deletions

View File

@ -17,8 +17,7 @@ limitations under the License.
package i2c package i2c
import ( import (
"bytes" "fmt"
"encoding/binary"
"log" "log"
"strings" "strings"
"time" "time"
@ -27,24 +26,24 @@ import (
) )
var ( var (
Debug = true // Set this to true to see debugging information debug = false // Set this to true to see debugging information
// Register this Driver // Register this Driver
_ gobot.Driver = (*MCP23017Driver)(nil) _ 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 {
IODIR byte // I/O direction register: 0=output / 1=input IODIR uint8 // I/O direction register: 0=output / 1=input
IPOL byte // Input polarity register: 0=normal polarity / 1=inversed IPOL uint8 // Input polarity register: 0=normal polarity / 1=inversed
GPINTEN byte // Interrupt on change control register: 0=disabled / 1=enabled GPINTEN uint8 // Interrupt on change control register: 0=disabled / 1=enabled
DEFVAL byte // Default compare register for interrupt on change DEFVAL uint8 // Default compare register for interrupt on change
INTCON byte // Interrupt control register: bit set to 0= use defval bit value to compare pin value/ bit set to 1= pin value compared to previous pin value INTCON uint8 // Interrupt control register: bit set to 0= use defval bit value to compare pin value/ bit set to 1= pin value compared to previous pin value
IOCON byte // Configuration register IOCON uint8 // Configuration register
GPPU byte // Pull-up resistor configuration register: 0=enabled / 1=disabled GPPU uint8 // Pull-up resistor configuration register: 0=enabled / 1=disabled
INTF byte // Interrupt flag register: 0=no interrupt / 1=pin caused interrupt INTF uint8 // Interrupt flag register: 0=no interrupt / 1=pin caused interrupt
INTCAP byte // Interrupt capture register, captures pin values during interrupt: 0=logic low / 1=logic high INTCAP uint8 // Interrupt capture register, captures pin values during interrupt: 0=logic low / 1=logic high
GPIO byte // Port register, reading from this register reads the port GPIO uint8 // Port register, reading from this register reads the port
OLAT byte // Output latch register, write modifies the pins: 0=logic low / 1=logic high OLAT uint8 // Output latch register, write modifies the pins: 0=logic low / 1=logic high
} }
// Registers in the MCP23017 have different address based on which bank is used. // Registers in the MCP23017 have different address based on which bank is used.
@ -62,7 +61,8 @@ func getBank(bnk uint8) bank {
return bank{PortA: port{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A}, PortB: port{0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A}} return bank{PortA: port{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A}, PortB: port{0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A}}
} }
// MCP23017Config contains the device configuration settings to be loaded at startup. // MCP23017Config contains the device configuration for the IOCON register.
// These fields should only be set with values 0 or 1.
type MCP23017Config struct { type MCP23017Config struct {
Bank uint8 Bank uint8
Mirror uint8 Mirror uint8
@ -73,12 +73,12 @@ type MCP23017Config struct {
Intpol uint8 Intpol uint8
} }
// getBytes returns the mcp23017 configuration as a slice of bytes. // GetUint8Value returns the configuration data as a packed value.
func (conf *MCP23017Config) getBytes() []byte { func (mc *MCP23017Config) GetUint8Value() uint8 {
return []byte{conf.Bank, conf.Mirror, conf.Seqop, conf.Disslw, conf.Haen, conf.Odr, conf.Intpol} return mc.Bank<<7 | mc.Mirror<<6 | mc.Seqop<<5 | mc.Disslw<<4 | mc.Haen<<3 | mc.Odr<<2 | mc.Intpol<<1
} }
// MCP23107Driver contains the driver configuration parameters. // MCP23017Driver contains the driver configuration parameters.
type MCP23017Driver struct { type MCP23017Driver struct {
name string name string
connection I2c connection I2c
@ -101,14 +101,15 @@ func NewMCP23017Driver(a I2c, name string, conf MCP23017Config, deviceAddress in
} }
m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} { m.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} {
pin := params["pin"].(float64) pin := params["pin"].(uint8)
val := params["val"].(float64) val := params["val"].(uint8)
port := params["port"].(string) port := params["port"].(string)
return m.WriteGPIO(pin, val, port) err := m.WriteGPIO(pin, val, port)
return map[string]interface{}{"err": err}
}) })
m.AddCommand("ReadGPIO", func(params map[string]interface{}) interface{} { m.AddCommand("ReadGPIO", func(params map[string]interface{}) interface{} {
pin := params["pin"].(float64) pin := params["pin"].(uint8)
port := params["port"].(string) port := params["port"].(string)
val, err := m.ReadGPIO(pin, port) val, err := m.ReadGPIO(pin, port)
return map[string]interface{}{"val": val, "err": err} return map[string]interface{}{"val": val, "err": err}
@ -117,34 +118,31 @@ func NewMCP23017Driver(a I2c, name string, conf MCP23017Config, deviceAddress in
return m return m
} }
// Name return the driver name.
func (m *MCP23017Driver) Name() string { return m.name } func (m *MCP23017Driver) Name() string { return m.name }
// Connection returns the I2c connection.
func (m *MCP23017Driver) Connection() gobot.Connection { return m.connection.(gobot.Connection) } func (m *MCP23017Driver) Connection() gobot.Connection { return m.connection.(gobot.Connection) }
// Halt stops the driver.
func (m *MCP23017Driver) Halt() (err []error) { return } func (m *MCP23017Driver) Halt() (err []error) { return }
// Start writes initialization bytes and reads. // Start writes the device configuration.
func (m *MCP23017Driver) Start() (errs []error) { func (m *MCP23017Driver) Start() (errs []error) {
if err := m.connection.I2cStart(m.mcp23017Address); err != nil { if err := m.connection.I2cStart(m.mcp23017Address); err != nil {
return []error{err} return []error{err}
} }
// Set IOCON register with the given configuration. // Set IOCON register with MCP23017 configuration.
selectedPort := m.getPort("A") // IOCON address is the same for Port A or B. ioconReg := m.getPort("A").IOCON // IOCON address is the same for Port A or B.
var ioval uint8 ioconVal := m.conf.GetUint8Value()
buf := bytes.NewReader(m.conf.getBytes()) if err := m.connection.I2cWrite(m.mcp23017Address, []uint8{ioconReg, ioconVal}); err != nil {
err := binary.Read(buf, binary.LittleEndian, &ioval)
if err != nil {
return []error{err}
}
if err := m.connection.I2cWrite(m.mcp23017Address, []byte{selectedPort.IOCON, ioval}); err != nil {
return []error{err} return []error{err}
} }
return return
} }
// WriteGPIO writes a value to a gpio pin (0-7) and a // WriteGPIO writes a value to a gpio pin (0-7) and a port (A or B).
// port (A or B). func (m *MCP23017Driver) WriteGPIO(pin uint8, val uint8, portStr string) (err error) {
func (m *MCP23017Driver) WriteGPIO(pin float64, val float64, portStr string) (err error) {
selectedPort := m.getPort(portStr) selectedPort := m.getPort(portStr)
// Set IODIR register bit for given pin to an output. // Set IODIR register bit for given pin to an output.
if err := m.write(selectedPort.IODIR, uint8(pin), 0); err != nil { if err := m.write(selectedPort.IODIR, uint8(pin), 0); err != nil {
@ -159,19 +157,19 @@ func (m *MCP23017Driver) WriteGPIO(pin float64, val float64, portStr string) (er
// ReadGPIO reads a value from a given gpio pin (0-7) and a // ReadGPIO reads a value from a given gpio pin (0-7) and a
// port (A or B). // port (A or B).
func (m *MCP23017Driver) ReadGPIO(pin float64, portStr string) (val bool, err error) { func (m *MCP23017Driver) ReadGPIO(pin uint8, portStr string) (val uint8, err error) {
selectedPort := m.getPort(portStr) selectedPort := m.getPort(portStr)
gpio, err := m.read(selectedPort.GPIO) val, err = m.read(selectedPort.GPIO)
if err != nil { if err != nil {
return false, err return val, err
} }
return ((1 << uint8(pin) & gpio) != 0), nil return (1 << uint8(pin) & val), nil
} }
// SetPullUp sets the pull up state of a given pin based on the value: // SetPullUp sets the pull up state of a given pin based on the value:
// val = 1 pull up enabled. // val = 1 pull up enabled.
// val = 0 pull up disabled. // val = 0 pull up disabled.
func (m *MCP23017Driver) SetPullUp(pin uint8, val byte, portStr string) error { func (m *MCP23017Driver) SetPullUp(pin uint8, val uint8, portStr string) error {
selectedPort := m.getPort(portStr) selectedPort := m.getPort(portStr)
if err := m.write(selectedPort.GPPU, pin, val); err != nil { if err := m.write(selectedPort.GPPU, pin, val); err != nil {
return err return err
@ -182,7 +180,7 @@ func (m *MCP23017Driver) SetPullUp(pin uint8, val byte, portStr string) error {
// SetGPIOPolarity will change a given pin's polarity based on the value: // SetGPIOPolarity will change a given pin's polarity based on the value:
// val = 1 opposite logic state of the input pin. // val = 1 opposite logic state of the input pin.
// val = 0 same logic state of the input pin. // val = 0 same logic state of the input pin.
func (m *MCP23017Driver) SetGPIOPolarity(pin uint8, val byte, portStr string) (err error) { func (m *MCP23017Driver) SetGPIOPolarity(pin uint8, val uint8, portStr string) (err error) {
selectedPort := m.getPort(portStr) selectedPort := m.getPort(portStr)
if err := m.write(selectedPort.IPOL, pin, val); err != nil { if err := m.write(selectedPort.IPOL, pin, val); err != nil {
return err return err
@ -192,7 +190,7 @@ func (m *MCP23017Driver) SetGPIOPolarity(pin uint8, val byte, portStr string) (e
// write gets the value of the passed in register, and then overwrites // write gets the value of the passed in register, and then overwrites
// the bit specified by the pin, with the given value. // the bit specified by the pin, with the given value.
func (m *MCP23017Driver) write(reg byte, pin uint8, val byte) (err error) { func (m *MCP23017Driver) write(reg uint8, pin uint8, val uint8) (err error) {
var ioval uint8 var ioval uint8
iodir, err := m.read(reg) iodir, err := m.read(reg)
if err != nil { if err != nil {
@ -203,23 +201,33 @@ func (m *MCP23017Driver) write(reg byte, pin uint8, val byte) (err error) {
} else if val == 1 { } else if val == 1 {
ioval = setBit(iodir, uint8(pin)) ioval = setBit(iodir, uint8(pin))
} }
if err = m.connection.I2cWrite(m.mcp23017Address, []byte{reg, ioval}); err != nil { if debug {
log.Printf("Writing: MCP address: 0x%X, register: 0x%X\t, value: 0x%X\n", m.mcp23017Address, reg, ioval)
}
if err = m.connection.I2cWrite(m.mcp23017Address, []uint8{reg, ioval}); err != nil {
return err return err
} }
return nil return nil
} }
// Read returns the values in the given register. // read get the data from a given register. The I2cRead does not read a specific
func (m *MCP23017Driver) read(reg byte) (val uint8, err error) { // register from the device, rather it will read n bytes starting at the base
bytesToRead := int(reg) // device address. To read a specific register, read register + 1 bytes, and then index
v, err := m.connection.I2cRead(m.mcp23017Address, bytesToRead+1) // the result with the given register to get the value.
func (m *MCP23017Driver) read(reg uint8) (val uint8, err error) {
register := int(reg)
bytesToRead := register + 1
v, err := m.connection.I2cRead(m.mcp23017Address, bytesToRead)
if err != nil { if err != nil {
return val, err return val, err
} }
if Debug { if len(v) != bytesToRead {
log.Printf("Register addr:0x%X val: 0x%X\n", reg, v[bytesToRead]) return val, fmt.Errorf("Read was unable to get %d bytes for register: 0x%X\n", bytesToRead, reg)
} }
return v[bytesToRead], nil if debug {
log.Printf("Reading: MCP address: 0x%X, register:0x%X\t,value: 0x%X\n", m.mcp23017Address, reg, v[register])
}
return v[register], nil
} }
// getPort return the port (A or B) given a string and the bank. // getPort return the port (A or B) given a string and the bank.

View File

@ -10,24 +10,70 @@ import (
"github.com/hybridgroup/gobot/gobottest" "github.com/hybridgroup/gobot/gobottest"
) )
type i2cMcpTestAdaptor struct {
name string
i2cMcpReadImpl func(int, int) ([]byte, error)
i2cMcpWriteImpl func() error
i2cMcpStartImpl func() error
}
func (t *i2cMcpTestAdaptor) I2cStart(int) (err error) {
return t.i2cMcpStartImpl()
}
func (t *i2cMcpTestAdaptor) I2cRead(address int, numBytes int) (data []byte, err error) {
return t.i2cMcpReadImpl(address, numBytes)
}
func (t *i2cMcpTestAdaptor) I2cWrite(int, []byte) (err error) {
return t.i2cMcpWriteImpl()
}
func (t *i2cMcpTestAdaptor) Name() string { return t.name }
func (t *i2cMcpTestAdaptor) Connect() (errs []error) { return }
func (t *i2cMcpTestAdaptor) Finalize() (errs []error) { return }
func newMcpI2cTestAdaptor(name string) *i2cMcpTestAdaptor {
return &i2cMcpTestAdaptor{
name: name,
i2cMcpReadImpl: func(address int, numBytes int) ([]byte, error) {
return []byte{}, nil
},
i2cMcpWriteImpl: func() error {
return nil
},
i2cMcpStartImpl: func() error {
return nil
},
}
}
var pinValPort = map[string]interface{}{
"pin": uint8(7),
"val": uint8(0),
"port": "A",
}
var pinPort = map[string]interface{}{
"pin": uint8(7),
"port": "A",
}
func initTestMCP23017Driver(b uint8) (driver *MCP23017Driver) { func initTestMCP23017Driver(b uint8) (driver *MCP23017Driver) {
driver, _ = initTestMCP23017DriverWithStubbedAdaptor(b) driver, _ = initTestMCP23017DriverWithStubbedAdaptor(b)
return return
} }
func initTestMCP23017DriverWithStubbedAdaptor(b uint8) (*MCP23017Driver, *i2cTestAdaptor) { func initTestMCP23017DriverWithStubbedAdaptor(b uint8) (*MCP23017Driver, *i2cMcpTestAdaptor) {
adaptor := newI2cTestAdaptor("adaptor") adaptor := newMcpI2cTestAdaptor("adaptor")
return NewMCP23017Driver(adaptor, "bot", MCP23017Config{Bank: b}, 0x20), adaptor return NewMCP23017Driver(adaptor, "bot", MCP23017Config{Bank: b}, 0x20), adaptor
} }
func TestNewMCP23017Driver(t *testing.T) { func TestNewMCP23017Driver(t *testing.T) {
var bm interface{} = NewMCP23017Driver(newI2cTestAdaptor("adaptor"), "bot", MCP23017Config{}, 0x20) var bm interface{} = NewMCP23017Driver(newMcpI2cTestAdaptor("adaptor"), "bot", MCP23017Config{}, 0x20)
_, ok := bm.(*MCP23017Driver) _, ok := bm.(*MCP23017Driver)
if !ok { if !ok {
t.Errorf("NewMCP23017Driver() should have returned a *MCP23017Driver") t.Errorf("NewMCP23017Driver() should have returned a *MCP23017Driver")
} }
b := NewMCP23017Driver(newI2cTestAdaptor("adaptor"), "bot", MCP23017Config{}, 0x20) b := NewMCP23017Driver(newMcpI2cTestAdaptor("adaptor"), "bot", MCP23017Config{}, 0x20)
gobottest.Assert(t, b.Name(), "bot") gobottest.Assert(t, b.Name(), "bot")
gobottest.Assert(t, b.Connection().Name(), "adaptor") gobottest.Assert(t, b.Connection().Name(), "adaptor")
} }
@ -37,13 +83,13 @@ func TestMCP23017DriverStart(t *testing.T) {
gobottest.Assert(t, len(mcp.Start()), 0) gobottest.Assert(t, len(mcp.Start()), 0)
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return errors.New("write error") return errors.New("write error")
} }
err := mcp.Start() err := mcp.Start()
gobottest.Assert(t, err[0], errors.New("write error")) gobottest.Assert(t, err[0], errors.New("write error"))
adaptor.i2cStartImpl = func() error { adaptor.i2cMcpStartImpl = func() error {
return errors.New("start error") return errors.New("start error")
} }
err = mcp.Start() err = mcp.Start()
@ -52,56 +98,101 @@ func TestMCP23017DriverStart(t *testing.T) {
func TestMCP23017DriverHalt(t *testing.T) { func TestMCP23017DriverHalt(t *testing.T) {
mcp := initTestMCP23017Driver(0) mcp := initTestMCP23017Driver(0)
gobottest.Assert(t, len(mcp.Halt()), 0) gobottest.Assert(t, len(mcp.Halt()), 0)
} }
func TestMCP23017DriverCommandsWriteGPIO(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
}
result := mcp.Command("WriteGPIO")(pinValPort)
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestMCP23017DriverCommandsReadGPIO(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return make([]byte, b), nil
}
result := mcp.Command("ReadGPIO")(pinPort)
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestMCP23017DriverWriteGPIO(t *testing.T) { func TestMCP23017DriverWriteGPIO(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return nil return nil
} }
err := mcp.WriteGPIO(7, 0, "A") err := mcp.WriteGPIO(7, 0, "A")
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
}
// write error func TestMCP23017DriverCommandsWriteGPIOErrIODIR(t *testing.T) {
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return errors.New("write error") return errors.New("write error")
} }
err = mcp.WriteGPIO(7, 0, "A") err := mcp.WriteGPIO(7, 0, "A")
gobottest.Assert(t, err, errors.New("write error"))
}
func TestMCP23017DriverCommandsWriteGPIOErrOLAT(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return make([]byte, b), nil
}
numCalls := 1
adaptor.i2cMcpWriteImpl = func() error {
if numCalls == 2 {
return errors.New("write error")
}
numCalls++
return nil
}
err := mcp.WriteGPIO(7, 0, "A")
gobottest.Assert(t, err, errors.New("write error")) gobottest.Assert(t, err, errors.New("write error"))
} }
func TestMCP23017DriverReadGPIO(t *testing.T) { func TestMCP23017DriverReadGPIO(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
val, _ := mcp.ReadGPIO(7, "A") val, _ := mcp.ReadGPIO(7, "A")
gobottest.Assert(t, val, true) gobottest.Assert(t, val, uint8(0))
// read error // read error
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return nil, errors.New("read error") return make([]byte, b), errors.New("read error")
} }
_, err := mcp.ReadGPIO(7, "A") _, err := mcp.ReadGPIO(7, "A")
gobottest.Assert(t, err, errors.New("read error")) gobottest.Assert(t, err, errors.New("read error"))
// empty value from read
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return make([]byte, b), errors.New("Read came back with no data")
}
_, err = mcp.ReadGPIO(7, "A")
gobottest.Assert(t, err, errors.New("Read came back with no data"))
} }
func TestMCP23017DriverSetPullUp(t *testing.T) { func TestMCP23017DriverSetPullUp(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return nil return nil
} }
err := mcp.SetPullUp(7, 0, "A") err := mcp.SetPullUp(7, 0, "A")
@ -109,10 +200,10 @@ func TestMCP23017DriverSetPullUp(t *testing.T) {
// write error // write error
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return errors.New("write error") return errors.New("write error")
} }
err = mcp.SetPullUp(7, 0, "A") err = mcp.SetPullUp(7, 0, "A")
@ -121,10 +212,10 @@ func TestMCP23017DriverSetPullUp(t *testing.T) {
func TestMCP23017DriverSetGPIOPolarity(t *testing.T) { func TestMCP23017DriverSetGPIOPolarity(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return nil return nil
} }
err := mcp.SetGPIOPolarity(7, 0, "A") err := mcp.SetGPIOPolarity(7, 0, "A")
@ -132,10 +223,10 @@ func TestMCP23017DriverSetGPIOPolarity(t *testing.T) {
// write error // write error
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return errors.New("write error") return errors.New("write error")
} }
err = mcp.SetGPIOPolarity(7, 0, "A") err = mcp.SetGPIOPolarity(7, 0, "A")
@ -147,10 +238,10 @@ func TestMCP23017DriverWrite(t *testing.T) {
// clear bit // clear bit
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
port := mcp.getPort("A") port := mcp.getPort("A")
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return nil return nil
} }
err := mcp.write(port.IODIR, uint8(7), 0) err := mcp.write(port.IODIR, uint8(7), 0)
@ -159,10 +250,10 @@ func TestMCP23017DriverWrite(t *testing.T) {
// set bit // set bit
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
port = mcp.getPort("B") port = mcp.getPort("B")
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return nil return nil
} }
err = mcp.write(port.IODIR, uint8(7), 1) err = mcp.write(port.IODIR, uint8(7), 1)
@ -170,10 +261,10 @@ func TestMCP23017DriverWrite(t *testing.T) {
// write error // write error
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}, nil return make([]byte, b), nil
} }
adaptor.i2cWriteImpl = func() error { adaptor.i2cMcpWriteImpl = func() error {
return errors.New("write error") return errors.New("write error")
} }
err = mcp.write(port.IODIR, uint8(7), 0) err = mcp.write(port.IODIR, uint8(7), 0)
@ -181,58 +272,78 @@ func TestMCP23017DriverWrite(t *testing.T) {
// read error // read error
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{}, errors.New("read error") return make([]byte, b), errors.New("read error")
} }
err = mcp.write(port.IODIR, uint8(7), 0) err = mcp.write(port.IODIR, uint8(7), 0)
gobottest.Assert(t, err, errors.New("read error")) gobottest.Assert(t, err, errors.New("read error"))
//debug
debug = true
log.SetOutput(ioutil.Discard)
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return make([]byte, b), nil
}
adaptor.i2cMcpWriteImpl = func() error {
return nil
}
err = mcp.write(port.IODIR, uint8(7), 1)
gobottest.Assert(t, err, nil)
debug = false
log.SetOutput(os.Stdout)
} }
func TestMCP23017DriverReadPort(t *testing.T) { func TestMCP23017DriverReadPort(t *testing.T) {
// read // read
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
port := mcp.getPort("A") port := mcp.getPort("A")
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
adaptor.i2cReadImpl = func() ([]byte, error) { return []byte{255}, nil
return []byte{255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, nil
} }
val, _ := mcp.read(port.IODIR) val, _ := mcp.read(port.IODIR)
gobottest.Assert(t, val, uint8(255)) gobottest.Assert(t, val, uint8(255))
// read error // read error
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{}, errors.New("read error") return make([]byte, b), errors.New("read error")
} }
val, err := mcp.read(port.IODIR) val, err := mcp.read(port.IODIR)
gobottest.Assert(t, val, uint8(0)) gobottest.Assert(t, val, uint8(0))
gobottest.Assert(t, err, errors.New("read error")) gobottest.Assert(t, err, errors.New("read error"))
// read
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
port = mcp.getPort("A")
adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{}, nil
}
_, err = mcp.read(port.IODIR)
gobottest.Assert(t, err, errors.New("Read was unable to get 1 bytes for register: 0x0\n"))
// debug // debug
Debug = true debug = true
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0) mcp, adaptor = initTestMCP23017DriverWithStubbedAdaptor(0)
port = mcp.getPort("A") port = mcp.getPort("A")
adaptor.i2cReadImpl = func() ([]byte, error) { adaptor.i2cMcpReadImpl = func(a int, b int) ([]byte, error) {
return []byte{255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, nil return []byte{255}, nil
} }
val, _ = mcp.read(port.IODIR) val, _ = mcp.read(port.IODIR)
gobottest.Assert(t, val, uint8(255)) gobottest.Assert(t, val, uint8(255))
Debug = false debug = false
log.SetOutput(os.Stdout) log.SetOutput(os.Stdout)
} }
func TestMCP23017DriverGetPort(t *testing.T) { func TestMCP23017DriverGetPort(t *testing.T) {
// port a // port A
mcp := initTestMCP23017Driver(0) mcp := initTestMCP23017Driver(0)
expectedPort := getBank(0).PortA expectedPort := getBank(0).PortA
actualPort := mcp.getPort("A") actualPort := mcp.getPort("A")
gobottest.Assert(t, expectedPort, actualPort) gobottest.Assert(t, expectedPort, actualPort)
// port b // port B
mcp = initTestMCP23017Driver(0) mcp = initTestMCP23017Driver(0)
expectedPort = getBank(0).PortB expectedPort = getBank(0).PortB
actualPort = mcp.getPort("B") actualPort = mcp.getPort("B")
@ -244,7 +355,7 @@ func TestMCP23017DriverGetPort(t *testing.T) {
actualPort = mcp.getPort("") actualPort = mcp.getPort("")
gobottest.Assert(t, expectedPort, actualPort) gobottest.Assert(t, expectedPort, actualPort)
// port a bank 1 // port A bank 1
mcp = initTestMCP23017Driver(1) mcp = initTestMCP23017Driver(1)
expectedPort = getBank(1).PortA expectedPort = getBank(1).PortA
actualPort = mcp.getPort("") actualPort = mcp.getPort("")
@ -258,7 +369,7 @@ func TestSetBit(t *testing.T) {
} }
func TestClearBit(t *testing.T) { func TestClearBit(t *testing.T) {
var expectedVal uint8 = 0 var expectedVal uint8
actualVal := clearBit(128, 7) actualVal := clearBit(128, 7)
gobottest.Assert(t, expectedVal, actualVal) gobottest.Assert(t, expectedVal, actualVal)
} }