Last active
August 29, 2015 14:06
-
-
Save karaage0703/ea9bc912bcf65629e38f to your computer and use it in GitHub Desktop.
shutdown script for Nakayoshi Camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*0 coding: utf-8 -*- | |
import RPi.GPIO as GPIO | |
import time | |
import syslog | |
import os | |
# use GPIO 17 as shutdown button | |
WatchGpioNo = 17 | |
#LEDGpioNo = 4 | |
SleepStepSec = 0.1 | |
def KeepWatchForSeconds(seconds): | |
GoFlag = True | |
if seconds < 1: | |
seconds = 1 | |
while seconds > 0: | |
time.sleep(SleepStepSec) | |
seconds -= SleepStepSec | |
if (GPIO.input(WatchGpioNo) == True): | |
GoFlag = False | |
break | |
return GoFlag | |
def CallShutdown(): | |
syslog.syslog(syslog.LOG_NOTICE, "Going shutdown by GPIO.") | |
os.system("/sbin/shutdown -h now 'Poweroff by GPIO'") | |
if __name__ == '__main__': | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(WatchGpioNo, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
try: | |
while True: | |
syslog.syslog(syslog.LOG_INFO, "Waiting for falling edge on port %d" % WatchGpioNo) | |
GPIO.wait_for_edge(WatchGpioNo, GPIO.FALLING) | |
syslog.syslog(syslog.LOG_INFO, "Falling edge detected.") | |
if KeepWatchForSeconds(5): | |
CallShutdown() | |
break | |
GPIO.cleanup() # clean up GPIO on normal exit | |
except KeyboardInterrupt: | |
GPIO.cleanup() # clean up GPIO on CTRL+C exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment