-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
161 lines (148 loc) · 4.54 KB
/
remote.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Load library functions we want
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
import pygame
# Set which GPIO pins the drive outputs are connected to
DRIVE_1 = 17
DRIVE_2 = 22
DRIVE_3 = 24
DRIVE_4 = 23
# Set all of the drive pins as output pins
GPIO.setup(DRIVE_1, GPIO.OUT)
GPIO.setup(DRIVE_2, GPIO.OUT)
GPIO.setup(DRIVE_3, GPIO.OUT)
GPIO.setup(DRIVE_4, GPIO.OUT)
GPIO.output(DRIVE_1, GPIO.LOW)
GPIO.output(DRIVE_4, GPIO.LOW)
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
axisUpDown = 1 # Joystick axis to read for up / down position
axisUpDownInverted = False # Set this to True if up and down appear to be swapped
axisLeftRight = 3 # Joystick axis to read for left / right position
axisLeftRightInverted = False # Set this to True if left and right appear to be swapped
interval = 0.1 # Time between keyboard updates in seconds, smaller responds faster but uses more processor time
moveUp = False
moveDown = False
moveLeft = False
moveRight = False
try:
print('Press [ESC] to quit')
# Loop indefinitely
leftState = GPIO.LOW
rightState = GPIO.LOW
while True:
# Get the currently pressed keys on the keyboard
events = pygame.event.get()
for event in events:
# Keys have changed, generate the command list based on keys
if event.type == pygame.JOYBUTTONDOWN:
if j.get_button(0):
print("Pressed: X")
elif j.get_button(1):
print("Pressed: Circle")
elif j.get_button(2):
print("Pressed: Triangle")
elif j.get_button(3):
print("Pressed: Square")
elif j.get_button(4):
print("Pressed: L1")
elif j.get_button(5):
print("Pressed: R1")
elif j.get_button(6):
print("Pressed: L2")
elif j.get_button(7):
print("Pressed: R2")
elif j.get_button(8):
print("Pressed: SHARE")
elif j.get_button(9):
print("Pressed: OPTIONS")
elif j.get_button(10):
print("Pressed: PS Button")
elif j.get_button(11):
print("Pressed: Left Analog")
elif j.get_button(12):
print("Pressed: Right Analog")
elif event.type == pygame.JOYBUTTONUP:
if j.get_button(0):
print("Released: X")
elif j.get_button(1):
print("Released: Circle")
elif j.get_button(2):
print("Released: Triangle")
elif j.get_button(3):
print("Released: Square")
elif j.get_button(4):
print("Released: L1")
elif j.get_button(5):
print("Released: R1")
elif j.get_button(6):
print("Released: L2")
elif j.get_button(7):
print("Released: R2")
elif j.get_button(8):
print("Released: SHARE")
elif j.get_button(9):
print("Released: OPTIONS")
elif j.get_button(10):
print("Released: PS Button")
elif j.get_button(11):
print("Released: Left Analog")
elif j.get_button(12):
print("Released: Right Analog")
elif event.type == pygame.JOYAXISMOTION:
moveUp = False
moveDown = False
moveRight = False
moveLeft = False
# A joystick has been moved, read axis positions (-1 to +1)
upDown = j.get_axis(axisUpDown)
leftRight = j.get_axis(axisLeftRight)
# Invert any axes which are incorrect
if axisUpDownInverted:
upDown = -upDown
if axisLeftRightInverted:
leftRight = -leftRight
# Determine Up / Down values
if upDown < -0.1:
moveUp = True
moveDown = False
elif upDown > 0.1:
moveUp = False
moveDown = True
else:
moveUp = False
moveDown = False
# Determine Left / Right values
if leftRight < -0.1:
moveLeft = True
moveRight = False
elif leftRight > 0.1:
moveLeft = False
moveRight = True
else:
moveLeft = False
moveRight = False
if moveLeft:
leftState = GPIO.LOW
rightState = GPIO.HIGH
elif moveRight:
leftState = GPIO.HIGH
rightState = GPIO.LOW
elif moveUp:
leftState = GPIO.HIGH
rightState = GPIO.HIGH
else:
leftState = GPIO.LOW
rightState = GPIO.LOW
GPIO.output(DRIVE_1, leftState)
GPIO.output(DRIVE_4, rightState)
# Wait for the interval period
# time.sleep(0.1)
# Disable all drives
except KeyboardInterrupt:
# CTRL+C exit, disable all drives
print("EXITING NOW")
j.quit()