Advertisement
Guest User

GlaDOS with direct PS4 pad control (RaspberryPi, pca9685)

a guest
Apr 7th, 2020
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.37 KB | None | 0 0
  1. # Yep I didn't even deleted the comments form pygame example and left camelCase.
  2.  
  3. import pygame
  4. import math
  5.  
  6. # imports for i2c board control
  7. from board import SCL, SDA
  8. import busio
  9. from adafruit_pca9685 import PCA9685
  10. from adafruit_motor import servo
  11. import time
  12.  
  13. i2c = busio.I2C(SCL, SDA)
  14.  
  15. pca = PCA9685(i2c)
  16. pca.frequency = 100
  17.  
  18. servo0 = servo.Servo(pca.channels[0], min_pulse=1000, max_pulse=2000)
  19. servo1 = servo.Servo(pca.channels[1], min_pulse=1000, max_pulse=2000)
  20. servo2 = servo.Servo(pca.channels[2], min_pulse=1000, max_pulse=2000)
  21. servo3 = servo.Servo(pca.channels[4], min_pulse=1000, max_pulse=2000)
  22.  
  23. print("i2c done")
  24.  
  25. def valmap(value, istart=-1.0, istop=1.0, ostart=0, ostop=180):
  26.     return ostart + (ostop - ostart) * ((value - istart) / (istop - istart))
  27.  
  28. def truncate_float(val, d_place):
  29.     tens = 10**d_place
  30.     if val > 0:
  31.         return math.floor(val*tens)/tens
  32.     else:
  33.         return math.ceil(val*tens)/tens
  34.  
  35. axax=[0,0,0,0,0,0]
  36.  
  37.  
  38. # Define some colors.
  39. BLACK = pygame.Color('black')
  40. WHITE = pygame.Color('white')
  41.  
  42. # This is a simple class that will help us print to the screen.
  43. # It has nothing to do with the joysticks, just outputting the
  44. # information.
  45. class TextPrint(object):
  46.     def __init__(self):
  47.         self.reset()
  48.         self.font = pygame.font.Font(None, 20)
  49.  
  50.     def tprint(self, screen, textString):
  51.         textBitmap = self.font.render(textString, True, BLACK)
  52.         screen.blit(textBitmap, (self.x, self.y))
  53.         self.y += self.line_height
  54.  
  55.     def reset(self):
  56.         self.x = 10
  57.         self.y = 10
  58.         self.line_height = 15
  59.  
  60.     def indent(self):
  61.         self.x += 10
  62.  
  63.     def unindent(self):
  64.         self.x -= 10
  65.  
  66.  
  67. pygame.init()
  68.  
  69. # Set the width and height of the screen (width, height).
  70. screen = pygame.display.set_mode((500, 700))
  71.  
  72. pygame.display.set_caption("My Game")
  73.  
  74. # Loop until the user clicks the close button.
  75. done = False
  76.  
  77. # Used to manage how fast the screen updates.
  78. clock = pygame.time.Clock()
  79.  
  80. # Initialize the joysticks.
  81. pygame.joystick.init()
  82.  
  83. # Get ready to print.
  84. textPrint = TextPrint()
  85.  
  86. # -------- Main Program Loop -----------
  87. while not done:
  88.     #
  89.     # EVENT PROCESSING STEP
  90.     #
  91.     # Possible joystick actions: JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN,
  92.     # JOYBUTTONUP, JOYHATMOTION
  93.     for event in pygame.event.get(): # User did something.
  94.         if event.type == pygame.QUIT: # If user clicked close.
  95.             done = True # Flag that we are done so we exit this loop.
  96.         elif event.type == pygame.JOYBUTTONDOWN:
  97.             print("Joystick button pressed.")
  98.         elif event.type == pygame.JOYBUTTONUP:
  99.             print("Joystick button released.")
  100.  
  101.     #
  102.     # DRAWING STEP
  103.     #
  104.     # First, clear the screen to white. Don't put other drawing commands
  105.     # above this, or they will be erased with this command.
  106.     screen.fill(WHITE)
  107.     textPrint.reset()
  108.  
  109.     # Get count of joysticks.
  110.     joystick_count = pygame.joystick.get_count()
  111.  
  112.     textPrint.tprint(screen, "Number of joysticks: {}".format(joystick_count))
  113.     textPrint.indent()
  114.  
  115.     # For each joystick:
  116.     for i in range(joystick_count):
  117.         joystick = pygame.joystick.Joystick(i)
  118.         joystick.init()
  119.  
  120.         textPrint.tprint(screen, "Joystick {}".format(i))
  121.         textPrint.indent()
  122.  
  123.         # Get the name from the OS for the controller/joystick.
  124.         name = joystick.get_name()
  125.  
  126.         textPrint.tprint(screen, "Joystick name: {}".format(name))
  127.  
  128.         # Usually axis run in pairs, up/down for one, and left/right for
  129.         # the other.
  130.         axes = joystick.get_numaxes()
  131.  
  132.         textPrint.tprint(screen, "Number of axes: {}".format(axes))
  133.         textPrint.indent()
  134.  
  135.         # here is where magic happens :P
  136.         used_axes = [0,1,3,4] #on rasberry PI
  137.         for i in used_axes:
  138.             axis = joystick.get_axis(i)
  139.             textPrint.tprint(screen, "Axis {} value: {:>6.3f} {}".format(i, axis, int(valmap(axis))))
  140.             axax[i] = int(valmap(axis))
  141.         textPrint.unindent()
  142.  
  143.         servo0.angle = axax[0]
  144.         servo1.angle = axax[1]
  145.         servo2.angle = axax[3]
  146.         servo3.angle = axax[4]
  147.  
  148.         buttons = joystick.get_numbuttons()
  149.         textPrint.tprint(screen, "Number of buttons: {}".format(buttons))
  150.         textPrint.indent()
  151.  
  152.         for i in range(buttons):
  153.             button = joystick.get_button(i)
  154.             textPrint.tprint(screen,
  155.                              "Button {:>2} value: {}".format(i, button))
  156.         textPrint.unindent()
  157.  
  158.         hats = joystick.get_numhats()
  159.         textPrint.tprint(screen, "Number of hats: {}".format(hats))
  160.         textPrint.indent()
  161.  
  162.         # Hat position. All or nothing for direction, not a float like
  163.         # get_axis(). Position is a tuple of int values (x, y).
  164.         for i in range(hats):
  165.             hat = joystick.get_hat(i)
  166.             textPrint.tprint(screen, "Hat {} value: {}".format(i, str(hat)))
  167.         textPrint.unindent()
  168.  
  169.         textPrint.unindent()
  170.         # DEBUG:
  171.  
  172.  
  173.     #
  174.     # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
  175.     #
  176.  
  177.     # Go ahead and update the screen with what we've drawn.
  178.     pygame.display.flip()
  179.  
  180.     # Limit to 20 frames per second.
  181.     clock.tick(200)
  182.  
  183. # Close the window and quit.
  184. # If you forget this line, the program will 'hang'
  185. # on exit if running from IDLE.
  186. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement