Raspberry Pi 備忘録 / Mbedもあるよ!

Raspberry Pi であれこれやった事の記録

電子工作 ボタンを押しっぱなしで・・・

ボタンを押すと、現在時刻を話して、
ボタンを約3秒押し続けると、シャットダウンしますと話すスクリプト

#!/usr/bin/python
# coding=utf-8
import RPi.GPIO as GPIO
import os
import time
import threading

PIN = 11

GPIO.setmode(GPIO.BOARD)

GPIO.setup(PIN,GPIO.IN)


class MyThread(threading.Thread):
        def __init__(self):
                threading.Thread.__init__(self)

        def run(self):
                print("  === start sub thread ===")

                count   = 0 
                while True:
                        time.sleep(0.1)

                        val = GPIO.input(PIN)
                        if val == 1:
                                count += 1
                                if count > 30: 
                                        os.system('/usr/local/etc/aquestalkpi/AquesTalkPi "シャットダウンします" -g 100 | aplay -D plughw:0,0 -q')
                                        break

                        elif count > 0:
                                os.system('date +"%I時%M分%S秒" | /usr/local/etc/aquestalkpi/AquesTalkPi -g 100 -f -| aplay -D plughw:0,0 -q')
                                count = 0 
                                time.sleep(2)
                print("  === end sub thread ===")

th = MyThread()
th.setDaemon(True)
th.start()

while True:
        stdent = raw_input()
        print(stdent)
        if stdent == 'q':
                break

GPIO.cleanup()