arduino-mpduino/ttycontrol.py

89 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python
from serialmpcduino import SerialMPCduino
import subprocess
import time
tty = '/dev/ttyACM0'
baud = 9600
mpdhost = 'fatserf.thuis'
mpcformat = "+%title%\t%artist%\t%album%"
title = None
artist = None
album = None
volume = 0
#progress = 0
repeat = True
shuffle = True
random = True
single = True
consume = True
playing = True
#mpctext = subprocess.check_output(['mpc', 'current', '--wait', '-h', mpdhost, '-f', mpcformat])
if True:
ser = SerialMPCduino(tty, baud)
time.sleep(0.5)
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.playing = playing
ser.volume = volume
#ser.progress = progress
ser.title = title
ser.artist = artist
ser.album = album
ser.repeat =repeat
ser.shuffle = shuffle
time.sleep(0.8)