softshut.py

      #!/usr/bin/python
      import time
      from subprocess import call
      import RPi.GPIO as gpio

      pinBt=19
      pinAC=21
      pinLed=23
      pinOut=15

      dlyShutdownBt = 5
      dlyShutdownAC = 10

      dlyLedOk = 1
      dlyLedFast = 0.1

      def loop():
        hasBt = False
        hasAC = False
        timeBt = 0
        timeAC = 0
        dlyLed = dlyLedOk
        terminated = False

        while gpio.input(pinBt)==0 and gpio.input(pinAC)==0:
          for i in range(0,3):
            gpio.output(pinLed, True)
            gpio.output(pinOut, True)
            time.sleep(dlyLedFast)
            gpio.output(pinLed, False)
            gpio.output(pinOut, False)
            time.sleep(dlyLedFast)
          time.sleep(dlyLedOk)

        while not terminated:

          if gpio.input(pinBt)==0:
            if timeBt > 0:
              print "Bt OK"
              timeBt = 0
          else:
            if timeBt == 0:
              print "Bt failed"
              timeBt = time.time()
            else:
              if time.time() - timeBt > dlyShutdownBt:
                print "Bt shutdown"
                doShutdown()
                terminated = True

          if gpio.input(pinAC)>0:
            if timeAC > 0:
              print "AC OK"
              timeAC = 0
          else:
            if timeAC == 0:
              print "AC failed"
              timeAC = time.time()
            else:
              if time.time() - timeAC > dlyShutdownAC:
                print "AC shutdown"
                doShutdown()
                terminated = True

          if (timeAC > 0  or  timeBt > 0) and dlyLed == dlyLedOk:
            dlyLed = dlyLedFast
          elif timeAC == 0  and  timeBt == 0  and  dlyLed != dlyLedOk:
            dlyLed = dlyLedOk

          gpio.output(pinLed, True)
          gpio.output(pinOut, True)
          time.sleep(dlyLed)
          gpio.output(pinLed, False)
          gpio.output(pinOut, False)
          time.sleep(dlyLed)

      def eventButton(pin):
        print "Button shutdown"
        doShutdown()

      def doShutdown():
        print "SHUTDOWN!!!"
        call('halt', shell=False)

      gpio.setmode(gpio.BOARD)
      gpio.setup(pinBt, gpio.IN)
      gpio.setup(pinAC, gpio.IN)
      gpio.setwarnings(False)
      gpio.setup(pinLed, gpio.OUT)
      gpio.setup(pinOut, gpio.OUT)
      loop()
      gpio.cleanup()