/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : Fan.h                                                         */
/* Author   : rsm                                                           */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 03/21/2004                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include "Fan.h"
#include "envUtils.h"
#include "PeriodicTask.h"
#include "Syslog.h"

Boolean debug = False;

Fan::Fan()
   : PeriodicTask("fanDriver")
{
  //
  // Initialize:
  //
  envOpen( FanEnvIOBase );       // Set the base address of the fan card.
  port_init();                // Initialize SHT1x

   _log = new FanLog(this, DataLog::BinaryFormat);

   _fanOutput = new FanOutput();

   addPeriodicCallback(FanPollPeriod, 
		       (CallbackMethod)Fan::periodicComputation );
}


Fan::~Fan()
{
   Syslog::write(" Fan Destructor Executing.");
   delete _log;
   delete _fanOutput;
}

void Fan::periodicComputation()
{
   Boolean debug = False;
   int tempNow[2], tempHi[2], tempLo[2], pCounts;
   dprintf(" Fan::  Start executing periodicComputation().\n");
   int i = 0;
   int ana[8];
   //
   // Check for any commands.  Execute them if requested.
   //
   _fanOutput->read();
   //
   // Write DS1620 temperature limits if requested:
   //

#if 0
   printf("_fanOutput->WriteTempLimits == %d \n", 
	  _fanOutput->FanData.WriteTempLimits );
   fflush(stdout);
#endif

   //printf("_fanOutput->FanData.data.TestFlag2 == %d \n", 
   //	  _fanOutput->FanData.data.TestFlag2);

   if( _fanOutput->FanData.WriteTempLimits )
   {
      printf("Fan:: Got here!\n");
      //
      // 1/2 degree Centigrade per count for the DS1620, hence the *2 in the
      // argument list below.
      //
      envWriteTemp( _fanOutput->FanData.SetTempHi*2, 
		    _fanOutput->FanData.SetTempLo*2, 
		    _fanOutput->FanData.Ch );

      _fanOutput->FanData.WriteTempLimits = False;
   }
   //
   // Toggle LED if requested:
   //
   if( _fanOutput->FanData.NewLedState == True )
   {
      short state;
      if(       _fanOutput->FanData.LedState == LedOn ) state = 1;
      else if ( _fanOutput->FanData.LedState == LedOff) state = 0;
      envDiagLED( state );
      _fanOutput->FanData.NewLedState = False;
   }
   //
   // Now, read in the environmental sensors and pass the data to the server.
   //
   // Read the on-fan-card DS1620, on port 0, and the remote
   // DS1620, on the power board, on port1.
   //
   envReadTemp(tempNow,   tempHi,   tempLo,   Temp1620Status,   0);
   envReadTemp(tempNow+1, tempHi+1, tempLo+1, Temp1620Status+1, 1);
   for( i=0; i<2; i++ )
   {
      Temp1620[i]   = ((double)tempNow[i])/2.;
      Temp1620Hi[i] = ((double)tempHi[i])/2.;
      Temp1620Lo[i] = ((double)tempLo[i])/2.;
   }
   //
   // Now read the humidity and temperature from the SHT1x.
   //
   Humidity = envHumidity();
   TempSHT1x= envTemperature(0);  //1=> Deg. F, 0=> Deg. C
   //
   // Read the A/D.  Save the data here and in shared memory.
   //
//   for( i=0; i<8; i++ ) 
//   {
//      ana[i]=envReadAna(i);
//      _fanOutput->FanData.data.Ana[i]=ana[i];
//   }
   //
   // Compute the pressure from the Motorola MPX2200
   //
   ana[3] = envReadAna(3);
   Pressure= ((double) ana[3])*pCnts2kPa - pOffset;
   //
   // Write the data out to shared memory so the server can read it:
   //
   for( i=0; i<2; i++ )
   {
      _fanOutput->FanData.data.Temp1620[i]       = Temp1620[i];
      _fanOutput->FanData.data.Temp1620Hi[i]     = Temp1620Hi[i];
      _fanOutput->FanData.data.Temp1620Lo[i]     = Temp1620Lo[i];
      _fanOutput->FanData.data.Temp1620Status[i] = Temp1620Status[i];
   }
   _fanOutput->FanData.data.Pressure = Pressure;
   _fanOutput->FanData.data.Humidity = Humidity;
   _fanOutput->FanData.data.TempSHT1x= TempSHT1x;
   //
   _fanOutput->write();
   //
   // Log the data.
   //
   _log->write();
   dprintf(" Fan::  Finish executing periodicComputation().\n");
}
