Skip to content

Instantly share code, notes, and snippets.

@raspberrytipsnl
Created February 3, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspberrytipsnl/151b6386719518487f65c399447dae0e to your computer and use it in GitHub Desktop.
Save raspberrytipsnl/151b6386719518487f65c399447dae0e to your computer and use it in GitHub Desktop.
Internet radio Raspberry Pi + PiFace CAD
#!/usr/bin/env python3
# requires `mplayer` to be installed
# v1.0
# https://raspberrytips.nl/internet-radio-luisteren-raspberry-pi/
from time import sleep
import os
import sys
import signal
import shlex
import math
import lirc
PY3 = sys.version_info[0] >= 3
if not PY3:
print("Radio only works with python3")
sys.exit(1)
from threading import Barrier
import subprocess
import pifacecommon
import pifacecad
from pifacecad.lcd import LCD_WIDTH
UPDATE_INTERVAL = 1
STATIONS = [
{'name': "NPO Radio 2",
'source': 'http://icecast.omroep.nl/radio2-bb-mp3',
'info': "NPO Radio 2"},
{'name': "NPO 3FM",
'source': 'http://icecast.omroep.nl/3fm-bb-mp3',
'info': None},
{'name': "Radio 538",
'source': 'http://vip-icecast.538.lw.triple-it.nl/RADIO538_MP3.m3u',
'info': None},
{'name': "Q-music",
'source': 'http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3.m3u',
'info': None},
{'name': "Sky Radio",
'source': 'http://provisioning.streamtheworld.com/pls/skyradio.pls',
'info': None}
]
PLAY_SYMBOL = pifacecad.LCDBitmap(
[0x10, 0x18, 0x1c, 0x1e, 0x1c, 0x18, 0x10, 0x0])
PAUSE_SYMBOL = pifacecad.LCDBitmap(
[0x0, 0x1b, 0x1b, 0x1b, 0x1b, 0x1b, 0x0, 0x0])
PLAY_SYMBOL_INDEX = 0
PAUSE_SYMBOL_INDEX = 1
class Radio(object):
def __init__(self, cad, start_station=0):
self.current_station_index = start_station
self.playing_process = None
cad.lcd.blink_off()
cad.lcd.cursor_off()
cad.lcd.backlight_on()
cad.lcd.store_custom_bitmap(PLAY_SYMBOL_INDEX, PLAY_SYMBOL)
cad.lcd.store_custom_bitmap(PAUSE_SYMBOL_INDEX, PAUSE_SYMBOL)
self.cad = cad
@property
def current_station(self):
"""Returns the current station dict."""
return STATIONS[self.current_station_index]
@property
def playing(self):
return self._is_playing
@playing.setter
def playing(self, should_play):
if should_play:
self.play()
else:
self.stop()
@property
def text_status(self):
"""Returns a text represenation of the playing status."""
if self.playing:
return "Now Playing"
else:
return "Stopped"
def play(self):
"""Plays the current radio station."""
print("Playing {}.".format(self.current_station['name']))
if self.current_station['source'].split("?")[0][-3:] in ['m3u', 'pls']:
play_command = "mplayer -quiet -playlist {stationsource}".format(
stationsource=self.current_station['source'])
else:
play_command = "mplayer -quiet {stationsource}".format(
stationsource=self.current_station['source'])
self.playing_process = subprocess.Popen(
play_command,
shell=True,
preexec_fn=os.setsid)
self._is_playing = True
self.update_display()
def stop(self):
"""Stops the current radio station."""
print("Stopping radio.")
os.killpg(self.playing_process.pid, signal.SIGTERM)
self._is_playing = False
self.update_playing()
def change_station(self, new_station_index):
"""Change the station index."""
was_playing = self.playing
if was_playing:
self.stop()
self.current_station_index = new_station_index % len(STATIONS)
if was_playing:
self.play()
def next_station(self, event=None):
self.change_station(self.current_station_index + 1)
def previous_station(self, event=None):
self.change_station(self.current_station_index - 1)
def update_display(self):
self.update_station()
self.update_playing()
def update_playing(self):
"""Updated the playing status."""
if self.playing:
char_index = PLAY_SYMBOL_INDEX
else:
char_index = PAUSE_SYMBOL_INDEX
self.cad.lcd.set_cursor(0, 0)
self.cad.lcd.write_custom_bitmap(char_index)
def update_station(self):
"""Updates the station status."""
message = self.current_station['name'].ljust(LCD_WIDTH-1)
self.cad.lcd.set_cursor(1, 0)
self.cad.lcd.write(message)
def toggle_playing(self, event=None):
if self.playing:
self.stop()
else:
self.play()
def close(self):
self.stop()
self.cad.lcd.clear()
self.cad.lcd.backlight_off()
def radio_preset_switch(event):
global radio
radio.change_station(event.pin_num)
if __name__ == "__main__":
try:
subprocess.call(["mplayer"], stdout=open('/dev/null'))
except OSError as e:
if e.errno == os.errno.ENOENT:
print(
"MPlayer was not found, install with "
"`sudo apt-get install mplayer`")
sys.exit(1)
else:
raise
cad = pifacecad.PiFaceCAD()
global radio
radio = Radio(cad)
radio.play()
global end_barrier
end_barrier = Barrier(2)
switchlistener = pifacecad.SwitchEventListener(chip=cad)
for pstation in range(4):
switchlistener.register(
pstation, pifacecad.IODIR_ON, radio_preset_switch)
switchlistener.register(4, pifacecad.IODIR_ON, end_barrier.wait)
switchlistener.register(5, pifacecad.IODIR_ON, radio.toggle_playing)
switchlistener.register(6, pifacecad.IODIR_ON, radio.previous_station)
switchlistener.register(7, pifacecad.IODIR_ON, radio.next_station)
switchlistener.activate()
end_barrier.wait()
radio.close()
switchlistener.deactivate()
@raspberrytipsnl
Copy link
Author

Aansluitschema en overige informatie kun je terugvinden op:

https://raspberrytips.nl/internet-radio-luisteren-raspberry-pi/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment