-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonHandler.py
64 lines (47 loc) · 2 KB
/
ButtonHandler.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
'''
Created on Nov 7, 2017
@author: frank
'''
try:
import RPi.GPIO as GPIO # pip install RPi.GPIO
gpioAvailable = True
except ImportError:
gpioAvailable = False
import time
class ButtonHandler():
port_controller_green = 26
port_controller_red = 19
port_player1_green = 13
port_player1_red = 06
port_player2_green = 21
port_player2_red = 20
if gpioAvailable:
GPIO.setmode(GPIO.BCM)
GPIO.setup(port_controller_green, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(port_controller_red, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(port_player1_green, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(port_player1_red, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(port_player2_green, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(port_player2_red, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def getButton(self, buttonNumber):
# Return the button state
if gpioAvailable == False:
return 0
else:
if buttonNumber == 1:
return (0 if GPIO.input(self.port_controller_green) == 1 else 1)
elif buttonNumber == 2:
return GPIO.input(self.port_controller_red)
elif buttonNumber == 3:
return (0 if GPIO.input(self.port_player1_green) == 1 else 1)
elif buttonNumber == 4:
return GPIO.input(self.port_player1_red)
elif buttonNumber == 5:
return (0 if GPIO.input(self.port_player2_green) == 1 else 1)
elif buttonNumber == 6:
return GPIO.input(self.port_player2_red)
if __name__ == "__main__":
btn = ButtonHandler()
while (True):
print(str(btn.getButton(1)) + "," + str(btn.getButton(2)) + "," + str(btn.getButton(3)) + "," + str(btn.getButton(4)) + "," + str(btn.getButton(5)) + "," + str(btn.getButton(6)))
time.sleep(0.1)