hvmpd.sh: add keys
authorHugo Villeneuve <hugo@hugovil.com>
Thu, 31 Oct 2024 05:26:00 +0000 (01:26 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Tue, 12 Nov 2024 03:40:44 +0000 (22:40 -0500)
recipes-multimedia/hvmpd-manager/files/hvmpd.sh

index daa427b..32bf889 100644 (file)
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 . /usr/bin/hvutils.sh
 
@@ -23,23 +23,54 @@ led_off() {
     echo 0 > /sys/class/leds/status/brightness
 }
 
+is_mpc_playing() {
+    if mpc | grep -q "\[playing\]"; then
+        return 0
+    else
+        return 1
+    fi
+}
+
+keys_manager() {
+    while true; do
+        #  0 -> key pressed
+        # 10 -> key NOT pressed
+        evtest --query /dev/input/event5 EV_KEY KEY_PLAYPAUSE
+        play=$?
+
+        if [ "${play_prev}" != "${play}" -a ${play} -eq 0 ]; then
+            log_info "mpc toggle"
+            mpc toggle 1> /dev/null
+        fi
+
+        play_prev=${play}
+
+        if is_mpc_playing ; then
+           led_on
+            log_dbg "[playing]"
+        else
+           led_off
+            log_dbg "[paused]"
+        fi
+
+        sleep 0.1
+    done
+}
+
 lcd_init
 
-while true; do
-    # Future bouton play/pause:
-    # mpc toggle -> pause/play/pause...
+# Start function in the background
+keys_manager &
 
+# Save PID
+pid=$!
+
+while true; do
     # mpc -f "<%name%> <%title%> <%album%>" current
     current="$(mpc current)"
     log_dbg "current = ${current}"
 
-    if [ "${current}" = "" ]; then
-       led_off
-        song=""
-        artist=""
-    else
-        led_on
-
+    if is_mpc_playing ; then
         # | awk '{$1=$1};1' --> trim spaces before and after
 
         station="${current}"
@@ -75,6 +106,13 @@ while true; do
                 log_dbg "artist = ${artist}"
             fi
         fi
+    else
+        song=""
+        artist=""
+
+        if [ "${station}" = "" ]; then
+            station="HVMPD"
+        fi
     fi
 
     station_prev="${station}"
@@ -98,3 +136,6 @@ while true; do
 
     sleep 1
 done
+
+# Kill keys_manager
+kill ${pid} >/dev/null 2>&1