use Win32; use Win32::OLE qw(in with); use Win32::Clipboard; # # $clip = Win32::Clipboard(); $newline = chr(13) . chr(10); $PI = 4 * atan2(1,1); $POINTS = 16; $PERIODS = 1; $AMPLITUDE = 1.0; $GAIN = 1.0; $PHASE_INV = "OFF"; $DEBUG = "OFF"; $FORMAT = "HEX"; $GENFILE = "OFF"; $HELP = "OFF"; ################################################################################ system("cls"); while (1) { # loop forever printf << "~~~~~~~~~~"; ================================================================================ W A V E G E N E R A T I O N U T I L I T Y Jan.Uhrin\@siemens.com, 26.1.2005 -------------------------------------------------------------------------------- Settings ------------------------------- p - Set the number of Points per period. Current value: $POINTS n - Set Number of periods. Current value: $PERIODS a - Set wave Amplitude. Current value: $AMPLITUDE g - Set wave Gain (only taken in account for DEC values) Current value: $GAIN i - Toggle phase Inversion. Current value: $PHASE_INV d - Toggle debug mode (print val. to screen) Current value: $DEBUG f - Toggle Number Format (HEX, BIN, DEC) Current value: $FORMAT w - Toggle writing to file 'wave.txt' Current value: $GENFILE Actions ------------------------------- 1 - Generate SIN 2 - Generate COS 3 - Generate ASCENDING RAMP 4 - Generate DESCENDING RAMP 5 - Generate RANDOM 6 - Generate TRIANGLE 7 - Generate SQUARE h - Help (simple) v - View generated wave.txt using plot command (plot wave.txt) x - Exit ~~~~~~~~~~ printf("Press corresponding key followed by [ENTER]... "); $_ = &getLine; # get the line chop; # remove newline if ($_ =~ /^[1-9]/) { &genWave($_); } if ($_ eq "p") { &getValue(*POINTS, "Enter new number of points"); } if ($_ eq "n") { &getValue(*PERIODS, "Enter new number of periods"); } if ($_ eq "a") { &getValue(*AMPLITUDE, "Enter new amplitude in range 0.0 to 1.0"); } if ($_ eq "g") { &getValue(*GAIN, "Enter new gain (must be positive)"); } if ($_ eq "i") { &tglValue(*PHASE_INV, "Phase inversion" , ("ON", "OFF")); } if ($_ eq "d") { &tglValue(*DEBUG, "Debug mode" , ("ON", "OFF")); } if ($_ eq "f") { &tglValue(*FORMAT, "Number Format" , ("HEX", "BIN", "DEC")); } if ($_ eq "w") { &tglValue(*GENFILE, "File-generation mode" , ("ON", "OFF")); } if ($_ eq "v") { system("plot wave.txt"); } if ($_ eq "h") { &help; } if ($_ eq "x") { exit; } system("cls"); } # end while 1 ################################################################################ sub getValue { # Get value from STDIN local(*value, $msg) = (@_); my($line); printf("\n%s: ", $msg); $line = &getLine; chop($line); $value = eval($line); } ################################################################################ sub tglValue { # Toggle value local(*value, $msg, @value_array) = (@_); my($i); for ($i=0;$i=$POINTS/4) && ($i < 3*$POINTS/4)) { $floa = 2.0 - $floa;} if (($i>=3*$POINTS/4) && ($i < $POINTS)) { $floa = $floa - 4.0;} } if ($param eq "7") { if ($i<$POINTS/2) { $floa = 1; } else { $floa = -1;} } # SQUARE if ($param eq "8") { $floa = 0.0; } # NOT IMPLEMENTED YET if ($param eq "9") { $floa = 0.0; } # NOT IMPLEMENTED YET # # Amplitude and phase adjusting $floa *= $AMPLITUDE; # Adjust the amplitude if ($PHASE_INV eq "ON") { $floa = - $floa; } # Invert the phase # # Change of the output number format if ($FORMAT eq "HEX") { $result = &floa2hex($floa); } elsif ($FORMAT eq "BIN") { $result = &hex2bin( &floa2hex($floa), ""); } else { if ($GAIN == 1.0) { $result = $floa; } else { $result = int(($GAIN * $floa) + 0.5); } } # # if ($DEBUG eq "ON") { printf("$result\n"); } $clipboard .= $result . $newline; # Generating clipboard content. Clipboard requires somehow strange "newline" $filecontent .= $result . "\n"; # Generating output file content. } # $i } # $p if ($GENFILE eq "ON") { open(OUT, ">wave.txt") or die("-- ERROR creating file wave.txt.\n"); printf "-- Note: Generated waveform was written to wave.txt\n"; printf OUT $filecontent; close(OUT); } if ($DEBUG eq "ON") { printf "-" x 79 . "\n"; printf("Generated waveform was printed above. Press [ENTER] to continue ... "); $line = &getLine; } $clip->Set($clipboard); # Set the clipboard printf "-- Note: Generated waveform was placed into Clipboard\n"; } ################################################################################ sub floa2hex { # Conversion from decimal to 32-bit hexadecimal. Input range is <-1,1) my($in) = (@_); my($hex); my($i); if ($in > 1) { printf STDERR "-- Overflow (positive) for number $in.\tSaturating to 7FFFFFFF.\n"; return "7FFFFFFF"; } if ($in < -1) { printf STDERR "-- Overflow (negative) for number $in.\tSaturating to 10000000.\n"; return "10000000"; } $hex = sprintf("%X", $in*(2**31)); if (($hex eq "80000000") && ($in > 0)) { $hex = "7FFFFFFF"; } # Taking care of +1.0 value if (length($hex) < 8) { for ($i=length($hex);$i<8;$i++) { $hex = "0" . $hex; } } return $hex; } ################################################################################ sub hex2bin { # Conversion from HEX to BINARY hex2bin(value,separator) my($in,$sep) = (@_); my($i); my($h); my($result) = ""; $in = lc($in); if (length($sep)>0) { $sep="_";} for ($i=0;$i; return($line); } ################################################################################ sub help { printf << "~~~~~~~~~~"; -------------------------------------------------------------------------------- H E L P -------------------------------------------------------------------------------- Wave generation utility generates number sets representing various waves (e.g. sine, cosine, ramp, ..) All settings and actions are menu driven. The generated data sets are always written to Clipboard (for easy copying); writing to file is optional. There are three output data types supported: Hexadecimal (32-bit), decimal and binary 2's complement (32-bit). The amplitude is by default in range <-1,1). For decimal format optional gain is considered (g - option) by which the amplitude is multiplied (e.g. g ca nbe set to 2**15 to obtain dynamic range of 16-bits). The Hexadecimal and Binary numbers are scaled for this amplitude range. If the generated amplitude is greater than abs(1.0), hexa or binary output will be saturated. For decimal output there is no limitation for amplitude. -------------------------------------------------------------------------------- Press [ENTER] to continue ... ~~~~~~~~~~ &getLine; }