1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # readToneFile reads tone sequences and commands from ~/.toneFile # format is: tones _#_ times _#_ command _#_ comments sub readToneFile { #give it a full path to .toneFile if on windows open(TONEFILE,"$ENV{HOME}/.toneFile") or die "no tone file: $!"; while(<TONEFILE>){ if( !/^#/ ){ my @arrLine = split "_#_"; $toneHash{ "$arrLine[0] $arrLine[1]" }{ tones } = $arrLine[0]; $toneHash{ "$arrLine[0] $arrLine[1]" }{ times } = $arrLine[1]; $toneHash{ "$arrLine[0] $arrLine[1]" }{ cmd } = $arrLine[2]; $toneHash{ "$arrLine[0] $arrLine[1]" }{ comment } = $arrLine[3]; }#if not a comment line }#for each line in file close(TONEFILE); }#readToneFile |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | sub compareToneSequences { my $baseCount = @baseTones; my $countMatch = 0; # record how many tones matched for my $toneFromFile ( keys %toneHash ) { my @confTones = split " ", $toneHash{$toneFromFile}{tones}; my @confTimes = split " ", $toneHash{$toneFromFile}{times}; my $confCount = @confTones; next unless( $confCount == $baseCount ); # as a learning aid, the matching and non matching portions of the # comparison are printed out, so at least you can see what is going # wrong while trying to remember your tone codes my $pos =0; my $toneMatchFail = 0; for( @baseTones ) { my $tonDiff = abs($confTones[$pos] - $baseTones[$pos]); my $tonStr = "t $pos b $baseTones[$pos] ". "c $confTones[$pos] \n"; my $timeDiff = abs($confTimes[$pos] - $baseTimes[$pos]); my $timStr = "t $pos b $baseTimes[$pos] ". "c $confTimes[$pos] d $timeDiff\n"; if( $tonDiff > $MAX_TONE_DEV ) { $toneMatchFail = 1; if( $option ){ print "NOTE DISSONANCE $tonStr" } }else { if( $option ){ print "NOTE MATCH $tonStr" } }#if tone detected outside of deviation # if it's an exact match, increment the matching counter if( $timeDiff < $MAX_TIME_DEV ){ if( $option ){ print "TIME MATCH $timStr" } $countMatch++; }else{ if( $option ){ print "TIME DISSONANCE $timStr" } last; }# deviation check $pos++; }# for each tone to check if( $countMatch == $confCount && $toneMatchFail == 0 ) { my $cmd = $toneHash{$toneFromFile}{ cmd }; if( $option ){ print "run: $cmd\n" } $cmd =`$cmd`; if( $option ){ print "result: $cmd\n" } last; # otherwise, make the count of matches zero, in order to not reset }else { $countMatch = 0; } }#for each tone in tone file # if the match count is zero, exit and don't reset variables so a longer # tone sequence can be entered and checked if( $countMatch == 0 ){ return() } # if a match occured, reset the variables so it won't match another pattern $toneCount = 0; @baseTones = (); @baseTimes = (); }#compareToneSeqeunces |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | if( $option eq "-c" ){ print "enter a tone sequence:\n"; $startTime = getEpochSeconds(); # reset time out start while( my $sndPeekOutput = <STDIN> ) { $currTime = getEpochSeconds(); # check if there has not been a tone in a while if( $currTime - $startTime > $MAX_TIMEOUT_LENGTH ){ $timeOut = 1; # exit the loop }else{ # if a tone has been entered before timeout, reset timers so # more tones can be entered if( $toneCount != $toneAge ){ $startTime = $currTime; # reset timer for longer delay $toneAge = $toneCount; # synchronize tone counts }# if a new tone came in }# if timer not reached readTones( $sndPeekOutput ); if( $timeOut == 1 ){ last } }#while stdin if( @baseTones ){ print "place the following line in $ENV{HOME}/.toneFile\n\n"; for( @baseTones ){ print "$_ " } print "_#_ "; for( @baseTimes ){ print "$_ " } print "_#_ (command here) _#_ <comments here>\n\n"; }#if tones entered |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | }else { # main code loop to listen for tones and run commands readToneFile(); $startTime = getEpochSeconds(); while( my $sndPeekOutput = <STDIN> ) { $currTime = getEpochSeconds(); if( $currTime - $startTime > $LISTEN_TIMEOUT ){ $toneCount = 0; @baseTones = (); @baseTimes = (); $startTime = $currTime; if( $option ){ print "listen timeout - resetting tones \n" } }else{ if( $toneCount != $toneAge ){ $startTime = $currTime; # reset timer for longer delay $toneAge = $toneCount; # synchronize tone counts }# if a new tone came in compareToneSequences(); }#if not reset timeout readTones( $sndPeekOutput ); }#while stdin }#if option set |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |