-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_switches.py
executable file
·76 lines (64 loc) · 1.98 KB
/
test_switches.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
#!/usr/bin/env python
#
# Test push buttons for the Raspberry PI internet radio
#
# $Id: test_switches.py,v 1.5 2014/04/18 13:16:27 bob Exp $
#
# Author Bob Rathbone
# Web site http://www.bobrathbone.com
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
import sys
import time
import RPi.GPIO as GPIO
# Switch definitions
MENU_SWITCH = 25
LEFT_SWITCH = 14
RIGHT_SWITCH = 15
UP_SWITCH = 17
DOWN_SWITCH = 18
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setwarnings(False)
# For rev 1 boards with no inbuilt pull-up/down resistors the
# Wire the GPIO inputs to ground via a 10K resistor and uncomment these lines
#GPIO.setup(MENU_SWITCH, GPIO.IN)
#GPIO.setup(UP_SWITCH, GPIO.IN)
#GPIO.setup(DOWN_SWITCH, GPIO.IN)
#GPIO.setup(LEFT_SWITCH, GPIO.IN)
#GPIO.setup(RIGHT_SWITCH, GPIO.IN)
# For rev 2 boards with inbuilt pull-up/down resistors the
# following lines are used instead of the above, so
# there is no need to physically wire the 10k resistors
GPIO.setup(MENU_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(UP_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(DOWN_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(LEFT_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(RIGHT_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while True:
try:
menu_switch = GPIO.input(MENU_SWITCH)
up_switch = GPIO.input(UP_SWITCH)
down_switch = GPIO.input(DOWN_SWITCH)
left_switch = GPIO.input(LEFT_SWITCH)
right_switch = GPIO.input(RIGHT_SWITCH)
if menu_switch:
print "menu_switch"
elif up_switch:
print "up_switch"
elif down_switch:
print "down_switch"
elif left_switch:
print "left_switch"
elif right_switch:
print "right_switch"
time.sleep(0.5)
except KeyboardInterrupt:
print "\nExit"
GPIO.setwarnings(False)
GPIO.cleanup()
sys.exit(0)
# End of program