-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.py
75 lines (69 loc) · 1.51 KB
/
control.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
from evdev import InputDevice,categorize,ecodes
import RPi.GPIO as GPIO
gamepad = InputDevice('/dev/input/event0')
#button codes
A=304 #0,1
B=305 #0,1
Y=308 #0,1
X=307 #0,1
R1=311 #0,1
L1=310 #0,1
select=314 #0,1
start=315 #0,1
home=316 #0,1
L3=317 #0,1
R3=318 #0,1
R2=5 #0,255
L2=2 #0,255
LX=0 #-32768,32767
LY=1 #-32768,32767 sign inverted
RY=4 #-32768,32767 sign inverted
RX=3 #-32768,32767
V=17 #-1,0,1 sign inverted
H=16 #-1,0,1
#GPIOPins
r1=2
r2=3
rs=4
l1=5
l2=6
ls=7
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(r1,GPIO.OUT)
GPIO.setup(r2,GPIO.OUT)
GPIO.setup(rs,GPIO.OUT)
GPIO.setup(l1,GPIO.OUT)
GPIO.setup(l2,GPIO.OUT)
GPIO.setup(ls,GPIO.OUT)
rx=0
ry=0
rms=0
lms=0
for event in gamepad.read_loop():
if event.code is RY:
ry=-event.value/415
if event.code is RX:
rx=(event.value/365)/4
rms=ry-rx
lms=ry+rx
rm=GPIO.PWM(rs,50)
lm=GPIO.PWM(ls,50)
if(rms>=0):
GPIO.output(r1,GPIO.HIGH)
GPIO.output(r2,GPIO.LOW)
rm.start(rms)
if(rms<0):
GPIO.output(r1,GPIO.LOW)
GPIO.output(r2,GPIO.HIGH)
rm.start(-rms)
if(lms>=0):
GPIO.output(l1,GPIO.HIGH)
GPIO.output(l2,GPIO.LOW)
lm.start(lms)
if(lms<0):
GPIO.output(l1,GPIO.LOW)
GPIO.output(l2,GPIO.HIGH)
lm.start(-lms)
print "LS= "+str(lms)+" RS= "+str(rms)