39
39
class Adafruit_MCP230XX (object ):
40
40
OUTPUT = 0
41
41
INPUT = 1
42
-
42
+
43
43
def __init__ (self , address , num_gpios ):
44
44
assert num_gpios >= 0 and num_gpios <= 16 , "Number of GPIOs must be between 0 and 16"
45
45
self .i2c = Adafruit_I2C (address = address )
@@ -58,7 +58,7 @@ def __init__(self, address, num_gpios):
58
58
self .direction |= self .i2c .readU8 (MCP23017_IODIRB ) << 8
59
59
self .i2c .write8 (MCP23017_GPPUA , 0x00 )
60
60
self .i2c .write8 (MCP23017_GPPUB , 0x00 )
61
-
61
+
62
62
def _changebit (self , bitmap , bit , value ):
63
63
assert value == 1 or value == 0 , "Value is %s must be 1 or 0" % value
64
64
if value == 0 :
@@ -82,12 +82,12 @@ def pullup(self, pin, value):
82
82
if self .num_gpios <= 16 :
83
83
lvalue = self ._readandchangepin (MCP23017_GPPUA , pin , value )
84
84
if (pin < 8 ):
85
- return
85
+ return
86
86
else :
87
87
return self ._readandchangepin (MCP23017_GPPUB , pin - 8 , value ) << 8
88
88
89
89
# Set pin to either input or output mode
90
- def config (self , pin , mode ):
90
+ def config (self , pin , mode ):
91
91
if self .num_gpios <= 8 :
92
92
self .direction = self ._readandchangepin (MCP23017_IODIRA , pin , mode )
93
93
if self .num_gpios <= 16 :
@@ -113,7 +113,7 @@ def output(self, pin, value):
113
113
114
114
self .outputvalue = self ._readandchangepin (MCP23017_IODIRA , pin , value , self .outputvalue )
115
115
return self .outputvalue
116
-
116
+
117
117
def input (self , pin ):
118
118
assert pin >= 0 and pin < self .num_gpios , "Pin number %s is invalid, only 0-%s are valid" % (pin , self .num_gpios )
119
119
assert self .direction & (1 << pin ) != 0 , "Pin %s not set to input" % pin
@@ -124,35 +124,35 @@ def input(self, pin):
124
124
value |= self .i2c .readU8 (MCP23017_GPIOB ) << 8
125
125
return value & (1 << pin )
126
126
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 )
130
130
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
135
135
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 )
141
141
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 )
148
148
149
- def write8 (self , value ):
150
- self .i2c .write8 (MCP23008_OLATA , value )
149
+ def write8 (self , value ):
150
+ self .i2c .write8 (MCP23008_OLATA , value )
151
151
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 )
156
156
157
157
# RPi.GPIO compatible interface for MCP23017 and MCP23008
158
158
@@ -174,27 +174,27 @@ def output(self, pin, value):
174
174
self .chip .output (pin , value )
175
175
def pullup (self , pin , value ):
176
176
self .chip .pullup (pin , value )
177
-
177
+
178
178
179
179
if __name__ == '__main__' :
180
180
# ***************************************************
181
181
# Set num_gpios to 8 for MCP23008 or 16 for MCP23017!
182
182
# ***************************************************
183
183
mcp = Adafruit_MCP230XX (address = 0x20 , num_gpios = 8 ) # MCP23008
184
184
# mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 16) # MCP23017
185
-
185
+
186
186
# Set pins 0, 1 and 2 to output (you can set pins 0..15 this way)
187
187
mcp .config (0 , mcp .OUTPUT )
188
188
mcp .config (1 , mcp .OUTPUT )
189
189
mcp .config (2 , mcp .OUTPUT )
190
-
190
+
191
191
# Set pin 3 to input with the pullup resistor enabled
192
192
mcp .config (3 , mcp .INPUT )
193
193
mcp .pullup (3 , 1 )
194
-
194
+
195
195
# Read input pin and display the results
196
196
print "Pin 3 = %d" % (mcp .input (3 ) >> 3 )
197
-
197
+
198
198
# Python speed test on output 0 toggling at max speed
199
199
print "Starting blinky on pin 0 (CTRL+C to quit)"
200
200
while (True ):
0 commit comments