|
| 1 | +# Copyright (c) 2014 Adafruit Industries |
| 2 | +# Author: Tony DiCola |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +# THE SOFTWARE. |
| 21 | +import math |
| 22 | + |
| 23 | +import Adafruit_GPIO as GPIO |
| 24 | +import Adafruit_GPIO.I2C as I2C |
| 25 | + |
| 26 | + |
| 27 | +class MCP230xxBase(GPIO.BaseGPIO): |
| 28 | + """Base class to represent an MCP230xx series GPIO extender. Is compatible |
| 29 | + with the Adafruit_GPIO BaseGPIO class so it can be used as a custom GPIO |
| 30 | + class for interacting with device. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, address, busnum=I2C.get_default_bus()): |
| 34 | + """Initialize MCP230xx at specified I2C address and bus number. If bus |
| 35 | + is not specified it will default to the appropriate platform detected bus. |
| 36 | + """ |
| 37 | + self._i2c = I2C.Device(address, busnum) |
| 38 | + # Assume starting in ICON.BANK = 0 mode (sequential access). |
| 39 | + # Compute how many bytes are needed to store count of GPIO. |
| 40 | + self.gpio_bytes = int(math.ceil(self.NUM_GPIO/8.0)) |
| 41 | + # Buffer register values so they can be changed without reading. |
| 42 | + self.iodir = [0x00]*self.gpio_bytes # Default direction to all inputs. |
| 43 | + self.gppu = [0x00]*self.gpio_bytes # Default to pullups disabled. |
| 44 | + self.gpio = [0x00]*self.gpio_bytes |
| 45 | + # Write current direction and pullup buffer state. |
| 46 | + self.write_iodir() |
| 47 | + self.write_gppu() |
| 48 | + |
| 49 | + def _validate_pin(self, pin): |
| 50 | + # Raise an exception if pin is outside the range of allowed values. |
| 51 | + if pin < 0 or pin >= self.NUM_GPIO: |
| 52 | + raise ValueError('Invalid GPIO value, must be between 0 and {0}.'.format(self.NUM_GPIO)) |
| 53 | + |
| 54 | + def setup(self, pin, value): |
| 55 | + """Set the input or output mode for a specified pin. Mode should be |
| 56 | + either GPIO.OUT or GPIO.IN. |
| 57 | + """ |
| 58 | + self._validate_pin(pin) |
| 59 | + # Set bit to 1 for input or 0 for output. |
| 60 | + if value == GPIO.IN: |
| 61 | + self.iodir[pin/8] |= 1 << (pin%8) |
| 62 | + elif value == GPIO.OUT: |
| 63 | + self.iodir[pin/8] &= ~(1 << (pin%8)) |
| 64 | + else: |
| 65 | + raise ValueError('Unexpected value. Must be GPIO.IN or GPIO.OUT.') |
| 66 | + self.write_iodir() |
| 67 | + |
| 68 | + def output(self, pin, value): |
| 69 | + """Set the specified pin the provided high/low value. Value should be |
| 70 | + either GPIO.HIGH/GPIO.LOW or a boolean (True = high). |
| 71 | + """ |
| 72 | + self._validate_pin(pin) |
| 73 | + # Set bit on or off. |
| 74 | + if value: |
| 75 | + self.gpio[pin/8] |= 1 << (pin%8) |
| 76 | + else: |
| 77 | + self.gpio[pin/8] &= ~(1 << (pin%8)) |
| 78 | + # Write GPIO state. |
| 79 | + self.write_gpio() |
| 80 | + |
| 81 | + def output_pins(self, pins): |
| 82 | + """Set multiple pins high or low at once. Pins should be a dict of pin |
| 83 | + name to pin value (HIGH/True for 1, LOW/False for 0). All provided pins |
| 84 | + will be set to the given values. |
| 85 | + """ |
| 86 | + # Set each changed pin's bit. |
| 87 | + for pin, value in pins.iteritems(): |
| 88 | + if value: |
| 89 | + self.gpio[pin/8] |= 1 << (pin%8) |
| 90 | + else: |
| 91 | + self.gpio[pin/8] &= ~(1 << (pin%8)) |
| 92 | + # Write GPIO state. |
| 93 | + self.write_gpio() |
| 94 | + |
| 95 | + def input(self, pin): |
| 96 | + """Read the specified pin and return GPIO.HIGH/True if the pin is pulled |
| 97 | + high, or GPIO.LOW/False if pulled low. |
| 98 | + """ |
| 99 | + self._validate_pin(pin) |
| 100 | + # Get GPIO state. |
| 101 | + gpio = self._i2c.readList(self.GPIO, self.gpio_bytes) |
| 102 | + # Return True if pin's bit is set. |
| 103 | + return (gpio[pin/8] & 1 << (pin%8)) > 0 |
| 104 | + |
| 105 | + def pullup(self, pin, enabled): |
| 106 | + """Turn on the pull-up resistor for the specified pin if enabled is True, |
| 107 | + otherwise turn off the pull-up resistor. |
| 108 | + """ |
| 109 | + self._validate_pin(pin) |
| 110 | + if enabled: |
| 111 | + self.gppu[pin/8] |= 1 << (pin%8) |
| 112 | + else: |
| 113 | + self.gppu[pin/8] &= ~(1 << (pin%8)) |
| 114 | + self.write_gppu() |
| 115 | + |
| 116 | + def write_gpio(self, gpio=None): |
| 117 | + """Write the specified byte value to the GPIO registor. If no value |
| 118 | + specified the current buffered value will be written. |
| 119 | + """ |
| 120 | + if gpio is not None: |
| 121 | + self.gpio = gpio |
| 122 | + self._i2c.writeList(self.GPIO, self.gpio) |
| 123 | + |
| 124 | + def write_iodir(self, iodir=None): |
| 125 | + """Write the specified byte value to the IODIR registor. If no value |
| 126 | + specified the current buffered value will be written. |
| 127 | + """ |
| 128 | + if iodir is not None: |
| 129 | + self.iodir = iodir |
| 130 | + self._i2c.writeList(self.IODIR, self.iodir) |
| 131 | + |
| 132 | + def write_gppu(self, gppu=None): |
| 133 | + """Write the specified byte value to the GPPU registor. If no value |
| 134 | + specified the current buffered value will be written. |
| 135 | + """ |
| 136 | + if gppu is not None: |
| 137 | + self.gppu = gppu |
| 138 | + self._i2c.writeList(self.GPPU, self.gppu) |
| 139 | + |
| 140 | + |
| 141 | +class MCP23017(MCP230xxBase): |
| 142 | + """MCP23017-based GPIO class with 16 GPIO pins.""" |
| 143 | + # Define number of pins and registor addresses. |
| 144 | + NUM_GPIO = 16 |
| 145 | + IODIR = 0x00 |
| 146 | + GPIO = 0x12 |
| 147 | + GPPU = 0x0C |
| 148 | + |
| 149 | + def __init__(self, address=0x20, **kwargs): |
| 150 | + super(MCP23017, self).__init__(address, **kwargs) |
| 151 | + |
| 152 | + |
| 153 | +class MCP23008(MCP230xxBase): |
| 154 | + """MCP23008-based GPIO class with 8 GPIO pins.""" |
| 155 | + # Define number of pins and registor addresses. |
| 156 | + NUM_GPIO = 8 |
| 157 | + IODIR = 0x00 |
| 158 | + GPIO = 0x09 |
| 159 | + GPPU = 0x06 |
| 160 | + |
| 161 | + def __init__(self, address=0x20, **kwargs): |
| 162 | + super(MCP23008, self).__init__(address, **kwargs) |
0 commit comments