Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 1545337

Browse files
author
K. Townsend
committedDec 20, 2012
Added 8/16-bit helper methods courtesy andig!
1 parent 80eeed2 commit 1545337

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed
 

‎Adafruit_MCP230xx/Adafruit_MCP230xx.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,35 @@ def input(self, pin):
126126
value |= temp
127127
return value & (1 << pin)
128128

129-
129+
def readU8(self):
130+
result = self.i2c.readU8(MCP23017_OLATA)
131+
return(result)
132+
133+
def readS8(self):
134+
result = self.i2c.readU8(MCP23017_OLATA)
135+
if (result > 127): result -= 256
136+
return result
137+
138+
def readU16(self):
139+
assert self.num_gpios >= 16, "16bits required"
140+
lo = self.i2c.readU8(MCP23017_OLATA)
141+
hi = self.i2c.readU8(MCP23017_OLATB)
142+
return((hi << 8) | lo)
143+
144+
def readS16(self):
145+
assert self.num_gpios >= 16, "16bits required"
146+
lo = self.i2c.readU8(MCP23017_OLATA)
147+
hi = self.i2c.readU8(MCP23017_OLATB)
148+
if (hi > 127): hi -= 256
149+
return((hi << 8) | lo)
150+
151+
def write8(self, value):
152+
self.i2c.write8(MCP23017_OLATA, value)
153+
154+
def write16(self, value):
155+
assert self.num_gpios >= 16, "16bits required"
156+
self.i2c.write8(MCP23017_OLATA, value & 0xFF)
157+
self.i2c.write8(MCP23017_OLATB, (value >> 8) & 0xFF)
130158

131159
# RPi.GPIO compatible interface for MCP23017 and MCP23008
132160

0 commit comments

Comments
 (0)