/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : Seabird.h                                                         */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _Seabird_H
#define _Seabird_H

#include "StreamSerialDriver.h"
#include "SeabirdOutput.h"
#include "SeabirdLog.h"
#include "DepthSensorIF.h"
#include "Attributes.h"

/*
CLASS 
Ctd

DESCRIPTION
Seabird Device Driver Interface

AUTHOR
John Rieffel

COMMENTS: Large sections of this code lifted directly from Odyssey
code file cond_temp.h and .c.  Thank you very much.

*/
#define    SBE_PRES_RANGE 0  //Altex configured vehicle appears not to ask
                             // JAR 12/09/99
#define MAXNAMELEN 20
#define MAXTAGLEN 10
#define MAXNUMVOLTAGES 6


// THESE ARE CALIBRATION VALUES -- TO BE CHANGED
// CURRENTLY USING TP2185 
// and CP1722
/*
#define    T_A   3.68006968e-03
#define    T_B   6.02841367e-04
#define    T_C   1.61590477e-05
#define    T_D   2.23203474e-06#define    T_F0  2892.171


#define    C_A   1.34276393e-07
#define    C_B   4.85446233e-01
#define    C_C  -4.14760004e+00
#define    C_D  -6.411519262-05
#define    C_M   6.0
#define    EPS  -9.57e-08
*/

/*-----------------------------------------------------------------------*
  (from Odyssey Code)

  First do temp conversion:

      lf = log( f0 / f Hz )

       T = 1 / (a + b*lf + c*lf^2 + d*lf^3)  - 273.15

  Next conductivity (uses temperature)

      C (S/m) = (a*(c kHz)^m + b*(c kHz)^2 + c + d*T) /
                (10*(1 + eps*P))
      where P is dbar and T is deg C
 *-----------------------------------------------------------------------*/


class Seabird : public StreamSerialDriver {
  
  friend class SeabirdLog;

public:

  Seabird(SerialDevice *serialDevice, char *configFile,
	  char *instName);

  virtual ~Seabird();

protected:

  SeabirdLog *_log;
  time_t _lastFlush;
  time_t _flushInterval;

  ////////////////////////////////////////////////////////////////////
  // Seabird calibration attributes
  Attributes attributes;

  void createCTDAttributes();
  void loadCTDConfigFile( char *filename );
  void reportCTDAttributes();

  double T_A, T_B, T_C, T_D, T_F0, C_A, C_B, C_C, C_D, C_M, EPS;
  char condCalibTag[MAXTAGLEN];
  char tempCalibTag[MAXTAGLEN];
  
  int NUMVOLTAGES;
  // char v1name[MAXNAMELEN];
  //char v2name[MAXNAMELEN];
  //char v3name[MAXNAMELEN];
  //char v4name[MAXNAMELEN];
  //char v5name[MAXNAMELEN];
  //char v6name[MAXNAMELEN];
  
  //  char *v1name;
  // char *v2name;
  //char *v3name;
  // char *v4name;
  // char *v5name;
  //char *v6name;
  
 int offset;

 double voltages[MAXNUMVOLTAGES];
 
 int i;
  
  ////////////////////////////////////////////////////////////////////
  // must use depth measurement to calculate cond. accurately
  DepthSensorIF *depthSensor;
  
  double read_temp;
  double read_cond;
  double read_tfreq;
  double read_cfreq;

  ////////////////////////////////////////////////////////////////////
  // must know whether DepthSensor Server is/not running
  Boolean depthSensorRunning;
  double depthSensor_pres; //in dbar?
  
  timespec temp_readtime;
  timespec cond_readtime;
  
  SeabirdOutput *_output;
  
  // currently no way to access these two fns for now
  void standby(void);
  void halt(void);
  

  ///////////////////////////////////////////////////////////////////
  // functions to calculate temperature and conductivity from raw
  // frequencies
  double calculate_Temp(double f); // assume ITPS68 (whatever that means)
  double calculate_Cond(double f, double t, double p); // ditto

  ////////////////////////////////////////////////////////////////////
  // initialization routine
  // Simply clears serial port and then confirms that
  // lines beginning with the character "R" are streaming from the
  // device 
  virtual DeviceIF::Status initialize();

  ////////////////////////////////////////////////////////////////////
  // Process a record
  virtual DeviceIF::Status  processRecord(unsigned char *record, int nRecordBytes);

  ////////////////////////////////////////////////////////////////////
  // EventCode to trigger when device has been initialized
  //  virtual EventCode initializedEventCode();

};

#endif
