Hardware Play/Pause Button

Raspberry Pi related support

Re: Hardware Play/Pause Button

Postby rastus » 31 May 2016, 13:50

Ohhhh.... so your powering the HifiBerry DAC separately to the Pi.... or powering the Pi via the power to the DAC?

Edit: I checked again.... You are... this is the better option. So sound wise, it is better yes? No? Than powering the DAC via the Pi's micro USB? What about powering the Pi and the DAC separately to each other... have you tried that? Both having separate power supplies?

I've thought of this with my DAC (PiFi)... how to power it separately to the Pi's power. It is, as I understand, better to isolate the power supply from the Pi's power supply.
User avatar
rastus
 
Posts: 353
Joined: 21 Aug 2015, 10:29

Re: Hardware Play/Pause Button

Postby PeteB » 31 May 2016, 14:25

Correct. The micro USB is bypassed. I am supplying power via a heavier cable, a conventional barrel connector, through a filter (not shown in the pics), and directly to the 40-pin header.

The DAC+ and the Pi are both supplied from the header, so the DAC does not have to get its power through the Pi. Noise generated on the Pi 2, stays on the Pi 2. Noise that gets out of the power supply (50mV max ripple), is caught by the filter.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby rastus » 31 May 2016, 14:37

A USB cable with a ferrite core/bead/thingie.... I bought some, attached one to the Pi's power micro USB cable, close to the Pi itself. Also, one on my powered USB hub's power cable, and another one on the USB cable from the USB hub to the Pi's USB connector. Not sure it does anything, made me feel good though... felt like I was an EE. Though... I think it's 'all things' considered, battery pack, cables, ferrites, powered USB hub, etc. Anyway, nice call.

Edit: Heard they can be useful on Ethernet cables too. Also, I keep the USB WiFi dongle away from the Pi itself by using the powered USB hub too. The Pi really is a DIY project, saves money too... and it's downright enjoyable too (especially when music is playing through it).
User avatar
rastus
 
Posts: 353
Joined: 21 Aug 2015, 10:29

Re: Hardware Play/Pause Button

Postby PeteB » 02 Jun 2016, 04:34

This is an example Python2 script for a hardware Play/Pause button using an mpc toggle command. It may seem a little long, because it includes some step-by-step directions, but the actual script is just four lines of code. The rest is just overhead.

This is the code I used to test a hardware play/pause function and the momentary push button shown in the pics above. It requires one momentary switch, connected to the Pi 40-pin header with two jumpers.

The script toggles play/pause once, when a button connected to header pin 7 (GPIO4) is pressed.
(the example assumes that python2 is already installed).

Directions:

Open an empty file in the editor, paste the example below into the file, and save the file.

Code: Select all
nano hw_pause_play.py


Code: Select all
#!/usr/bin/env python2
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BOARD)

#Select an unused GPIO header pin as input
InputPin = 7

# Set 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 (pin pulled to ground, falling edge)
GPIO.wait_for_edge(InputPin, GPIO.FALLING)

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


Make the file executable using chmod +x:

Code: Select all
chmod +x hw_pause_play.py


The new script can now be tested from the command line:

Code: Select all
python2 hw_pause_play.py


If you are satisfied it does what you want, save or copy the file to its permanent location

Code: Select all
cp hw_pause_play.py /var/www/command/


...and make sure it is executable in the new location!

Code: Select all
chmod +x /var/www/command/hw_pause_play.py
Last edited by PeteB on 02 Jun 2016, 05:13, edited 1 time in total.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby PeteB » 02 Jun 2016, 04:42

Example service for a hardware play/pause button.

In order for the script from the post above to run automatically on power up, it needs another small file to tell the os to create a service.

Directions:

First, create an empty systemd unit file, and paste the code below into it.

Code: Select all
nano /usr/lib/systemd/system/hw_pause_play.service


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.

Code: Select all
systemctl enable hw_pause_play


Start the service.

Code: Select all
systemctl start hw_pause_play


Make sure the service has started with no errors

Code: Select all
systemctl status hw_pause_play


Reboot and test the pause/play button function.
Last edited by PeteB on 02 Jun 2016, 05:06, edited 1 time in total.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby PeteB » 02 Jun 2016, 04:56

Some useful links:

Link to a guide to GPIO pins on all models of the Pi:

http://www.raspberrypi-spy.co.uk/2012/0 ... -and-pins/

The script above uses an mpc command to toggle play/pause. MPC is a useful command-line client for MPD (Music Player Daemon), the software that controls the actual playback of music files in RuneAudio. There is a mpc command corresponding to every function in the RuneAudio playback screen. (The same mpc commands can be used to map buttons on a remote control.) Link to a man page for mpc including commands and syntax:

http://manpages.ubuntu.com/manpages/pre ... mpc.1.html

Link to more about systemd, unit files, and systemctl commands in the Arch man pages (unlike the rest of the stuff above, this part is not exactly light reading):

https://wiki.archlinux.org/index.php/Systemd

Here is how you can confirm that python2 is already installed:

Code: Select all
python2 --version


and how to install it if it is not:

Code: Select all
pacman -Sy python2


Note:

I have done the best to simply copy, cut and paste, examples and code which were already posted in this forum by Frank/hondagx35, by XploD, and others. In other words this is mostly code written and already tested by other people, so it should be fairly safe to use... :mrgreen:
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby rlsten » 02 Jun 2016, 17:01

This is great information! Thanks, Pete, for all your work in putting this together.

Do you think it would it be possible to wire and program a 5-way switch such as this https://www.sparkfun.com/products/10063 to navigate between the pages of the interface, scroll up and down on each page, and execute a command (such as "add to queue" when on the library page, or toggle play/pause when on the playback page, etc.?
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Hardware Play/Pause Button

Postby PeteB » 02 Jun 2016, 17:28

rlsten wrote:... Do you think it would it be possible to wire and program a 5-way switch such as this https://www.sparkfun.com/products/10063 to navigate between the pages of the interface, scroll up and down on each page, and execute a command (such as "add to queue" when on the library page, or toggle play/pause when on the playback page, etc.?

Not using the same method, no. Meaning, not using mpc commands alone.

You can program a remote control (or a switch) for any of the functions in the Playback screen.

To do what you want, you may have to program the switch to act like a pointstick (simulate a mouse), or use tab navigation in the Rune UI. I have not been able to use tabs with my wireless keyboard, so I cannot say it would work. Frank has posted that it does, so I have to assume he is correct and it is my keyboard that is faulting somehow.

If tab navigation and the directional keys work, then you could program that switch to do the same thing.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Hardware Play/Pause Button

Postby rlsten » 03 Jun 2016, 15:44

Thanks, Pete. That makes sense. But coding a switch to act like a mouse might be beyond me.

I may have a simpler solution: a small touchpad like this: http://www.ergonomictouchpad.com/ergonomic_touchpad.php

I would have to shorten the cable significantly and run it inside the enclosure. They make an even smaller version, but inexplicably it has a PS-2 connector and requires a USB adapter -- way too large for my enclosure.

Here is what the enclosure looks like now:
2016-06-01 19.31.13.jpg
2016-06-01 19.31.13.jpg (180.75 KiB) Viewed 4081 times


It is basically a 3D Printed enclosure for a Raspberry Pi 3, a Chord Mojo portable DAC, 4 SD cards in SD Card readers attached via USB, a 5300 mAh Lithium battery, and a Tontec 3.5" Screen. The Chord Mojo is attached to the Pi via USB. The purpose is to hide all the cabling between the storage, source, battery, and DAC. I'm using the Chord Mojo because it provides tremendous sound quality and is able to drive full-size headphones.
rlsten
 
Posts: 118
Joined: 25 May 2015, 18:06

Re: Hardware Play/Pause Button

Postby PeteB » 03 Jun 2016, 17:48

The mousepad looks like the right solution if yu want something more than one or two buttons, and the enclosure looks really nice! Why don't you post some pics in the DIY section, perhaps it will inspire other people to give it a try.
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

support RuneAudio Donate with PayPal

PreviousNext

Return to Raspberry Pi

Who is online

Users browsing this forum: Bing [Bot] and 7 guests
cron