-
Notifications
You must be signed in to change notification settings - Fork 15
/
testbutton.py
executable file
·51 lines (38 loc) · 1.19 KB
/
testbutton.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
# From picamera2 examples: capture_jpeg.py
#!/usr/bin/python3
import time, requests, signal, os
from gpiozero import LED, Button
#instantiate buttons
shutter_button = Button(16, hold_time = 2)
led = LED(20)
def handle_pressed():
print("button pressed!")
led.on()
def handle_held():
print("button held!")
def handle_released():
print("button released!")
led.off()
#################################
# For RPi debugging:
# Handle Ctrl+C script termination gracefully
# (Otherwise, it shuts down the entire Pi -- bad)
#################################
def handle_keyboard_interrupt(sig, frame):
print('Ctrl+C received, stopping script')
led.off() #make sure LED is off before exiting
#weird workaround I found from rpi forum to shut down script without crashing the pi
os.kill(os.getpid(), signal.SIGUSR1)
################################
# LISTEN FOR BUTTON PRESS EVENTS
################################
shutter_button.when_pressed = handle_pressed
shutter_button.when_held = handle_held
shutter_button.when_released = handle_released
# Handle Ctrl+C gracefully
signal.signal(signal.SIGINT, handle_keyboard_interrupt)
# Test LED independently
led.on()
time.sleep(1)
led.off()
signal.pause()