|
1 |
| -#!/usr/bin/python |
| 1 | +#!/usr/bin/python |
2 | 2 |
|
3 | 3 | # Copyright 2012 Daniel Berlin (with some changes by Adafruit Industries/Limor Fried)
|
4 | 4 | #
|
@@ -125,35 +125,35 @@ def input(self, pin):
|
125 | 125 | value |= temp
|
126 | 126 | return value & (1 << pin)
|
127 | 127 |
|
128 |
| - def readU8(self): |
129 |
| - result = self.i2c.readU8(MCP23008_OLATA) |
130 |
| - return(result) |
| 128 | + def readU8(self): |
| 129 | + result = self.i2c.readU8(MCP23008_OLATA) |
| 130 | + return(result) |
131 | 131 |
|
132 |
| - def readS8(self): |
133 |
| - result = self.i2c.readU8(MCP23008_OLATA) |
134 |
| - if (result > 127): result -= 256 |
135 |
| - return result |
| 132 | + def readS8(self): |
| 133 | + result = self.i2c.readU8(MCP23008_OLATA) |
| 134 | + if (result > 127): result -= 256 |
| 135 | + return result |
136 | 136 |
|
137 |
| - def readU16(self): |
138 |
| - assert self.num_gpios >= 16, "16bits required" |
139 |
| - lo = self.i2c.readU8(MCP23017_OLATA) |
140 |
| - hi = self.i2c.readU8(MCP23017_OLATB) |
141 |
| - return((hi << 8) | lo) |
| 137 | + def readU16(self): |
| 138 | + assert self.num_gpios >= 16, "16bits required" |
| 139 | + lo = self.i2c.readU8(MCP23017_OLATA) |
| 140 | + hi = self.i2c.readU8(MCP23017_OLATB) |
| 141 | + return((hi << 8) | lo) |
142 | 142 |
|
143 |
| - def readS16(self): |
144 |
| - assert self.num_gpios >= 16, "16bits required" |
145 |
| - lo = self.i2c.readU8(MCP23017_OLATA) |
146 |
| - hi = self.i2c.readU8(MCP23017_OLATB) |
147 |
| - if (hi > 127): hi -= 256 |
148 |
| - return((hi << 8) | lo) |
| 143 | + def readS16(self): |
| 144 | + assert self.num_gpios >= 16, "16bits required" |
| 145 | + lo = self.i2c.readU8(MCP23017_OLATA) |
| 146 | + hi = self.i2c.readU8(MCP23017_OLATB) |
| 147 | + if (hi > 127): hi -= 256 |
| 148 | + return((hi << 8) | lo) |
149 | 149 |
|
150 |
| - def write8(self, value): |
151 |
| - self.i2c.write8(MCP23008_OLATA, value) |
| 150 | + def write8(self, value): |
| 151 | + self.i2c.write8(MCP23008_OLATA, value) |
152 | 152 |
|
153 |
| - def write16(self, value): |
154 |
| - assert self.num_gpios >= 16, "16bits required" |
155 |
| - self.i2c.write8(MCP23017_OLATA, value & 0xFF) |
156 |
| - self.i2c.write8(MCP23017_OLATB, (value >> 8) & 0xFF) |
| 153 | + def write16(self, value): |
| 154 | + assert self.num_gpios >= 16, "16bits required" |
| 155 | + self.i2c.write8(MCP23017_OLATA, value & 0xFF) |
| 156 | + self.i2c.write8(MCP23017_OLATB, (value >> 8) & 0xFF) |
157 | 157 |
|
158 | 158 | # RPi.GPIO compatible interface for MCP23017 and MCP23008
|
159 | 159 |
|
@@ -181,21 +181,21 @@ def pullup(self, pin, value):
|
181 | 181 | mcp = Adafruit_MCP230XX(address = 0x20, num_gpios = 8)
|
182 | 182 |
|
183 | 183 | # ***************************************************
|
184 |
| - # Set num_gpios to 8 for MCP23008 or 16 for MCP23017! |
185 |
| - # If you have a new Pi you may also need to add: |
186 |
| - # busnum = 1 |
187 |
| - # *************************************************** |
188 |
| - |
189 |
| - # Set pins 0, 1 and 2 to output (you can set pins 0..15 this way) |
| 184 | + # Set num_gpios to 8 for MCP23008 or 16 for MCP23017! |
| 185 | + # If you have a new Pi you may also need to add: |
| 186 | + # busnum = 1 |
| 187 | + # *************************************************** |
| 188 | + |
| 189 | + # Set pins 0, 1 and 2 to output (you can set pins 0..15 this way) |
190 | 190 | mcp.config(0, mcp.OUTPUT)
|
191 | 191 | mcp.config(1, mcp.OUTPUT)
|
192 | 192 | mcp.config(2, mcp.OUTPUT)
|
193 |
| - |
194 |
| - # Set pin 3 to input with the pullup resistor enabled |
| 193 | + |
| 194 | + # Set pin 3 to input with the pullup resistor enabled |
195 | 195 | mcp.pullup(3, 1)
|
196 | 196 | # Read pin 3 and display the results
|
197 | 197 | print "%d: %x" % (3, mcp.input(3) >> 3)
|
198 |
| - |
| 198 | + |
199 | 199 | # Python speed test on output 0 toggling at max speed
|
200 | 200 | while (True):
|
201 | 201 | mcp.output(0, 1) # Pin 0 High
|
|
0 commit comments