Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full integration of PirateAudioHAT #1112

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from
11 changes: 11 additions & 0 deletions components/audio/Hifiberry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# How to setup a Hifiberry sound card

These instructions are for Hifiberyy soundcards:

<https://www.hifiberry.com/shop/#boards>

More details and troubleshooting can be found here: <https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/HiFiBerry-Soundcard-Details>.

## Automatic Script

Please use `setup_Hifiberry.sh` script to set it up to work with Phoniebox.
95 changes: 95 additions & 0 deletions components/audio/Hifiberry/setup_Hifiberry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env bash

HOME_DIR="/home/pi"
JUKEBOX_HOME_DIR="${HOME_DIR}/RPi-Jukebox-RFID"

TYPE=$1

if [[ -n "$TYPE" ]]; then
echo "Configuring Hifiberry ${TYPE} sound card"
else
echo "Error: please pass miniamp or amp2 to script."
exit -1
fi

question() {
local question=$1
read -p "${question} (y/n)? " choice
case "$choice" in
y|Y ) ;;
n|N ) exit 0;;
* ) echo "Error: invalid" ; question ${question};;
esac
}

printf "Please make sure that the Hifiberry is connected...\n"
question "Continue"

printf "Adding settings to /boot/config.txt...\n"
if [[ ! -f /boot/config.txt.bak ]]; then
sudo cp /boot/config.txt /boot/config.txt.bak
fi

if ! sudo grep -qe "dtoverlay=hifiberry-dac" /boot/config.txt; then
echo "dtoverlay=hifiberry-dac" | sudo tee -a /boot/config.txt > /dev/null
fi

printf "Adding settings to /etc/asound.conf...\n"
# Create backup of /etc/asound.conf if it already exists
if [[ -f /etc/asound.conf && ! -f /etc/asound.conf.bak ]]; then
sudo cp /etc/asound.conf /etc/asound.conf.bak
fi

# Do not add, but replace content if file already exists
sudo tee /etc/asound.conf << EOF > /dev/null
pcm.hifiberry {
type softvol
slave.pcm "plughw:CARD=sndrpihifiberry,DEV=0"
control.name "Master"
control.card 1
}
pcm.!default {
type plug
slave.pcm "hifiberry"
}
ctl.!default {
type hw
card 1
}
EOF

# Create backup of /etc/mpd.conf if it already exists
if [[ -f /etc/mpd.conf && ! -f /etc/mpd.conf.bak ]]; then
sudo cp /etc/mpd.conf /etc/mpd.conf.bak
fi

printf "Add hifiberry as audio_output in /etc/mpd.conf...\n"
# Only add, if it does not exist already
if ! sudo grep -qe "HiFiBerry DAC+ Lite" /etc/mpd.conf; then
sudo sed -i "/# An example of an ALSA output:/ r /dev/stdin" /etc/mpd.conf <<'EOG'
audio_output {
enabled "yes"
type "alsa"
name "HiFiBerry DAC+ Lite"
device "hifiberry"
auto_resample "no"
auto_channels "no"
auto_format "no"
dop "no"
}
EOG
else
printf "/etc/mpd.conf is already configured. Skipping...\n"
fi

printf "Set mixer_control name in /etc/mpd.conf...\n"
mixer_control_name="Master" # for miniamp

if [ "${TYPE}" == "amp2" ]; then
# see https://github.com/MiczFlor/RPi-Jukebox-RFID/issues/1198#issuecomment-750757106
mixer_control_name="Digital"
fi

sudo sed -i -E "s/^(\s*mixer_control\s*\")[^\"]*(\"\s*# optional)/\1\\${mixer_control_name}\2/" /etc/mpd.conf

printf "You should reboot later to apply the settings.\n"
12 changes: 7 additions & 5 deletions components/audio/PirateAudioHAT/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ These instructions are for the following Pimoroni PirateAudio HATs:
The PirateAudio HATs use the same DAC as the hifiberry, so some of the instructions
from <https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/HiFiBerry-Soundcard-Details> can be applied as well.

The `setup_pirateAudioHAT.sh` script can be used to set it up to work with Phoniebox.
## Automatic Script

## Install steps in writing
Please use `setup_pirateAudioHAT.sh` script to set it up to work with Phoniebox.

## Manual install steps (not recommended)

(Discussions regarding *Pirate Audio HAT* should take place in the same thread where the below instructions were taken from: [#950](https://github.com/MiczFlor/RPi-Jukebox-RFID/issues/950)

NOTE: changes to the installation should find their way into the script `setup_pirateAudioHAT.sh`. Please create pull requests *after* having tested your changes. :)

1. Connect Pirate Audio Hat to your Raspberry Pi
2. Install Phoniebox (develop branch!)
2. Install Phoniebox
3. Stop and disable the GPIO button service:
`sudo systemctl stop phoniebox-gpio-buttons.service`
`sudo systemctl disable phoniebox-gpio-buttons.service`
`sudo systemctl stop phoniebox-gpio-control.service`
`sudo systemctl disable phoniebox-gpio-control.service`
4. Add the following two lines to /boot/config.txt
`gpio=25=op,dh`
`dtoverlay=hifiberry-dac`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Phoniebox Pirate Audio Display Service
After=network.target phoniebox-rfid-reader.service

[Service]
User=pi
Group=pi
Restart=always
WorkingDirectory=/home/pi/RPi-Jukebox-RFID
ExecStart=/home/pi/RPi-Jukebox-RFID/components/audio/PirateAudioHAT/pirate_audio_display.py

[Install]
WantedBy=multi-user.target
48 changes: 48 additions & 0 deletions components/audio/PirateAudioHAT/pirate_audio_display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from mpd import MPDClient
import time

import ST7789


MESSAGE = "Hello World! How are you today?"

# Create ST7789 LCD display class.
disp = ST7789.ST7789(
port=0,
cs=ST7789.BG_SPI_CS_FRONT, # BG_SPI_CSB_BACK or BG_SPI_CS_FRONT
dc=9,
backlight=19, # 18 for back BG slot, 19 for front BG slot.
spi_speed_hz=80 * 1000 * 1000
)

# Initialize display.
disp.begin()

WIDTH = disp.width
HEIGHT = disp.height


img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))

draw = ImageDraw.Draw(img)

font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)

size_x, size_y = draw.textsize(MESSAGE, font)

text_x = disp.width
text_y = (80 - size_y) // 2

t_start = time.time()

while True:
x = (time.time() - t_start) * 100
x %= (size_x + disp.width)
draw.rectangle((0, 0, disp.width, 80), (0, 0, 0))
draw.text((int(text_x - x), text_y), MESSAGE, font=font, fill=(255, 255, 255))
disp.display(img)
7 changes: 7 additions & 0 deletions components/audio/PirateAudioHAT/requirements-mopidy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# PirateAudioHAT related for spotify edition
# You need to install these with `sudo python3 -m pip install --upgrade --force-reinstall -q -r requirements-mopidy.txt`

Mopidy-PiDi
mopidy-raspberry-gpio
pidi-display-pil
pidi-display-st7789
7 changes: 2 additions & 5 deletions components/audio/PirateAudioHAT/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# PirateAudioHAT related requirements
# PirateAudioHAT related requirements (for classic edition)
# You need to install these with `sudo python3 -m pip install --upgrade --force-reinstall -q -r requirements.txt`

Mopidy-PiDi
pidi-display-pil
pidi-display-st7789
mopidy-raspberry-gpio
st7789
110 changes: 42 additions & 68 deletions components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
HOME_DIR="/home/pi"
JUKEBOX_HOME_DIR="${HOME_DIR}/RPi-Jukebox-RFID"

EDITION=$1

if [[ -n "$EDITION" ]]; then
echo "Configuring Pirate Audio HAT for ${EDITION} edition"
else
echo "Error: please pass classic or spotify to script."
exit -1
fi

question() {
local question=$1
read -p "${question} (y/n)? " choice
Expand All @@ -16,10 +25,11 @@ question() {
printf "Please make sure that the Pirate Audio HAT is connected...\n"
question "Continue"

printf "Stopping and disabling GPIO button service...\n"
#TODO this might not be necessary
sudo systemctl stop phoniebox-gpio-buttons.service
sudo systemctl disable phoniebox-gpio-buttons.service
if [ "${EDITION}" == "spotify" ]; then
printf "Stopping and disabling GPIO control service...\n"
sudo systemctl stop phoniebox-gpio-control.service
sudo systemctl disable phoniebox-gpio-control.service
fi

printf "Adding settings to /boot/config.txt...\n"
if [[ ! -f /boot/config.txt.bak ]]; then
Expand All @@ -30,75 +40,23 @@ fi
if ! sudo grep -qe "gpio=25=op,dh" /boot/config.txt; then
echo "gpio=25=op,dh" | sudo tee -a /boot/config.txt > /dev/null
fi
if ! sudo grep -qe "dtoverlay=hifiberry-dac" /boot/config.txt; then
echo "dtoverlay=hifiberry-dac" | sudo tee -a /boot/config.txt > /dev/null
fi

printf "Adding settings to /etc/asound.conf...\n"
# Create backup of /etc/asound.conf if it already exists
if [[ -f /etc/asound.conf && ! -f /etc/asound.conf.bak ]]; then
sudo cp /etc/asound.conf /etc/asound.conf.bak
fi

# Do not add, but replace content if file already exists
sudo tee /etc/asound.conf << EOF > /dev/null
pcm.hifiberry {
type softvol
slave.pcm "plughw:CARD=sndrpihifiberry,DEV=0"
control.name "Master"
control.card 1
}
pcm.!default {
type plug
slave.pcm "hifiberry"
}
ctl.!default {
type hw
card 1
}
EOF

# Create backup of /etc/mpd.conf if it already exists
if [[ -f /etc/mpd.conf && ! -f /etc/mpd.conf.bak ]]; then
sudo cp /etc/mpd.conf /etc/mpd.conf.bak
fi

printf "Add hifiberry as audio_output in /etc/mpd.conf...\n"
# Only add, if it does not exist already
if ! sudo grep -qe "HiFiBerry DAC+ Lite" /etc/mpd.conf; then
sudo sed -i "/# An example of an ALSA output:/ r /dev/stdin" /etc/mpd.conf <<'EOG'
audio_output {
enabled "yes"
type "alsa"
name "HiFiBerry DAC+ Lite"
device "hifiberry"
auto_resample "no"
auto_channels "no"
auto_format "no"
dop "no"
}
EOG
else
printf "/etc/mpd.conf is already configured. Skipping...\n"
fi

printf "Set mixer_control name in /etc/mpd.conf...\n"
mixer_control_name="Master"
sudo sed -i -E "s/^(\s*mixer_control\s*\")[^\"]*(\"\s*# optional)/\1\\${mixer_control_name}\2/" /etc/mpd.conf
bash "${JUKEBOX_HOME_DIR}/component/audio/Hifiberry/setup_Hifiberry.sh"

printf "Activating SPI...\n"
sudo raspi-config nonint do_spi 0

printf "Installing Python dependencies...\n"
sudo apt-get -y -qq install python3-pil python3-numpy

printf "Installing mopidy plugins...\n"
sudo python3 -m pip install --upgrade --force-reinstall -q -r "${JUKEBOX_HOME_DIR}"/components/audio/PirateAudioHAT/requirements.txt
if [ "${EDITION}" == "spotify" ]; then
printf "Installing Python packages...\n"
sudo python3 -m pip install --upgrade --force-reinstall -q -r "${JUKEBOX_HOME_DIR}"/components/audio/PirateAudioHAT/requirements-mopidy.txt

# Only add, if it does not exist already
printf "Editing mopidy configuration...\n"
if ! sudo grep -qe "raspberry-gpio" /etc/mopidy/mopidy.conf; then
sudo tee -a /etc/mopidy/mopidy.conf << EOH > /dev/null
# Only add, if it does not exist already
printf "Editing mopidy configuration...\n"
if ! sudo grep -qe "raspberry-gpio" /etc/mopidy/mopidy.conf; then
sudo tee -a /etc/mopidy/mopidy.conf << EOH > /dev/null

[raspberry-gpio]
enabled = true
Expand All @@ -111,12 +69,28 @@ bcm20 = volume_up,active_low,150
enabled = true
display = st7789
EOH
else
printf "/etc/mopidy/mopidy.conf is already configured. Skipping...\n"
fi

printf "Enable access for modipy user...\n"
sudo usermod -a -G spi,i2c,gpio,video mopidy
else
printf "/etc/mopidy/mopidy.conf is already configured. Skipping...\n"
fi
printf "Installing Python packages...\n"
sudo python3 -m pip install --upgrade --force-reinstall -q -r "${JUKEBOX_HOME_DIR}"/components/audio/PirateAudioHAT/requirements.txt

cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/gpio_settings.ini.pirate-audio-hat.sample "${JUKEBOX_HOME_DIR}"/settings/gpio_settings.ini

printf "Enable access for modipy user...\n"
sudo usermod -a -G spi,i2c,gpio,video mopidy
sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/phoniebox-pirateaudio-display.service.sample "/etc/systemd/system/phoniebox-pirateaudio-display.service"
sudo chown root:root /etc/systemd/system/phoniebox-pirateaudio-display.service
sudo chmod 644 /etc/systemd/system/phoniebox-pirateaudio-display.service
# enable the services needed
sudo systemctl enable phoniebox-pirateaudio-display

# restart services
sudo systemctl restart phoniebox-gpio-control
sudo systemctl restart phoniebox-pirateaudio-display
fi

printf "You need to reboot to apply the settings.\n"
question "Do you want to reboot now"
Expand Down
34 changes: 34 additions & 0 deletions misc/sampleconfigs/gpio_settings.ini.pirate-audio-hat.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[DEFAULT]
enabled: True

[PlayPause]
enabled: True
Type: Button
Pin: 5
pull_up: True
bouncetime: 250
functionCall: functionCallPlayerPause

[VolumeUp]
enabled: True
Type: Button
Pin: 20
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need also to add pin 24, see pimoroni/pirate-audio#63 (comment)

pull_up: True
bouncetime: 250
functionCall: functionCallVolU

[VolumeDown]
enabled: True
Type: Button
Pin: 6
pull_up: True
bouncetime: 250
functionCall: functionCallVolD

[NextSong]
enabled: True
Type: Button
Pin: 16
pull_up: True
bouncetime: 250
functionCall: functionCallPlayerNext
Loading