3939class Adafruit_MCP230XX (object ):
4040 OUTPUT = 0
4141 INPUT = 1
42-
42+
4343 def __init__ (self , address , num_gpios ):
4444 assert num_gpios >= 0 and num_gpios <= 16 , "Number of GPIOs must be between 0 and 16"
4545 self .i2c = Adafruit_I2C (address = address )
@@ -58,7 +58,7 @@ def __init__(self, address, num_gpios):
5858 self .direction |= self .i2c .readU8 (MCP23017_IODIRB ) << 8
5959 self .i2c .write8 (MCP23017_GPPUA , 0x00 )
6060 self .i2c .write8 (MCP23017_GPPUB , 0x00 )
61-
61+
6262 def _changebit (self , bitmap , bit , value ):
6363 assert value == 1 or value == 0 , "Value is %s must be 1 or 0" % value
6464 if value == 0 :
@@ -82,12 +82,12 @@ def pullup(self, pin, value):
8282 if self .num_gpios <= 16 :
8383 lvalue = self ._readandchangepin (MCP23017_GPPUA , pin , value )
8484 if (pin < 8 ):
85- return
85+ return
8686 else :
8787 return self ._readandchangepin (MCP23017_GPPUB , pin - 8 , value ) << 8
8888
8989 # Set pin to either input or output mode
90- def config (self , pin , mode ):
90+ def config (self , pin , mode ):
9191 if self .num_gpios <= 8 :
9292 self .direction = self ._readandchangepin (MCP23017_IODIRA , pin , mode )
9393 if self .num_gpios <= 16 :
@@ -113,7 +113,7 @@ def output(self, pin, value):
113113
114114 self .outputvalue = self ._readandchangepin (MCP23017_IODIRA , pin , value , self .outputvalue )
115115 return self .outputvalue
116-
116+
117117 def input (self , pin ):
118118 assert pin >= 0 and pin < self .num_gpios , "Pin number %s is invalid, only 0-%s are valid" % (pin , self .num_gpios )
119119 assert self .direction & (1 << pin ) != 0 , "Pin %s not set to input" % pin
@@ -124,35 +124,35 @@ def input(self, pin):
124124 value |= self .i2c .readU8 (MCP23017_GPIOB ) << 8
125125 return value & (1 << pin )
126126
127- def readU8 (self ):
128- result = self .i2c .readU8 (MCP23008_OLATA )
129- return (result )
127+ def readU8 (self ):
128+ result = self .i2c .readU8 (MCP23008_OLATA )
129+ return (result )
130130
131- def readS8 (self ):
132- result = self .i2c .readU8 (MCP23008_OLATA )
133- if (result > 127 ): result -= 256
134- return result
131+ def readS8 (self ):
132+ result = self .i2c .readU8 (MCP23008_OLATA )
133+ if (result > 127 ): result -= 256
134+ return result
135135
136- def readU16 (self ):
137- assert self .num_gpios >= 16 , "16bits required"
138- lo = self .i2c .readU8 (MCP23017_OLATA )
139- hi = self .i2c .readU8 (MCP23017_OLATB )
140- return ((hi << 8 ) | lo )
136+ def readU16 (self ):
137+ assert self .num_gpios >= 16 , "16bits required"
138+ lo = self .i2c .readU8 (MCP23017_OLATA )
139+ hi = self .i2c .readU8 (MCP23017_OLATB )
140+ return ((hi << 8 ) | lo )
141141
142- def readS16 (self ):
143- assert self .num_gpios >= 16 , "16bits required"
144- lo = self .i2c .readU8 (MCP23017_OLATA )
145- hi = self .i2c .readU8 (MCP23017_OLATB )
146- if (hi > 127 ): hi -= 256
147- return ((hi << 8 ) | lo )
142+ def readS16 (self ):
143+ assert self .num_gpios >= 16 , "16bits required"
144+ lo = self .i2c .readU8 (MCP23017_OLATA )
145+ hi = self .i2c .readU8 (MCP23017_OLATB )
146+ if (hi > 127 ): hi -= 256
147+ return ((hi << 8 ) | lo )
148148
149- def write8 (self , value ):
150- self .i2c .write8 (MCP23008_OLATA , value )
149+ def write8 (self , value ):
150+ self .i2c .write8 (MCP23008_OLATA , value )
151151
152- def write16 (self , value ):
153- assert self .num_gpios >= 16 , "16bits required"
154- self .i2c .write8 (MCP23017_OLATA , value & 0xFF )
155- self .i2c .write8 (MCP23017_OLATB , (value >> 8 ) & 0xFF )
152+ def write16 (self , value ):
153+ assert self .num_gpios >= 16 , "16bits required"
154+ self .i2c .write8 (MCP23017_OLATA , value & 0xFF )
155+ self .i2c .write8 (MCP23017_OLATB , (value >> 8 ) & 0xFF )
156156
157157# RPi.GPIO compatible interface for MCP23017 and MCP23008
158158
@@ -174,27 +174,27 @@ def output(self, pin, value):
174174 self .chip .output (pin , value )
175175 def pullup (self , pin , value ):
176176 self .chip .pullup (pin , value )
177-
177+
178178
179179if __name__ == '__main__' :
180180 # ***************************************************
181181 # Set num_gpios to 8 for MCP23008 or 16 for MCP23017!
182182 # ***************************************************
183183 mcp = Adafruit_MCP230XX (address = 0x20 , num_gpios = 8 ) # MCP23008
184184 # mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 16) # MCP23017
185-
185+
186186 # Set pins 0, 1 and 2 to output (you can set pins 0..15 this way)
187187 mcp .config (0 , mcp .OUTPUT )
188188 mcp .config (1 , mcp .OUTPUT )
189189 mcp .config (2 , mcp .OUTPUT )
190-
190+
191191 # Set pin 3 to input with the pullup resistor enabled
192192 mcp .config (3 , mcp .INPUT )
193193 mcp .pullup (3 , 1 )
194-
194+
195195 # Read input pin and display the results
196196 print "Pin 3 = %d" % (mcp .input (3 ) >> 3 )
197-
197+
198198 # Python speed test on output 0 toggling at max speed
199199 print "Starting blinky on pin 0 (CTRL+C to quit)"
200200 while (True ):
0 commit comments