#!/usr/bin/python
import sys
import threading
import time
import Queue
import signal
import subprocess
import RPi.GPIO as GPIO
print("Tonbert Player")
BASEDIR = "/home/pi/Music"
BIB = {
"0003923273":"Conni - 2007 - Conni schlaeft im Kindergarten - Connie geht in den Zoo",
"0014017502":"Sven Nordqvist - 2008 - Ein Feuerwerk fuer den Fuchs",
}
# CTRL+C to stop threads
def signal_handler_sigint(sig, frame):
global wait_for_input
wait_for_input = False
print "closing Tonbert Player"
time.sleep(1)
print "Goodbye"
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler_sigint)
wait_for_input = True
# Async readline
def add_input(input_queue):
global wait_for_input
while wait_for_input:
input_queue.put(sys.stdin.readline())
# Send command to VLC
def send_command(cmd):
#subprocess.call(["curl", "-u", ":player", "
http://127.0.0.1:8080/requests/status.xml?command=" + cmd])
subprocess.call(["curl", "-s", "-o", "/dev/null", "-u", ":player", "
http://127.0.0.1:8080/requests/status.xml?command=" + cmd])
# pause/unpause player
def vlc_pause():
print "VLC: pause / unpause"
send_command("pl_pause")
# empty playlist
def vlc_empty():
print "VLC: empty playlist"
send_command("pl_empty")
# load new dir
def vlc_load(dir):
vlc_empty()
print "VLC: load " + dir
send_command("in_play&input=" + dir)
# next track
def vlc_next():
print "VLC: next track"
send_command("pl_next")
# previous track
def vlc_previous():
print "VLC: previous track"
send_command("pl_previous")
# set volume
def vlc_volume(vol):
print "VLC: change volume to " + str(vol)
send_command("volume&val=" + str(vol))
# Main controller
def player_controller():
# Setup GPIOs
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# start readline thread
input_queue = Queue.Queue()
input_thread = threading.Thread(target=add_input, args=(input_queue,))
input_thread.deamon = True
input_thread.start()
# Start VLC thread
volume = 240
print "Starting VLC..."
vlc = subprocess.Popen(["cvlc"])
time.sleep(2)
vlc_volume(volume)
vlc_load(BASEDIR + "/winxp_start.mp3")
time.sleep(8)
vlc_empty()
print "VLC ready"
# Main Loop
unlocked = True
current_id = ""
bounce_volp = False
bounce_next = False
bounce_pause = False
bounce_prev = False
bounce_voln = False
while True:
#print "waiting for input"
bt_volp = GPIO.input(14) == GPIO.LOW and unlocked
bt_next = GPIO.input(15) == GPIO.LOW and unlocked
bt_pause = GPIO.input(18) == GPIO.LOW and unlocked
bt_prev = GPIO.input(23) == GPIO.LOW and unlocked
bt_voln = GPIO.input(24) == GPIO.LOW and unlocked
# Get ID from input loop
if not input_queue.empty():
id = input_queue.get().strip()
# Check for locked state
if id == "0009213236":
unlocked = True
id = ""
if id == "0009211794":
unlocked = False
id = ""
# if ID is in BIB, load and play it
if id != current_id and id in BIB:
current_id = id
dir = BASEDIR + "/" + BIB[id]
dir = dir.replace(" ", "%20")
print "Now playing: " + dir
vlc_load(dir)
id = ""
# check buttons
if bt_volp:
if not bounce_volp:
volume = min(320, volume+16)
vlc_volume(volume)
bounce_volp = True
else:
bounce_volp = False
if bt_voln:
if not bounce_voln:
volume = max(16, volume-16)
vlc_volume(volume)
bounce_voln = True
else:
bounce_voln = False
if bt_next:
if not bounce_next:
vlc_next()
bounce_next = True
else:
bounce_next = False
if bt_pause:
if not bounce_pause:
vlc_pause()
bounce_pause = True
else:
bounce_pause = False
if bt_prev:
if not bounce_prev:
vlc_previous()
bounce_prev = True
else:
bounce_prev = False
# Wait for 100ms for a 10Hz loop
time.sleep(0.1)
player_controller()