Advertisement
Musical_Muze

8x8_single_dot_movement_with_controller

Sep 13th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import inputs
  4. from inputs import get_gamepad
  5.  
  6. GPIO.setmode(GPIO.BCM)
  7. GPIO.setwarnings(False)
  8.  
  9. #setup the pins
  10.  
  11. #OUT
  12. GPIO.setup(12, GPIO.OUT) #matrix pin 13, row 1
  13. GPIO.setup(22, GPIO.OUT) #matrix pin 3, row 2
  14. GPIO.setup(27, GPIO.OUT) #matrix pin 4, row 3
  15. GPIO.setup(25, GPIO.OUT) #matrix pin 10, row 4
  16. GPIO.setup(17, GPIO.OUT) #matrix pin 6, row 5
  17. GPIO.setup(24, GPIO.OUT) #matrix pin 11, row 6
  18. GPIO.setup(23, GPIO.OUT) #matrix pin 15, row 7
  19. GPIO.setup(18, GPIO.OUT) #matrix pin 16, row 8
  20.  
  21. #IN
  22. GPIO.setup(21, GPIO.OUT) #matrix pin 9, column 8
  23. GPIO.setup(20, GPIO.OUT) #matrix pin 14, column 7
  24. GPIO.setup(26, GPIO.OUT) #matrix pin 8, column 6
  25. GPIO.setup(16, GPIO.OUT) #matrix pin 12, column 5
  26. GPIO.setup(19, GPIO.OUT) #matrix pin 1, column 4
  27. GPIO.setup(13, GPIO.OUT) #matrix pin 7, column 3
  28. GPIO.setup(6, GPIO.OUT) #matrix pin 2, column 2
  29. GPIO.setup(5, GPIO.OUT) #matrix pin 5, column 1
  30.  
  31. #function to switch all pins off
  32. def off():
  33.     GPIO.output(12,0)
  34.     GPIO.output(22,0)
  35.     GPIO.output(27,0)
  36.     GPIO.output(25,0)
  37.     GPIO.output(17,0)
  38.     GPIO.output(24,0)
  39.     GPIO.output(23,0)
  40.     GPIO.output(18,0)
  41.     GPIO.output(21,0)
  42.     GPIO.output(20,0)
  43.     GPIO.output(26,0)
  44.     GPIO.output(16,0)
  45.     GPIO.output(19,0)
  46.     GPIO.output(13,0)
  47.     GPIO.output(6,0)
  48.     GPIO.output(5,0)
  49.  
  50. off()
  51.  
  52. #the GPIO pins for determining column, from left to right
  53. xCoord = [5,6,13,19,16,26,20,21]
  54. xInput = 0
  55.  
  56. #the GPIO pins for determining row, from top to bottom
  57. yCoord = [12,22,27,25,17,24,23,18]
  58. yInput = 0
  59.  
  60. #function for lighting up a single dot, given x and y input
  61. def coord(column, row):
  62.     for i in range(len(yCoord)):
  63.         if(i != row):
  64.             GPIO.output(yCoord[i],1)
  65.     GPIO.output(xCoord[column],1)
  66.     GPIO.output(yCoord[row],0)
  67.  
  68. #function to get input from the controller
  69. def getEventCode():
  70.     x = get_gamepad()
  71.     for i in x:
  72.         return i.code, i.state
  73.  
  74. #determine a starting point
  75. xInput = 4
  76. yInput = 4
  77. coord(xInput,yInput)
  78.  
  79. #input loop, will stop if the B button is pressed
  80. while(True):
  81.    
  82.     code = getEventCode()
  83.  
  84.     #determine which axis
  85.     if (code[0] == "ABS_HAT0Y"):    #if on the Y axis
  86.         #then determine which direction
  87.         if (code[1] == -1):     #if up
  88.             #reset the matrix
  89.             off()
  90.             #change the coord value by one in the determined direction
  91.             yInput = yInput - 1
  92.             #call the function to turn on the light with the new coord value(s)
  93.             coord(xInput,yInput)
  94.             #output the direction to the console for debugging purposes
  95.             print("up")
  96.         elif (code[1] == 1):    #if down
  97.             off()
  98.             yInput = yInput + 1
  99.             coord(xInput,yInput)
  100.             print("down")
  101.     elif (code [0] == "ABS_HAT0X"):    #if on the X axis
  102.         if (code[1] == -1):     #if left
  103.             off()
  104.             xInput = xInput - 1
  105.             coord(xInput,yInput)
  106.             print("left")
  107.         elif (code[1] == 1):    #if right
  108.             off()
  109.             xInput = xInput + 1
  110.             coord(xInput,yInput)
  111.             print("right")
  112.     #If the user wants to end the program, he must press the B button
  113.     elif(code[0] == "BTN_EAST"):
  114.         off()
  115.         break
  116.  
  117. #    time.sleep(0.25)
  118.  
  119. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement