Skip to content

Commit

Permalink
fix write/read gpio
Browse files Browse the repository at this point in the history
  • Loading branch information
ulisesflynn committed Jun 30, 2018
1 parent 088f12b commit 88b78ff
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions drivers/i2c/mcp23017_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,8 @@ func (m *MCP23017Driver) WriteGPIO(pin uint8, val uint8, portStr string) (err er
if err != nil {
return err
}
// set or clear iodir value, a 1 sets the pin as an input, 0 as an output
var iodirVal uint8
if val == 1 {
iodirVal = clearBit(iodir, uint8(pin))
} else {
iodirVal = setBit(iodir, uint8(pin))
}
// set pin as output by clearing bit
iodirVal := clearBit(iodir, uint8(pin))
// write IODIR register bit
err = m.write(selectedPort.IODIR, uint8(pin), uint8(iodirVal))
if err != nil {
Expand Down Expand Up @@ -266,6 +261,18 @@ func (m *MCP23017Driver) WriteGPIO(pin uint8, val uint8, portStr string) (err er
// port (A or B).
func (m *MCP23017Driver) ReadGPIO(pin uint8, portStr string) (val uint8, err error) {
selectedPort := m.getPort(portStr)
// read current value of IODIR register
iodir, err := m.read(selectedPort.IODIR)
if err != nil {
return 0, err
}
// set pin as input by setting bit
iodirVal := setBit(iodir, uint8(pin))
// write IODIR register bit
err = m.write(selectedPort.IODIR, uint8(pin), uint8(iodirVal))
if err != nil {
return 0, err
}
val, err = m.read(selectedPort.GPIO)
if err != nil {
return val, err
Expand Down

0 comments on commit 88b78ff

Please sign in to comment.