#! perl -w # watches CTS on serial port for contact closure with RTS # CTS is pulled low with 10K to DTR which is set low # modified 2/22/10 for quieter logging of noisy reed relay # on current sensor on ejector pump by only logging first # instance in 10 sec ($thresh) window ######################### We start with some black magic to print on failure. BEGIN { $| = 1; print "logclosure.plx loaded "; } END {print "not ok 1\n" unless $loaded;} use Win32::SerialPort 0.05; #use Win32::GuiTest qw(:ALL); $loaded = 1; print "ok\n"; ######################### End of black magic. #*********** SPECIFY COMM PORT HERE *************** my $file = "COM1"; #my $file = "COM4"; my $ob; # 2: Constructor $ob = new Win32::SerialPort ($file) or die "Can't open $file\n"; $ob->databits(8); $ob->baudrate(1200); $ob->parity("none"); $ob->stopbits(1); $ob->handshake("none"); $ob->buffers(100,100); $ob->write_settings||undef $ob; print "couldn't set up port\n" unless $ob; $ob->error_msg(1); # use built-in error messages $ob->user_msg(1); open LOG,">log.txt" or die "couldn't open log file\n"; select (LOG); $|++; select(STDOUT); $start=time(); $lt=localtime($start); print"Started $lt\n"; print LOG "Started $lt\n"; # new inits for quieter logging $oldt=0; $thresh=10; #****************************************************** # DEFINE PROGRAM WINDOW TO SEND TO HERE #****************************************************** #$target="Winamp"; #$target="SqView - A2"; #****************************************************** # DEFINE KEYS TO SEND HERE #****************************************************** # BUTTON HELD DOWN sub hold { $t=time()-$start; if($t-$oldt > $thresh){ $t=time()-$start; print LOG "$lt\n"; print "$lt: HOLDING..."; $oldt=$t; } #SendKeys("c"); } # end hold # RELEASED AFTER HOLD OR CLICK AND HOLD sub unhold { print "RELEASED\n"; #SendKeys("c"); } # end unhold # BUTTON CLICKED sub click { $t=time()-$start; if($t-$oldt > $thresh){ print LOG "$lt\n"; print "$lt: CLICK\n"; $oldt=$t; } # $tt=time(); # print "tt is $tt\n"; #SendKeys("x"); } # end click # BUTTON DOUBLE CLICKED sub dblclick { $t=time()-$start; if($t-$oldt > $thresh){ print LOG "$lt\n"; print "$lt: DOUBLE CLICK\n"; $oldt=$t; } #SendKeys("v"); # close LOG; # exit 0; } # end dblclick # CLICK AND HOLD sub clkhold { $t=time()-$start; if($t-$oldt > $thresh){ print LOG "$lt\n"; print "$lt: CLICK AND HOLD...\n"; $oldt=$t; } } # end clkhold #******** FIRST HILTON TEST *********** # CTS hi when button pressed # set handshake lines to provide ref hi and lo $ob->rts_active("yes"); $ob->dtr_active("no"); # config of button sense timing $sleeptime=0.02; #$sleeptime=1; $debounce=2; $holdtime=10; $dbltime=6; #my @windows = FindWindowLike("", $target,"",""); #if (not @windows) { die "Could not find \"$target\"\n";} # this finds the main Winamp window #$targetwin=$windows[0]; $|=1; # bring target window to foreground #SetForegroundWindow($targetwin); # $state values: # 0 up, waiting # 1 down, click or hold # 2 down, held # 3 up, clicked waiting for dbl click # 4 down, one click already $state=0; #********* MAIN LOOP WATCHING BUTTON ************* while(1){ # read status; if down $dn=1; $stat=$ob->modemlines; $dn=($stat & $ob->MS_CTS_ON); $lt=localtime(time()); #$x++; #print "$dn $x\n"; #if($stat & $ob->MS_CTS_ON){$dn=1;} #else{$dn=0;} # update downtime or uptime if($dn){$d++;$u=0;} else {$u++;$d=0;} # state machine to call action subroutines if($dn && $state==0 && $d>$debounce) {$state=1;} if($dn && $state==1 && $d>$holdtime) {$state=2; hold();} if($dn && $state==3 && $d>$debounce) {$state=4;} if($dn && $state==4 && $d>$holdtime) {$state=2; clkhold();} if (!$dn && $state==1) {$state=3;} if (!$dn && $state==2) {$state=0; unhold();} if (!$dn && $state==3 && $u>$dbltime) {$state=0; click();} if (!$dn && $state==4) {$state=0; dblclick();} select undef,undef,undef,$sleeptime; }