Skip to content

Instantly share code, notes, and snippets.

@ednisley
Created February 16, 2016 00:27
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 ednisley/f60e5b4377e9fdb986d8 to your computer and use it in GitHub Desktop.
Save ednisley/f60e5b4377e9fdb986d8 to your computer and use it in GitHub Desktop.
Python Source Code: Raspberry Pi Simple-minded Streaming Radio
from evdev import InputDevice,ecodes,KeyEvent
import subprocess32
Media = {'KEY_KP7' : ['mplayer','http://relay.publicdomainproject.org:80/classical.aac'],
'KEY_KP8' : ['mplayer','http://relay.publicdomainproject.org:80/jazz_swing.aac'],
'KEY_KP9' : ['mplayer','http://live.str3am.com:2070/wmht1'],
'KEY_KP6' : ['mplayer','http://pubint.ic.llnwd.net/stream/pubint_wamc'],
'KEY_KP1' : ['mplayer','-playlist','http://dir.xiph.org/listen/5423257/listen.m3u'],
'KEY_KP2' : ['mplayer','-playlist','http://dir.xiph.org/listen/5197460/listen.m3u'],
'KEY_KP3' : ['mplayer','-playlist','http://dir.xiph.org/listen/5372471/listen.m3u'],
'KEY_KP0' : ['mplayer','-playlist','http://dir.xiph.org/listen/5420157/listen.m3u']
}
Controls = {'KEY_KPSLASH' : '/',
'KEY_KPASTERISK' : '*',
'KEY_KPDOT' : ' '
}
k=InputDevice('/dev/input/keypad')
print 'Starting mplayer'
p = subprocess32.Popen(Media['KEY_KP7'],stdin=subprocess32.PIPE)
print ' ... running'
for e in k.read_loop():
if (e.type == ecodes.EV_KEY) and (KeyEvent(e).keystate == 1):
kc = KeyEvent(e).keycode
if kc == 'KEY_NUMLOCK':
continue
print "Got: ",kc
if kc == 'KEY_BACKSPACE':
print 'Backspace = shutdown!'
p = subprocess32.call(['sudo','halt'])
break
if kc in Controls:
print 'Control:', kc
p.stdin.write(Controls[kc])
if kc in Media:
print 'Switching stream to ',Media[kc]
print ' ... halting'
p.communicate(input='q')
print ' ... restarting'
p = subprocess32.Popen(Media[kc],stdin=subprocess32.PIPE)
print ' ... running'
print "Out of loop!"
@ednisley
Copy link
Author

More details on my blog at http://wp.me/poZKh-5DW

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