Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
shutdown script for Nakayoshi Camera
#!/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
You can’t perform that action at this time.