Hardware Play/Pause Button

Raspberry Pi related support

Hardware Play/Pause Button

Postby rlsten » 30 May 2016, 14:54

I am working on an enclosure for a Raspberry Pi 3 running Rune Audio (the latest image). Ideally, I would like to use a 3.5" touch screen, but I can't find one that works very well. The Tontec screen is resistive and touch works very erratically. So, until a better screen comes along, I was thinking of just using the screen to show what is playing and select music remotely.

I was wondering if it was possible to set up a hardware switch or button to toggle between play and pause; that is, if Rune Audio was playing, pushing the button would pause it, and if Rune Audio was paused, pushing the button would start playback. The button wouldn't have a state per se; if would simply trigger the opposite of whatever state Rune Audio was in at the moment.

If this is possible then,
1) What type of button would work - momentary or some other kind? Preferably, the button should be as small as possible.
(2) What GPIO wiring would be necessary?
and
(3) What software modifications would be necessary?

Thanks very much,
Rod
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Hardware Play/Pause Button

Postby PeteB » 30 May 2016, 16:07

rlsten wrote:1) What type of button would work - momentary or some other kind? Preferably, the button should be as small as possible.
Any kind of momentary push button, small or large, a lot depends on how you mount it in your enclosure

rlsten wrote:(2) What GPIO wiring would be necessary?
One wire to an unused GPIO pin, and one wire to ground. The GPIO uses a weak internal pullup, so it is normally high. When the button is pressed, the pin goes low.

rlsten wrote:(3) What software modifications would be necessary?
You will need a small python script to execute a command to toggle the play/pause, and a service. At least that is the method I used to program a momentary switch to execute a soft shutdown.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby rlsten » 30 May 2016, 19:36

Thanks for the info, Pete!

I ordered up a couple of momentary switch buttons from Radio Shack.

As for the script, I have shamelessly copied your script for your momentary switch soft shutdown, and modified it to toggle play/pause. Do you think this will work, or do I need something more:

Code: Select all
!/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
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Hardware Play/Pause Button

Postby PeteB » 30 May 2016, 20:06

(chuckle) I copied both "my" script and the service from other people, so it must be ok... the point of that other thread was to encourage people to try it and to think of new stuff to diy.

It may be better to change "python" to "python2", although it may work as is, not sure. Python2 is included in RuneAudio by default.

Here is the service I used when I was testing play/pause. It assumes that the script has been moved into the command directory recommended by Frank, and made executable using chmod +x <scriptname>.

/usr/lib/systemd/system/hw_pause_play.service:
(edit: corrected a misplaced # symbol)
Code: Select all
[Unit]
Description=RuneAudio Pause/Play
After=mpd.service

[Service]
ExecStart=/var/www/command/hw_pause_play.py
TimeoutSec=0
Restart=always
RestartSec=1
StartLimitInterval=30
StartLimitBurst=20

[Install]
WantedBy=multi-user.target


Enable the service, and start it. Make sure the service has started with no errors.

Code: Select all
systemctl enable hw_pause_play

systemctl start hw_pause_play

systemctl status hw_pause_play


The systemctl commands I used while debugging were something like this:

enable, disable, start, stop, pause, status

*** edit ***

I assume you plan to use a subprocess call for the mpc toggle command, right?
Last edited by PeteB on 31 May 2016, 01:23, edited 1 time in total.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby PeteB » 30 May 2016, 20:42

I have only had time to prototype one momentary button, an IR sensor, and a power supply using the header pins instead of the micro-usb connector. The momentary button is assigned a function by the python script, so I can use it to test the soft-off function, play-pause, or any other single function.

Feel free to pick up where I left off... :D

Image


Image
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby rlsten » 30 May 2016, 22:04

Thanks again, Pete, for all your help. I can't wait to try it.

I assume you plan to use a subprocess call for the mpc toggle command, right?


After I posted that script I realized I had missed something on that line; however, I'm really not familiar with how exactly to make that subprocess call.

May I impose on you again Pete for the correct format?

Thanks,
Rod
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Hardware Play/Pause Button

Postby PeteB » 30 May 2016, 22:11

rlsten wrote:...May I impose on you again Pete for the correct format?


Code: Select all
.
.
.
# When pressed, execute this command:
print "*** Button press detected ***" # (print statement added only for debugging)
subprocess.call("mpc toggle", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


I realize the other thread was left hanging. I will try to type up all my notes in the next day or so, and post them into this one.

Regards,

Pete
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby rlsten » 30 May 2016, 22:46

Thanks very much, Pete.

Rod
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Hardware Play/Pause Button

Postby rastus » 31 May 2016, 13:03

Nice work PeteB, as you may know I went for the button on the USB cable... your setup looks efficient, but too complex for me. Eeewww..... :geek:
User avatar
rastus
 
Posts: 353
Joined: 21 Aug 2015, 10:29

Re: Hardware Play/Pause Button

Postby PeteB » 31 May 2016, 13:29

rastus wrote:...your setup looks efficient, but too complex for me. Eeewww..... :geek:

This is true, but the prototype has three separate circuits on a tiny perf board, soon to be four. The ribbon cable makes it easier to disconnect/reconnect it. Otherwise I would have a little rats nest of wires on top of the Pi, and have to remember which one goes where... :roll:

For a momentary push button, all you need is two wires, like this:

Image

In the case above, the perf board adds nothing to the push button, it is only there to supply cleaner 5V through the 40-pin header, instead of the micro USB.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

support RuneAudio Donate with PayPal

Next

Return to Raspberry Pi

Who is online

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