-
-
Save fvdbosch/d16d30abeef939e88bb6 to your computer and use it in GitHub Desktop.
Internet Radio Player with Raspberry Pi, PiFace CAD and MPD/MPC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pifacecad | |
import os | |
import sys | |
import subprocess | |
from time import sleep | |
backlight = 0 | |
status = "stopped" | |
channel_pos = 0 | |
channelLink = ["mms://streaming.q-music.be/QBE_HI", "http://mp3.streampower.be/stubru-high.mp3"] | |
channelName = ["Q-Music BE", "Studio Brussel"] | |
# Update display and perform action depending on button pressed | |
def update_pin_text(event): | |
global channel_pos | |
global status | |
if(event.pin_num == 0): | |
status = "playing" | |
os.system('mpc play') | |
event.chip.lcd.set_cursor(0, 1) | |
event.chip.lcd.write_custom_bitmap(1) | |
event.chip.lcd.write(" ") | |
display_channel() | |
display_playlist() | |
elif(event.pin_num == 1): | |
status = "stopped" | |
os.system('mpc stop') | |
event.chip.lcd.set_cursor(0, 1) | |
event.chip.lcd.write_custom_bitmap(2) | |
event.chip.lcd.write(" ") | |
clear_channel() | |
elif(event.pin_num == 2 and status == "playing" and channel_pos > 1): | |
os.system('mpc prev') | |
clear_channel() | |
display_channel() | |
display_playlist() | |
elif(event.pin_num == 3 and status == "playing" and channel_pos < len(channelLink)): | |
os.system('mpc next') | |
clear_channel() | |
display_channel() | |
display_playlist() | |
elif(event.pin_num == 4): | |
global backlight | |
if(backlight == 1): | |
event.chip.lcd.backlight_off() | |
backlight = 0 | |
else: | |
event.chip.lcd.backlight_on() | |
backlight = 1 | |
elif(event.pin_num == 5): | |
sleep(1) | |
elif(event.pin_num == 6): | |
os.system('mpc volume -5') | |
display_volume() | |
elif(event.pin_num == 7): | |
os.system('mpc volume \+5') | |
display_volume() | |
else: | |
sleep(1) | |
# Create the playlist with configurable internet radio channels | |
def create_playlist(): | |
subprocess.check_output("mpc clear", shell=True, stderr=subprocess.STDOUT, universal_newlines=True) | |
for channel in channelLink: | |
subprocess.check_output("mpc add " + channel, shell=True, stderr=subprocess.STDOUT, universal_newlines=True) | |
# Define and store custom bitmaps to be displayed | |
def custom_bitmaps(): | |
speaker = pifacecad.LCDBitmap([1,3,15,15,15,3,1,0]) | |
play = pifacecad.LCDBitmap([0,8,12,14,12,8,0,0]) | |
stop = pifacecad.LCDBitmap([0,31,31,31,31,31,0,0]) | |
playlist = pifacecad.LCDBitmap([2,3,2,2,14,30,12,0]) | |
cad.lcd.store_custom_bitmap(0, speaker) | |
cad.lcd.store_custom_bitmap(1, play) | |
cad.lcd.store_custom_bitmap(2, stop) | |
cad.lcd.store_custom_bitmap(3, playlist) | |
# Init display by showing idle state | |
def init_display(): | |
cad.lcd.blink_off() | |
cad.lcd.cursor_off() | |
cad.lcd.clear() | |
cad.lcd.set_cursor(11, 1) | |
cad.lcd.write_custom_bitmap(0) | |
display_volume() | |
# Retrieve and display channel name based on playlist position | |
def display_channel(): | |
global channel_pos | |
channel = subprocess.check_output("mpc status | grep playing", shell=True, stderr=subprocess.STDOUT, universal_newlines=True) | |
channel = channel[channel.find("#")+1:channel.find("/")] | |
channel_pos = int(channel) | |
channel = channelName[int(channel)-1] | |
cad.lcd.set_cursor(0, 0) | |
cad.lcd.write(channel) | |
# Write an empty row | |
def clear_channel(): | |
cad.lcd.set_cursor(0, 0) | |
cad.lcd.write(" ") | |
# Retrieve plylist position and size from mpc and display | |
def display_playlist(): | |
playlist = subprocess.check_output("mpc status | grep playing", shell=True, stderr=subprocess.STDOUT, universal_newlines=True) | |
playlist = playlist[playlist.find("#")+1:playlist.find("/")+2] | |
cad.lcd.set_cursor(4, 1) | |
cad.lcd.write_custom_bitmap(3) | |
cad.lcd.write(playlist) | |
# Retrieve volume level from mpc and display | |
def display_volume(): | |
volume = subprocess.check_output("mpc status | grep volume", shell=True, stderr=subprocess.STDOUT, universal_newlines=True) | |
volume = volume[7:volume.find("%")+1] | |
cad.lcd.set_cursor(12, 1) | |
cad.lcd.write(volume) | |
cad = pifacecad.PiFaceCAD() | |
custom_bitmaps() | |
create_playlist() | |
init_display() | |
listener = pifacecad.SwitchEventListener(chip=cad) | |
for i in range(8): | |
listener.register(i, pifacecad.IODIR_FALLING_EDGE, update_pin_text) | |
listener.activate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment