/ adafruit_mcp230xx / mcp23008.py
mcp23008.py
1 # SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries 2 # SPDX-FileCopyrightText: 2019 Carter Nelson 3 # 4 # SPDX-License-Identifier: MIT 5 6 """ 7 `mcp23008` 8 ==================================================== 9 10 CircuitPython module for the MCP23008 I2C I/O extenders. 11 12 * Author(s): Tony DiCola 13 """ 14 15 from micropython import const 16 from .mcp230xx import MCP230XX 17 from .digital_inout import DigitalInOut 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 _MCP23008_ADDRESS = const(0x20) 24 _MCP23008_IODIR = const(0x00) 25 _MCP23008_IPOL = const(0x01) 26 _MCP23008_GPINTEN = const(0x02) 27 _MCP23008_DEFVAL = const(0x03) 28 _MCP23008_INTCON = const(0x04) 29 _MCP23008_IOCON = const(0x05) 30 _MCP23008_GPPU = const(0x06) 31 _MCP23008_INTF = const(0x07) 32 _MCP23008_INTCAP = const(0x08) 33 _MCP23008_GPIO = const(0x09) 34 35 36 class MCP23008(MCP230XX): 37 """Supports MCP23008 instance on specified I2C bus and optionally 38 at the specified I2C address. 39 """ 40 41 def __init__(self, i2c, address=_MCP23008_ADDRESS): 42 super().__init__(i2c, address) 43 44 # Reset to all inputs with no pull-ups and no inverted polarity. 45 self.iodir = 0xFF 46 self.gppu = 0x00 47 self._write_u8(_MCP23008_IPOL, 0x00) 48 49 @property 50 def gpio(self): 51 """The raw GPIO output register. Each bit represents the 52 output value of the associated pin (0 = low, 1 = high), assuming that 53 pin has been configured as an output previously. 54 """ 55 return self._read_u8(_MCP23008_GPIO) 56 57 @gpio.setter 58 def gpio(self, val): 59 self._write_u8(_MCP23008_GPIO, val) 60 61 @property 62 def iodir(self): 63 """The raw IODIR direction register. Each bit represents 64 direction of a pin, either 1 for an input or 0 for an output mode. 65 """ 66 return self._read_u8(_MCP23008_IODIR) 67 68 @iodir.setter 69 def iodir(self, val): 70 self._write_u8(_MCP23008_IODIR, val) 71 72 @property 73 def gppu(self): 74 """The raw GPPU pull-up register. Each bit represents 75 if a pull-up is enabled on the specified pin (1 = pull-up enabled, 76 0 = pull-up disabled). Note pull-down resistors are NOT supported! 77 """ 78 return self._read_u8(_MCP23008_GPPU) 79 80 @gppu.setter 81 def gppu(self, val): 82 self._write_u8(_MCP23008_GPPU, val) 83 84 def get_pin(self, pin): 85 """Convenience function to create an instance of the DigitalInOut class 86 pointing at the specified pin of this MCP23008 device. 87 """ 88 assert 0 <= pin <= 7 89 return DigitalInOut(pin, self)