[GUIDE] use a numpad to control RuneAudio on raspi

Suggestions/requests/ideas for RuneAudio core features and functions

[GUIDE] use a numpad to control RuneAudio on raspi

Postby waves » 30 Mar 2016, 17:35

Guide: use a numpad (usb or wireless) for basic RuneAudio controls like play/pause, volume, next/previous track and to load a few favorite playlists.

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.

1.jpg
1.jpg (73.84 KiB) Viewed 4761 times


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
Last edited by waves on 01 Apr 2016, 11:23, edited 9 times in total.
waves
 
Posts: 125
Joined: 31 Dec 2014, 10:33

Re: [GUIDE] use a numpad to control RuneAudio on raspi

Postby hondagx35 » 30 Mar 2016, 23:22

Hi waves,

thank you for the nice tutorial.

Code: Select all
[Unit]
    Description=start numpad.sh on boot
    Requires=
    After=

    [Service]

    Type=forking
    ExecStart=/script/numpad.sh

    [Install]
    WantedBy=multi-user.target


(Note: on boot numpad.sh runs and then starts numpad.py . This is a workaround since I had trouble getting numpad.py to run directly from a service.)


You have to specify the full path:
Code: Select all
ExecStart=/usr/bin/python2 /script/numpad.py


Frank
User avatar
hondagx35
 
Posts: 3042
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: [GUIDE] use a numpad to control RuneAudio on raspi

Postby waves » 31 Mar 2016, 08:42

hondagx35 wrote:You have to specify the full path:
Code: Select all
ExecStart=/usr/bin/python2 /script/numpad.py


Thank you Frank. I have python3 (don't remember if I updated that myself or not) so I had to set
Code: Select all
ExecStart=/usr/bin/python3 /script/numpad.py

and remove the line
Code: Select all
Type=forking


I'll shortly update the guide and remove the numpad.sh bit.
edit: updated now
waves
 
Posts: 125
Joined: 31 Dec 2014, 10:33

Re: [GUIDE] use a numpad to control RuneAudio on raspi

Postby Tall Person » 10 Nov 2017, 00:43

...so do I use the .py in this file or .sh?
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

Re: [GUIDE] use a numpad to control RuneAudio on raspi

Postby waves » 18 Nov 2017, 12:43

Tall Person wrote:...so do I use the .py in this file or .sh?


There is no .sh file in the latest version of the tutorial, simply follow the steps in the first post.
waves
 
Posts: 125
Joined: 31 Dec 2014, 10:33

Re: [GUIDE] use a numpad to control RuneAudio on raspi

Postby Fredrik » 04 Mar 2021, 16:43

Thank you very much for this excellent tutorial!
I am happy to report that this method also works with my USB volume knob.

https://www.amazon.com/Control-AIMOS-Lo ... 258&sr=8-5
Raspberry Pi 3 Model B Plus
Inno-maker HIFI AMP HAT (Running as HIFIBerry Amp)
AIMOS USB Volume Control
20 x 4 LCD HD44780
Fredrik
 
Posts: 3
Joined: 04 Mar 2021, 16:25

support RuneAudio Donate with PayPal


Return to Feature request

Who is online

Users browsing this forum: No registered users and 4 guests
cron