190 lines
6.3 KiB
Python
190 lines
6.3 KiB
Python
|
import pygame
|
||
|
import sys
|
||
|
import requests
|
||
|
import time
|
||
|
|
||
|
# Initialize Pygame
|
||
|
pygame.init()
|
||
|
|
||
|
# Set up the screen
|
||
|
screen_width = 600
|
||
|
screen_height = 400
|
||
|
screen = pygame.display.set_mode((screen_width, screen_height))
|
||
|
pygame.display.set_caption("Joystick Control")
|
||
|
|
||
|
# Set up colors
|
||
|
BLACK = (0, 0, 0)
|
||
|
WHITE = (255, 255, 255)
|
||
|
RED = (255, 0, 0)
|
||
|
GREEN = (0, 255, 0)
|
||
|
GRAY = (150, 150, 150)
|
||
|
|
||
|
# Get joystick
|
||
|
joystick_count = pygame.joystick.get_count()
|
||
|
if joystick_count == 0:
|
||
|
print("No joystick detected")
|
||
|
pygame.quit()
|
||
|
sys.exit()
|
||
|
joystick = pygame.joystick.Joystick(0)
|
||
|
joystick.init()
|
||
|
|
||
|
# Set up font
|
||
|
font = pygame.font.SysFont(None, 36)
|
||
|
|
||
|
# Set up clock
|
||
|
clock = pygame.time.Clock()
|
||
|
|
||
|
# Initial request status and toggle status
|
||
|
request_status = False
|
||
|
request_text = "Not Enabled"
|
||
|
toggle_status = False # False for disabled, True for enabled
|
||
|
|
||
|
# IP and port for the request
|
||
|
ip_address = "10.0.0.29"
|
||
|
port = "5000"
|
||
|
|
||
|
# Flag to track focus on IP and Port entry
|
||
|
ip_focused = False
|
||
|
port_focused = False
|
||
|
|
||
|
# Main loop
|
||
|
running = True
|
||
|
while running:
|
||
|
for event in pygame.event.get():
|
||
|
if event.type == pygame.QUIT:
|
||
|
running = False
|
||
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||
|
# Check if mouse click is inside IP entry box
|
||
|
if ip_rect.collidepoint(event.pos):
|
||
|
ip_focused = True
|
||
|
port_focused = False
|
||
|
# Check if mouse click is inside Port entry box
|
||
|
elif port_rect.collidepoint(event.pos):
|
||
|
port_focused = True
|
||
|
ip_focused = False
|
||
|
# Check if mouse click is inside toggle button
|
||
|
elif toggle_rect.collidepoint(event.pos):
|
||
|
toggle_status = not toggle_status
|
||
|
request_status = False # Reset request status when toggling
|
||
|
else:
|
||
|
ip_focused = False
|
||
|
port_focused = False
|
||
|
elif event.type == pygame.KEYDOWN:
|
||
|
# Capture user input for IP and Port
|
||
|
if ip_focused:
|
||
|
if event.key == pygame.K_BACKSPACE:
|
||
|
ip_address = ip_address[:-1]
|
||
|
if event.key == pygame.K_TAB:
|
||
|
port_focused = True
|
||
|
ip_focused = False
|
||
|
elif event.unicode.isdigit() or event.unicode == '.':
|
||
|
ip_address += event.unicode
|
||
|
elif port_focused:
|
||
|
if event.key == pygame.K_BACKSPACE:
|
||
|
port = port[:-1]
|
||
|
if event.key == pygame.K_TAB:
|
||
|
port_focused = False
|
||
|
ip_focused = True
|
||
|
elif event.unicode.isdigit():
|
||
|
port += event.unicode
|
||
|
|
||
|
# Clear the screen
|
||
|
screen.fill(BLACK)
|
||
|
|
||
|
axis_x = int(joystick.get_axis(0) * 100)
|
||
|
axis_y = int(joystick.get_axis(1) * 100)
|
||
|
|
||
|
if axis_x > -10 and axis_x < 10:
|
||
|
axis_x = 0
|
||
|
if axis_y > -10 and axis_y < 10:
|
||
|
axis_y = 0
|
||
|
|
||
|
# Draw IP entry box
|
||
|
ip_text = font.render("IP:", True, WHITE)
|
||
|
screen.blit(ip_text, (10, 25))
|
||
|
ip_rect = pygame.Rect(70, 20, 150, 36)
|
||
|
pygame.draw.rect(screen, GRAY if ip_focused else WHITE, ip_rect, 2)
|
||
|
ip_text_surface = font.render(ip_address, True, WHITE)
|
||
|
screen.blit(ip_text_surface, (ip_rect.x + 5, ip_rect.y + 5))
|
||
|
|
||
|
# Draw Port entry box
|
||
|
port_text = font.render("Port:", True, WHITE)
|
||
|
screen.blit(port_text, (10, 75))
|
||
|
port_rect = pygame.Rect(70, 70, 150, 36)
|
||
|
pygame.draw.rect(screen, GRAY if port_focused else WHITE, port_rect, 2)
|
||
|
port_text_surface = font.render(port, True, WHITE)
|
||
|
screen.blit(port_text_surface, (port_rect.x + 5, port_rect.y + 5))
|
||
|
|
||
|
# Draw toggle button
|
||
|
toggle_text = font.render("Enabled" if toggle_status else "Disabled", True, WHITE)
|
||
|
toggle_rect = pygame.Rect(10, 130, 150, 36)
|
||
|
pygame.draw.rect(screen, GREEN if toggle_status else RED, toggle_rect)
|
||
|
pygame.draw.rect(screen, WHITE, toggle_rect, 2)
|
||
|
screen.blit(toggle_text, (toggle_rect.x + 10, toggle_rect.y + 5))
|
||
|
|
||
|
# Draw request status
|
||
|
status_text = font.render("Status:", True, WHITE)
|
||
|
screen.blit(status_text, (320, 10))
|
||
|
if request_status:
|
||
|
pygame.draw.circle(screen, GREEN, (455, 23), 12)
|
||
|
else:
|
||
|
pygame.draw.circle(screen, RED, (455, 23), 12)
|
||
|
|
||
|
if request_text != "":
|
||
|
request_error_text = font.render(request_text, True, WHITE)
|
||
|
screen.blit(request_error_text, (350, 55))
|
||
|
|
||
|
# Draw the ball speed
|
||
|
speed_text = font.render(f"X Speed: {axis_x}%, Y Speed: {axis_y}%", True, WHITE)
|
||
|
screen.blit(speed_text, (10, 300))
|
||
|
|
||
|
|
||
|
# Update the display
|
||
|
pygame.display.flip()
|
||
|
|
||
|
# Send data request every 200 ms if toggle is enabled
|
||
|
if toggle_status and time.time() % 0.15 < 0.01: # approximate 150 ms interval
|
||
|
if ip_address and port:
|
||
|
data_packet = { }
|
||
|
if axis_y > 0:
|
||
|
data_packet["up"] = axis_y
|
||
|
data_packet["down"] = 0
|
||
|
elif axis_y < 0:
|
||
|
data_packet["down"] = -axis_y
|
||
|
data_packet["up"] = 0
|
||
|
if axis_x > 0:
|
||
|
data_packet["right"] = axis_x
|
||
|
data_packet["left"] = 0
|
||
|
elif axis_x < 0:
|
||
|
data_packet["left"] = -axis_x
|
||
|
data_packet["right"] = 0
|
||
|
if axis_x == 0 and axis_y == 0:
|
||
|
data_packet["right"] = 0
|
||
|
data_packet["left"] = 0
|
||
|
data_packet["up"] = 0
|
||
|
data_packet["down"] = 0
|
||
|
|
||
|
url = f"http://{ip_address}:{port}/move_camera"
|
||
|
try:
|
||
|
response = requests.post(url, json=data_packet, timeout=0.2)
|
||
|
print(f"sent: {data_packet}")
|
||
|
if response.status_code == 200:
|
||
|
request_status = True
|
||
|
else:
|
||
|
print(response.data)
|
||
|
request_status = False
|
||
|
except requests.Timeout:
|
||
|
request_status = False
|
||
|
request_text = "IP Not Found"
|
||
|
except requests.ConnectionError:
|
||
|
request_status = False
|
||
|
request_text = ""
|
||
|
else:
|
||
|
request_text = "IP and Port empty"
|
||
|
|
||
|
# Cap the frame rate
|
||
|
clock.tick(60)
|
||
|
|
||
|
# Quit Pygame
|
||
|
pygame.quit()
|