/****************************************************************************/
/* Summary  :                                                               */
/* Filename : EchoSounderDriver.h                                           */
/* Author   : Rob McEwen, Rich Henthorn                                     */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 2 March 2007                                                  */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#ifndef _ECHOSOUNDERDRIVER_H
#define _ECHOSOUNDERDRIVER_H
#include "PeriodicTask.h"
#include "Attributes.h"
#include "SerialDeviceExt.h"

#include "EchoSounderOutput.h"
#include "EchoSounderCommand.h"
#include "EchoSounderLog.h"
#include "TimeP.h"

#ifndef PI
#define PI          3.14159265358979323846
#endif

const int MAX_LW = 5;
const double DEFAULT_ESV = 1485.;
const unsigned EchoSounderPollPeriod = 1000;  //millisec.
const double trainDegPerStep = .3;
const short trainShift = 600;
//
// Had to move this outside the class so that both EchoSounderDriver and 
// _echoSounderTest could use it.
//
void printData( char *header, char headID, 
		char serialStatus, double dtrain, double sectorEng, 
                unsigned char maxRangeCfrm, double drange, short nBytes,
		short nBytesRead, double freq, 
		long crashCtr, char *echoData, Boolean WantToSeeIt = False );


class EchoSounderDriver : public Task {

   friend class EchoSounderLog;

   public:
      EchoSounderDriver(SerialDeviceExt *serialDevice, 
			char* configfile = "echoSounder.cfg");
      ~EchoSounderDriver();

      ////////////////////////////////////////////////////////////////////
      // Run.  This is an infinite loop that requests a ping, then blocks 
      // until either the data is received, or too much time passes.
      virtual void run();

   protected:
      SerialDeviceExt    *_device;
      EchoSounderLog     *_log;
      EchoSounderOutput  *_output;
      EchoSounderCommand *_command;
      char *_config;
      char  _interrogation[32];
      long  _maxRange;
      double  _noReturnRange;
      long  _startGain;
      long  _absorption;
      double _absorpEng;
      long  _pulseLen;
      long _pulseLenEng;
      long  _minRange;
      double  _minRangeEng;
      long  _triggerCtrl;
      Boolean _profile;
      long  _delay;
      char  _response[520];
      char  _header[4];
      char  _headID;
      char  _serialStatus;
      unsigned char  _maxRangeCfrm;
      short _range;
      short _ranges[EchoSounderIF::ScanArraySize];//Valid only for 2.4 deg stepsize.
      short _train;
      short _tindx, _toffset, _lastToffset, _tindxLast, _tindxLastLast;
      Boolean _firstRun;
      short _nBytes;
      short _nBytesRead;
      char  _echoData[EchoSounderIF::EchoArraySize];
      Boolean _running, _error;
      double _xzAngle;
      double _trainRef;
      long  _period;
      long  _compPeriod;
      double _freq;
      TimeIF::TimeSpec _updateTime;
      unsigned long _askTime, _lastAskTime, _ansTime;
      long _crashCtr;
      double _sectorEng;
      unsigned char _sector;
      double _stepSizeEng;
      long   _stepSize;
      short  _maxStepSize;
      long _acousticFreqEng;
      long _acousticFreq;
      double _pingPeriod;
      double _minGrazing;              //For simulation only.
      struct timespec _nsleepTime;

                  
      // Output
      //

      // Config attributes
      //
      
      Attributes _attributes;
      
      void init();
      void acquireData();
      void parseData();
      void createCfgAttributes();
      void loadConfigFile(const char* file);
      void printCfgAttributes();
      void resetSD();
      
   private:
};

#endif
