From: Giulio Maestrini Date: Sat, 28 Dec 2024 09:52:27 +0000 (+0100) Subject: MPD scripts reworked X-Git-Url: https://git.devmsg.it/cgi-bin/gitweb.cgi?a=commitdiff_plain;p=giulio%2F.git MPD scripts reworked --- diff --git a/aurora/color.sh b/aurora/color.sh index 5d95efc..4473e6a 100755 --- a/aurora/color.sh +++ b/aurora/color.sh @@ -51,14 +51,14 @@ function giallo { } function off { - gpioset 0 101=0 - gpioset 0 99=0 - gpioset 0 97=0 - gpioset 0 140=0 + gpioset 0 101=1 + gpioset 0 99=1 + gpioset 0 97=1 + gpioset 0 140=1 } -for (( i=0; i<40; i++ )) +for ((;;)) do rnd=$(shuf -i 1-7 -n 1) case $rnd in @@ -92,7 +92,6 @@ do ;; esac - sleep 15 + t=$(shuf -i 3-20 -n 1) + sleep "$t" done - -off diff --git a/aurora/init_mpd.sh b/aurora/init_mpd.sh new file mode 100755 index 0000000..d36c64f --- /dev/null +++ b/aurora/init_mpd.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +random_playlist () { + # pick a random playlist + cd /home/ubuntu/musica/playlists || exit + tot=$(ls -1 * | wc -l) + rnd_pick=$(shuf -i 1-"$tot" -n 1) + playlist_file=$(ls * | awk -v x="$rnd_pick" 'NR==x') + echo "$rnd_pick" > /home/ubuntu/musica/current_playlist + playlist_file=${playlist_file/.m3u/} + echo "$playlist_file" +} + +# update database and load a random playlist +mpc clear +playlist=$(random_playlist) +mpc load "$playlist" +mpc vol 60 +mpc play diff --git a/aurora/init_mpd_1.sh b/aurora/init_mpd_1.sh deleted file mode 100755 index fcf26a6..0000000 --- a/aurora/init_mpd_1.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -# start mpd daemon -mpd diff --git a/aurora/init_mpd_2.sh b/aurora/init_mpd_2.sh deleted file mode 100755 index 044299a..0000000 --- a/aurora/init_mpd_2.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash -# lights ON -/bin/bash /home/ubuntu/scripts/color.sh & - -# update database and load playlist -mpc update -mpc load aurora -mpc repeat - -# play a random entry of the playlist -sup=$(mpc playlist | wc -l) -rnd=$(shuf -i 1-"$sup" -n 1) -mpc play "$rnd" -sleep 600 - -# FADE OUT -# save current volume -vol=$(mpc vol | cut -d " " -f 2 | tr -d "%") -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 -sleep 1 -mpc volume -10 - -# stop and reset -mpc stop -mpc vol "$vol" diff --git a/aurora/playlist+.sh b/aurora/playlist+.sh new file mode 100644 index 0000000..9de62ff --- /dev/null +++ b/aurora/playlist+.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# gpiomon waits for two events (rising or falling) + +for((;;)); do + a=$(sudo gpiomon --bias pull-down -n 2 -F="%s" 0 108) + b=$(echo "$a" | tr -d '=') + mapfile -t sec_val <<<"$b" + n=$(( sec_val[1] - sec_val[0] )) + # n is the time difference between the two events (in seconds) + # check if music is playing + mpc | grep playing + status=$? + + if [ "$n" -gt 1 ]; then # long press, play next playlist + if [ "$status" == 0 ]; then + mpc clear + cd /home/ubuntu/musica/playlists || exit + p_c=$(cat /home/ubuntu/musica/current_playlist) + tot=$(ls -1 * | wc -l) + if [ "$p_c" -lt "$tot" ]; then + p_n=$(( p_c + 1 )) + else + p_n=1 + fi + echo "$p_n" > /home/ubuntu/musica/current_playlist + playlist_file=$(ls * | awk -v x="$p_n" 'NR==x') + playlist_file=${playlist_file/.m3u/} + mpc clear + mpc load "$playlist_file" + mpc repeat + mpc play + else + mpc play + fi + else # short press, play next song in the current playlist + if [ "$status" == 0 ]; then + mpc next + else + mpc play + fi + fi +done diff --git a/aurora/playlist-.sh b/aurora/playlist-.sh new file mode 100644 index 0000000..57c7d4a --- /dev/null +++ b/aurora/playlist-.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# gpiomon waits for two events (rising or falling) +for((;;)); do + a=$(sudo gpiomon --bias pull-down -n 2 -F="%s" 0 109) + b=$(echo "$a" | tr -d '=') + mapfile -t sec_val <<<"$b" + n=$(( sec_val[1] - sec_val[0] )) + # n is the time difference between the two events (in seconds) + # check if music is playing + mpc | grep playing + status=$? + + if [ "$n" -gt 1 ]; then # long press, play previous playlist + if [ "$status" == 0 ]; then + mpc clear + cd /home/ubuntu/musica/playlists || exit + p_c=$(cat /home/ubuntu/musica/current_playlist) + tot=$(ls -1 * | wc -l) + if [ "$p_c" -gt 2 ]; then + p_n=$(( p_c - 1 )) + else + p_n=$tot + fi + echo "$p_n" > /home/ubuntu/musica/current_playlist + playlist_file=$(ls * | awk -v x="$p_n" 'NR==x') + playlist_file=${playlist_file/.m3u/} + mpc clear + mpc load "$playlist_file" + mpc play + else + mpc play + fi + else # short press, play previous song in the current playlist + if [ "$status" == 0 ]; then + mpc prev + else + mpc play + fi + fi +done diff --git a/aurora/vol+.sh b/aurora/vol+.sh index 3db5124..6e6e4e1 100755 --- a/aurora/vol+.sh +++ b/aurora/vol+.sh @@ -7,10 +7,10 @@ function action { status=$? #if YES if [ "$status" == 0 ]; then - mpc volume +10 + mpc volume +5 #if NO else - /bin/bash /home/ubuntu/scripts/replay.sh & + mpc play fi } diff --git a/aurora/vol-.sh b/aurora/vol-.sh index 0be9812..e4ecbe8 100755 --- a/aurora/vol-.sh +++ b/aurora/vol-.sh @@ -1,21 +1,51 @@ #!/bin/bash -# run as root +# run as root, decrease volume (short press) or shutdown the system (long press) -function action { - # check if music is playing - mpc | grep playing - status=$? - #if YES - if [ "$status" == 0 ]; then - mpc volume -10 - #if NO - else - /bin/bash /home/ubuntu/scripts/replay.sh & - fi -} +for((;;)); do + a=$(sudo gpiomon --bias pull-down -n 2 -F="%s" 0 113) + b=$(echo "$a" | tr -d '=') + mapfile -t sec_val <<<"$b" + n=$(( sec_val[1] - sec_val[0] )) + # n is the time difference between the two events (in seconds) -for((;;)) -do - gpiomon --bias pull-down -s -n 1 -r 0 113 && action - sleep 0.7 + if [ "$n" -gt 3 ]; then # long press, shutdown + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + sleep 1 + mpc volume -10 + # ligths off + systemctl stop color + gpioset 0 101=1 + gpioset 0 99=1 + gpioset 0 97=1 + gpioset 0 140=1 + # shutdown + /usr/sbin/shutdown now + else # short press, decrease volume + # check if music is playing + mpc | grep playing + status=$? + #if YES + if [ "$status" == 0 ]; then + mpc volume -5 + #if NO + else + mpc play + fi + fi done