Skip to content

Commit

Permalink
i2c: additional refactoring MCP23017 driver to support new optional p…
Browse files Browse the repository at this point in the history
…arams

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 14, 2017
1 parent 7caad1d commit f9afd87
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
26 changes: 13 additions & 13 deletions drivers/i2c/mcp23017_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type MCP23017Driver struct {
connector Connector
connection Connection
Config
conf MCP23017Config
MCPConf MCP23017Config
gobot.Commander
gobot.Eventer
}
Expand All @@ -93,7 +93,7 @@ func NewMCP23017Driver(a Connector, options ...func(Config)) *MCP23017Driver {
name: gobot.DefaultName("MCP23017"),
connector: a,
Config: NewConfig(),
conf: MCP23017Config{},
MCPConf: MCP23017Config{},
Commander: gobot.NewCommander(),
Eventer: gobot.NewEventer(),
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func (m *MCP23017Driver) Start() (err error) {
}
// Set IOCON register with MCP23017 configuration.
ioconReg := m.getPort("A").IOCON // IOCON address is the same for Port A or B.
ioconVal := m.conf.GetUint8Value()
ioconVal := m.MCPConf.GetUint8Value()
if _, err := m.connection.Write([]uint8{ioconReg, ioconVal}); err != nil {
return err
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func WithMCP23017Bank(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Bank = val
d.MCPConf.Bank = val
} else {
panic("Trying to set Bank for non-MCP23017Driver")
}
Expand All @@ -220,7 +220,7 @@ func WithMCP23017Mirror(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Mirror = val
d.MCPConf.Mirror = val
} else {
panic("Trying to set Mirror for non-MCP23017Driver")
}
Expand All @@ -232,7 +232,7 @@ func WithMCP23017Seqop(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Seqop = val
d.MCPConf.Seqop = val
} else {
panic("Trying to set Seqop for non-MCP23017Driver")
}
Expand All @@ -244,7 +244,7 @@ func WithMCP23017Disslw(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Disslw = val
d.MCPConf.Disslw = val
} else {
panic("Trying to set Disslw for non-MCP23017Driver")
}
Expand All @@ -256,7 +256,7 @@ func WithMCP23017Haen(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Haen = val
d.MCPConf.Haen = val
} else {
panic("Trying to set Haen for non-MCP23017Driver")
}
Expand All @@ -268,7 +268,7 @@ func WithMCP23017Odr(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Odr = val
d.MCPConf.Odr = val
} else {
panic("Trying to set Odr for non-MCP23017Driver")
}
Expand All @@ -280,7 +280,7 @@ func WithMCP23017Intpol(val uint8) func(Config) {
return func(c Config) {
d, ok := c.(*MCP23017Driver)
if ok {
d.conf.Intpol = val
d.MCPConf.Intpol = val
} else {
panic("Trying to set Intpol for non-MCP23017Driver")
}
Expand Down Expand Up @@ -336,11 +336,11 @@ func (m *MCP23017Driver) getPort(portStr string) (selectedPort port) {
portStr = strings.ToUpper(portStr)
switch {
case portStr == "A":
return getBank(m.conf.Bank).PortA
return getBank(m.MCPConf.Bank).PortA
case portStr == "B":
return getBank(m.conf.Bank).PortB
return getBank(m.MCPConf.Bank).PortB
default:
return getBank(m.conf.Bank).PortA
return getBank(m.MCPConf.Bank).PortA
}
}

Expand Down
35 changes: 35 additions & 0 deletions drivers/i2c/mcp23017_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ func TestNewMCP23017Driver(t *testing.T) {
gobottest.Refute(t, b.Connection(), nil)
}

func TestNewMCP23017DriverBank(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Bank(1))
gobottest.Assert(t, b.MCPConf.Bank, uint8(1))
}

func TestNewMCP23017DriverMirror(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Mirror(1))
gobottest.Assert(t, b.MCPConf.Mirror, uint8(1))
}

func TestNewMCP23017DriverSeqop(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Seqop(1))
gobottest.Assert(t, b.MCPConf.Seqop, uint8(1))
}

func TestNewMCP23017DriverDisslw(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Disslw(1))
gobottest.Assert(t, b.MCPConf.Disslw, uint8(1))
}

func TestNewMCP23017DriverHaen(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Haen(1))
gobottest.Assert(t, b.MCPConf.Haen, uint8(1))
}

func TestNewMCP23017DriverOdr(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Odr(1))
gobottest.Assert(t, b.MCPConf.Odr, uint8(1))
}

func TestNewMCP23017DriverIntpol(t *testing.T) {
b := NewMCP23017Driver(newI2cTestAdaptor(), WithMCP23017Intpol(1))
gobottest.Assert(t, b.MCPConf.Intpol, uint8(1))
}

func TestMCP23017DriverStart(t *testing.T) {
mcp, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)

Expand Down

0 comments on commit f9afd87

Please sign in to comment.