G1sms/_CopyLaRadio/watch.sh

213 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
# BUTTONS GPIO NUMBERS FOR PHAT BEAT
# https://pinout.xyz/pinout/phat_beat#
# ADD TO /etc/local for autostart
#
ONOFF=12
VOLUP=16
VOLDO=26
BCK=13
PLAY=6
FWD=5
# ADD YOUR DEFAULT RADIO STREAM / PLAYLIST HERE - TRACK INFORMATION FOR RECORDING ;)
PLAYLIST="/home/pi/playlists/default.m3u"
function trim() {
local var="$*"
# remove slashes
var=$(echo "$var" | sed 's/\// /g')
var=${var//[^a-zA-Z0-9_\. ]/}
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}
function button() {
pin=$1
gpio -g mode $pin in # make sure pin is configured as an input
gpio -g mode $pin up # enable pull-up resistor
boucle=0
while :; do
gpio -g wfi $pin falling # wait for a button action
sleep 0.2
echo $pin
while [[ $(gpio -g read $pin) -eq 0 ]]
do
aplay bip.wav
sleep 0.5 # COUNT 1/2 SECOND PRESSED
boucle=$(bc -l <<< "$boucle + 1")
done
if [[ $boucle -gt 0 ]]; then
echo "LP-$pin-$boucle"
fi
boucle=0
done
}
boucle=0
# --- main loop ---
while :; do
read numbut
# WHAT IS ON AIR
watch="$(mpc | head -1):"
# EXTRACT ($radio :) $artist - $song
radio=$(echo "$watch" | cut -d ":" -f 1 | xargs)
if [[ "$radio" != "$watch" && $radio != "volume" && $radio != "http" ]]; then RADIO="$radio"; else radio="$watch"; RADIO=""; fi
play=$(echo "$watch" | cut -d ":" -f 2)
artist=$(echo "$play" | cut -d "-" -f 1)
artist=$(trim "$artist")
song=$(echo "$play" | cut -d "-" -f 2)
song=$(trim "$song")
if [[ "$song" == "" ]]; then
song=$(echo "$play" | cut -d " " -f 2)
song=$(trim "$song")
fi
# TODO: ADAPT BEHAVIOUR TO EACH RADIO STREAM
# NOVA RADIO PATCH
if [[ "$radio" == "Nova zz" ]]; then
artist=""
song=""
fi
echo "(CLICK $numbut) $RADIO: $artist / $song"
case "$numbut" in
$ONOFF)
echo "REC"
# BUTTON QUICK RELEASE
if [[ $(gpio -g read $ONOFF) -eq 1 ]]; then
# RECORD STREAMING SONG::GENERIC
if [[ "$RADIO" != "" && "$artist" != "" && "$song" != "" && "$artist" != "$song" ]]; then
what=$(grep "$RADIO|$artist|$song" /tmp/ytdl.list)
if [[ "$what" == "" ]]; then
~/parle.sh "Enregistrement ajouté."
echo "$RADIO|$artist|$song" >> /tmp/ytdl.list
else
~/parle.sh "Enregistrement déjà existant!"
fi
elif [[ "$artist" == "$song" ]]; then
~/parle.sh "Enregistrement $radio"
# USE COPY & MODULE FIFO
echo "$radio||" >> /tmp/ytdl.list # TODO REMOVE
else
~/parle.sh "Podcast ou Fichier local. Enregistrement inopérant."
fi
fi
;;
LP-$ONOFF-*)
# LONG PRESS TRACK IDENTIFICATION
playing=$(mpc | head -1 | cut -d ":" -f 1 | cut -d "/" -f 1 | xargs)
playing=$(trim "$playing")
~/parle.sh "$playing"
if [[ "$RADIO" != "" && "$artist" != "" && "$song" != "" && "$artist" != "$song" ]]; then
~/parle.sh "$artist - $song"
fi
;;
$PLAY)
echo "PLAY"
if [[ $(gpio -g read $PLAY) -eq 1 ]]; then
mpc toggle
fi
;;
LP-$PLAY-*)
sec=$(echo "$numbut" | cut -d "-" -f 3)
case "$sec" in
1)
# 1SEC
# Check if USB drive connected
if [[ -b /dev/sda1 ]]; then
~/parle.sh "Synchronisation musique sur clé U S B. Veuillez patienter"
sudo mount /dev/sda1 /mnt
sudo mkdir -p /mnt/CopyLaRadio
sudo rsync -rtgoDv /home/pi/music/ /mnt/CopyLaRadio
sudo umount /dev/sda1
~/parle.sh "Transfert terminé. Vous pouvez débrancher votre clé..."
else
# STOP MUSIC
~/parle.sh "STOP"
mpc stop
fi
;;
2)
# 2SEC = CHANGE MODE SHUFFLE LOCAL / DEFAULT PLAYLIST
if [[ "$RADIO" != "" && "$mode" == "" ]]; then
mode="LOCAL"
~/parle.sh "Lecture musique locale"
mpc clear
mpc listall | mpc add
mpc shuffle
~/parle.sh "Aléatoire"
mpc play
else
mode=""
mpc clear
mpc load default
~/parle.sh "Lecture liste par défaut"
mpc play
fi
;;
3)
# 3 SEC = HALT SYSTEM
~/parle.sh "Arrêt RAIQUE MACHINE"
sudo halt
;;
*)
# > 3 SEC = REBOOT SYSTEM
~/parle.sh "Redémarrage RAIQUE MACHINE"
sudo reboot
;;
esac
;;
$BCK)
echo "BCK"
mpc -q prev
;;
$FWD)
echo "FWD"
mpc -q next
;;
$VOLUP)
echo "VOLUP"
if [[ $(gpio -g read $VOLUP) -eq 1 ]]; then
mpc -q volume +10
vol=$(mpc volume | cut -f 2 -d ":")
vol=$(trim "$vol")
if [ "$vol" == "100%" ]; then ~/parle.sh "VOLUME MAXIMUM"; fi
fi
;;
LP-$VOLUP-*)
sec=$(echo "$numbut" | cut -d "-" -f 3)
sec=+$(bc -l <<< "$sec * 10")
mpc -q volume $sec
;;
$VOLDO)
echo "VOLDO"
if [[ $(gpio -g read $VOLDO) -eq 1 ]]; then
mpc -q volume -10
vol=$(mpc volume | cut -f 2 -d ":")
vol=$(trim "$vol")
if [ "$vol" == "0%" ]; then ~/parle.sh "VOLUME MINIMUM"; fi
fi
;;
LP-$VOLDO-*)
sec=$(echo "$numbut" | cut -d "-" -f 3)
sec=-$(bc -l <<< "$sec * 10")
mpc -q volume $sec
;;
*)
echo $numbut
;;
esac
done < <( button $ONOFF & button $VOLUP & button $VOLDO & button $BCK & button $PLAY & button $FWD & ) # buttons on GPIOs to monitor