diff --git a/components/audio/Hifiberry/README.md b/components/audio/Hifiberry/README.md new file mode 100644 index 000000000..a0259dc8c --- /dev/null +++ b/components/audio/Hifiberry/README.md @@ -0,0 +1,11 @@ +# How to setup a Hifiberry sound card + +These instructions are for Hifiberyy soundcards: + + + +More details and troubleshooting can be found here: . + +## Automatic Script + +Please use `setup_Hifiberry.sh` script to set it up to work with Phoniebox. diff --git a/components/audio/Hifiberry/setup_Hifiberry.sh b/components/audio/Hifiberry/setup_Hifiberry.sh new file mode 100644 index 000000000..366b5ad7e --- /dev/null +++ b/components/audio/Hifiberry/setup_Hifiberry.sh @@ -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" diff --git a/components/audio/PirateAudioHAT/README.md b/components/audio/PirateAudioHAT/README.md index adebac7ef..44b308f14 100644 --- a/components/audio/PirateAudioHAT/README.md +++ b/components/audio/PirateAudioHAT/README.md @@ -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 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` diff --git a/components/audio/PirateAudioHAT/phoniebox-pirateaudio-display.service.sample b/components/audio/PirateAudioHAT/phoniebox-pirateaudio-display.service.sample new file mode 100644 index 000000000..518a5b766 --- /dev/null +++ b/components/audio/PirateAudioHAT/phoniebox-pirateaudio-display.service.sample @@ -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 diff --git a/components/audio/PirateAudioHAT/pirate_audio_display.py b/components/audio/PirateAudioHAT/pirate_audio_display.py new file mode 100644 index 000000000..9807426fe --- /dev/null +++ b/components/audio/PirateAudioHAT/pirate_audio_display.py @@ -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) diff --git a/components/audio/PirateAudioHAT/requirements-mopidy.txt b/components/audio/PirateAudioHAT/requirements-mopidy.txt new file mode 100644 index 000000000..c6fc70c42 --- /dev/null +++ b/components/audio/PirateAudioHAT/requirements-mopidy.txt @@ -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 diff --git a/components/audio/PirateAudioHAT/requirements.txt b/components/audio/PirateAudioHAT/requirements.txt index 0380e1855..82a4dff2e 100644 --- a/components/audio/PirateAudioHAT/requirements.txt +++ b/components/audio/PirateAudioHAT/requirements.txt @@ -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 diff --git a/components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh b/components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh index add059670..439de00be 100755 --- a/components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh +++ b/components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh @@ -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 @@ -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 @@ -30,61 +40,8 @@ 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 @@ -92,13 +49,14 @@ 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 @@ -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" diff --git a/misc/sampleconfigs/gpio_settings.ini.pirate-audio-hat.sample b/misc/sampleconfigs/gpio_settings.ini.pirate-audio-hat.sample new file mode 100644 index 000000000..98575734a --- /dev/null +++ b/misc/sampleconfigs/gpio_settings.ini.pirate-audio-hat.sample @@ -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 +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 diff --git a/scripts/installscripts/buster-install-default.sh b/scripts/installscripts/buster-install-default.sh index f010c8b17..f3cbcabf1 100644 --- a/scripts/installscripts/buster-install-default.sh +++ b/scripts/installscripts/buster-install-default.sh @@ -1253,6 +1253,44 @@ finish_installation() { done esac + echo "If you are using a sound card, connect it to your RPi." + # Use -e to display response of user in the logfile + read -e -r -p "Have you connected your sound card [Y/n] " response + case "$response" in + [nN][oO]|[nN]) + ;; + *) + echo 'Please select the sound card you want to use' + options=("Hifiberry Miniamp" "Hifiberry AMP2" "Pimoroni Pirate Audio" "Manual configuration") + select opt in "${options[@]}"; do + case $opt in + "Hifiberry Miniamp") + bash "${jukebox_dir}"/components/audio/Hifiberry/setup_Hifiberry.sh miniamp + break + ;; + "Hifiberry AMP2") + bash "${jukebox_dir}"/components/audio/Hifiberry/setup_Hifiberry.sh amp2 + break + ;; + "Pimoroni Pirate Audio") + if [ "$SPOTinstall" == "YES" ]; then + bash "${jukebox_dir}"/components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh spotify + else + bash "${jukebox_dir}"/components/audio/PirateAudioHAT/setup_pirateAudioHAT.sh classic + fi + break + ;; + "Manual configuration") + echo "Please configure your sound card manually." + break + ;; + *) + echo "This is not a number" + ;; + esac + done + esac + echo echo "DONE. Let the sounds begin." echo "Find more information and documentation on the github account:"