-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoystick.py
More file actions
128 lines (96 loc) · 3.65 KB
/
Copy pathJoystick.py
File metadata and controls
128 lines (96 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Joystick Controller Simulator Module.
Simulates joystick input with movement detection and control mapping.
"""
from typing import Tuple, Dict
from utils import get_valid_input
class JoystickController:
"""Simulates a joystick controller."""
def __init__(self, center_x: float = 0.0, center_y: float = 0.0) -> None:
"""
Initialize joystick controller.
Args:
center_x: X coordinate of center position
center_y: Y coordinate of center position
"""
self.center_x = center_x
self.center_y = center_y
self.current_x = center_x
self.current_y = center_y
self.max_range = 100
def move(self, x: float, y: float) -> bool:
"""
Move joystick to specified position.
Args:
x: X coordinate (-100 to 100)
y: Y coordinate (-100 to 100)
Returns:
True if move was valid
"""
if abs(x) > self.max_range or abs(y) > self.max_range:
print(f"Error: Coordinates must be between -{self.max_range} and {self.max_range}")
return False
self.current_x = x
self.current_y = y
return True
def get_direction(self) -> str:
"""
Get the current direction of the joystick.
Returns:
Direction string (Up, Down, Left, Right, etc.)
"""
if self.current_x == 0 and self.current_y == 0:
return "Center"
directions = []
if self.current_y > 20:
directions.append("Up")
elif self.current_y < -20:
directions.append("Down")
if self.current_x > 20:
directions.append("Right")
elif self.current_x < -20:
directions.append("Left")
return " ".join(directions) if directions else "Center"
def reset(self) -> None:
"""Reset joystick to center position."""
self.current_x = self.center_x
self.current_y = self.center_y
def get_position(self) -> Tuple[float, float]:
"""Get current position."""
return (self.current_x, self.current_y)
def display_status(self) -> None:
"""Display joystick status."""
print(f"\n--- Joystick Status ---")
print(f"Position: X={self.current_x}, Y={self.current_y}")
print(f"Direction: {self.get_direction()}")
print(f"Distance from center: {((self.current_x**2 + self.current_y**2)**0.5):.2f}\n")
def main() -> None:
"""Main function to run the joystick simulator."""
joystick = JoystickController()
print("=== Joystick Controller Simulator ===\n")
while True:
print("Menu:")
print("1. Move joystick")
print("2. View status")
print("3. Reset to center")
print("4. Exit")
choice = input("\nSelect an option (1-4): ").strip()
if choice == '1':
x = get_valid_input("X coordinate (-100 to 100): ", float)
if x is not None:
y = get_valid_input("Y coordinate (-100 to 100): ", float)
if y is not None:
if joystick.move(x, y):
joystick.display_status()
elif choice == '2':
joystick.display_status()
elif choice == '3':
joystick.reset()
print("Joystick reset to center\n")
elif choice == '4':
print("Goodbye!")
break
else:
print("Invalid option. Please try again.\n")
if __name__ == "__main__":
main()