#!/usr/bin/env python import mpdclient2, usblcd, time, string import thread led = {0:'off', 1:'off', 2:'off', 3:'off', 4:'off', 5:'off'} def ledtask(): '''Task to control the function button LEDs. Note: LED[0] is the cursor buttons and OK LED[1:5] are buttons F1:F5.''' global led while(1): for i in range(5): if (led[i] == 'on' or led[i] == 'flash'): usblcd.led(i,1) else: usblcd.led(i,0) time.sleep(0.5) for i in range(5): if (led[i] == 'off' or led[i] == 'flash'): usblcd.led(i,0) else: usblcd.led(i,1) time.sleep(0.5) titleText = 'LiFiPod' titleText = titleText.center(20) # '1 2341 2341 2341 2341234' Prev,Play,Next,Stop,Menu stopText = ' %c %c %c %c Menu' % (127, '>', 126, 219) text0 = '' text1 = '' def displaytask(): '''Task to control the two twenty character lines of text on the LCD.''' global text0, text1 lasttext0 = '' lasttext1 = '' scroll0 = '' scroll1 = '' while(1): if (text0 != lasttext0 or text1 != lasttext1): if (text0 == '' and text1 ==''): usblcd.clear() else: if (text0 != lasttext0): print text0 usblcd.text(0,0,' '*20) usblcd.text(0,0,text0) if (text1 != lasttext1): print text1 usblcd.text(1,0,' '*20) usblcd.text(1,0,text1) lasttext0 = text0 lasttext1 = text1 scroll0 = text0 + ' * ' scroll1 = text1 + ' * ' else: time.sleep(0.2) if(scroll0.__len__() > 23): scroll0 = scroll0[1:] + scroll0[0] usblcd.text(0,0,scroll0) if(scroll1.__len__() > 23): scroll1 = scroll1[1:] + scroll1[0] usblcd.text(1,0,scroll1) #Setup LCD usblcd.open() usblcd.clear() #setup mpd #mpd = mpdclient2.connect(host='192.168.1.15') mpd = mpdclient2.connect(host='localhost') vol = string.atoi(mpd.status()['volume']) #turn on required leds if (mpd.status()['state'] == 'play'): led[2]='on' if (mpd.status()['state'] == 'pause'): led[2]='flash' if (mpd.status()['state'] == 'stop'): led[4]='on' text0 = titleText text1 = stopText thread.start_new_thread(ledtask,()) thread.start_new_thread(displaytask,()) # Mapping for the button codes returned from usblcd.read(). button = ('none', 'plus', 'minus', 'f1', 'f2', 'f3', 'f4', 'f5', 'left', 'right', 'up', 'down', 'ok') while(1): lastfile = '' while(mpd.status()['state'] == 'play'): track = mpd.currentsong() file = track['file'] if (file != lastfile): text0 = track['artist'] text1 = track['title'] lastfile = file press = usblcd.read() if (press > 0): while (usblcd.read() !=0): time.sleep(0.1) if (press == 1): vol = vol + 5 #print vol mpd.setvol(vol) if (press == 2): vol = vol - 5 #print vol mpd.setvol(vol) if (press == 6): mpd.stop() led[2]='off' led[4]='on' text0 = titleText text1 = stopText break if (press == 4): mpd.pause(1) led[2]='flash' if (press == 3): mpd.previous() break if (press == 5): mpd.next() break else: time.sleep(0.2) while(mpd.status()['state'] != 'play'): press = usblcd.read() if (press > 0): while (usblcd.read() !=0): time.sleep(0.1) if (press == 4): mpd.play() led[2]='on' led[4]='off' break if (press == 6): mpd.stop() led[2]='off' led[4]='on' break else: time.sleep(0.1)