/****************************************************************************/
/* 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 LogWriter;

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.
   //
   Crossbow(LogWriter *w = NULL);

   ~Crossbow();

   // Return 0 if full record was read
   // Otherwise, return the number of bytes still needed for
   // a full record.
   // Return < 0 on error.
   //
   int read_data();

   // Return the latest full data record in the buffer supplied by the caller.
   // Return the number of bytes written to the buffer (0 or more).
   // Return < 0 on error.
   //
   int get_record(const char *buffer);

protected:

   int  find_start();
   void process_data();
   unsigned short calcCRC();

   float i2_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;

   struct Crossbow::XB_Record _record;
   LogWriter* _logger;
};

#endif