Difference between revisions of "Code/tp-theft-basic"
(Moved from Script for theft alarm using HDAPS#A_basic_script) |
m |
||
Line 37: | Line 37: | ||
open(POS,"<",$pos_file) or die "Can't open HDAPS file $pos_file: $!\n"; | open(POS,"<",$pos_file) or die "Can't open HDAPS file $pos_file: $!\n"; | ||
$_=<POS>; | $_=<POS>; | ||
− | m/^\((-?\d | + | m/^\((-?\d ),(-?\d )\)$/ or die "Can't parse $pos_file content\n"; |
return ($1,$2); | return ($1,$2); | ||
} | } | ||
Line 44: | Line 44: | ||
my $sum=0; | my $sum=0; | ||
my $sumsq=0; | my $sumsq=0; | ||
− | my $n=$#_ | + | my $n=$#_ 1; |
for my $v (@_) { | for my $v (@_) { | ||
− | $sum | + | $sum = $v; |
− | $sumsq | + | $sumsq = $v*$v; |
} | } | ||
return sqrt($n*$sumsq - $sum*$sum)/($n*($n-1)); | return sqrt($n*$sumsq - $sum*$sum)/($n*($n-1)); | ||
Line 72: | Line 72: | ||
my $tilted = $xdev>$thresh || $ydev>$thresh; | my $tilted = $xdev>$thresh || $ydev>$thresh; | ||
− | if ($tilted | + | if ($tilted |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 17:41, 10 April 2007
- !/usr/bin/perl
- tp-theft v0.1 (http://thinkwiki.org/wiki/Script_for_theft_alarm_using_HDAPS)
- This script uses the HDAPS accelerometer found on recent ThinkPad models
- to emit an audio alarm when the laptop is tilted. In sufficiently
- populated environments, it can be used as a laptop theft deterrent.
- This file is placed in the public domain and may be freely distributed.
use strict; use warnings;
- Siren volume and content
- Audio volume (0..100)
my $volume = 70;
- Synthesize a siren for 1.0 seconds:
my $play_cmd = "sox -t nul /dev/null -t ossdsp /dev/dsp synth 1.0 sine 2000-4000 sine 4000-2000";
- Play a file:
- my $play_cmd = "play keep_your_hands_off_me.wav";
- Other tweakables
my $thresh = 0.20; # tilt threshold (increase value to decrease sensitivity) my $interval = 0.1; # sampling interval in seconds my $depth = 10; # number of recent samples to analyze my $pos_file='/sys/devices/platform/hdaps/position'; my $verbose = 1;
- Code
sub get_pos {
open(POS,"<",$pos_file) or die "Can't open HDAPS file $pos_file: $!\n"; $_=<POS>; m/^\((-?\d ),(-?\d )\)$/ or die "Can't parse $pos_file content\n"; return ($1,$2);
}
sub stddev {
my $sum=0; my $sumsq=0; my $n=$#_ 1; for my $v (@_) {
$sum = $v; $sumsq = $v*$v;
} return sqrt($n*$sumsq - $sum*$sum)/($n*($n-1));
}
my (@XHIST, @YHIST); my ($x,$y) = get_pos; for (1..$depth) {
push(@XHIST,$x); push(@YHIST,$y);
} my $alarm_file; # flags ongoing alarm (and stores saved mixer settings)
while (1) {
my ($x,$y) = get_pos; shift(@XHIST); push(@XHIST,$x); shift(@YHIST); push(@YHIST,$y); my $xdev = stddev(@XHIST); my $ydev = stddev(@YHIST);
# Print variance and history print "X: v=$xdev (".join(',',@XHIST).") Y: v=$ydev (".join(",",@YHIST).")\n" if $verbose>1;
my $tilted = $xdev>$thresh || $ydev>$thresh;
if ($tilted