Skip to content

Commit

Permalink
Merge pull request #35 from shadowclaw/master
Browse files Browse the repository at this point in the history
Adding support for defining input pin polarity
  • Loading branch information
FoamyGuy authored Oct 20, 2020
2 parents 3f04abb + 9121fc3 commit 34b9c1d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
21 changes: 19 additions & 2 deletions adafruit_mcp230xx/digital_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ def switch_to_output(self, value=False, **kwargs):
self.direction = digitalio.Direction.OUTPUT
self.value = value

def switch_to_input(self, pull=None, **kwargs):
def switch_to_input(self, pull=None, invert_polarity=False, **kwargs):
"""Switch the pin state to a digital input with the provided starting
pull-up resistor state (optional, no pull-up by default). Note that
pull-up resistor state (optional, no pull-up by default) and input polarity. Note that
pull-down resistors are NOT supported!
"""
self.direction = digitalio.Direction.INPUT
self.pull = pull
self.invert_polarity = invert_polarity

# pylint: enable=unused-argument

Expand Down Expand Up @@ -131,3 +132,19 @@ def pull(self, val):
except AttributeError as error:
# MCP23016 doesn't have a `gppu` register.
raise ValueError("Pull-up/pull-down resistors not supported.") from error

@property
def invert_polarity(self):
"""The polarity of the pin, either True for an Inverted or
False for an normal.
"""
if _get_bit(self._mcp.ipol, self._pin):
return True
return False

@invert_polarity.setter
def invert_polarity(self, val):
if val:
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
else:
self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin)
39 changes: 39 additions & 0 deletions adafruit_mcp230xx/mcp23017.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#
# SPDX-License-Identifier: MIT

# pylint: disable=too-many-public-methods

"""
`mcp23017`
====================================================
Expand All @@ -23,6 +25,7 @@
_MCP23017_IODIRA = const(0x00)
_MCP23017_IODIRB = const(0x01)
_MCP23017_IPOLA = const(0x02)
_MCP23017_IPOLB = const(0x03)
_MCP23017_GPINTENA = const(0x04)
_MCP23017_DEFVALA = const(0x06)
_MCP23017_INTCONA = const(0x08)
Expand Down Expand Up @@ -162,6 +165,42 @@ def get_pin(self, pin):
assert 0 <= pin <= 15
return DigitalInOut(pin, self)

@property
def ipol(self):
"""The raw IPOL output register. Each bit represents the
polarity value of the associated pin (0 = normal, 1 = inverted), assuming that
pin has been configured as an input previously.
"""
return self._read_u16le(_MCP23017_IPOLA)

@ipol.setter
def ipol(self, val):
self._write_u16le(_MCP23017_IPOLA, val)

@property
def ipola(self):
"""The raw IPOL A output register. Each bit represents the
polarity value of the associated pin (0 = normal, 1 = inverted), assuming that
pin has been configured as an input previously.
"""
return self._read_u8(_MCP23017_IPOLA)

@ipola.setter
def ipola(self, val):
self._write_u8(_MCP23017_IPOLA, val)

@property
def ipolb(self):
"""The raw IPOL B output register. Each bit represents the
polarity value of the associated pin (0 = normal, 1 = inverted), assuming that
pin has been configured as an input previously.
"""
return self._read_u8(_MCP23017_IPOLB)

@ipolb.setter
def ipolb(self, val):
self._write_u8(_MCP23017_IPOLB, val)

@property
def interrupt_configuration(self):
"""The raw INTCON interrupt control register. The INTCON register
Expand Down

0 comments on commit 34b9c1d

Please sign in to comment.