Internet Radio Player with Raspberry Pi, PiFace CAD and MPD/MPC

I recently worked with the PiFace Control and Display for my Santa Catcher, in which I used the PiFace CAD as an IR receiver and display without using any of the local controls.
So I decided to create an Internet Radio player which could be controlled using the PiFace CAD’s buttons.

The result is mainly a software project, but I had fun diving a little deeper into the Python programming language.

Project Components

For this project, I used following components:

  • Raspberry Pi Model B running Raspbian
  • PiFace Control and Display
  • Enclosure for Pi and PiFace CAD
  • Wi-Pi
  • Portable speaker
  • 5V 1A micro USB power supply

IMG_3150

Required Software

As mentioned earlier, I started off by using Raspbian with wifi and ssh enabled.

These were the modifications on top of Raspbian to get everything I needed up and running:

  • Updated Raspbian to latest version
    • sudo apt-get upgrade
    • sudo apt-get update
  • Enabled SPI support for the PiFace CAD
    • sudo raspi-config
    • Select option 8: “Advanced options”
    • Select option A5: “SPI”
    • Select “Yes”
    • Select “Finish” to exit
  • Installed the mpd (music player daemon) and mpc (music player controller) applications
    • sudo apt-get install mpd mpc

Python Script

From this point on, everything is done via Python.

Using Python, I am able to control the mpc application using the PiFace CAD’s buttons and display the status on its LCD.

Code explanation

You can find the full code a bit further down, but I’ll try to explain some bits of code here.

Custom bitmaps

I wanted to display some icons on the LCD to clarify the meaning of some values being displayed or to visualise the current state.
It is possible to define “custom bitmaps” to be displayed. This tool comes in very handy to create your own. (be sure to select 5×8 when using PiFace CAD)

Following function defines and stores custom bitmaps:

The custom bitmaps can then be displayed on the LCD as follows, using the id of the corresponding bitmap:

MPC status

In order to display correct information on the LCD (volume & playlist position and size), I rely on the information coming from the mpc application itself.

A sample output looks as follows:

Using Python, it is possible to execute that command, take the output and parse the interesting bits of information.

Playlist:

This bit of code executes the “mpc status” command and only keeps the part of the output containing the word “playing”.
Then, it parses the output and keeps the part after “#” and up to two character after the “/”. This covers playlists of up to 99 items.

The playlist information is then written to the LCD, with an icon to clarify the meaning of the value.

Volume:

Similar to the playlist parsing, this bit of code takes the output of the “mpc status” command containing the word “volume”.
It parses the characters after the word “volume” up to and including the “%” character.

The volume is then written to the LCD.

Event listener

The script needs to know when a button on the PiFace CAD has been pressed and trigger the appropriate action.

I recuperated this bit of code from one of the PiFace examples and extended it to fit my application.

First, a listener is defined which will register which button has been pressed:

The listener executes the specified function in which it execute different actions depending on the button that was pressed:

Above actions correspond to following button mapping:

IMG_3152

Full code

The code still requires to be cleaned up a little, but it should be clear what I’m trying to do.

The code is available on GitHub.

Autostart

The script needs to be started automatically after the system booted. This is done by adding following entry to /etc/rc.local:

Demo

Leave a Reply

Your email address will not be published. Required fields are marked *