diff --git a/adafruit_mcp230xx/digital_inout.py b/adafruit_mcp230xx/digital_inout.py index 0877a31..25cbe71 100644 --- a/adafruit_mcp230xx/digital_inout.py +++ b/adafruit_mcp230xx/digital_inout.py @@ -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 @@ -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) diff --git a/adafruit_mcp230xx/mcp23017.py b/adafruit_mcp230xx/mcp23017.py index 13289d6..bd13567 100644 --- a/adafruit_mcp230xx/mcp23017.py +++ b/adafruit_mcp230xx/mcp23017.py @@ -3,6 +3,8 @@ # # SPDX-License-Identifier: MIT +# pylint: disable=too-many-public-methods + """ `mcp23017` ==================================================== @@ -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) @@ -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