#!/usr/bin/env perl

## O-scope period/frequency solver.
## Assumes 5 ticks per division.
##
## Enter time/division like: 10m (for 10 milli seconds), .05u (for .05 micro 
## seconds) etc.

## This file Copyright 2000 <contact@gbppr.org> under the GPL
## NO WARRANTY. Please send bug reports / patches / reports.

print "\n\n\t\tOscilloscope Time/Frequency Figure'er Outer\n\n";

while (!m/^([0-9.]+)(m|M|u|U|s|S+)$/) {
      undef $_; undef $number; undef $unit;
      print "\nm = milliseconds\n".
      	    "u = microseconds\n".
            "s = just seconds\n\n".
            "Enter time/division scale  [value][m,u,s]: ";
      chomp($_ = <STDIN>);
}

if (m/^([0-9.]+)(m|M|u|U|s|S+)$/) {
 	$number = $1;
 	$unit = $2;
}

if ($unit =~ /m/i) {
	$time = ($number * 0.001) / 5;
	$unit = "milli";
}

elsif ($unit =~ /u/i) {
	$time = ($number * 0.000001) / 5;
	$unit = "micro";
}

elsif ($unit =~ /s/i) {
	$time = $number;
	$unit = "";
}

print "\n";

while (!$tick) {
	print "Enter number of ticks signal spans: ";
	chomp($tick = <STDIN>);
	$tick =~ tr/0-9.//csd;
}

$period = $time * $tick;
$periodm = $period * 1000;
$periodu = $period * 1000000;

$freq = 1 / $period;
$freqk = $freq / 1000;
$freqm = $freq / 1000000;

printf "\n\n\n     Scale : %.2f %ssecond%s per division\n", $number, $unit, ($number == 1) ? "" : "s";
print  "     Ticks : $tick\n\n";
printf "    Period : %.10f second%s\n", $period, ($period == 1) ? "" : "s";
printf "           : %.10f millisecond%s\n", $periodm, ($periodm == 1) ? "" : "s";
printf "           : %.6f microsecond%s\n", $periodu, ($periodu == 1) ? "" : "s";
printf " Frequency : %.6f Hz\n".
       "           : %.6f kHz\n".
       "           : %.6f MHz\n\n", $freq, $freqk, $freqm;
