1

Okay, so I am building a sweet vintage style boombox for my nana using a RCA Victor X551 case, a Rpi3, running runeaudio. I have most of everything figured out, but could use some help with the programming end. I have a python file which will play/pause the audio, but I don't have it actually working. I don't understand where to place the file, or what it needs to run as a "service"?

Here is my file:

    !/usr/bin/env python
    import RPi.GPIO as GPIO
    import time
    import subprocess
    GPIO.setmode(GPIO.BOARD)

    # Select unused GPIO header pin to be used to toggle play/pause
    InputPin = 13

    # Set selected pin to input, and enable internal pull-up
    GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    # Wait for a button press on the selected pin (pulled to ground, falling edge)
    GPIO.wait_for_edge(InputPin, GPIO.FALLING)

    # When pressed, toggle play/pause
    mpc toggle

note: I stole this from someone who posted it on the runeaudio forums. I don't understand it. Is like 1 what I am looking for? subprocess?

I also have a rotary encoder which I would like to use as volume control. I am new to all this maker stuff, and any help sure would be appreciated. I do know the mpc commands I would like to use... but that's about it.

  • If you have trouble getting the script executing properly at boot, you could combine Domme's answer with this. – goldilocks Jul 26 '16 at 16:09
  • I know this is an old question now, but i've written a kernel driver for using a rotary encoder as a volume control, source and instructions can be found here: github.com/JamesGKent/rotary_volume – James Kent Apr 6 '18 at 15:21
  • @JamesKent That is awesome! I am still taking retro radios I find at antique shops, gutting them, replacing with rPi and volumio these days (great xmas presents) and have about 15 rotary encoders left. Next time I start in on one I will absolutely use your driver. Nice work! – Jakob Vendegna Apr 7 '18 at 13:29
  • @Jay5 good to know, I can't guarantee it will work with all software (there is a bit of a hack in there to make it work with kodi) but I'd be glad to know it helped someone else. If you have any issues or want to get it working with another OS or software let me know, I'd be happy to help – James Kent Apr 7 '18 at 13:33
1

If your code is working fine and you just need it to be run whenever after each boot process, you can create a systemd service:

  1. Create a file called something like "/etc/systemd/system/pyscriptstarter.service" which should contain the following:

    [Unit]
    Description=Fancy Bash script to start fancy Python script
    
    [Service]
    ExecStart=/usr/bin/bashscript.sh
    
    [Install]
    WantedBy=multi-user.target 
    
  2. Create the file "/usr/bin/bashscript.sh" which should contain:

    python /path_to_python_script/pyscript.py
    
  3. Make "/usr/bin/bashscript.sh" executable with:

    sudo chmod 755 /usr/bin/bashscript.sh
    
  4. Enable the service with:

    sudo systemctl enable pyscriptstarter.service
    

Of course, you have to edit the line to match your path and the script's filename. Good luck!

  • Thank you for your reply, but I don't have that file. I am running runeaudio, which uses archlinux, not debian based. I know the file I am looking for is likely in /etc I just need to dig a bit. – Jakob Vendegna Jul 26 '16 at 13:19
  • Oops, have you tried creating a systemd service? I modified my answer, hope it works for you now. – Domme Jul 26 '16 at 15:17
  • Since that process is persistent, I think you should use Type=forking in the [Service] section and then actually fork to the background in the shell script, i.e., add an & to the end of python /path_to_python_script/pyscript.py. With a space, so python /path_to_python_script/pyscript.py &. Otherwise this may run in the foreground, which init (systemd) will not like if it lasts more than a few seconds. – goldilocks Jul 26 '16 at 15:58
  • Also it's generally tidier to use /usr/local/bin for stuff like this, esp. to avoid name collisions with system software. – goldilocks Jul 26 '16 at 16:07
  • Thank you. I think I've got it figured out now. I really appreciate the help. It took a lot more reading, but I now understand the Python file, have re written it, and started as a system service at boot. I'm not sure if it's running in the foreground or background. So the & at the end will ensure its running in the background, correct? – Jakob Vendegna Jul 27 '16 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.