#!/usr/bin/env python from serialmpcduino import SerialMPCduino import subprocess import time import warnings import serial.tools.list_ports tty = None baud = 9600 verbose = 1 mpdhost = 'fatserf.thuis' mpcformat = "+%title%\t%artist%\t%album%" title = None artist = None album = None volume = 0 #progress = 0 repeat = 0 shuffle = 0 random = 0 single = 0 consume = 0 playing = 0 # Get TTY arduino_ports = [] while not arduino_ports: arduino_ports = [ p.device for p in serial.tools.list_ports.comports() if 'Arduino' in p.description ] if not arduino_ports: raise IOError("No Arduino found") if len(arduino_ports) > 1: if noinput is True: raise IOError("Too many Arduinos found") else: while tty not in arduino_ports: tty = raw_input("Enter a tty: "+" ,".join(arduino_ports)+ ";") else: tty = arduino_ports[0] try: ser = SerialMPCduino(tty, baud, verbose) while True: mpctext = subprocess.check_output(['mpc', '-h', mpdhost, '-f', mpcformat]) # Parse output i = 0 for line in mpctext.rstrip().split("\n"): if i == 0: # Make sure it still works if not line.startswith('+'): playing = False break line = line[1:] # Parse it line = line.split("\t") title = line[0].strip() artist = line[1].strip() album = line[2].strip() elif i == 1: line = line.replace(" "," ") line = line.replace(" ","\t") line = line.split("\t") playing = line[0][1:-1] == "playing" #progress = line[-1][1:-2] elif i == 2: line = line.replace(" ","\t") line = line.replace(" ","") line = line.split("\t") volume = line[0].split(':')[1] repeat = line[1].split(':')[1] == 'on' random = line[2].split(':')[1] == 'on' single = line[3].split(':')[1] == 'on' consume = line[4].split(':')[1] == 'on' i = i +1 #print("Title: {}".format(title)) #print("Artist: {}".format(artist)) #print("Album: {}".format(album)) #print("Volume: {}".format(volume)) #print("Progress: {}".format(progress)) #print("Repeat: {}".format(repeat)) #print("Shuffle: {}".format(shuffle)) #print("Random: {}".format(random)) #print("Single: {}".format(single)) #print("Consume: {}".format(consume)) #print("Playing: {}".format(playing)) #raw_input("") ser.volume = volume #ser.progress = progress ser.title = title ser.artist = artist ser.album = album ser.repeat =repeat ser.shuffle = shuffle ser.playing = playing time.sleep(0.5) finally: ser.close()