#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream.h>

#include "CTDOutput.h"
#include "SharedData.h"
#include "PeriodicTask.h"
#include "SerialDevice.h"
#include "time.h"
#include "Syslog.h"
#include "CTDLog.h"
#include "ParosciIF.h"

//below moved to .cc file to obfuscate
//#define BAUD 4800
//#define PARITY "EVEN"
//#define STOP_BITS 1
//#define DATA_BITS 7
//#define READ_TIMEOUT 100

//#define STATUS_UNINITIALIZED -1 
/*
CLASS 
CTD

DESCRIPTION
CTD 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
// 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 CTD : public PeriodicTask{
  
  friend class CTDLog;
 public:
  ////////////////////////////////////////////////////////////////////
  // constructor, not used...
  CTD();

  ////////////////////////////////////////////////////////////////////
// overloaded constructor
  CTD(SerialDevice *interface);

////////////////////////////////////////////////////////////////////
// desctructor
  ~CTD();
  
////////////////////////////////////////////////////////////////////
// Periodic Callback Function
  virtual void runCTD();
  
 protected:

  CTDLog *_log;
  
////////////////////////////////////////////////////////////////////
// must use depth measurement to calculate cond. accurately
  ParosciIF *parosci;
  
  double read_temp;
  double read_cond;
  double read_tfreq;
  double read_cfreq;

////////////////////////////////////////////////////////////////////
// must know whether Parosci Server is/not running
  int Parosci_Running;
  double parosci_pres; //in dbar?
  
  timespec temp_readtime;
  timespec cond_readtime;
  
  CTDOutput *_output;
  CTDOutput::Data _outputData;
  SerialDevice *_device;
  
  // currently no way to access these two fns for now
      
    void standby(void);
  void halt(void);
  
////////////////////////////////////////////////////////////////////
// talks to device
  int read_CTD();

///////////////////////////////////////////////////////////////////
// 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

////////////////////////////////////////////////////////////////////
// generic data-getting fn
  int getData(void);

////////////////////////////////////////////////////////////////////
// initialization routine
  int init();

////////////////////////////////////////////////////////////////////
// unused, for now
  int read(char *,int, int);
  int write(char *, int);


};
