use Getopt::Long; use Device::ParallelPort; use Device::ParallelPort::drv::auto; use Time::HiRes qw( usleep ); initialize(); if ($interval_usec) { print "\$interval_usec = $interval_usec\n"; run_pin_frequency_time($pin,$interval_usec,$length); #while (1) { # $port->set_bit(0,1); # usleep ($interval_usec); # $port->set_bit(0,0); # usleep ($interval_usec); #} } else { print "Error: \$inverval_usec is not set."; } sub initialize { $port = Device::ParallelPort->new('auto'); if (@_) { my ( $hertz, $milliseconds, $length ) = @_; } GetOptions( 'hertz:s' => \$hertz, 'ms:s' => \$milliseconds, 'time:s' => \$length ); if ($hertz) { # convert to microseconds for Time::HiRes $interval_usec = (1/$hertz) * 1000000; } elsif ($milliseconds) { # convert to microseconds for Time::HiRes $interval_usec = ($1) * 1000; } else { print "\nEnter pin [0-7]: "; chomp($pin = ); print "\nEnter rate in hertz: "; # convert to microseconds for Time::HiRes chomp($interval_usec = (1 / * 1000000)); print "\nEnter length in seconds: "; chomp($length = ); } } sub run_pin_frequency_time { my ($pin, $interval,$time) = @_; # Put in timer code here while(1) { $port->set_bit($pin,1); usleep ($interval_usec); $port->set_bit($pin,0); usleep ($interval_usec); } } #DEBUG print "\$ARGV[0] = $ARGV[0]\n"; #DEBUG print "\$ARGV[1] = $ARGV[1]\n"; #DEBUG print "\$ARGV[2] = $ARGV[2]\n"; # if ($ARGV[0] =~ /^-[hH][ertz]*?/) { # if ($ARGV[1] =~ /(\d+)/) { #DEBUG print "$1 - $2 - $3\n"; #1/time eq hertz->sec, sec*1x10^6==microsecond # $interval_usec = (1/$1) * 1000000; # } else { # print "\nSyntax error, I think. Try: $0 -h 314\n"; # } # } elsif ($ARGV[0] =~ /^-[mM][sS][econd]*?/) { # if ($ARGV[1] =~ /(\d+)/) { # $interval_usec = ($1) * 1000; # } else { # print "\nSyntax error, I think. Try: $0 -s 5000"; # } # } elsif ($ARGV[0] =~ /^-[mM][sS][econd]*?/) { # if ($ARGV[1] =~ /(\d+)/) { # } # } else { # print "Enter rate in hertz: "; # chomp($interval_usec = (1 / * 1000)); # }