forked from wasilewskiJ/nalewak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio_button.py
75 lines (56 loc) · 1.89 KB
/
gpio_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
# If you have RuntimeError: Not running on a RPi!
# Run the following commands:
# sudo chown root:$USER /dev/gpiomem
# sudo chmod g+rw /dev/gpiomem
from gpiozero import Buzzer
from time import sleep
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import move_pour
import threading
running = False
buzzer = Buzzer(17)
def buzzer_on(lenght=0.3,delay=0):
"""
Activates the buzzer for a specified duration after an optional delay.
Args:
length (float): Duration for which the buzzer should remain on (in seconds).
delay (float): Delay before the buzzer turns on (in seconds).
"""
sleep(delay)
buzzer.on()
sleep(lenght)
buzzer.off()
def button_callback(channel=None):
"""
Callback function that gets executed when the button is pressed. It initiates the move_pour process
in a new thread and activates the buzzer.
Args:
channel: Optional. The GPIO channel associated with the button. Not used in this function.
"""
global running
if running:
print("Button was pushed, but already running!")
return
running = True
print("Button was pushed!")
buzzer_therad = threading.Thread(target=buzzer_on, args=(0.3,0.1))
t1 = threading.Thread(target=move_pour.run)
buzzer_therad.start()
buzzer_therad.join()
t1.start()
t1.join()
running = False
if __name__ == "__main__":
move_pour.home()
move_pour.max()
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM) # Use physical pin numbering
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 10 to be an input pin and set initial value to be pulled hight (on)
buzzer_on(0.2)
sleep(0.2)
buzzer_on(0.2)
print("Listning for button press. Ctrl+C to quit\n\n")
while True:
if GPIO.input(4) == GPIO.HIGH:
button_callback()
GPIO.cleanup() # Clean up