Skip to content

Commit 34b9c1d

Browse files
authored
Merge pull request #35 from shadowclaw/master
Adding support for defining input pin polarity
2 parents 3f04abb + 9121fc3 commit 34b9c1d

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

adafruit_mcp230xx/digital_inout.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ def switch_to_output(self, value=False, **kwargs):
6060
self.direction = digitalio.Direction.OUTPUT
6161
self.value = value
6262

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

7172
# pylint: enable=unused-argument
7273

@@ -131,3 +132,19 @@ def pull(self, val):
131132
except AttributeError as error:
132133
# MCP23016 doesn't have a `gppu` register.
133134
raise ValueError("Pull-up/pull-down resistors not supported.") from error
135+
136+
@property
137+
def invert_polarity(self):
138+
"""The polarity of the pin, either True for an Inverted or
139+
False for an normal.
140+
"""
141+
if _get_bit(self._mcp.ipol, self._pin):
142+
return True
143+
return False
144+
145+
@invert_polarity.setter
146+
def invert_polarity(self, val):
147+
if val:
148+
self._mcp.ipol = _enable_bit(self._mcp.ipol, self._pin)
149+
else:
150+
self._mcp.ipol = _clear_bit(self._mcp.ipol, self._pin)

adafruit_mcp230xx/mcp23017.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44
# SPDX-License-Identifier: MIT
55

6+
# pylint: disable=too-many-public-methods
7+
68
"""
79
`mcp23017`
810
====================================================
@@ -23,6 +25,7 @@
2325
_MCP23017_IODIRA = const(0x00)
2426
_MCP23017_IODIRB = const(0x01)
2527
_MCP23017_IPOLA = const(0x02)
28+
_MCP23017_IPOLB = const(0x03)
2629
_MCP23017_GPINTENA = const(0x04)
2730
_MCP23017_DEFVALA = const(0x06)
2831
_MCP23017_INTCONA = const(0x08)
@@ -162,6 +165,42 @@ def get_pin(self, pin):
162165
assert 0 <= pin <= 15
163166
return DigitalInOut(pin, self)
164167

168+
@property
169+
def ipol(self):
170+
"""The raw IPOL output register. Each bit represents the
171+
polarity value of the associated pin (0 = normal, 1 = inverted), assuming that
172+
pin has been configured as an input previously.
173+
"""
174+
return self._read_u16le(_MCP23017_IPOLA)
175+
176+
@ipol.setter
177+
def ipol(self, val):
178+
self._write_u16le(_MCP23017_IPOLA, val)
179+
180+
@property
181+
def ipola(self):
182+
"""The raw IPOL A output register. Each bit represents the
183+
polarity value of the associated pin (0 = normal, 1 = inverted), assuming that
184+
pin has been configured as an input previously.
185+
"""
186+
return self._read_u8(_MCP23017_IPOLA)
187+
188+
@ipola.setter
189+
def ipola(self, val):
190+
self._write_u8(_MCP23017_IPOLA, val)
191+
192+
@property
193+
def ipolb(self):
194+
"""The raw IPOL B output register. Each bit represents the
195+
polarity value of the associated pin (0 = normal, 1 = inverted), assuming that
196+
pin has been configured as an input previously.
197+
"""
198+
return self._read_u8(_MCP23017_IPOLB)
199+
200+
@ipolb.setter
201+
def ipolb(self, val):
202+
self._write_u8(_MCP23017_IPOLB, val)
203+
165204
@property
166205
def interrupt_configuration(self):
167206
"""The raw INTCON interrupt control register. The INTCON register

0 commit comments

Comments
 (0)