-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons.py
99 lines (74 loc) · 3.21 KB
/
buttons.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
import RPi.GPIO as GPIO
import time
def initialize():
# Set GPIO mode to BCM
GPIO.setmode(GPIO.BCM)
# Define GPIO pins for buttons
RED_PIN = 6
YELLOW_PIN = 13
GREEN_PIN = 19
BLUE_PIN = 26
# Set up GPIO pins as inputs with pull-down resistors
GPIO.setup(RED_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(YELLOW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(GREEN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(BLUE_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
colors = ["RED", "YELLOW", "GREEN", "BLUE"]
results = []
return RED_PIN, YELLOW_PIN, GREEN_PIN, BLUE_PIN, colors, results
def button_callback(channel, colors, results):
ts = time.time()
results.append({"ts": ts, "color": colors[channel]})
# print(f"{colors[channel]} Button pressed at time {ts}")
def setup_button_events(RED_PIN, YELLOW_PIN, GREEN_PIN, BLUE_PIN, colors, results):
# Add event detection for each button
GPIO.add_event_detect(RED_PIN, GPIO.RISING, callback=lambda channel: button_callback(0, colors, results), bouncetime=200)
GPIO.add_event_detect(YELLOW_PIN, GPIO.RISING, callback=lambda channel: button_callback(1, colors, results), bouncetime=200)
GPIO.add_event_detect(GREEN_PIN, GPIO.RISING, callback=lambda channel: button_callback(2, colors, results), bouncetime=200)
GPIO.add_event_detect(BLUE_PIN, GPIO.RISING, callback=lambda channel: button_callback(3, colors, results), bouncetime=200)
def main():
RED_PIN, YELLOW_PIN, GREEN_PIN, BLUE_PIN, colors, results = initialize()
setup_button_events(RED_PIN, YELLOW_PIN, GREEN_PIN, BLUE_PIN, colors, results)
try:
time.sleep(55) # Keep the program running
except KeyboardInterrupt:
pass
finally:
# Clean up GPIO on program exit
GPIO.cleanup()
# time.sleep(2)
return results
if __name__ == "__main__":
main()
#import RPi.GPIO as GPIO
# import time
# # Set GPIO mode to BCM
# GPIO.setmode(GPIO.BCM)
# # Define GPIO pins for buttons
# RED_PIN = 6
# YELLOW_PIN = 13
# GREEN_PIN = 19
# BLUE_PIN = 26
# # Set up GPIO pins as inputs with pull-down resistors
# GPIO.setup(RED_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# GPIO.setup(YELLOW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# GPIO.setup(GREEN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# GPIO.setup(BLUE_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# colors = ["RED", "YELLOW", "GREEN", "BLUE"]
# def button_callback(channel):
# ts = time.time()
# print(f"{colors[channel]} Button pressed at time {ts}")
# # Add event detection for each button
# GPIO.add_event_detect(RED_PIN, GPIO.RISING, callback=lambda channel: button_callback(0), bouncetime=200)
# GPIO.add_event_detect(YELLOW_PIN, GPIO.RISING, callback=lambda channel: button_callback(1), bouncetime=200)
# GPIO.add_event_detect(GREEN_PIN, GPIO.RISING, callback=lambda channel: button_callback(2), bouncetime=200)
# GPIO.add_event_detect(BLUE_PIN, GPIO.RISING, callback=lambda channel: button_callback(3), bouncetime=200)
# try:
# print("Press Ctrl+C to exit")
# while True:
# time.sleep(1) # Keep the program running
# except KeyboardInterrupt:
# pass
# finally:
# # Clean up GPIO on program exit
# GPIO.cleanup()