#!/usr/bin/env python """MCP230XX, python module for the MCP230008 and MCP230017 GPIO expander chips created September 9, 2017 last modified October 8, 2017""" """ Copyright 2017 Owain Martin This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ import time, sys, smbus class MCP230XX: def __init__(self, chip, i2cAddress, regScheme = '16bit'): self.i2cAddress = i2cAddress self.bus =smbus.SMBus(1) self.chip = chip if self.chip == 'MCP23008': self.bank = '8bit' else: self.bank = regScheme self.callBackFuncts=[] for i in range(0,17): self.callBackFuncts.append(['empty','emtpy']) return def single_access_read(self, reg=0x00): """single_access_read, function to read a single data register of the MCP230xx gpio expander""" dataTransfer=self.bus.read_byte_data(self.i2cAddress,reg) return dataTransfer def single_access_write(self, reg=0x00, regValue=0x0): """single_access_write, function to write to a single data register of the MCP230xx gpio expander""" self.bus.write_byte_data(self.i2cAddress, reg, regValue) return def register_bit_select(self, pin, reg8A, reg16A, reg8B, reg16B): """register_bit_select, function to return the proper register and bit position to use for a particular GPIO and GPIO function""" # need to add way of throwing an error if pin is outside # of 0-15 range if pin >= 0 and pin < 8: bit = pin if self.bank == '16bit': reg = reg16A else: # self.bank = '8bit' reg = reg8A elif pin >= 8 and pin <16: bit = pin - 8 if self.bank == '16bit': reg = reg16B else: # self.bank = '8bit' reg = reg8B return reg, bit def interrupt_options(self, outputType = 'activehigh', bankControl = 'separate'): """interrupt_options, function to set the options for the 2 interrupt pins""" if self.bank == '16bit': reg = 0x0A else: # self.bank = '8bit' reg = 0x05 if outputType == 'activelow': odrBit = 0 intpolBit = 0 elif outputType == 'opendrain': odrBit = 1 intpolBit = 0 else: # outputType = 'activehigh' odrBit = 0 intpolBit = 1 if bankControl == 'both': mirrorBit = 1 else: # bankControl = 'separate' mirrorBit = 0 regValue = self.single_access_read(reg) regValue = regValue & 0b10111001 regValue = regValue | (mirrorBit<<6) + (odrBit<<2) + (intpolBit<<1) self.single_access_write(reg, regValue) return def set_register_addressing(self, regScheme='8bit'): """set_register_addressing, function to change how the registers are mapped. For an MCP23008, bank should always equal '8bit'. This sets bit 7 of the IOCON register""" if self.bank == '16bit': reg = 0x0A else: # self.bank = '8bit' reg = 0x05 if regScheme == '16bit': bankBit = 0 self.bank = '16bit' else: # regScheme = '8bit' bankBit = 1 self.bank = '8bit' regValue = self.single_access_read(reg) regValue = regValue & 0b01111111 regValue = regValue | (bankBit<<7) self.single_access_write(reg, regValue) return def set_mode(self, pin, mode, pullUp='disable'): """set_mode, function to set up a GPIO pin to either an input or output. The input pullup resistor can also be enabled. This sets the appropriate bits in the IODIRA/B and GPPUA/B registers""" # GPIO direction set up section reg, bit = self.register_bit_select(pin ,reg8A=0x00,reg16A=0x00, reg8B=0x10, reg16B=0x01) regValue = self.single_access_read(reg) if mode == 'output': mask = 0b11111111 & ~(1<> bit return value def input_at_interrupt(self, pin): """input_at_interrupt, function to get the current level of a GPIO input pin when an interrupt has occurred by reading the appropriate bit in the INTCAPA/B registers""" reg, bit = self.register_bit_select(pin ,reg8A=0x08,reg16A=0X10, reg8B=0X18, reg16B=0x11) regValue = self.single_access_read(reg) mask = 0x00 | (1<> bit return value def add_interrupt(self, pin, callbackFunctLow='empty', callbackFunctHigh='empty'): """add_interrupt, function to set up the interrupt options for a specific GPIO including callback functions to be executed when an interrupt occurs""" # set bit in GPINTENA/B registers reg, bit = self.register_bit_select(pin ,reg8A=0x02,reg16A=0X04, reg8B=0X12, reg16B=0x05) regValue = self.single_access_read(reg) mask = 0x00 | (1<