Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a rolling average to the hackspeed script #413

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions polybar-scripts/info-hackspeed/info-hackspeed.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
# shellcheck disable=SC2016,SC2059

KEYBOARD_ID="AT Translated Set 2 keyboard"
Expand All @@ -8,7 +8,10 @@ KEYBOARD_ID="AT Translated Set 2 keyboard"
METRIC=cpm
FORMAT="# %d $METRIC"

INTERVAL=20
INTERVAL=1

# The amount of INTERVAL which must be 0 to reset the rolling average
RESET_INTERVAL=10

# If you have a keyboard layout that is not listed here yet, create a condition
# yourself. $3 is the key index. Use `xinput test "AT Translated Set 2 keyboard"`
Expand Down Expand Up @@ -43,6 +46,8 @@ printf '' > "$hackspeed_cache"
xinput test "$KEYBOARD_ID" | \
stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" &

array=()

while true; do
# Ask the kernel how big the file is with the command `stat`. The number we
# get is the file size in bytes, which equals the amount of dots the file
Expand All @@ -58,7 +63,36 @@ while true; do
# then divide
value=$((lines * multiply_by / divide_by))

printf "$FORMAT\\n" "$value"
if [ "${#array[@]}" -gt "60" ]; then
array=("${array[@]:1}")
fi

reset="1";

for (( i=1; i<=$RESET_INTERVAL; i++ ))
do
if [ "${#array[@]}" -ge "$i" ]; then
if [ "${array[${#array[@]} - $i]}" -ne "0" ]; then
reset="0"
break
fi
fi
done

if [ "$reset" -eq "1" ]; then
array=()
fi

array+=($value)

avg=0
for n in "${array[@]}"
do
avg=$((avg+n))
done
avg=$((avg/${#array[@]}))

printf "$FORMAT\\n" "$avg"

sleep $INTERVAL
done