X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=c%2Fgoertzel%2Fsleepmon.sh;h=646ad56650514f15cdc980f424de6928b46c4d11;hb=32f185d8351d21158af3876767fcf13bc80af123;hp=cb28c7187de5e2a7d82c66cfa7f70024293fd5b9;hpb=f1a48c15ae9a0c44ea229911a7523b35d13a0e48;p=mirrors%2FPrograms.git diff --git a/c/goertzel/sleepmon.sh b/c/goertzel/sleepmon.sh index cb28c71..646ad56 100755 --- a/c/goertzel/sleepmon.sh +++ b/c/goertzel/sleepmon.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Simple sleep monitor (Harvie 2012) # # You probably have soundcard with one output and input @@ -12,33 +12,67 @@ # Wake up # Enjoy your data - -out=/tmp/sleeplog-"$(date +%F_%r)".txt +out=/tmp/sleeplog-"$(date +%F_%T)".txt +graphout="${out%%.*}.png" speaker-test -t sine &>/dev/null & +pid_test=$! tresh=10 -lastdate=0 +lastdate="$(date +%s)" +laststate=0 +history=''; +historymax=120; +historylen='10 30 60 90 120' screen=false -while getopts "s" OPT; do +graph=false + +while getopts "sg" OPT; do test "$OPT" == 's' && screen=true; + test "$OPT" == 'g' && graph=true; done -arecord | ./goertzel -i -q -a -t $tresh -s 2000 | while read line; do + +echo "Writing log to: $out"; +$graph && echo "Writing graph to: $graphout" +echo +arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do date="$(date +%s)" time="$(echo "$line" | cut -f 1)" level="$(echo "$line" | cut -f 2)" - echo -ne "$time\t$date\t$(date '+%F%t%r')\t" - test "$level" -gt "$tresh" && { - echo -n "Nothing detected..."; + test "$level" -gt "$tresh" && state=false || state=true + $state && statenum=1 || statenum=0; + $state && statename='MOTION!' || statename='Nothing'; + + echo -ne "$time\t$date $(date '+%F %T') $statenum" + + #History + after=$(( $date - $lastdate)) + test $historymax -gt 0 && { + history=$(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" | head -c $historymax) + for len in $historylen; do + on="$(echo -n ${history::$len} | tr -d 0 | wc -c)" + on="$(echo "scale=2; $on/$len" | bc)" + LC_ALL=C printf " %.2f" "$on" + done + } + + #Debug + echo -e "\t($statename $level After $after secs)"; + + #Fun with values + $state && { $screen && xset dpms force off || true; } || { - echo -n "Motion detected!!!!"; $screen && xset dpms force on; } - test "$lastdate" != 0 && { - after=$(( $date - $lastdate)) - echo -ne "\t$level After $after secs"; - } - echo; + $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null & + + #Prepare invariants for next round lastdate="$date"; + laststate="$statenum"; done | tee "$out" -kill $! -echo "Your file: $out" +kill $pid_test; sleep 0.2 +echo +echo "Your log: $out" +$graph && { + ./sleepplot.sh "$out" "$graphout" &>/dev/null + echo "Your graph: $graphout" +}