Linux 笔记本基于“敲打”的命令

时间:2016-09-12来源:网络

接下来是 knockListen 子程序前 行负责读取当前的加速器数据值并对基本的值读取进行调整如果加速器的数量在某一维度上大于更新上限值那么 checkKnock 变量就被设置为 为了调整这个程序使它只响应我们需要的敲打事件或类似的加速值我们需要扩大更新上限例如我们可以将 ThinkPad 放到自己的汽车中并让它在检测到硬加速(或减速)时更改 MP 播放列表

如果敲打笔记本的力度足够大并且大于了更新上限那么就会导致调用getEpochMicroSeconds 子程序然后 diffInterval 变量会在两次敲打事件之间被赋值这个值将很多击打力度大于更新上限的很多快速加速读取压缩到一个时间中如果没有间隔上限检查一次硬敲打就会被注册成很多事件就仿佛是加速器连续一段时间产生大量事件一样这种行为对于用户的视力和触觉来说都是无法感知到的但对于 HDAPS 来说显然并非如此如果已经达到了间隔上限那么敲打间隔会被记录在 baseKnocks 数组中然后将两次敲打之间的间隔重置

仔细修改这些变量可以帮助对程序进行优化从而识别出您特有的敲打风格缩小更新上限并扩大周期上限可以检测出更多间隔的轻微敲打机械敲打设备或特定的敲打方法可能会需要降低间隔上限从而识别出独特的敲打事件

清单 knockListen 子程序

sub knockListen() { my $checkKnock = ; ($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 nonevents like # bashing the enter key or hitting the side with the mouse if( abs ($currX) > $UPDATE_THRESHOLD) { $checkKnock = ; } if( abs ($currY) > $UPDATE_THRESHOLD) { $checkKnock = ; } if( $checkKnock == ){ 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 == ){ $diffInterval = } if( $option ){ print Knock: $knockCount ## last: [$currVal] curr: [$prevInterval] ; print difference is: $diffIntervaln; } push @baseKnocks $diffInterval; $knockCount++; }# if the difference interval is greater than the threshold $prevInterval = $currVal; }#if checkknock passed }#knockListen

在创建敲打模式时该模式会被放入 ~/knockFile 文件中并使用下面的子程序进行读取

清单 读取敲打文件

sub readKnockFile { open(KNCKFILE$ENV{HOME}/knockFile) or die no knock file: $!; while(){ if( !/^#/ ){ my @arrLine = split _#_; $knockHash{ $arrLine[] }{ cmd } = $arrLine[]; $knockHash{ $arrLine[] }{ comment } = $arrLine[]; }#if not a comment line }#for each line in file close(KNCKFILE); }#readKnockFile

当 knockListen 获得敲打模式时它会将该模式与从 readKnockFile 中读取的敲打模式进行比较下面的 compareKnockSequences 子程序会对敲打之间的时间进行简单的区别检查注意敲打之间的差别并不是简单混合在一起的很多次敲打时的少量时间差别并不会累积成总体的匹配失效

第一个要比较的是敲打的次数因为我们没有必要将一个七次敲打的序列与一个两次敲打的序列进行比较如果敲打的次数与 ~/knockFile 中现有的敲打序列匹配每次敲打之间的差别也少于最大敲打偏差那么这次敲打就可以认为是完全匹配的在允许敲打序列进行不精确匹配时最大敲打偏差非常关键我们可以增大最大敲打偏差来使敲打节奏更加自由但是要注意这可能会导致敲打模式匹配不正确例如我们可以在所期望的时间之前或之后半秒钟允许自己的敲打模式发生偏离但这仍然可以匹配这样就可以有效地说明刮脸和理发 可以与 Mary 姓 Little Lamb 匹配因此在修改这个参数时一定要小心

如果完整的模式可以匹配就会运行 ~/knockFile 中指定的命令如果启用了冗余模式则会打印结果下一个步骤是如果没有找到匹配项就退出这个子程序如果找到了匹配项就重置所记录的敲打序列这个步骤会执行 compareKnockSequences 子程序

清单 比较敲打序列

sub compareKnockSequences { my $countMatch = ; # record how many knocks matched # for each knock sequence in the config file for( keys %knockHash ){ # get the timings between knocks my @confKnocks = split; # if the count of knocks match if( $knockCount eq @confKnocks ){ my $knockDiff = ; my $counter = ; for( $counter=; $counter$knockCount; $counter++ ){ $knockDiff = abs($confKnocks[$counter] $baseKnocks[$counter]); my $knkStr = k $counter b $baseKnocks[$counter] c $confKnocks[$counter] d $knockDiffn; # if its an exact match increment the matching counter if( $knockDiff $MAX_KNOCK_DEV ){ if( $option ){ print MATCH $knkStr } $countMatch++; # if the knocks dont match move on to the next pattern in the list }else{ if( $option ){ print DISSONANCE $knkStr } last; }# deviation check }#for each knock }#if number of knocks matches # if the count of knocks is an exact match run the command if( $countMatch eq @confKnocks ){ my $cmd = system( $knockHash{@confKnocks }{ cmd } ); if( $option ){ print $cmdn } last; # otherwise make the count of matches zero in order to not reset }else{ $countMatch = ; } }#for keys # if the match count is zero exit and dont reset variables so a longer # knock sequence can be entered and checked if( $countMatch == ){ return() } # if a match occurred reset the variables so it wont match another pattern $knockCount = ; @baseKnocks = (); }#compareKnockSequences

1 2 3 4

关键词: Linux 命令

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW

或用微信扫描左侧二维码

相关文章

查看电脑版