/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : PA200LR.cc                                                     */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#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 "PA200LR.h"
#include "PA200LRLog.h"
#include "PA200LROutput.h"
#include "Syslog.h"

#define BAUD 9600
#define PARITY "NONE"
#define STOP_BITS 1
#define DATA_BITS 8
#define READ_TIMEOUT 750

#define PA200LRPERIOD 2000

#define MaxRecordBytes 200

PA200LR::PA200LR(SerialDevice *device, Boolean verbose)
  : StreamSerialDriver("PA200LR", device, MaxRecordBytes, "\xa",
		       READ_TIMEOUT )
{
  Syslog::write("PA200LR: constructing...\n");
  _device = device;
  _output = new PA200LROutput();
  _verbose = verbose;

  try {
    _log = new PA200LRLog( this, DataLog::BinaryFormat );
  }
  catch ( ... ) {
    throw Exception("PA200LR:PA200LR() - PA200LRLog contructor failed\n");
  }

}


PA200LR::~PA200LR()
{
  delete _output;
}


/* Init function simply clears serial port and then confirms that
   lines beginning with the character "R" are streaming from the
   device */

DeviceIF::Status PA200LR::initialize(void)
{
  char buf[30];
  
  Syslog::write("PA200LR: initializing...\n");

  _output->data.range = 0.00;

  try
  {
    _output->write();
  }
  catch (SharedData::AccessError error)
  {
    fprintf(stderr, "%s",error.msg);
    return DeviceIF::Error;
  }

  _device->clearPort();
  _device->flushReceiveBuf();
  
  // look for m to confirm data is streaming
  
  Syslog::write("PA200LR: confirming data stream...\n");

  if (_device->confirm("m",1000) == -1)
  {
    Syslog::write("PA200LR: data not streaming...\n");
    return DeviceIF::Error;
  }
  else
  {
    Syslog::write("PA200LR: data is streaming...\n");
  }

  _output->data.deviceReady = True;
  _output->write();

  return DeviceIF::Ok;
} 


DeviceIF::Status PA200LR::processRecord(unsigned char *record, int nBytes)
{
  Boolean debug = _verbose;

  dprintf("processRecord() - %s", record);

  char *reply = (char*)record;

  _range = atof(reply);

  dprintf("Range is %lf", _range);

  _log->write();

  return DeviceIF::Ok;
}

