forked from tyspa1/Jeopardy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbutton.py
140 lines (116 loc) · 3.88 KB
/
button.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#Poll Raspberry Pi Buttons and control LEDS
#By Tyler Spadgenske
import RPi.GPIO as gpio
import time
class Poll():
def __init__(self):
self.DEBUG = True
#Setup pins and board
gpio.setmode(gpio.BCM)
#Pin 17 is Team 1
gpio.setup(17, gpio.IN, pull_up_down=gpio.PUD_DOWN)
#Pin 22 is Team 2
gpio.setup(22, gpio.IN, pull_up_down=gpio.PUD_DOWN)
#Pin 4 is Team 3
gpio.setup(4, gpio.IN, pull_up_down=gpio.PUD_DOWN)
#Pin 27 is Team 4
gpio.setup(27, gpio.IN, pull_up_down=gpio.PUD_DOWN)
#Pin 5 is Team 5
gpio.setup(5, gpio.IN, pull_up_down=gpio.PUD_DOWN)
#GPIO Needs to be setup between 3.3v and pins above
#Put pins in variables
self.center_button = 22
self.right_button = 4
self.left_button = 17
self.fourth_button = 27
self.fifth_button = 5
self.first = ''
self.center_press = 0
self.right_press = 0
self.left_press = 0
self.fourth_press = 0
self.fifth_press = 0
def reset(self):
self.first = ''
self.center_press = 0
self.right_press = 0
self.left_press = 0
self.fourth_press = 0
self.fifth_press = 0
def poll(self): #Returns first button pressed
#Variables for press count (one for each button)
self.center_press = 0
self.right_press = 0
self.left_press = 0
self.fourth_press = 0
self.fifth_press = 0
#Check for button presses
while True:
self.first = self.check()
if self.first != '':
break
time.sleep(.5)
return self.first
def check(self):
center_input = gpio.input(self.center_button)
left_input = gpio.input(self.left_button)
right_input = gpio.input(self.right_button)
fourth_input = gpio.input(self.fourth_button)
fifth_input = gpio.input(self.fifth_button)
#Center buttons stuff
if center_input == gpio.HIGH:
if self.center_press > 0:
if self.DEBUG:
print('ADMIN: Console button 2 has been pressed')
self.first = 1
else:
if self.DEBUG:
pass
self.center_press += 1
#Right buttons stuff
if right_input == gpio.HIGH:
if self.right_press > 0:
if self.DEBUG:
print('ADMIN: Console button 3 has been pressed')
self.first = 2
else:
if self.DEBUG:
pass
self.right_press += 1
#Left button stuff
if left_input == gpio.HIGH:
if self.left_press > 0:
if self.DEBUG:
print('ADMIN: Console button 1 has been pressed')
self.first = 0
else:
if self.DEBUG:
pass
self.left_press += 1
#Fourth button stuff
if fourth_input == gpio.HIGH:
if self.fourth_press > 0:
if self.DEBUG:
print('ADMIN: Console button 4 has been pressed')
self.first = 3
else:
if self.DEBUG:
pass
self.fourth_press += 1
#Fifth button stuff
if fifth_input == gpio.HIGH:
if self.fifth_press > 0:
if self.DEBUG:
print('ADMIN: Console button 5 has been pressed')
self.first = 4
else:
if self.DEBUG:
pass
self.fifth_press += 1
return self.first
if __name__ == '__main__':
test = Poll()
while True:
test.first = ''
winner = test.poll()
print(winner)