Skip to content

Instantly share code, notes, and snippets.

@mohanjith
Created February 19, 2015 10:48
Show Gist options
  • Save mohanjith/e2c9ba336b80805e2aca to your computer and use it in GitHub Desktop.
Save mohanjith/e2c9ba336b80805e2aca to your computer and use it in GitHub Desktop.
Python script for Raspberry Pi Bv2 with Adafruit 2.8" TFT resistive touch screen that will play/pause playback, play Yle X3M or Radio Nova with tactile buttons using GPIO
import RPi.GPIO as GPIO
from mpd import MPDClient
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
global client
global radio1, radio2
radio1 = False
radio2 = False
def reconnect_mpd():
global client
client = MPDClient()
client.timeout = 10
client.idletimeout = None
noConnection = True
while noConnection:
try:
client.connect("localhost", 6600)
noConnection=False
except Exception, e:
print e
noConnection=True
time.sleep(15)
print "Connection Established!"
def get_playback_status():
global client
try:
status = client.status()
s = status["state"]
status = client.status()
s = status["state"]
except Exception as e:
print e
s = "error"
return s
def play_pause(channel):
global client
status = get_playback_status()
if status == "error":
reconnect_mpd()
status = get_playback_status()
if status == "pause":
print "Resume"
client.pause(0)
elif status == "play":
print "Pause"
client.pause(1)
else:
print "Play"
client.play()
def play_radio1(channel):
global client, radio1
print "Yle X3M"
if get_playback_status() == "error":
reconnect_mpd()
if radio1 == False:
radio1 = client.addid("mms://mediau.yle.fi/livex3m")
client.playid(radio1)
def play_radio2(channel):
global client, radio2
print "Radio Nova"
if get_playback_status() == "error":
reconnect_mpd()
if radio2 == False:
radio2 = client.addid("http://icelive0.41168-icelive0.cdn.qbrick.com/5050/41168_radionova1.mp3")
client.playid(radio2)
GPIO.add_event_detect(18, GPIO.RISING, callback=play_pause, bouncetime=300)
GPIO.add_event_detect(27, GPIO.RISING, callback=play_radio1, bouncetime=300)
GPIO.add_event_detect(22, GPIO.RISING, callback=play_radio2, bouncetime=300)
reconnect_mpd()
while True:
time.sleep(15)
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment