Setting up the PhatBeat buttons to be automatically available

Hello,

Apologies if this is a duplicate, I did do a search.

I just bought a PhatBeat which eventually I intend to hook up to a spare Pi Zero, but for the moment I attached it to a Raspberry Pi 3 Model A. This Pi is running the lite version of Raspbian Buster to which I installed Mopidy and has been running nicely for a couple of weeks, via the Model A’s audio jack.

I added the PhatBeat to my Pi and ran the one-line installer from Yarr-niversity and everything went smoothly as far as I know; I’m sitting listening to music via the PhatBeat at the moment after wiring in a set of hi-fi speakers.

However, what isn’t working is the hardware buttons. The Getting Started page describes how to use some Python decorators but doesn’t explain how to make them go without you needing to type into an IDE. From searching this forum I find references to a service called phatbeatd which apparently makes the buttons work, but this isn’t installed on my Pi and I can’t seem to find any references for how to install this service.

Can anyone offer suggestions on how to install this service or otherwise get something that will auto-start and listen for button presses? I’m mainly interested in the shutdown button - I’d previously followed a tutorial for listening for the GPIO pins 5 and 6 to be shorted in order to do a controlled shutdown, and given the GPIO pins are now used by the PhatBeat I need another solution other than yanking the plug out.

Hope this isn’t too much of a noob question and I haven’t missed out anything dumb.

Thanks;
Richard Moss

I have a pirate radio setup using the phat Beat.
If memory serves me I followed this guide.
https://learn.pimoroni.com/tutorial/sandyj/internet-radio-on-your-pirate-radio
My buttons work as advertised.

Enabling i2c kills the default dtoverlay=gpio-shutdown . You can still use it you just have to map it to another GPIO pin. On my fan Shim I use the following
dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up
The Fan shim grounds GPIO 3 (pin 5) and GPIO 17 (pin 11). 17 is the BCM number not the physical pin number.

EDIT: If you get those buttons working with their defaults, pressing the power button on the pHat Beat will have it do a proper shutdown. It maps out to GPIO 12 (pin 32).

Or try dtoverlay=gpio-shutdown,gpio_pin=12,active_low=1,gpio_pull=up

Hello,

Thanks for the reply. I just tried the curl https://get.pimoroni.com/vlcradio | bash command referenced in the article you linked - aside from the VLC Radio program it seems to be pretty much the same as the core installer. It did give a warning that the VLC Radio Installer can’t be installed on this version of Raspbian, which is actually totally fine as I don’t want it.

Regretfully however, even after rebooting the buttons still don’t do anything. Running sudo service --status-all doesn’t show anything Phat (assuming the phatbeatd is something real).

A little puzzling if it is supposed to “just work”.

Regards;
Richard Moss

I do believe the buttons work in / from VLC radio? It’s whats running on boot up for me. My Pirate Radio isn’t running Buster though. Jessie I believe, I haven’t had to reinstall in ages and, “if it ain’t broke, don’t fix it”. ;) So I just leave it as is as long as its working.

I just did a test where I ran python and added a decorator to print a message when the power button was pushed - said message was printed when I pressed the button. So it looks like things are installed and working. And the VDU meter works perfectly well, although I wish I could change the scale as I have it running at about 20% of volume which means pretty much only the first two lights actually come on.

So it’s just something that stays resident that is missing I guess. If the phatbeatd service is part of VLC then that’s a little annoying if it won’t install on buster, but I suppose there’s probably something else that can be done. My goal was to test it on this pi and then doing a fresh install for a Pi Zero, perhaps I could downgrade to Jessie but I’d rather be running the latest version of things as I’m not as familiar with the Linux world as I am with Windows.

Regards;
Richard Moss

Going by the product page the pHat Beat installer sets up the DAC as the audio source and adds a Alsa plugin for the VU meter.
Best Guess is you have to setup the Buttons for what ever Audio program you want to use?
How to make use of the buttons from python is documented here.
http://docs.pimoroni.com/phatbeat/

There is a fix for the VU meter.
You can increase the sensitivity on the Phat Beat VU Meter by doing the following:
cd Pimoroni/pivumeter
nano src/devices/phat-beat.c
Change line 88 to:
int bar = (meter_level * (meter_level / 8) / 131071.0f) * (brightness * (NUM_PIXELS/2));
Recompile the library:
sudo make
sudo make install
Sudo reboot

Now your VU meter is more sensitive! Tweak the values to your liking.

1 Like

Hello,

Thanks for the tip re changing the source for the VDU meter, I’ve tried a few values and it definitely works, just need to try and find something pleasing!

Regards;
Richard Moss

Another forum member figured that out. I just clipped and pasted into a text file for future reference.
I set my volume at my usual level and went for about 3/4 of the way up. That way I can go a little louder or lower and still get something view-able that’s not pegged fully in the red or completely blank. Before the tweak I got maybe 2 LED’s showing.

Hello everyone, I would like to continue Richard’s thread because it seems that I am facing the same issue but have not seen the proper solution (apart from running the entire pirate radio setup script)… Of course I have also tried to find an answer on the web but without success (hope I have not overlooked something obvious).
I have succesfully installed Mopidy and Snapclient on my Raspberry Zero W equipped with a pHAT Beat and running Raspi OS Buster (I used the Pimoroni installation script to set up pHAT Beat). So far everything is working nicely, but I have run into troubles when trying to add functionality to the pHAT Beat buttons via the decorators of the phatbeat python library on GitHub. And interestingly, it does work when I simply enter the commands into the Python3 interpreter (run via sudo) but NOT when I start a python script from the command line (again via sudo). Let’s take the alsamixer mute control as an example:

import phatbeat
import subprocess

@phatbeat.on(phatbeat.BTN_PLAYPAUSE)
def togglemute(pin):
    subprocess.run(['amixer', 'set', 'Master', 'toggle'])

As said, typing that into the pyhton interpreter allows toggling alsamixer mute whereas running a script (with shebang in the first line, of course) does not, it just finishes without any effect (and without error messages). How can I run such a script successfully (should be launched at system boot once it’s working properly)?

Best regards, Felix

P.S: Yes, you are right, I am a Python newbie so please forgive me in case I have made a stupid mistake ;-)…

Ok, one step further: The script works as long as it is not terminated so if I add a waiting loop (or an endless one…) the buttons react as they are supposed to, but when the script terminates it leaves behind the pHAT Beat in an ill-defined state which can make some LEDs light up rather randomly (and obviously shuts off any button functionality). However, seems the solution is quite close because in the vlc-radio project folder of the above-mentioned Github repository there are two scripts for a phatbeatd (i.e. a daemon serving right this prupore for the vlc-radio). Guess with a few adaptations this should solve my problem! Will let you know…

I have been successful in daemonising my script using the phatbeatd example for the vlc-radio; the daemon now starts at system boot and the buttons work as planned. The only remaining flaw is the above-mentioned LED flickering (well, that’s not too severe after all). A socket-based implementation of the pivumeter seems to solve this issue (see dicussion here in the forum), and on that way also the scale of the pivumeter can be tweaked to a reasonable sensitivity. Guess I will go for that one, although with lower priority. If I find out how to do so, I am going to upload my phatbeatd version to the Github phatbeat repository, just in case someone is intereseted.

1 Like