From 0121ef274d7188b6e52f56b1a5ccbf8f97c6fd7b Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Mon, 11 Nov 2024 20:51:03 -0500 Subject: [PATCH] keypress --- .../hvmpd-manager/files/hvmpd.sh | 93 +++++++++++++++++-- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/recipes-multimedia/hvmpd-manager/files/hvmpd.sh b/recipes-multimedia/hvmpd-manager/files/hvmpd.sh index a83bbcd..dde10c8 100644 --- a/recipes-multimedia/hvmpd-manager/files/hvmpd.sh +++ b/recipes-multimedia/hvmpd-manager/files/hvmpd.sh @@ -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 } -- 2.20.1