清单 5. 时间分隔器
sub getEpochMicroSeconds {
my $TIMEVAL_T = "LL"; # LL for microseconds
my $timeVal = pack($TIMEVAL_T, ());
syscall(&SYS_gettimeofday, $timeVal, 0) != -1 or die "micro seconds: $!";
my @vals = unpack( $TIMEVAL_T, $timeVal );
$timeVal = $vals[0] . $vals[1];
$timeVal = substr( $timeVal, 6);
my $padLen = 10 - length($timeVal);
$timeVal = $timeVal . "0" x $padLen;
return($timeVal);
}#getEpochMicroSeconds
sub getEpochSeconds {
my $TIMEVAL_T = "LL"; # LL for microseconds
my $start = pack($TIMEVAL_T, ());
syscall(&SYS_gettimeofday, $start, 0) != -1 or die "seconds: $!";
return( (unpack($TIMEVAL_T, $start))[0] );
}#getEpochSeconds
|
接下来是 knockListen
子程序,前 5 行负责读取当前的加速器数据值,并对基本的值读取进行调整。如果加速器的数量在某一维度上大于更新上限值,那么 checkKnock
变量就被设置为 1。为了调整这个程序,使它只响应我们需要的敲打事件或类似的加速值,我们需要扩大更新上限。例如,我们可以将 ThinkPad 放到自己的汽车中,并让它在检测到硬加速(或减速)时更改 MP3 播放列表。
如果敲打笔记本的力度足够大,并且大于了更新上限,那么就会导致调用 getEpochMicroSeconds
子程序。然后 diffInterval
变量会在两次敲打事件之间被赋值。这个值将很多击打力度大于更新上限的很多快速加速读取压缩到一个时间中。如果没有间隔上限检查,一次硬敲打就会被注册成很多事件,就仿佛是加速器连续一段时间产生大量事件一样。这种行为对于用户的视力和触觉来说都是无法感知到的,但对于 HDAPS 来说显然并非如此。如果已经达到了间隔上限,那么敲打间隔会被记录在 baseKnocks
数组中,然后将两次敲打之间的间隔重置。
仔细修改这些变量可以帮助对程序进行优化,从而识别出您特有的敲打风格。缩小更新上限并扩大周期上限可以检测出更多间隔的轻微敲打。机械敲打设备或特定的敲打方法可能会需要降低间隔上限,从而识别出独特的敲打事件。
清单 6. knockListen 子程序
sub knockListen() {
my $checkKnock = 0;
($currX, $currY) = readPosition();
$currX -= $restX; # adjust for rest data state
$currY -= $restY; # adjust for rest data state
# require a high threshold of acceleration to ignore non-events like
# bashing the enter key or hitting the side with the mouse
if( abs ($currX) > $UPDATE_THRESHOLD) {
$checkKnock = 1;
}
if( abs ($currY) > $UPDATE_THRESHOLD) {
$checkKnock = 1;
}
if( $checkKnock == 1 ){
my $currVal = getEpochMicroSeconds();
my $diffInterval = abs($prevInterval - $currVal);
# hard knock events can create continuous acceleration across a large time
# threshold. requiring an elapsed time between knock events effectively
# reduces what appear as multiple events according to sleep_interval and
# update_threshold into a singular event.
if( $diffInterval > $INTERVAL_THRESHOLD ){
if( $knockCount == 0 ){ $diffInterval = 0 }
if( $option ){
print "Knock: $knockCount ## last: [$currVal] curr: [$prevInterval] ";
print "difference is: $diffInterval\n";
}
push @baseKnocks, $diffInterval;
$knockCount++;
}# if the difference interval is greater than the threshold
$prevInterval = $currVal;
}#if checkknock passed
}#knockListen
|
在创建敲打模式时,该模式会被放入 ~/.knockFile 文件中,并使用下面的子程序进行读取:
本新闻共
6页,当前在第
4页
1 2 3 4 5 6