24
24
import smbus
25
25
import time
26
26
27
-
28
- MCP23008_IODIRA = 0x00
29
-
30
27
MCP23017_IODIRA = 0x00
31
28
MCP23017_IODIRB = 0x01
32
29
MCP23017_GPIOA = 0x12
@@ -49,9 +46,9 @@ def __init__(self, address, num_gpios):
49
46
50
47
# set defaults
51
48
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 )
55
52
elif num_gpios > 8 and num_gpios <= 16 :
56
53
self .i2c .write8 (MCP23017_IODIRA , 0xFF ) # all inputs on port A
57
54
self .i2c .write8 (MCP23017_IODIRB , 0xFF ) # all inputs on port B
@@ -79,7 +76,7 @@ def _readandchangepin(self, port, pin, value, currvalue = None):
79
76
80
77
def pullup (self , pin , value ):
81
78
if self .num_gpios <= 8 :
82
- return self ._readandchangepin (MCP23008_GPPUA , pin , value )
79
+ return self ._readandchangepin (MCP23017_GPPUA , pin , value )
83
80
if self .num_gpios <= 16 :
84
81
if (pin < 8 ):
85
82
return self ._readandchangepin (MCP23017_GPPUA , pin , value )
@@ -89,7 +86,7 @@ def pullup(self, pin, value):
89
86
# Set pin to either input or output mode
90
87
def config (self , pin , mode ):
91
88
if self .num_gpios <= 8 :
92
- self .direction = self ._readandchangepin (MCP23008_IODIRA , pin , mode )
89
+ self .direction = self ._readandchangepin (MCP23017_IODIRA , pin , mode )
93
90
if self .num_gpios <= 16 :
94
91
if (pin < 8 ):
95
92
self .direction = self ._readandchangepin (MCP23017_IODIRA , pin , mode )
@@ -101,7 +98,7 @@ def config(self, pin, mode):
101
98
def output (self , pin , value ):
102
99
# assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
103
100
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 ))
105
102
if self .num_gpios <= 16 :
106
103
if (pin < 8 ):
107
104
self .outputvalue = self ._readandchangepin (MCP23017_GPIOA , pin , value , self .i2c .readU8 (MCP23017_OLATA ))
@@ -118,7 +115,7 @@ def input(self, pin):
118
115
assert pin >= 0 and pin < self .num_gpios , "Pin number %s is invalid, only 0-%s are valid" % (pin , self .num_gpios )
119
116
assert self .direction & (1 << pin ) != 0 , "Pin %s not set to input" % pin
120
117
if self .num_gpios <= 8 :
121
- value = self .i2c .readU8 (MCP23008_GPIOA )
118
+ value = self .i2c .readU8 (MCP23017_GPIOA )
122
119
elif self .num_gpios > 8 and self .num_gpios <= 16 :
123
120
value = self .i2c .readU16 (MCP23017_GPIOA )
124
121
temp = value >> 8
0 commit comments