2424import smbus
2525import time
2626
27-
28- MCP23008_IODIRA = 0x00
29-
3027MCP23017_IODIRA = 0x00
3128MCP23017_IODIRB = 0x01
3229MCP23017_GPIOA = 0x12
@@ -49,9 +46,9 @@ def __init__(self, address, num_gpios):
4946
5047 # set defaults
5148 if num_gpios <= 8 :
52- self .i2c .write8 (MCP23008_IODIRA , 0xFF ) # all inputs on port A
53- self .direction = self .i2c .readU8 (MCP23008_IODIRA )
54- self .i2c .write8 (MCP23008_GPPUA , 0x00 )
49+ self .i2c .write8 (MCP23017_IODIRA , 0xFF ) # all inputs on port A
50+ self .direction = self .i2c .readU8 (MCP23017_IODIRA )
51+ self .i2c .write8 (MCP23017_GPPUA , 0x00 )
5552 elif num_gpios > 8 and num_gpios <= 16 :
5653 self .i2c .write8 (MCP23017_IODIRA , 0xFF ) # all inputs on port A
5754 self .i2c .write8 (MCP23017_IODIRB , 0xFF ) # all inputs on port B
@@ -79,7 +76,7 @@ def _readandchangepin(self, port, pin, value, currvalue = None):
7976
8077 def pullup (self , pin , value ):
8178 if self .num_gpios <= 8 :
82- return self ._readandchangepin (MCP23008_GPPUA , pin , value )
79+ return self ._readandchangepin (MCP23017_GPPUA , pin , value )
8380 if self .num_gpios <= 16 :
8481 if (pin < 8 ):
8582 return self ._readandchangepin (MCP23017_GPPUA , pin , value )
@@ -89,7 +86,7 @@ def pullup(self, pin, value):
8986 # Set pin to either input or output mode
9087 def config (self , pin , mode ):
9188 if self .num_gpios <= 8 :
92- self .direction = self ._readandchangepin (MCP23008_IODIRA , pin , mode )
89+ self .direction = self ._readandchangepin (MCP23017_IODIRA , pin , mode )
9390 if self .num_gpios <= 16 :
9491 if (pin < 8 ):
9592 self .direction = self ._readandchangepin (MCP23017_IODIRA , pin , mode )
@@ -101,7 +98,7 @@ def config(self, pin, mode):
10198 def output (self , pin , value ):
10299 # assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
103100 if self .num_gpios <= 8 :
104- self .outputvalue = self ._readandchangepin (MCP23008_GPIOA , pin , value . self .i2c .readU8 (MCP23008_OLATA ))
101+ self .outputvalue = self ._readandchangepin (MCP23017_GPIOA , pin , value , self .i2c .readU8 (MCP23017_OLATA ))
105102 if self .num_gpios <= 16 :
106103 if (pin < 8 ):
107104 self .outputvalue = self ._readandchangepin (MCP23017_GPIOA , pin , value , self .i2c .readU8 (MCP23017_OLATA ))
@@ -118,7 +115,7 @@ def input(self, pin):
118115 assert pin >= 0 and pin < self .num_gpios , "Pin number %s is invalid, only 0-%s are valid" % (pin , self .num_gpios )
119116 assert self .direction & (1 << pin ) != 0 , "Pin %s not set to input" % pin
120117 if self .num_gpios <= 8 :
121- value = self .i2c .readU8 (MCP23008_GPIOA )
118+ value = self .i2c .readU8 (MCP23017_GPIOA )
122119 elif self .num_gpios > 8 and self .num_gpios <= 16 :
123120 value = self .i2c .readU16 (MCP23017_GPIOA )
124121 temp = value >> 8
0 commit comments