-
Notifications
You must be signed in to change notification settings - Fork 0
/
26_joystick.py
52 lines (44 loc) · 1.3 KB
/
26_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
50
51
52
#!/usr/bin/env python
#------------------------------------------------------
#
# This is a program for JoystickPS2 Module.
#
# This program depend on ADC0832 ADC chip. Follow
# the instruction book to connect the module and
# ADC0832 to your Raspberry Pi.
#
#------------------------------------------------------
import ADC0832_tmp
import RPi.GPIO as GPIO
import time
btn = 15 # Define button pin
def setup():
ADC0832_tmp.setup() # Setup ADC0832
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup button pin as input an pull it up
global state
state = ['up', 'down', 'left', 'right']
def getResult(): #get joystick result
if ADC0832_tmp.getResult1() == 0:
return 1 #up
if ADC0832_tmp.getResult1() == 255:
return 2 #down
if ADC0832_tmp.getResult() == 0:
return 3 #left
if ADC0832_tmp.getResult() == 255:
return 4 #right
if GPIO.input(btn) == 0:
print 'Button is pressed!' # Button pressed
def loop():
while True:
tmp = getResult()
if tmp != None:
print state[tmp - 1]
def destory():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destory()