/** \file
 *
 *  Contains the DVL_micro class declaration.
 *
 *  DVL_micro.h should only be included by DVL_micro.cpp
 *  Other classes should include DVL_microIF.h
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef DVL_MICRO_H
#define DVL_MICRO_H

#include "DVL_microIF.h"

#include "component/SyncComponent.h"
#include "io/LoadControl.h"
#include "io/UartStream.h"
#include "logger/Logger.h"

#include "data/Point3D.h"
#include "data/Point6D.h"
#include "data/Matrix3x3.h"

class UniversalDataReader;
class UniversalDataWriter;


#define UART_BUFSIZE 10240L

class DVL_micro : public SyncSensorComponent
{
public:

    DVL_micro( const Module* module );

    virtual ~DVL_micro();

    virtual void run();
    virtual RunState start();
    virtual RunState starting();
    virtual RunState pause();
    virtual RunState paused();
    virtual RunState resume();
    virtual RunState resuming();
    virtual RunState runnable();

    virtual RunState resetting()
    {
        return stop();
    }

    virtual RunState stop();
    virtual RunState stopping();
    virtual RunState stopped();
    void uninitialize();
    void logVoltageAndCurrent();
    /// Should return [myNamespace]::SIMULATE_HARDWARE, or [myNamespace]::POWER, etc
    virtual ConfigURI getConfigURI( ConfigOption configOption ) const;

private:
    DVL_micro( const DVL_micro& old );

    // scan configuraiton settings
    void readConfig();

    bool checkTimeouts( void );
    bool simPD6( void );
    bool readPD6( void );
    bool parsePD6( void );
    void processPD6( void );
    void writeData( void );
    void setAcousticWritersInvalid( bool invalid );

    bool hasPD6_;

    Timespan dvlTimeout_;
    Timespan dvlFailTimeout_; // How long we can go without valid data
    Timespan powerOnTimeout_; // How long it takes for the DVL to begin sending data
    Timestamp startTime_, dvlFailTime_;

    DataReader* power24vConverterDataReader_;

    UniversalDataWriter* altitudeWriter_;
    UniversalBlobWriter* velocityWrtGroundWriter_;
    UniversalDataWriter* xVelWrtGroundWriter_;
    UniversalDataWriter* yVelWrtGroundWriter_;
    UniversalDataWriter* zVelWrtGroundWriter_;

    DataWriter* veloInstFlagWriter_;
    DataWriter* beam1RangeWriter_;
    DataWriter* beam2RangeWriter_;
    DataWriter* beam3RangeWriter_;
    DataWriter* beam4RangeWriter_;

    bool isDataRequested();

    bool debug_;
    bool dvlTimeoutArmed_;
    char deviceResponse_[UART_BUFSIZE];
    UartStream uart_;
    LoadControl loadControl_;

    int status_;

    Point3D veloInst_;
    double veloInstX_;
    double veloInstY_;
    double veloInstZ_;
    double veloError_;
    int veloInstFlag_;

    double altEstimate_;
    double beam1Range_, beam2Range_, beam3Range_, beam4Range_;
    int beam1Good_, beam2Good_, beam3Good_, beam4Good_;
    double magDeviationDeg_;
    double magVariationDeg_;

    double rollOffset_;
    double pitchOffset_;
//    double headingOffset_;

    double rollDeg_;
    double pitchDeg_;
    double compassHeadingDeg_;
    double magneticHeadingDeg_;
    double headingDeg_;

    typedef enum
    {
        NQ_START,
        VEL_ON
    } StartupSequance;
    StartupSequance startup_;

    int bitError_;
};
#endif /*DVL_MICRO_H*/
