keypress
authorHugo Villeneuve <hugo@hugovil.com>
Tue, 12 Nov 2024 01:51:03 +0000 (20:51 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Tue, 12 Nov 2024 03:43:53 +0000 (22:43 -0500)
recipes-multimedia/hvmpd-manager/files/hvmpd.sh

index a83bbcd..dde10c8 100644 (file)
@@ -31,14 +31,91 @@ is_mpc_playing() {
     fi
 }
 
+declare -a key_prev=(
+[0]=0
+[1]=0
+[2]=0
+[3]=0
+[4]=0
+[5]=0
+)
+
+# Arg1: key code
+#
+# Return value:
+#   0 -> key pressed
+#   1 -> key NOT pressed
+key_pressed() {
+    local key_code=${1}
+    local dev
+    local evdev
+    local prev
+    local key
+    local id
+    local rc
+
+    case ${key_code} in
+        KEY_UP)
+            id=0
+            dev="0"
+            ;;
+        KEY_DOWN)
+            id=1
+            dev="3"
+            ;;
+        KEY_LEFT)
+            id=2
+            dev="4"
+            ;;
+        KEY_RIGHT)
+            id=3
+            dev="1"
+            ;;
+        KEY_ENTER)
+            id=4
+            dev="2"
+            ;;
+        KEY_PLAYPAUSE)
+            id=5
+            dev="5"
+            ;;
+        *)
+            log_err "Unsupported key: ${key_code}"
+            ;;
+    esac
+
+    evdev="/dev/input/event${dev}"
+
+    #  0 -> key pressed
+    # 10 -> key NOT pressed
+    evtest --query ${evdev} EV_KEY ${key_code}
+    if [ $? -eq 0 ]; then
+        key="1"
+    else
+        key="0"
+    fi
+
+    log_dbg "keycode: ${key_code}"
+    log_dbg "  evdev=$evdev"
+    log_dbg "  id=$id"
+    log_dbg "  key =$key"
+    log_dbg "  prev=$key"
+
+    if [ ${key_prev[${id}]} != "${key}" -a ${key} = "1" ]; then
+        rc=0
+        log_info "key pressed: ${key_code}"
+    else
+        rc=1
+    fi
+
+    key_prev[${id}]=${key}
+
+    return ${rc}
+}
+
 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
+        if key_pressed KEY_PLAYPAUSE ; then
             log_info "mpc toggle"
 
             if is_mpc_playing ; then
@@ -48,8 +125,6 @@ keys_manager() {
             fi
         fi
 
-        play_prev=${play}
-
         if is_mpc_playing ; then
            led_on
             log_dbg "[playing]"
@@ -58,7 +133,7 @@ keys_manager() {
             log_dbg "[paused]"
         fi
 
-        sleep 0.1
+        sleep 0.01
     done
 }