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

  $Id: _fanTestSimple.cc,v 1.1 2004/04/02 01:14:09 rob Exp $
  *-----------------------------------------------------------------------*/

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <unistd.h>             //sleep()
#include "System.h"
#include "envUtils.h"

const int envIOBase=0x340;

int main( int argc, char **argv ) 
{
  char buf[512];
  buf[511]='\0';
  int i=0;
  int state = 0;
  double humidity, tempSHT1x;
  //
  // Initialize:
  //
  envOpen( envIOBase );       // Set the base address of the fan card.
  port_init();                // Initialize SHT1x
  //
  // Enter test loop.
  //
  while( 1 )
  {
     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( i%2 ) state = 0;
	else      state = 1;
	envDiagLED( state );
	i++;
	printf("Iteration %d.\n", i);
     }
     //
     // Write temp high and low limits:
     //
     else if( !strnicmp( buf, "tw", 2) )
     {
	int high, low, ch, nRead;
	printf("Enter high temp, low temp, 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);
	//
	// 1/2 degree Centigrade per count for the DS1620:
	//
	envWriteTemp(high*2, low*2, ch);
	gets(buf);        //Clear any characters in the buffer.
     }
     //
     // Read temperature, pressure, and humidity sensors:
     //
     else if( !strnicmp( buf, "r", 1) )
     {
	int    pCounts;
	double pressure;
	//
	// Average atmospheric pressure at sea level 
	//(From http://ww2010.atmos.uiuc.edu/(Gh)/guides/mtr/fw/prs/def.rxml)
	// 29.92" Hg = 1.0 atm = 101.325 kPa = 1013.25 mb = 14.7 psi 
	// 
	// Data:  counts  kPa
	//         123    ambient - 10
	//         121            - 12
	//         119            - 14
	//         117            - 16
	//         114            - 18
	//         112            - 20
	//
	// Least squares gives .8974 kPa/count, with ambient = 120.6 kPa. 
	// (This seems high.)
	// 
	//const double pCnts2kPa = 101.325/131.;
	const double pCnts2kPa = .8974;
	printf("Hit any key to terminate this.\n");
	while( !kbhit() )
	{
	   int i = 0;
	   int TempNow[2], TempHi[2], TempLo[2], TempCfg[2];
	   double TempNowF[2];
	   for( i=0; i<2; i++ )
	   {
	      TempNow[i] = TempHi[i] = TempLo[i] = TempCfg[i] = 0;
	   }
	   //
	   // Read the on-fan-card DS1620, on port 0, and the remote DS1620,
	   // on the power board, on port1.  Scale by 1/2 degree Centigrade
	   // per count for the DS1620:
	   //
	   envReadTemp(TempNow, TempHi, TempLo, TempCfg, 0);
	   envReadTemp(TempNow+1, TempHi+1, TempLo+1, TempCfg+1, 1);
	   for( i=0; i<2; i++ ) TempNowF[i] = TempNow[i]/2.*9./5. + 32.;
	   //
	   // Now read the humidity and temperature from the SHT1x.
	   //
	   humidity = envHumidity();
	   tempSHT1x= envTemperature(1);  //1=> Deg. F, 0=> Deg. C
	   //
	   // Read the pressure from the Motorola MPX2200
	   //
	   pCounts = envReadAna(3);
	   pressure= ((double) pCounts)*pCnts2kPa;
	   printf("                Temp(C/F)  Hi(C)   Lo(C)   Cfg\n");
	   printf("DS1620 Fan Card: %4.1f/%4.1f  %4.1f     %4.1f     %2x\n", 
		  TempNow[0]/2., TempNowF[0], TempHi[0]/2., TempLo[0]/2., 
		  TempCfg[0]);
	   printf("DS1620 External: %4.1f/%4.1f  %4.1f     %4.1f     %2x\n", 
		  TempNow[1]/2., TempNowF[1], TempHi[1]/2., TempLo[1]/2., 
		  TempCfg[1]);
	   printf("MPX2200 Press  : %5.1f  kPa;   %3d counts\n", pressure, 
		  pCounts);
	   printf("SHT1x Temp     : %4.1f   Fahrenheit\n", tempSHT1x );
	   printf("SHT1x Humidity : %4.1f   Relative %%\n\n", 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() )
	{
	   for( i=0; i<8; i++ ) ana[i]=envReadAna(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");
  return 0;
}
