-
Notifications
You must be signed in to change notification settings - Fork 10
/
mcp23017.py
executable file
·88 lines (72 loc) · 2.17 KB
/
mcp23017.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/python
# A simple Python command line tool to control an MCP23017 I2C IO Expander
# By Nathan Chantrell http://nathan.chantrell.net
# GNU GPL V3
import smbus
import sys
import getopt
bus = smbus.SMBus(0) # For revision 1 Raspberry Pi, change to bus = smbus.SMBus(1) for revision 2.
address = 0x20 # I2C address of MCP23017
bus.write_byte_data(0x20,0x00,0x00) # Set all of bank A to outputs
bus.write_byte_data(0x20,0x01,0x00) # Set all of bank B to outputs
# Let them know how it works
def usage():
print 'Usage: mcp23017.py -b <bank> -o <output> -s <high|low>'
# Handle the command line arguments
def main():
try:
opts, args = getopt.getopt(sys.argv[1:],"hb:o:s:",["bank=","output=","state="])
if not opts:
usage()
sys.exit(2)
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-b", "--bank"):
bank = arg
elif opt in ("-o", "--output"):
output = int(arg)
elif opt in ("-s", "--state"):
state = arg
# Set the correct register for the banks
if bank == "a" :
register = 0x12
elif bank == "b" :
register = 0x13
else:
print "Error! Bank must be a or b"
sys.exit()
# Read current values from the IO Expander
value = bus.read_byte_data(address,register)
# Shift the bits for the register value, checking if they are already set first
if state == "high":
if (value >> output) & 1 :
print "Output GP"+bank.upper()+str(output), "is already high."
sys.exit()
else:
value += (1 << output)
elif state == "low":
if (value >> output) & 1 :
value -= (1 << output)
else:
print "Output GP"+bank.upper()+str(output), "is already low."
sys.exit()
elif state == "read":
if (value >> output) & 1 :
print "high"
else:
print "low"
sys.exit()
else:
print "Error! state must be high or low"
sys.exit()
# Now write to the IO expander
bus.write_byte_data(address,register,value)
# Tell them what we did
print "Output GP"+bank.upper()+str(output), "changed to", state
if __name__ == "__main__":
main()