Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- # -*- coding: utf-8 -*-
- from __future__ import print_function
- import RPi.GPIO as GPIO
- import os
- import time
- import random
- import shlex
- import subprocess
- # Pin Number
- PIN = 19
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- last_pin_status = 0
- SONG_DIR = '/home/pi/Music/'
- SONG_LIST = ['song01.wav', 'song02.wav', 'song03.wav']
- def play_song():
- song = random.choice(SONG_LIST)
- song_path = os.path.join(SONG_DIR, song)
- command = 'aplay %s' % (song_path)
- print(command)
- subprocess.Popen(shlex.split(command))
- while True:
- pin_status = GPIO.input(PIN)
- if last_pin_status == 1 and pin_status == 0:
- play_song()
- last_pin_status = pin_status
- time.sleep(0.1)
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement