#!/usr/bin/perl do './dim.pl'; do './motor.pl'; print "fish go\n"; use Time::HiRes qw(time sleep); $|=1; $motordir="s"; # stop, up, down $timestep=.1; while(1) { # # watch stdin for new line # $rin = $win = $ein = ''; vec($rin,fileno(STDIN),1) = 1; $ein = $rin | $win; ($nfound,$timeleft) = select($rout=$rin, $wout=$win, $eout=$ein, $timestep); $sall=0; if( $nfound ) { sysread(STDIN,$in,10000); readinput($in); $sall=1; } # # update all fades # $now=time; $finished=""; foreach $dev (keys %fades) { ($st,$et,$sl,$el) = @{$fades{$dev}}; if( $now > $et ) { dim($dev,$el); delete $fades{$dev}; $sall=1; $finished.="$dev "; } else { dim($dev, $sl + ($el-$sl) * ($now-$st)/($et-$st)); } print "\nfinished $finished\n" if $finished; #print "doing fade $st,$et,$sl,$el\n"; } showall() if $sall; # # move motor # motordown() if $motordir eq "d"; motorup() if $motordir eq "u"; # # show stat if time has passed # if( time > $timelastshown+10 ) { showall(); } } sub showall { my($c)=0; # return if time-$timelastshown < .2; $timelastshown = time; print "\n"; foreach(@devs) { print " $_: "; print int(getlevel($_)); print "\n" if $c++==9; } print " sundown: $mpos\nfish: "; } sub createfade { my( $dev,$end,$dur ) = @_; return unless length($dev)==2; $nowt = time; $endt = $nowt+$dur; $startl = getlevel($dev); $endl = $end; # print "create $dev=$nowt,$endt,$startl,$endl\n"; $fades{$dev} = [$nowt,$endt,$startl,$endl]; } sub readinput { $_ = shift; chomp; # # catch 'stop' command # if( /^stop/ ) { undef %fades; print "all fades cancelled\n"; return; } # # 'step' command if( /^step(.*)/ ) { $timestep = $1 if $1 > 0 && $1 < 5; print "timestep is $timestep\n"; return; } # # ''cue" # if( /^\'(.*)/ ) { $cue = $1; $res = `egrep \"^$1 \" cues`; ($c,$act) = $res=~/^([^ ]+) (.*)\n/; print "do cue $cue: $act\n"; readinput($act); print "done\n"; return; } # # was there a /time at the end? # $tm=0; $tm =$1 if s/\/(.*)//; # # 'bo' command (can accept a time) # if( /bo/ ) { print "blackout: "; foreach(@devs) { createfade($_,0,$tm) unless /s[1-4]/; } return; } # # parse out all the devices and levels into %m{dev}=level # undef %m; while( s/^(..)([-0-9\.]*)// ) { if( $1=~/m(.)/ ) { $motordir = $1; print "motor set to $motordir\n"; } else { $m{$1} = $2 unless $1 eq "\n"; print "read $1 to $2\n"; } } foreach (keys(%m)) { action($_,$m{$_}); } foreach(keys(%m)) { createfade($_,$m{$_},$tm); } print "$tm second fade\n"; }