X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fgoertzel%2Fsleepmon.sh;h=cfa02973e5144863ac161dc9f38d5f857c6225c6;hb=c2e6b73722f0d631bc33d7fcf2305d8b14ba6087;hp=42bbcad33f37a50fb2c249bd50b67b69835d7fc3;hpb=2fbfcde7b31158edd31950a62904dcc72a3966c3;p=mirrors%2FPrograms.git diff --git a/c/goertzel/sleepmon.sh b/c/goertzel/sleepmon.sh index 42bbcad..cfa0297 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,34 +12,61 @@ # Wake up # Enjoy your data +out=/tmp/sleeplog-"$(date +%F_%T)".txt +graphout="${out%%.*}.png" -out=/tmp/sleeplog-"$(date +%F_%r)".txt +killall speaker-test &>/dev/null speaker-test -t sine &>/dev/null & + +touch "$out" +bash ./sleepstats.sh "$out" &>/dev/null & + tresh=10 -lastdate=0 +lastdate="$(date +%s)" screen=false -while getopts "s" OPT; do +graph=false + +export LC_ALL=C + +#trap 'kill -9 $(jobs -p);' SIGINT + +while getopts "sg" OPT; do test "$OPT" == 's' && screen=true; + test "$OPT" == 'g' && graph=true; done -echo "Writing to file: $out"; -arecord | ./goertzel -n -q -l -t $tresh -d 4 | 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'; + + printf "%.2f\t %s %s %d" "$time" "$date" "$(date '+%F %T')" "$statenum" + + #Debug + after=$(( $date - $lastdate)) + printf " (%s %3d After %4d secs)\n" "$statename" "$level" "$after"; + + #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"; done | tee "$out" -kill $! -echo "Your file: $out" +kill $(jobs -p); sleep 0.2 +echo +echo "Your log: $out" +$graph && { + ./sleepplot.sh "$out" "$graphout" &>/dev/null + echo "Your graph: $graphout" +}