Numpads are inexpensive and small and convenient as a quick control panel in addition to the regular web interface.
This might also be easier for some to set up compared to adding a hardware knob to the Rune device. There are also wireless versions (with a USB dongle and long battery life) which you can place anywhere you want. You can cover the button labels with new printed labels or some plastic tape with labels drawn with a marker.
Overview:
1 create numpad.py and numpad_codes_test.py
2 use numpad_codes_test.py to get codes for numpad keys
3 update numpad.py with key codes and the actions you want
(for playlist actions also create playlists in Rune with matching names)
4 create a service that starts numpad.py on boot
Note: The steps presuppose that you have RuneAudio on a Raspberry Pi and SSH in and can run commands in the terminal.
Note: I did this on a Rune Raspberry install that has Python3. If you have Python2 (there is a step below for checking that) and try this then post a comment afterwards and say if it worked or if some change is needed for Python2 users. I'll then update the guide.
1 create numpad.py and numpad_codes_test.py
- navigate to the raspi device root and create a directory named "script"
- Code: Select all
mkdir script
- save the two files below to the newly created directory
numpad.py
- Code: Select all
import sys, time, os, subprocess
# USB numpad keys to mpc commands
fp = open('/dev/hidraw0', 'rb')
global t
t = time.time()
print("USB numpad keys to mpc commands")
while True:
buffer = fp.read(8)
if time.time() - t > 0.1: #prevents too many commands
for c in buffer:
if c != 0:
if c == 98: os.system("mpc volume -10") # numpad 0
if c == 99: os.system("mpc volume +10") # numpad del
if c == 92: os.system("mpc prev") # numpad 4
if c == 94: os.system("mpc next") # numpad 6
#load and play a playlist
if c == 86 or c == 42 or c == 85: # numpad minus or backspace or *
os.system("mpc clear") #clear queue
if c == 86: pl = "rock" #playlist name
if c == 42: pl = "blues"
if c == 85: pl = "soul"
os.system("mpc load " + pl) #load playlist
os.system("mpc volume 70")
os.system("mpc play")
#toggle between play and (pause or stop)
#Rune may freeze if webstream paused long, so stop streams
#todo find method to check if any webstream is playing
#temporary method: check for name of streams
if c == 88: #numpad enter
x = subprocess.check_output(["mpc", "status"])
if"[playing]" in str(x):
if "bbc_radio" in str(x) or "some other stream name" in str(x):
os.system("mpc stop")
else:
os.system("mpc pause")
else: os.system("mpc play")
print(c)
t = time.time()
# based on https://www.raspberrypi.org/forums/viewtopic.php?f=45&t=55100
# but Rune has python3 where c is int, so do not use ord()
numpad_codes_test.py
- Code: Select all
import sys, time, os
# USB numpad keys to mpc commands
# use this to check what key has what code
fp = open('/dev/hidraw0', 'rb')
global t
t = time.time()
print("USB numpad keys to mpc commands -- codes tester")
while True:
buffer = fp.read(8)
if time.time() - t > 0.1: #prevents too many commands
for c in buffer:
if c != 0:
print(c)
t = time.time()
# based on https://www.raspberrypi.org/forums/viewtopic.php?f=45&t=55100
# but python3 -> c is int, so do not use ord()
2 use numpad_codes_test.py to get codes for numpad keys
- run
- Code: Select all
python /script/numpad_codes_test.py
- press a key on the numpad. A two or three digit code will be printed on screen. Write a list of numpad keys and their codes.
- when done exit with ctrl+C
3 update numpad.py with key codes and the actions you want
- run
- Code: Select all
nano /script/numpad.py
- change the codes and the actions to do what you want.
(Type "mpc help" in terminal for a list of actions/commands)
Note that for playlist actions to work you must save a playlist in RuneAudio with the same name. For example if you set
- Code: Select all
if c == 86: pl = "rock"
then you must also have a playlist in RuneAudio named "rock".
- save the file after editing
- make the files executable (not sure but think this is needed). Run
- Code: Select all
chmod +x /script/numpad.py
- Now let us test it. Run
- Code: Select all
python /script/numpad.py
and check that everything works. If not go back and edit numpad.py again to your liking.
- exit the test with ctrl+C
4 create a service that automatically starts numpad.py on boot
- first check if you have Python version 3.something
- Code: Select all
python --version
If your version is "Python 2.something" then change "python3" in the service file code below to "python2". Otherwise leave the code unchanged.
- run
- Code: Select all
nano /etc/systemd/system/numpad.service
- paste this service file code and save
- Code: Select all
[Unit]
Description=start numpad.py on boot
Requires=
After=
[Service]
ExecStart=/usr/bin/python3 /script/numpad.py
[Install]
WantedBy=multi-user.target
- run
- Code: Select all
systemctl enable numpad.service
- doublecheck that the service is enabled
- Code: Select all
systemctl is-enabled numpad.service
- Now we are all set. Reboot and if all went well the numpad should now work.
Feel free to comment with any improvement you can think of to this guide.
changelog:
- skipped extra step with .sh file, thanks frank.
- shorter code, fixed small error