Advertisement
Miqueltozzz

Untitled

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