/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : _fanTest.cc                                                   */
/* Author   : Rob McEwen                                                    */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 26 Mar 04                                                     */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*-----------------------------------------------------------------------*

  $Id: _fanTest.cc,v 1.3 2004/06/07 17:02:56 dorado Exp $
  *-----------------------------------------------------------------------*/

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <signal.h>
#include <process.h>
#include <time.h>

#include "FanIF.h"
#include "TimeIF.h"
#include "Syslog.h"
#include "System.h"

void main( int argc, char **argv ) 
{
  FanIF *fanIF;
  FanIF::Data data;
  Boolean newData = False;

  char buf[512];
  buf[511]='\0';
  char errorBuf[256];
  int i=0;
  int count = 0;
  int state = 0;
  double humidity, tempSHT1x;

  pid_t _serverPid;

  const int sleepMillisecs = 500;
  struct timespec sleepTime;

  sleepTime.tv_sec = 0;
  sleepTime.tv_nsec = sleepMillisecs * 1000000;

  char *program = "fanServer";

  try 
  { 
     switch ((_serverPid = fork())) 
     {
	case 0:
	   // In child
	   // Execute server program
	   //execlp(program, program, "-serial", _serialParams->string(), 0);
	   execlp(program, program, NULL, NULL, NULL);

	   sprintf(errorBuf, 
		   "fanTest:  execlp() of \"%s\" failed", program);

	   perror(errorBuf);
 
	   break;

	case -1:
	   perror("fanTest: fork() failed.");
	   Syslog::write("fanTest: fork() failed.");
	   return;
     }

#if 0
     if ((_serverPid = System::spawn("fanServer", (const char *)NULL)) == -1)
     {
	Syslog::write("System::spawn(fanServer) failed\n");
     }
#endif

  }
  catch ( ... )
  {
     Syslog::write("fanTest -- "
		   "caught exception on spawning of Fan Server\n");
  }
  //
  // Wait for it to start before opening the interface:
  //
  sleep(1);

  try 
  {
    fanIF = new FanIF( "Fan", 1000 );
  }
  catch (...)
  {
    Syslog::write("fanTest -- Caught exception on IF creation\n");
    exit( -1 );
  }
  //
  // Enter test loop.  Wait for the other threads to execute their
  // constructors and finish initializing.
  //
  sleep(1);
  while (True) 
  {
     TimeIF::TimeSpec sampleTime;

     char *helpStr = " Type: \n"
	"    l(ed)  - toggle the red LED on the fan card.\n"
	"    r(ead) - Read temperature, humidity and pressure.\n"
	"    a(nalog)-Read analog inputs.\n"
	"    tw     - Write temp high and low limits.\n"
	"    q(uit) or e(xit) - to exit this program.\n";
     printf( "%s", helpStr );
     printf( "fanTest>");
     gets(buf);
     if( !strnicmp( buf, "q", 1) || !strnicmp( buf, "e", 1) ) break;
     //
     // Toggle the test LED:
     //
     else if( !strnicmp( buf, "led", 1) )
     {
	if( count%2 ) state = 0;
	else          state = 1;
	fanIF->EnvDiagLED( state );
	count++;
	printf("Iteration %d.\n", count);
     }
     //
     // Write temp high and low limits:
     //
     else if( !strnicmp( buf, "tw", 2) )
     {
	short high, low, ch, nRead;
	printf("Enter high temp (C), low temp (C), and channel number.\n");
	printf("  Channel 0 => DS1620 on the fan card.\n");
	printf("  Channel 1 => DS1620 on Sib's power board.\n");
	nRead = scanf("%d %d %d", &high, &low, &ch);
	if( nRead != 3 )
	{
	   printf("Error reading high temp, low temp, or channel values.\n");
	   continue;
	}
	printf("Values entered are %d %d %d.\n", high, low, ch);
	fanIF->EnvWriteTemp(high, low, ch);
	gets(buf);        //Clear any characters in the buffer.
     }
     //
     // Read temperature, pressure, and humidity sensors:
     //
     else if( !strnicmp( buf, "r", 1) )
     {
	printf("Hit any key to terminate this.\n");
	while( !kbhit() )
	{
	   int i = 0;
	   double Temp1620F[2];

	   fanIF->Get( &data );
	   //
	   // Compute Fahrenheit from Celcius:
	   //
	   for( i=0; i<2; i++ ) Temp1620F[i] = data.Temp1620[i]*9./5. + 32.;
	   //
	   printf("                Temp(C/F)  Hi(C)   Lo(C)   Cfg\n");
	   printf("DS1620 Fan Card: %4.1f/%4.1f  %4.1f     %4.1f     %2x\n", 
		  data.Temp1620[0], Temp1620F[0], data.Temp1620Hi[0], 
		  data.Temp1620Lo[0], data.Temp1620Status[0]);
	   printf("DS1620 External: %4.1f/%4.1f  %4.1f     %4.1f     %2x\n", 
		  data.Temp1620[1], Temp1620F[1], data.Temp1620Hi[1], 
		  data.Temp1620Lo[1], data.Temp1620Status[1]);
	   printf("MPX2200 Press  : %5.1f  kPa\n", data.Pressure);
	   printf("SHT1x Temp     : %4.1f/%4.1f (C/F)\n", data.TempSHT1x,
		  (data.TempSHT1x)*9./5.+32. );
	   printf("SHT1x Humidity : %4.1f   Relative %%\n\n", data.Humidity );
	   sleep(1);
	}
	gets(buf);        //Clear any characters in the buffer.
     }
     //
     // Read analog inputs:
     //
     else if( !strnicmp( buf, "a", 1) )
     {
	const double cnts2volts = 2.5/256.;
	int ana[8], i;
	printf("Hit any key to terminate this.\n");
	while( !kbhit() )
	{
	   fanIF->Get( &data );
	   for( i=0; i<8; i++ ) ana[i]=data.Ana[i];
	   printf("Counts: ");
	   for( i=0; i<8; i++ ) printf("%5.3d ", ana[i]);
	   printf("\n");
	   printf("Volts : ");
	   for( i=0; i<8; i++ ) printf("%5.3f ", ((double)ana[i])*cnts2volts);
	   printf("\n\n");
	   sleep(1);
	}
	gets(buf);        //Clear any characters in the buffer.
     }
     else printf(" Command not recognized.  Please try again.\n");
  }  //while( true )
  printf("\n Exiting fanTest.\n");

  delete fanIF;
  kill(0, SIGTERM);
}
