# Wake up
# Enjoy your data
-
out=/tmp/sleeplog-"$(date +%F_%T)".txt
+graphout="${out%%.*}.png"
speaker-test -t sine &>/dev/null &
pid_test=$!
tresh=10
historymax=120;
historylen='10 30 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
-echo "Writing to file: $out";
+
+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)"
} || {
$screen && xset dpms force on;
}
- ./sleepplot.sh "$out" &>/dev/null &
+ $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null &
#Prepare invariants for next round
lastdate="$date";
laststate="$statenum";
done | tee "$out"
-kill $pid_test
+kill $pid_test; sleep 0.2
echo
-echo "Your file: $out"
+echo "Your log: $out"
+$graph && {
+ ./sleepplot.sh "$out" "$graphout" &>/dev/null
+ echo "Your graph: $graphout"
+}
exit 23
}
+in="$1";
+graphout="${in%%.*}.png"
+test -n "$2" && graphout="$2";
+
#Approximate size of graph
-size="$(tail -n 1 "$1" | cut -d . -f 1)"
+size="$(tail -n 1 "$in" | cut -d . -f 1)"
test $size -gt 3600 && size="$(( $size/10 ))" #For prolonged periods
size="$(( 600 + $size ))"
#Plot it
gnuplot << EOF
-set output "$1.png"
+set output "$graphout"
set terminal png size $size,300
set title "Sleep motions"
set grid
#set pointsize 0.5
-plot "$1" using 2:5 title "Sensor state" with steps,\
-"" using 2:(\$6*3) title "Avg last last 10 seconds" with lines,\
-"" using 2:(\$7*3) title "Avg last last 30 seconds" with lines,\
-"" using 2:(\$8*3) title "Avg last last 120 seconds" with lines\
+plot\
+"$in" using 2:5 title "Sensor state" with steps,\
+"" using 2:(\$6*3) title "Avg. 10s" with lines,\
+"" using 2:(\$7*3) title "Avg. 30s" with lines,\
+"" using 2:(\$8*3) title "Avg. 120s" smooth bezier\
EOF