|
| 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 | +from .digital_inout import DigitalInOut |
| 19 | + |
| 20 | +__version__ = "0.0.0-auto.0" |
| 21 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx.git" |
| 22 | + |
| 23 | +# pylint: disable=bad-whitespace |
| 24 | +_MCP23016_ADDRESS = const(0x20) |
| 25 | +_MCP23016_GPIO0 = const(0x00) |
| 26 | +_MCP23016_GPIO1 = const(0x01) |
| 27 | +_MCP23016_OLAT0 = const(0x02) |
| 28 | +_MCP23016_OLAT1 = const(0x03) |
| 29 | +_MCP23016_IPOL0 = const(0x04) |
| 30 | +_MCP23016_IPOL1 = const(0x05) |
| 31 | +_MCP23016_IODIR0 = const(0x06) |
| 32 | +_MCP23016_IODIR1 = const(0x07) |
| 33 | +_MCP23016_INTCAP0 = const(0x08) |
| 34 | +_MCP23016_INTCAP1 = const(0x09) |
| 35 | +_MCP23016_IOCON0 = const(0x0A) |
| 36 | +_MCP23016_IOCON1 = const(0x0B) |
| 37 | + |
| 38 | + |
| 39 | +class MCP23016(MCP230XX): |
| 40 | + """Supports MCP23016 instance on specified I2C bus and optionally |
| 41 | + at the specified I2C address. |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__(self, i2c, address=_MCP23016_ADDRESS): |
| 45 | + super().__init__(i2c, address) |
| 46 | + |
| 47 | + # Reset to all inputs and no inverted polarity. |
| 48 | + self.iodir0 = 0xFF |
| 49 | + self.iodir1 = 0xFF |
| 50 | + self._write_u8(_MCP23016_IPOL0, 0x00) |
| 51 | + self._write_u8(_MCP23016_IPOL1, 0x00) |
| 52 | + |
| 53 | + @property |
| 54 | + def iodir0(self): |
| 55 | + """The raw IODIR0 direction register. Each bit represents |
| 56 | + direction of a pin, either 1 for an input or 0 for an output mode. |
| 57 | + """ |
| 58 | + return self._read_u8(_MCP23016_IODIR0) |
| 59 | + |
| 60 | + @iodir0.setter |
| 61 | + def iodir0(self, val): |
| 62 | + self._write_u8(_MCP23016_IODIR0, val) |
| 63 | + |
| 64 | + @property |
| 65 | + def iodir1(self): |
| 66 | + """The raw IODIR0 direction register. Each bit represents |
| 67 | + direction of a pin, either 1 for an input or 0 for an output mode. |
| 68 | + """ |
| 69 | + return self._read_u8(_MCP23016_IODIR1) |
| 70 | + |
| 71 | + @iodir1.setter |
| 72 | + def iodir1(self, val): |
| 73 | + self._write_u8(_MCP23016_IODIR1, val) |
| 74 | + |
| 75 | + @property |
| 76 | + def gpio0(self): |
| 77 | + """The raw GPIO 0 output register. Each bit represents the |
| 78 | + output value of the associated pin (0 = low, 1 = high), assuming that |
| 79 | + pin has been configured as an output previously. |
| 80 | + """ |
| 81 | + return self._read_u8(_MCP23016_GPIO0) |
| 82 | + |
| 83 | + @gpio0.setter |
| 84 | + def gpio0(self, val): |
| 85 | + self._write_u8(_MCP23016_GPIO0, val) |
| 86 | + |
| 87 | + @property |
| 88 | + def gpio1(self): |
| 89 | + """The raw GPIO 1 output register. Each bit represents the |
| 90 | + output value of the associated pin (0 = low, 1 = high), assuming that |
| 91 | + pin has been configured as an output previously. |
| 92 | + """ |
| 93 | + return self._read_u8(_MCP23016_GPIO1) |
| 94 | + |
| 95 | + @gpio1.setter |
| 96 | + def gpio1(self, val): |
| 97 | + self._write_u8(_MCP23016_GPIO1, val) |
| 98 | + |
| 99 | + def clear_int0(self): |
| 100 | + """Clears port 0 interrupts.""" |
| 101 | + self._read_u8(_MCP23016_INTCAP0) |
| 102 | + |
| 103 | + def clear_int1(self): |
| 104 | + """Clears port 1 interrupts.""" |
| 105 | + self._read_u8(_MCP23016_INTCAP1) |
0 commit comments