Guest User

Untitled

a guest
May 14th, 2024
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. import pygame
  2. import time
  3. import matplotlib.pyplot as plt
  4.  
  5. version = "1.0.6"
  6. print()
  7. print("   _____ __  _      __      ___                __                     ")
  8. print("  / ___// /_(_)____/ /__   /   |  ____  ____ _/ /_  ______  ___  _____")
  9. print("  \__ \/ __/ / ___/ //_/  / /| | / __ \/ __ `/ / / / /_  / / _ \/ ___/")
  10. print(" ___/ / /_/ / /__/ ,<    / ___ |/ / / / /_/ / / /_/ / / /_/  __/ /    ")
  11. print("/____/\__/_/\___/_/|_|  /_/  |_/_/ /_/\__,_/_/\__, / /___/\___/_/     ")
  12. print("                                             /____/                   ")
  13. print(f"v.{version} by John Punch")
  14. print()
  15.  
  16. def main():
  17.     pygame.init()
  18.     pygame.joystick.init()
  19.  
  20.     joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
  21.     if not joysticks:
  22.         print("No controller found")
  23.         input("Press Enter to exit...")
  24.         return
  25.  
  26.     joystick = joysticks[0]
  27.     joystick.init()
  28.  
  29.     points = []
  30.     times = []
  31.     prev_x = None
  32.     start_time = None
  33.     last_significant_time = None
  34.     similarity_threshold = 0.02
  35.     zero_count = 0
  36.     timeout = 2.0
  37.  
  38.     print("---")
  39.     print("Step 1: Deflect the left stick of the gamepad fully to the right on the X-axis.")
  40.  
  41.     while True:
  42.         pygame.event.pump()
  43.         x = joystick.get_axis(0)
  44.         current_time = time.time()
  45.  
  46.         if x >= 0.9:
  47.             if not start_time:  # Start timing on initial deflection
  48.                 start_time = current_time
  49.             if not points:
  50.                 points.append(x)
  51.                 times.append(0)  # Initialize the first time point at 0
  52.                 print(f"Step 2: Now release the stick!")
  53.                 prev_x = x
  54.                 last_significant_time = current_time
  55.         elif points:
  56.             elapsed_time = current_time - start_time
  57.             if abs(x - prev_x) > similarity_threshold or not last_significant_time:
  58.                 if not last_significant_time:
  59.                     last_significant_time = current_time  # Update significant movement time
  60.                 points.append(x)
  61.                 times.append(elapsed_time)
  62.                 print(f"{x:.5f} at {elapsed_time:.5f} seconds")
  63.                 prev_x = x
  64.                 zero_count = 0
  65.             elif abs(x) < 0.05:
  66.                 zero_count += 1
  67.                 if zero_count >= 3:
  68.                     break
  69.             if last_significant_time and (current_time - last_significant_time > timeout):
  70.                 print("Timeout reached with no significant movement.")
  71.                 break
  72.  
  73.     if points:
  74.         test_duration = times[-1]
  75.         max_time = max(times)
  76.         inverted_times = [max_time - t for t in times]  # Inverting the time data
  77.         # Remove initial datapoint
  78.         inverted_times = inverted_times[1:]
  79.         points = points[1:]
  80.         print()
  81.         print("TEST RESULTS:")
  82.         print("-------------")
  83.         print(f"Number of points: {len(points)}")
  84.         print(f"Test duration: {test_duration:.5f} seconds")
  85.         with open("stick_release_data.txt", "w") as file:
  86.             data_str = '\n'.join(f"{point:.5f} at {time:.5f} seconds" for point, time in zip(points, inverted_times))
  87.             file.write(data_str)
  88.         print("\nData saved to stick_release_data.txt")
  89.        
  90.         # Plotting the data with inverted time
  91.         plt.style.use('dark_background')
  92.         plt.figure(figsize=(10, 6))
  93.         plt.plot(points, inverted_times, marker='o', linestyle='-', color='#80CBC4', markerfacecolor='#80CBC4', label='Joystick Movement')
  94.         plt.title('Joystick Movement Over Time (Inverted Time)', color='white')
  95.         plt.xlabel('Joystick Position', color='white')
  96.         plt.ylabel('Inverted Time (seconds)', color='white')
  97.         plt.axvline(0, color='#FF5252', linewidth=1, linestyle='--', label='Center Position')
  98.         plt.grid(True, color='#616161')
  99.         plt.gca().set_facecolor('#424242')  # Match axes background to figure background
  100.         plt.gcf().set_facecolor('#424242')  # Ensure the figure background matches the axes
  101.         plt.tight_layout()  # Apply tight layout to ensure all plot elements are neatly contained within the figure
  102.         plt.legend()
  103.         plt.show()
  104.  
  105.         print("-------------")
  106.         print("Support me: https://ko-fi.com/gamepadla")
  107.         print("I'm on Reddit: https://www.reddit.com/user/JohnnyPunch")
  108.         print("*To open a link, hold down the Ctrl key")
  109.         print()
  110.         input("Press Enter to exit...")
  111.     else:
  112.         print("No stick movement detected.")
  113.  
  114. if __name__ == "__main__":
  115.     main()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment