Skip to content

Instantly share code, notes, and snippets.

@haimiko
Forked from anonymous/mcpinput.py
Created January 20, 2013 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haimiko/4581263 to your computer and use it in GitHub Desktop.
Save haimiko/4581263 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import smbus
import sys
import time
bus = smbus.SMBus(1)
address = 0x20
bus.write_byte_data(address,0x00,0x00) # set A7-A0 as output pins
bus.write_byte_data(address,0x01,0x07) # set B0-B3 as input pins , B4-7 as output 0000111
bankA=0x12
bankB=0x13
def main():
a = 0
b = 0
inp = 0
delay = 0.05
#Turnoff all outputs in bank A
a = 255 # 11111111
bus.write_byte_data(address,bankA,a)
#Reset all buttons
#you need to bring them up using a 10KOhm resistor connected to 3.3v
#You can't just do the following using code and expect it to be stable
#If you do the following without the 3.3v it will be unpredictable.
b = 255 # 11111111
bus.write_byte_data(address,bankB,b)
while True:
# read input from bank B and store to "inp" variable
inp = bus.read_byte_data(address,bankB)
#Check if button B0 is pressed, light relay A6
if inp == 254: # 11111110 (B0)
#print "B7"
bus.write_byte_data(address,bankA,191) #light relay A6 10111111
#Check if button B1 is pressed, light relay A7
if inp == 253: # 11111101 (B1)
#print "B6"
bus.write_byte_data(address,bankA,127) #light relay A7 01111111
#Check if button B2 is pressed, light relay A6
if inp == 251: # 11111011 (B2)
#print "B5"
bus.write_byte_data(address,bankA,255) #turn off all relay A7 11111111
time.sleep(delay)
print str(inp)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment