-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons1234.py
executable file
·266 lines (206 loc) · 5.96 KB
/
buttons1234.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
import signal
import pigpio
import time
from threading import Timer
print("""buttons12.py - Detect which button has been pressed
This example should demonstrate how to:
1. set up pigpio to read buttons
2. determine which button has been pressed
3. test two-button clicks
Press Ctrl+C to exit!
""")
pi = pigpio.pi() # pi accesses the local Pi's GPIO
# The buttons on Pirate Audio are connected to pins 5, 6, 16 and 24
# Boards prior to 23 January 2020 used 5, 6, 16 and 20
# try changing 24 to 20 if your Y button doesn't work.
BUTTONS = [5, 6, 16, 24]
# These correspond to buttons A, B, X and Y respectively
LABELS = ['A', 'B', 'X', 'Y']
steady = 20000 # microseconds
# https://forums.raspberrypi.com/viewtopic.php?t=238131
# remember to start pigpio - sudo pigpiod
pressed = pigpio.LOW
released = pigpio.HIGH
class TwoButtons():
def __init__(self,
button1,button2,button3,button4,
callback1,callback2,callback3,callback4,
callback12,callback13,callback14,
callback23,callback24,
callback34):
self.last = ""
# get a pigpio instance
self.pi = pigpio.pi()
# set up buttons
self.button1 = button1
self.button2 = button2
self.button3 = button3
self.button4 = button4
# set up glitch filter to debounce each switch
self.pi.set_glitch_filter(self.button1, steady)
self.pi.set_glitch_filter(self.button2, steady)
self.pi.set_glitch_filter(self.button3, steady)
self.pi.set_glitch_filter(self.button4, steady)
# set up each button as an input
self.pi.set_mode(self.button1, pigpio.INPUT)
self.pi.set_mode(self.button2, pigpio.INPUT)
self.pi.set_mode(self.button3, pigpio.INPUT)
self.pi.set_mode(self.button4, pigpio.INPUT)
# create a callback for when button is pressed
self.pi.callback(self.button1, pigpio.EITHER_EDGE, self.button_pressed)
self.pi.callback(self.button2, pigpio.EITHER_EDGE, self.button_pressed)
self.pi.callback(self.button3, pigpio.EITHER_EDGE, self.button_pressed)
self.pi.callback(self.button4, pigpio.EITHER_EDGE, self.button_pressed)
self.callback1 = callback1
self.callback2 = callback2
self.callback3 = callback3
self.callback4 = callback4
self.callback12 = callback12
self.callback13 = callback13
self.callback14 = callback14
self.callback23 = callback23
self.callback24 = callback24
self.callback34 = callback34
def clrlast_cb(self):
self.last = ""
def callbackTwo(self,both):
if both != self.last:
self.last = both
clrlastTimer = Timer( 0.1, self.clrlast_cb, args=() )
clrlastTimer.start()
if both == "12":
self.callback12()
elif both == "13":
self.callback13()
elif both == "14":
self.callback14()
elif both == "23":
self.callback23()
elif both == "24":
self.callback24()
elif both == "34":
self.callback34()
# common button press callback for both buttons
def button_pressed(self, pin, button, tick):
both = None
if pin == self.button1:
if button == pressed:
for _ in range(10):
if self.pi.read(self.button2) == pressed:
both = "12"
break
if self.pi.read(self.button3) == pressed:
both = "13"
break
if self.pi.read(self.button4) == pressed:
both = "14"
break
time.sleep(0.005)
if not both:
if self.pi.read(self.button1) == pressed:
self.callback1()
if self.pi.read(self.button2) == pressed:
self.callback2()
if self.pi.read(self.button3) == pressed:
self.callback3()
if self.pi.read(self.button4) == pressed:
self.callback4()
if both:
self.callbackTwo(both)
elif pin == self.button2:
if button == pressed:
for _ in range(10):
if self.pi.read(self.button1) == pressed:
both = "12"
break
if self.pi.read(self.button3) == pressed:
both = "23"
break
if self.pi.read(self.button4) == pressed:
both = "24"
break
time.sleep(0.005)
if not both:
if self.pi.read(self.button1) == pressed:
self.callback1()
if self.pi.read(self.button2) == pressed:
self.callback2()
if self.pi.read(self.button3) == pressed:
self.callback3()
if self.pi.read(self.button4) == pressed:
self.callback4()
if both:
self.callbackTwo(both)
elif pin == self.button3:
if button == pressed:
for _ in range(10):
if self.pi.read(self.button1) == pressed:
both = "13"
break
if self.pi.read(self.button2) == pressed:
both = "23"
break
if self.pi.read(self.button4) == pressed:
both = "34"
break
time.sleep(0.005)
if not both:
if self.pi.read(self.button1) == pressed:
self.callback1()
if self.pi.read(self.button2) == pressed:
self.callback2()
if self.pi.read(self.button3) == pressed:
self.callback3()
if self.pi.read(self.button4) == pressed:
self.callback4()
if both:
self.callbackTwo(both)
elif pin == self.button4:
if button == pressed:
for _ in range(10):
if self.pi.read(self.button1) == pressed:
both = "14"
break
if self.pi.read(self.button2) == pressed:
both = "24"
break
if self.pi.read(self.button3) == pressed:
both = "34"
break
time.sleep(0.005)
if not both:
if self.pi.read(self.button1) == pressed:
self.callback1()
if self.pi.read(self.button2) == pressed:
self.callback2()
if self.pi.read(self.button3) == pressed:
self.callback3()
if self.pi.read(self.button4) == pressed:
self.callback4()
if both:
self.callbackTwo(both)
def cb1():
print("button 1 cb")
def cb2():
print("button 2 cb")
def cb3():
print("button 3 cb")
def cb4():
print("button 4 cb")
def cb12():
print("button 12 cb")
def cb13():
print("button 13 cb")
def cb14():
print("button 14 cb")
def cb23():
print("button 23 cb")
def cb24():
print("button 24 cb")
def cb34():
print("button 34 cb")
TwoButtons(5,6,16,24,cb1,cb2,cb3,cb4,cb12,cb13,cb14,cb23,cb24,cb34)
while True:
# keep the program alive
signal.pause()