[ create a new paste ] login | about

Link: http://codepad.org/b5IkKMY2    [ raw code | fork ]

meigrafd - Python, pasted on Feb 5:
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# http://www.forum-raspberrypi.de/Thread-reboot-shutdown-pi-reboot-windows-rechner?pid=265171#pid265171
#
# v0.1 by meigrafd
#
from __future__ import print_function
from time import sleep, time
from RPi import GPIO
from Queue import Queue
from functools import partial


def interrupt_Event(q, channel):
    q.put( (channel, GPIO.input(channel)) )


def main(switchPi=4, switchWin=17, specialTime=3):
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(switchPi, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(switchWin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    queue=Queue()
    triggerTime=0
    GPIO.add_event_detect(switchPi, GPIO.BOTH, callback=partial(interrupt_Event, queue), bouncetime=150)
    GPIO.add_event_detect(switchWin, GPIO.BOTH, callback=partial(interrupt_Event, queue), bouncetime=150)
    try:
        while True:
            job = queue.get()
            pin = job[0]
            state = job[1]
            print(pin, state)
            
            if state == GPIO.HIGH:
                triggerTime = time()
            elif state == GPIO.LOW:
                triggerTime = time() - triggerTime
                
                if pin == switchPi:
                    if triggerTime < specialTime:
                        print("%s: reboot pi" % triggerTime)
                    
                    elif triggerTime > specialTime:
                        print("%s: shutdown pi" % triggerTime)
                
                elif pin == switchWin:
                    if triggerTime > specialTime:
                        print("%s: shutdown win" % triggerTime)
                    
                    else:
                        print("time '%s' not valid" % triggerTime)
    
    
    except (KeyboardInterrupt, SystemExit):
        GPIO.cleanup()
        print("\nQuit\n")


if __name__ == "__main__":
    main()


#EOF


Create a new paste based on this one


Comments: