/****************************************************************************/
/* Copyright (c) 2015 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : Power Buoy Data Logger Crossbow AHRS data source class        */
/* Filename : Crossbow.hpp                                                  */
/* Author   : Henthorn                                                      */
/* Project  : Power Buoy                                                    */
/* Version  : 1.0                                                           */
/* Created  : 04/13/2015                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
/*                                                                          */
/* This class is the data source reader for the Crossbow instrument.        */
/* It is derived from the generic FileSource class.                         */
/*                                                                          */
/****************************************************************************/

#ifndef _CROSSBOW_
#define _CROSSBOW_

#include "FileSource.hpp"

#define XB_MSG_SIZE  49

class CSVWriter;

class Crossbow : public FileSource
{

public:

   // Define Crossbow data structure
   //
   struct XB_Record {
      float rollAngle;          // Radians -pi to +pi
      float pitchAngle;         // Radians -pi to +pi
      float yawAngleTrue;       // (true north) Radians -pi to +pi

      float xRateCorrected;     // Corrected X angular rate, rads/sec
      float yRateCorrected;     // Corrected Y angular rate, rads/sec
      float zRateCorrected;     // Corrected Z angular rate, rads/sec

      float xAccel;             // X accelerometer, g
      float yAccel;             // Y accelerometer, g
      float zAccel;             // Z accelerometer, g

      float nVelocity;          // North velocity, m/s
      float eVelocity;          // East  velocity, m/s
      float dVelocity;          // Down  velocity, m/s

      float longitude;          // GPS longitude, radians
      float latitude;           // GPS latitude,  radians
      float altitude;           // GPS altitude,  meters (-100 to 16284)

      float xRateTemp;          // X rate temperature, C
   };


   // Open a connection to the data source.
   // If the pointer to the writer object is NULL
   // no data is written.
   //
   Crossbow(CSVWriter *w);

   ~Crossbow();

   // Return 0 on error
   // Return the (0 - SourceID) if a full record was read from that source
   // Otherwise, return the number of bytes still needed for
   // a full record.
   // 
   // int v = read_data(0);
   // if (v > 0)
   //   still some reading to do
   // else if (v < 0)
   //   complete record from source (0 - v)
   // else
   //   v == 0, error
   //
   int read_data(char verbose);

   char get_record(XB_Record *r);

protected:

   int  find_start();
   void process_data();
   unsigned short calcCRC();

   float i2_value(char* raw, float scale);
   float u2_value(char* raw, float scale);
   float i4_value(char* raw, float scale);
   float u4_value(char* raw, float scale);

   char _readbuf[XB_MSG_SIZE];
   char _recvbuf[XB_MSG_SIZE];
   char _in_msg;
   char _preamble;
   unsigned char _recvptr;
   unsigned _rlen;

   // Double-buffered data record.
   // Use _p to point to the published record buffer
   // _record[_p] is the current stable record
   // _record[1-_p] is the record that is in flux
   // 
   struct Crossbow::XB_Record _record[2];
   char                       _p;    // Value will always be either 0 or 1


   CSVWriter* _logger;

   char _verbose;
};

#endif
