2014년 8월 11일 월요일

[라즈베리파이] Python Sample Code (EventPlayer.py)

#EventPlayer.py
from time import sleep
import os
from subprocess import Popen, PIPE
import RPi.GPIO as GPIO
import pyinotify

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
GPIO.setup(27, GPIO.IN)
GPIO.setup(22, GPIO.IN)
GPIO.setup(7, GPIO.IN)
GPIO.setup(8, GPIO.IN)
GPIO.setup(9, GPIO.IN)
GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(18, GPIO.IN)

startDelay = 20
sw = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
swold = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

p = Popen(['mpg321', '-R', 'n', '-g',  '100'], stdin=PIPE, stdout=PIPE)

wm = pyinotify.WatchManager()
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE

class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
print "Create: %s" % event.pathname
p.stdin.write('STOP\n')


def process_IN_DELETE(self, event):
print "Remove: %s" % event.pathname
p.stdin.write('STOP\n')

notifier = pyinotify.ThreadedNotifier(wm, EventHandler())
notifier.start()

wdd = wm.add_watch('/media', mask, rec=True)

while True:
sw[0] = GPIO.input(17)
sw[1] = GPIO.input(27)
sw[2] = GPIO.input(22)
sw[3] = GPIO.input(7)
sw[4] = GPIO.input(8)
sw[5] = GPIO.input(9)
sw[6] = GPIO.input(10)
sw[7] = GPIO.input(11)
sw[8] = GPIO.input(23)
sw[9] = GPIO.input(24)
sw[10] = GPIO.input(18)

sleep(0.1)

if( startDelay == 0 ):
for x in range(0, 9):
if( (swold[x] == 1) and (sw[x] == 0) ):
printstr = 'clicked' + str(x)
print(printstr)
cmdStr = 'LOAD /home/pi/Pops/00' + str(x+1) + '.mp3\n'
p.stdin.write(cmdStr)
if( (swold[9] == 1) and (sw[9] == 0) ):
print('clicked Stop')
p.stdin.write('STOP\n')
if( (swold[10] == 1) and (sw[10] == 0) ):
print('clicked Quit')
p.stdin.write('QUIT\n')
wm.rm_watch(wdd.values())
notifier.stop()
exit()
else:
startDelay = startDelay - 1

for x in range(0, 11): swold[x] = sw[x]

댓글 2개:

  1. 답글
    1. 내용에 보시면 /media 라는 부분이 있습니다. media 디렉토리 아래에 있는 파일에 접근하는거죠..

      삭제