Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- import inputs
- from inputs import get_gamepad
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- #setup the pins
- #OUT
- GPIO.setup(12, GPIO.OUT) #matrix pin 13, row 1
- GPIO.setup(22, GPIO.OUT) #matrix pin 3, row 2
- GPIO.setup(27, GPIO.OUT) #matrix pin 4, row 3
- GPIO.setup(25, GPIO.OUT) #matrix pin 10, row 4
- GPIO.setup(17, GPIO.OUT) #matrix pin 6, row 5
- GPIO.setup(24, GPIO.OUT) #matrix pin 11, row 6
- GPIO.setup(23, GPIO.OUT) #matrix pin 15, row 7
- GPIO.setup(18, GPIO.OUT) #matrix pin 16, row 8
- #IN
- GPIO.setup(21, GPIO.OUT) #matrix pin 9, column 8
- GPIO.setup(20, GPIO.OUT) #matrix pin 14, column 7
- GPIO.setup(26, GPIO.OUT) #matrix pin 8, column 6
- GPIO.setup(16, GPIO.OUT) #matrix pin 12, column 5
- GPIO.setup(19, GPIO.OUT) #matrix pin 1, column 4
- GPIO.setup(13, GPIO.OUT) #matrix pin 7, column 3
- GPIO.setup(6, GPIO.OUT) #matrix pin 2, column 2
- GPIO.setup(5, GPIO.OUT) #matrix pin 5, column 1
- #function to switch all pins off
- def off():
- GPIO.output(12,0)
- GPIO.output(22,0)
- GPIO.output(27,0)
- GPIO.output(25,0)
- GPIO.output(17,0)
- GPIO.output(24,0)
- GPIO.output(23,0)
- GPIO.output(18,0)
- GPIO.output(21,0)
- GPIO.output(20,0)
- GPIO.output(26,0)
- GPIO.output(16,0)
- GPIO.output(19,0)
- GPIO.output(13,0)
- GPIO.output(6,0)
- GPIO.output(5,0)
- off()
- #the GPIO pins for determining column, from left to right
- xCoord = [5,6,13,19,16,26,20,21]
- xInput = 0
- #the GPIO pins for determining row, from top to bottom
- yCoord = [12,22,27,25,17,24,23,18]
- yInput = 0
- #function for lighting up a single dot, given x and y input
- def coord(column, row):
- for i in range(len(yCoord)):
- if(i != row):
- GPIO.output(yCoord[i],1)
- GPIO.output(xCoord[column],1)
- GPIO.output(yCoord[row],0)
- #function to get input from the controller
- def getEventCode():
- x = get_gamepad()
- for i in x:
- return i.code, i.state
- #determine a starting point
- xInput = 4
- yInput = 4
- coord(xInput,yInput)
- #input loop, will stop if the B button is pressed
- while(True):
- code = getEventCode()
- #determine which axis
- if (code[0] == "ABS_HAT0Y"): #if on the Y axis
- #then determine which direction
- if (code[1] == -1): #if up
- #reset the matrix
- off()
- #change the coord value by one in the determined direction
- yInput = yInput - 1
- #call the function to turn on the light with the new coord value(s)
- coord(xInput,yInput)
- #output the direction to the console for debugging purposes
- print("up")
- elif (code[1] == 1): #if down
- off()
- yInput = yInput + 1
- coord(xInput,yInput)
- print("down")
- elif (code [0] == "ABS_HAT0X"): #if on the X axis
- if (code[1] == -1): #if left
- off()
- xInput = xInput - 1
- coord(xInput,yInput)
- print("left")
- elif (code[1] == 1): #if right
- off()
- xInput = xInput + 1
- coord(xInput,yInput)
- print("right")
- #If the user wants to end the program, he must press the B button
- elif(code[0] == "BTN_EAST"):
- off()
- break
- # time.sleep(0.25)
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement