Code/tp-theft-lcars

From ThinkWiki
Jump to: navigation, search
#!/usr/bin/perl
# tp-theft-lcars v0.2 (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.
#
# Here comes a modified version which requires an internet connection and gnash.
# It shows a LCARS red alert flash animation from Star Trek instead of a beep.
# You may choose your favourite by adding/removing the sharps.

use strict;
use warnings;

##############################
# Audio volume (0..100)
my $volume = 70;

# Voyager alert system status
my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/lcars_files/alert%20system%20status.swf";

# Voyager self destruct
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/lcars_files/voyager%20self%20destruct.swf";

# Voyager red alert memory allocation
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/lcars_files/red%20alert%20memory%20allocation.swf";

# TNG ancillary red alert monitor
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/lcars_files/lcars_ancillary_red_alert_monitor.swf";

# Captain Kirk to the bridge
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/cin/tos%20red%20alert%20panel.swf";

# retro red alert 
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/ano%20gifs/st_original_redalert.swf";

# DS9: USS Defiant red alert 
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/lcars_files/LCARS%20Defiant%20red%20alert.swf";

# Voyager emergency scan
# my $play_cmd = "gtk-gnash --fullscreen http://www.lcars.org.uk/lcars_files/LCARS_prey_emergency_scan_analysis.swf";

##############################
# 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 && !(defined($alarm_file) && -f $alarm_file)) {
	print "ALARM\n" if $verbose>0;
	$alarm_file = `mktemp /tmp/hdaps-tilt.XXXXXXXX` or die "mktemp: $?";
	chomp($alarm_file);
	system('/bin/bash', '-c', <<"EOF")==0 or die "Failed: $?";
($play_cmd) &
EOF
    }

    select(undef, undef, undef, $interval); # sleep
}