-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystick.py
49 lines (38 loc) · 964 Bytes
/
joystick.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
import RPi.GPIO as GPIO
import time
import sys
pin_x = 15
pin_xm = 18
pin_y = 23
pin_ym = 24
pin_press = 14
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_x, GPIO.IN)
GPIO.setup(pin_xm, GPIO.IN)
GPIO.setup(pin_y, GPIO.IN)
GPIO.setup(pin_ym, GPIO.IN)
GPIO.setup(pin_press, GPIO.IN)
def loop():
x, xm, y, ym, press = GPIO.input(pin_x), GPIO.input(pin_xm), GPIO.input(pin_y), GPIO.input(pin_ym), GPIO.input(
pin_press)
if x:
sys.stdout.write('right ')
elif not xm:
sys.stdout.write('left ')
if y:
sys.stdout.write('up ')
elif not ym:
sys.stdout.write('down ')
if press:
sys.stdout.write('pressed')
sys.stdout.flush()
print('')
if __name__ == '__main__':
try:
setup()
while True:
loop()
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()