#!/usr/bin/perl

use IO::Socket;

if ($#ARGV == -1 ) {
  print STDERR "\n This program is used for simulation of SQM-LE results.\n";
  print STDERR "\n To display the actual raw counts, frequency, and temperature ADC values.\n";
  print STDERR "\tUsage:\t$0 r sqm_le_addr sqm_le_port\n";
  print STDERR "\tEx.:\t$0 r 192.168.1.145 10001\n";
  print STDERR "\tReturns: s,0000000360c,0000000360f,0000000360t\n";
  print STDERR "\tFields:\t\tconfirmation\n";
  print STDERR "\t\t\traw counts\n";
  print STDERR "\t\t\traw frequency\n";
  print STDERR "\t\t\traw temperature ADC value\n";
  print STDERR "\n To set the SQM-LE internal inputs, and read the resultant calculation.\n";
  print STDERR "\tSee code for setting loop range.\n";
  print STDERR "\tUsage:\t$0 s sqm_le_addr sqm_le_port\n";
  print STDERR "\tEx.:\t$0 s 192.168.1.145 10001 > simcalc.csv\n\n";
  print STDERR "\tReturns:\tS,0000094000c,0000000000f,0000000245t,r, 18.04m,0000000000Hz,0000094000c,0000000.204s, 029.0C\n";
  print STDERR "\tFields:\t\tconfirmation\n";
  print STDERR "\t\t\tsimulated counts\n";
  print STDERR "\t\t\tsimulated frequency\n";
  print STDERR "\t\t\tsimulated temperature ADC value\n";
  print STDERR "\t\t\tconfirmation\n";
  print STDERR "\t\t\tcalculated mpsas\n";
  print STDERR "\t\t\tfrequency used for calculation\n";
  print STDERR "\t\t\tcounts used for calculation\n";
  print STDERR "\t\t\tcalculated period from counts\n";
  print STDERR "\t\t\ttemperature used for calculation\n";
  exit;
}

if (! defined($ARGV[0])) {
  print STDERR "(r)ead or (s)et command required.\n";
  exit;
} else {
  $DesiredOperation = $ARGV[0];
}

if (! defined($ARGV[1])) {
  print STDERR "The address or IP of the SQM-LE is required\n";
  exit;
} else {
  $sqm_le_addr = $ARGV[1];
}

if (! defined($ARGV[2])) {
  print STDERR "Port number not provided so 10001 assumed\n";
  $sqm_le_port = 10001;
} else {
  $sqm_le_port = $ARGV[2];
}


$remote = IO::Socket::INET->new(PeerAddr => $sqm_le_addr,
				PeerPort => $sqm_le_port,
				Proto    => 'tcp') 
|| die("Cannot connect to port $sqm_le_port on $sqm_le_addr:$!");

$remote->autoflush(1);
print STDERR "[Connected to $sqm_le_addr:$sqm_le_port]\n";

#Read simulation values
if ($DesiredOperation eq "r") {
	#-- Initialize response string
	$str="";

	#-- Send request to remote SQM
	print $remote "sx";

	#-- Add response to string
	$str .= <$remote>;

	#-- If newline, print string and start next count
	if ($str =~ /\n/) {
	print $str;
	break;
	}

	#-- Close remote socket
	$remote->close;

	exit;
}


#Set simulation values
if ($DesiredOperation eq "s") {

# Send:
# PER1use 32bit
# TMR0use 32bit
# V_Tempuse 16bit

#-- Send request to remote SQM
#              0         1         2        
#              01234567890123456789012345678
# print $remote "D0000000000,0000013317,00244x";


$PeriodCounter=100;
$Frequency=0;
$TemperatureSensor=245;

for ($PeriodCounter = 10000; $PeriodCounter <= 100000; $PeriodCounter=$PeriodCounter+500) {

	#-- Initialize response string
	$str="";

	printf $remote "S%010d,%010d,%05dx",$PeriodCounter,$Frequency,$TemperatureSensor;

	#-- Add response to string
	$str .= <$remote>;

	#-- If newline, print string and start next count
	if ($str =~ /\n/) {

#	0         1         2         3         4         5         6         7         8         9         
#	012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
#	S,0000094000c,0000000000f,0000000245t,r, 18.04m,0000000000Hz,0000094000c,0000000.204s, 029.0C

		#counts
		$SimCounts = substr($str,61 , 10); 

		#magnitude
		$ResultMags = substr($str,41 , 5);

		#Temperature
		$SimTemp = substr($str,86 , 6); 

		printf "%10s,%5s,%5s\n",$SimCounts,$ResultMags,$SimTemp;
		next;
	}

}

	#-- Close remote socket
	$remote->close;

	exit;
}

