Created
March 31, 2017 06:36
-
-
Save vizigr0u/5308bef88934967b687ca6921846f49b to your computer and use it in GitHub Desktop.
python script to test kitsch bent board buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
PortAKeys = { 0x04: 'B', 0x08 : 'A', 0x10: 'R2', 0x20: 'R1', 0x40: 'X', 0x80: 'Y'} | |
PortBKeys = { 0x01 : 'Up', 0x02 : 'L1', 0x04 : 'L2', 0x08: 'Left', 0x10: 'Right', 0x20: 'Down', 0x40: 'Select', 0x80: 'Start' } | |
import smbus | |
I2C_DEV = 0x20 #7 bit address (will be left shifted to add the read write bit) | |
PULLUP_A = 0x0c | |
PULLUP_B = 0x0d | |
PORT_A = 0x12 | |
PORT_B = 0x13 | |
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1) | |
def init_pullup(address): | |
bus.write_byte_data(I2C_DEV, address, 0xFF) | |
def read_port(address): | |
return bus.read_byte_data(I2C_DEV, address) | |
def getKeyNames(byte, dic): | |
keys = "" | |
for k,name in dic.iteritems(): | |
if k & byte == 0: | |
keys += name + ' ' | |
return keys | |
init_pullup(PULLUP_A) | |
init_pullup(PULLUP_B) | |
oldA = 0 | |
oldB = 0 | |
while True: | |
A, B = read_port(PORT_A), read_port(PORT_B) | |
if A != oldA or B != oldB: | |
keyNames = getKeyNames(A, PortAKeys) + " " + getKeyNames(B, PortBKeys) | |
print "A: {:02x} B: {:02x} {}".format(255 - A, 255 - B, keyNames) | |
oldA = A | |
oldB = B |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment