]> git.devmsg.it Git - giulio/.git/commitdiff
MPD scripts reworked master
authorGiulio Maestrini <giuliomaestrini@posteo.net>
Sat, 28 Dec 2024 09:52:27 +0000 (10:52 +0100)
committerGiulio Maestrini <giuliomaestrini@posteo.net>
Sat, 28 Dec 2024 09:52:27 +0000 (10:52 +0100)
aurora/color.sh
aurora/init_mpd.sh [new file with mode: 0755]
aurora/init_mpd_1.sh [deleted file]
aurora/init_mpd_2.sh [deleted file]
aurora/playlist+.sh [new file with mode: 0644]
aurora/playlist-.sh [new file with mode: 0644]
aurora/vol+.sh
aurora/vol-.sh

index 5d95efcfbb53fce470506cec832fd5e884cd68ed..4473e6a180bff9a15ce609d13e2892d132ea53f5 100755 (executable)
@@ -51,14 +51,14 @@ function giallo {
 }
 
 function off {
 }
 
 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
 do
        rnd=$(shuf -i 1-7 -n 1)
        case $rnd in
@@ -92,7 +92,6 @@ do
        ;;
        esac
 
        ;;
        esac
 
-       sleep 15
+       t=$(shuf -i 3-20 -n 1)
+       sleep "$t"
 done
 done
-
-off
diff --git a/aurora/init_mpd.sh b/aurora/init_mpd.sh
new file mode 100755 (executable)
index 0000000..d36c64f
--- /dev/null
@@ -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 (executable)
index fcf26a6..0000000
+++ /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 (executable)
index 044299a..0000000
+++ /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 (file)
index 0000000..9de62ff
--- /dev/null
@@ -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 (file)
index 0000000..57c7d4a
--- /dev/null
@@ -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
index 3db5124ab7af359beae6fea8cf1aed58dc56c62a..6e6e4e1c0f46d7f01c93fc514cb54360c4b5e85b 100755 (executable)
@@ -7,10 +7,10 @@ function action {
        status=$?
        #if YES
        if [ "$status" == 0 ]; then
        status=$?
        #if YES
        if [ "$status" == 0 ]; then
-               mpc volume +10
+               mpc volume +5
        #if NO
        else
        #if NO
        else
-               /bin/bash /home/ubuntu/scripts/replay.sh &
+               mpc play
        fi
 }
 
        fi
 }
 
index 0be981273f002b47d2b4342a441ac808cbb659b2..e4ecbe8c09612f4ea5d620d516b4830f120a74aa 100755 (executable)
@@ -1,21 +1,51 @@
 #!/bin/bash
 #!/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
 done