Skip to content

Commit d7a3153

Browse files
committed
Add support for the old, deprecated MCP23016 expander, too.
The main difference with the 017 is fewer features and no ability to r/w all 16 I/O atomically.
1 parent f72c4ee commit d7a3153

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

adafruit_mcp230xx/mcp23016.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2019 Carter Nelson
3+
# SPDX-FileCopyrightText: 2020 Facebook Inc.
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
"""
8+
`mcp23016`
9+
====================================================
10+
11+
CircuitPython module for the MCP23016 I2C I/O extenders.
12+
13+
* Author(s): Diego Elio Pettenò (based on mcp23017.py)
14+
"""
15+
16+
from micropython import const
17+
from .mcp230xx import MCP230XX
18+
19+
__version__ = "0.0.0-auto.0"
20+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git"
21+
22+
# pylint: disable=bad-whitespace
23+
_MCP23016_ADDRESS = const(0x20)
24+
_MCP23016_GPIO0 = const(0x00)
25+
_MCP23016_GPIO1 = const(0x01)
26+
_MCP23016_OLAT0 = const(0x02)
27+
_MCP23016_OLAT1 = const(0x03)
28+
_MCP23016_IPOL0 = const(0x04)
29+
_MCP23016_IPOL1 = const(0x05)
30+
_MCP23016_IODIR0 = const(0x06)
31+
_MCP23016_IODIR1 = const(0x07)
32+
_MCP23016_INTCAP0 = const(0x08)
33+
_MCP23016_INTCAP1 = const(0x09)
34+
_MCP23016_IOCON0 = const(0x0A)
35+
_MCP23016_IOCON1 = const(0x0B)
36+
37+
38+
class MCP23016(MCP230XX):
39+
"""Supports MCP23016 instance on specified I2C bus and optionally
40+
at the specified I2C address.
41+
"""
42+
43+
def __init__(self, i2c, address=_MCP23016_ADDRESS):
44+
super().__init__(i2c, address)
45+
46+
# Reset to all inputs and no inverted polarity.
47+
self.iodir0 = 0xFF
48+
self.iodir1 = 0xFF
49+
self._write_u8(_MCP23016_IPOL0, 0x00)
50+
self._write_u8(_MCP23016_IPOL1, 0x00)
51+
52+
@property
53+
def iodir0(self):
54+
"""The raw IODIR0 direction register. Each bit represents
55+
direction of a pin, either 1 for an input or 0 for an output mode.
56+
"""
57+
return self._read_u8(_MCP23016_IODIR0)
58+
59+
@iodir0.setter
60+
def iodir0(self, val):
61+
self._write_u8(_MCP23016_IODIR0, val)
62+
63+
@property
64+
def iodir1(self):
65+
"""The raw IODIR0 direction register. Each bit represents
66+
direction of a pin, either 1 for an input or 0 for an output mode.
67+
"""
68+
return self._read_u8(_MCP23016_IODIR1)
69+
70+
@iodir1.setter
71+
def iodir1(self, val):
72+
self._write_u8(_MCP23016_IODIR1, val)
73+
74+
@property
75+
def gpio0(self):
76+
"""The raw GPIO 0 output register. Each bit represents the
77+
output value of the associated pin (0 = low, 1 = high), assuming that
78+
pin has been configured as an output previously.
79+
"""
80+
return self._read_u8(_MCP23016_GPIO0)
81+
82+
@gpio0.setter
83+
def gpio0(self, val):
84+
self._write_u8(_MCP23016_GPIO0, val)
85+
86+
@property
87+
def gpio1(self):
88+
"""The raw GPIO 1 output register. Each bit represents the
89+
output value of the associated pin (0 = low, 1 = high), assuming that
90+
pin has been configured as an output previously.
91+
"""
92+
return self._read_u8(_MCP23016_GPIO1)
93+
94+
@gpio1.setter
95+
def gpio1(self, val):
96+
self._write_u8(_MCP23016_GPIO1, val)
97+
98+
def clear_int0(self):
99+
"""Clears port 0 interrupts."""
100+
self._read_u8(_MCP23016_INTCAP0)
101+
102+
def clear_int1(self):
103+
"""Clears port 1 interrupts."""
104+
self._read_u8(_MCP23016_INTCAP1)

0 commit comments

Comments
 (0)