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 | #!/usr/bin/env python # coding=utf-8 # author:ksc import RPi.GPIO as GPIO import time import os,sys import signal GPIO.setmode(GPIO.BCM) #define GPIO pin pin_btn=7 pin_led=17 GPIO.setup(pin_btn, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_led, GPIO.OUT, initial=GPIO.LOW) press_time=0 count_down=10 led_on=1 def cleanup(): '''释放资源,不然下次运行是可能会收到警告 ''' print('clean up') GPIO.cleanup() def handleSIGTERM(signum, frame): #cleanup() sys.exit()#raise an exception of type SystemExit def onPress(channel): global press_time,count_down print('pressed') press_time+=1 if press_time >3: press_time=1 if press_time==1: GPIO.output(pin_led, 1) print('system will restart in %s'%(count_down)) elif press_time==2: print('system will halt in %s'%(count_down)) elif press_time==3: GPIO.output(pin_led, 0) print 'cancel ' count_down=10 GPIO.add_event_detect(pin_btn, GPIO.FALLING, callback= onPress,bouncetime=500) #signal.signal(signal.SIGTERM, handleSIGTERM) try: while True: if press_time==1: if count_down==0: print "start restart" os.system("shutdown -r -t 5 now") sys.exit() led_on=not led_on GPIO.output(pin_led, led_on)# blink led if press_time==2 and count_down==0: print "start shutdown" os.system("shutdown -t 5 now") sys.exit() if press_time==1 or press_time==2: count_down-=1 print "%s second"%(count_down) time.sleep(1) except KeyboardInterrupt: print('User press Ctrl+c ,exit;') finally: cleanup() |
1 2 3 4 5 6 | #创建程序,把代码粘贴进去保存 root@mypi:~# vi reboot.py #修改可执行 root@mypi:~# chomod 775 reboot.py #测试下 root@mypi:~# ./reboot.py |
1 2 3 4 5 | #若没问题就可以让它后台运行了, root@mypi:~# nohup ./reboot.py & #想结束后台运行? ps auxf #查找PID kill PID |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |