Commented unfinished mess
[mirrors/Programs.git] / c / goertzel / sleepmon.sh
CommitLineData
890439ae
TM
1#!/bin/sh
2# Simple sleep monitor (Harvie 2012)
3#
4# You probably have soundcard with one output and input
5# Take PIR motion sensor from security system
6# Use PIR to switch connection between left channels of output and input (tips of 3.5mm jacks)
7# Connect grounds of these two together (maybe you will not need it - depending on your soundcard)
8# Power up PIR
9# Launch this script
10# Use alsamixer and some GUI recorder (like audacity) to tune volume to some usable level
11# Place PIR facing to your bed and go sleep
12# Wake up
13# Enjoy your data
14
5e47bf73 15out=/tmp/sleeplog-"$(date +%F_%T)".txt
5769d767 16graphout="${out%%.*}.png"
890439ae 17speaker-test -t sine &>/dev/null &
991ef83f 18pid_test=$!
890439ae 19tresh=10
720d982e
TM
20lastdate="$(date +%s)"
21laststate=0
22history='';
23historymax=120;
6e6479ce 24historylen='10 30 60 90 120'
f51cf844 25screen=false
5769d767
TM
26graph=false
27
28while getopts "sg" OPT; do
f51cf844 29 test "$OPT" == 's' && screen=true;
5769d767 30 test "$OPT" == 'g' && graph=true;
f51cf844 31done
5769d767
TM
32
33echo "Writing log to: $out";
34$graph && echo "Writing graph to: $graphout"
35echo
19a509d1 36arecord | ./goertzel -n i -q -l c -t $tresh -d 4 | while read line; do
890439ae
TM
37 date="$(date +%s)"
38 time="$(echo "$line" | cut -f 1)"
39 level="$(echo "$line" | cut -f 2)"
720d982e
TM
40 test "$level" -gt "$tresh" && state=false || state=true
41 $state && statenum=1 || statenum=0;
42 $state && statename='MOTION!' || statename='Nothing';
43
44 echo -ne "$time\t$date $(date '+%F %T') $statenum"
45
46 #History
47 after=$(( $date - $lastdate))
48 test $historymax -gt 0 && {
49 history=$(echo -n "$(yes | tr '\ny' $laststate | head -c $after)$history" | head -c $historymax)
50 for len in $historylen; do
51 on="$(echo -n ${history::$len} | tr -d 0 | wc -c)"
52 on="$(echo "scale=2; $on/$len" | bc)"
53 LC_ALL=C printf " %.2f" "$on"
54 done
55 }
56
57 #Debug
58 echo -e "\t($statename $level After $after secs)";
59
60 #Fun with values
61 $state && {
f1a48c15 62 $screen && xset dpms force off || true;
890439ae 63 } || {
f1a48c15 64 $screen && xset dpms force on;
890439ae 65 }
5769d767 66 $graph && ./sleepplot.sh "$out" "$graphout" &>/dev/null &
720d982e
TM
67
68 #Prepare invariants for next round
890439ae 69 lastdate="$date";
720d982e 70 laststate="$statenum";
890439ae 71done | tee "$out"
5769d767 72kill $pid_test; sleep 0.2
19a509d1 73echo
5769d767
TM
74echo "Your log: $out"
75$graph && {
76 ./sleepplot.sh "$out" "$graphout" &>/dev/null
77 echo "Your graph: $graphout"
78}
This page took 0.178952 seconds and 4 git commands to generate.