[ create a new paste ] login | about

Link: http://codepad.org/EgflTKzJ    [ raw code | fork ]

meigrafd - Python, pasted on Mar 13:
#!/usr/bin/python
from __future__ import print_function
from functools import partial
import RPi.GPIO as GPIO
import time
import mpd
import sys

#GPIO, Taster
T_PREV = 7
T_NEXT = 13
T_PLAY = 15

MPD_HOST     = "localhost"
MPD_PORT     = 6600
MPD_PASSWORD = False   # optional password to connect to MPD

sys.setdefaultencoding("utf-8")

GPIO.setmode(GPIO.BOARD)
GPIO.setup(T_PREV, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(T_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(T_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

dictionary={}
#dictionary['song_title']=' '
dictionary['Volume']=70
dictionary['pause']=0

def interrupt_event(client, pin):
    if GPIO.input(T_NEXT):
        client.next()
        print("next")
    elif GPIO.input(T_PREV):
        client.previous()
        print("prev")
    elif GPIO.input(T_PLAY):
        if dictionary['pause'] == 1:
            dictionary['pause']=0
            client.pause(0)
            print("resume")
        else:
            dictionary['pause']=1
            client.pause(1)
            print("pause")

def connectMPD(client):
    try:
        client.timeout=10
        client.idletimeout=None
        client.connect(MPD_HOST, MPD_PORT)
    except mpd.ConnectionError, err:
        if "Already connected" in err:
            print(err)
            return True, None
        else:
            return False, err
    if MPD_PASSWORD:
        try:
            client.password=MPD_PASSWORD
        except mpd.CommandError, err:
            disconnectMPD(client)
            return False, err
    return True, None

def disconnectMPD(client):
    try:
        client.close()
        client.disconnect()
    except mpd.ConnectionError:
        pass

def get_songtitle(client):
    try:
        PlayCurrent = client.currentsong()
    except mpd.ConnectionError, e:
        print('Connection Error: ' + str(e))
        connected = connectMPD(client)
        PlayCurrent = client.currentsong()
    try: title_name = PlayCurrent['title']
    except: title_name = None
    #dictionary['song_title']=title_name
    return title_name

def get_station(client):
    try:
        PlayCurrent = client.currentsong()
    except mpd.ConnectionError, e:
        print('Connection Error: ' + str(e))
        connected = connectMPD(client)
        PlayCurrent = client.currentsong()
    try: station_name = PlayCurrent['name']
    except: station_name = None
    if not station_name:
        try: station_name = PlayCurrent['album']
        except: station_name = None
    if not station_name:
        station_name = None
    return station_name


def scroll(client):
    title = get_songtitle(client).decode('utf-8', errors='ignore')
    #sender = get_station(client).decode('utf-8', errors='ignore')
    if title and len(title) > 16:
        for i in range (0, len(title)):
            lcd(1, title[i:(i+15)])
            time.sleep(0.4)


if __name__ == '__main__':
    try:
        client = mpd.MPDClient()
        connected = connectMPD(client)
        client.setvol(dictionary['Volume'])
        GPIO.add_event_detect(T_PREV, GPIO.RISING, callback=partial(interrupt_event, client), bouncetime=200)
        GPIO.add_event_detect(T_NEXT, GPIO.RISING, callback=partial(interrupt_event, client), bouncetime=200)
        GPIO.add_event_detect(T_PLAY, GPIO.RISING, callback=partial(interrupt_event, client), bouncetime=200)
        print("Ready!")
        while True:
            scroll(client)
    except (KeyboardInterrupt, SystemExit):
        print("\nQuit\n")
    GPIO.cleanup()


Create a new paste based on this one


Comments: