-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I added events handling for shutdown + reboot and a status LED. #20
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,52 @@ | ||||||||||
#!/usr/bin/env python | ||||||||||
|
||||||||||
|
||||||||||
import signal | ||||||||||
import sys | ||||||||||
import RPi.GPIO as GPIO | ||||||||||
import subprocess | ||||||||||
import time | ||||||||||
|
||||||||||
PWR_BUTTON_GPIO = 3 # 9(GND) 5-GPIO3(+) | ||||||||||
RST_BUTTON_GPIO = 4 # 9(GND) 7-GPIO4(+) | ||||||||||
LED_STATUS_GPIO = 18 # 6(GND) 12-GPIO18(+) | ||||||||||
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two should be configurable. I'm thinking:
Suggested change
Not sure if casting to int is actually required, and we'd need to import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't thought through yet, what is the recommended way for the user to set these since this script runs at startup. Let me know if you have any alternative ideas here. |
||||||||||
DEBOUNCE_TIME = 300 | ||||||||||
|
||||||||||
# DEBUG | ||||||||||
# if __name__ == '__main__': | ||||||||||
# GPIO.setmode(GPIO.BCM) | ||||||||||
# GPIO.setup(PWR_BUTTON_GPIO, GPIO.IN) | ||||||||||
# pressed = False | ||||||||||
# while True: | ||||||||||
# # button is pressed when pin is LOW | ||||||||||
# if not GPIO.input(PWR_BUTTON_GPIO): | ||||||||||
# print("Button pressed!") | ||||||||||
# else: | ||||||||||
# print("Button not pressed!") | ||||||||||
# time.sleep(0.5) | ||||||||||
Comment on lines
+14
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments will need to be removed. |
||||||||||
|
||||||||||
def signal_handler(sig, frame): | ||||||||||
# print("Cleanup and exit...") | ||||||||||
GPIO.cleanup() | ||||||||||
sys.exit(0) | ||||||||||
|
||||||||||
def button_power_pressed_callback(channel): | ||||||||||
# print("POWER BUTTON PRESSED!") | ||||||||||
subprocess.call(['shutdown', '-h', 'now'], shell=False) | ||||||||||
|
||||||||||
GPIO.setmode(GPIO.BCM) | ||||||||||
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||||||||||
GPIO.wait_for_edge(3, GPIO.FALLING) | ||||||||||
def button_reboot_pressed_callback(channel): | ||||||||||
# print("REBOOT BUTTON PRESSED!") | ||||||||||
subprocess.call(['shutdown', '-r', 'now'], shell=False) | ||||||||||
|
||||||||||
subprocess.call(['shutdown', '-h', 'now'], shell=False) | ||||||||||
if __name__ == '__main__': | ||||||||||
GPIO.setmode(GPIO.BCM) | ||||||||||
|
||||||||||
GPIO.setup(LED_STATUS_GPIO, GPIO.OUT) | ||||||||||
GPIO.setup(PWR_BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||||||||||
GPIO.setup(RST_BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||||||||||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd need to figure out how to make |
||||||||||
|
||||||||||
GPIO.output(LED_STATUS_GPIO, GPIO.HIGH) | ||||||||||
GPIO.add_event_detect(PWR_BUTTON_GPIO, GPIO.FALLING, callback=button_power_pressed_callback, bouncetime=DEBOUNCE_TIME) | ||||||||||
GPIO.add_event_detect(RST_BUTTON_GPIO, GPIO.FALLING, callback=button_reboot_pressed_callback, bouncetime=DEBOUNCE_TIME) | ||||||||||
|
||||||||||
signal.signal(signal.SIGINT, signal_handler) | ||||||||||
signal.pause() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this. If this gets merged, I can update the original power button guide to include instructions for adding the status LED.