Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from __future__ import print_function
  5.  
  6. import RPi.GPIO as GPIO
  7. import os
  8. import time
  9. import random
  10. import shlex
  11. import subprocess
  12.  
  13. # Pin Number
  14. PIN = 19
  15.  
  16. GPIO.setmode(GPIO.BCM)
  17. GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  18. last_pin_status = 0
  19.  
  20. SONG_DIR = '/home/pi/Music/'
  21. SONG_LIST = ['song01.wav', 'song02.wav', 'song03.wav']
  22.  
  23.  
  24. def play_song():
  25. song = random.choice(SONG_LIST)
  26. song_path = os.path.join(SONG_DIR, song)
  27. command = 'aplay %s' % (song_path)
  28. print(command)
  29. subprocess.Popen(shlex.split(command))
  30.  
  31.  
  32. while True:
  33. pin_status = GPIO.input(PIN)
  34. if last_pin_status == 1 and pin_status == 0:
  35. play_song()
  36.  
  37. last_pin_status = pin_status
  38. time.sleep(0.1)
  39.  
  40. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement