/** \file
 *
 *  Contains the AnalogToDigital class declaration.
 *
 *  Copyright (c) 2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#ifndef ANALOGTODIGITAL_H_
#define ANALOGTODIGITAL_H_

#include "data/Slate.h"
#include "io/DeviceIOStream.h"

/**
 *  This class allows one to control loads on a load controller
 *  channel.
 *
 *  \ingroup io
 */
class AnalogToDigital
{
public:
    /// Constructor
    AnalogToDigital( const ConfigURI& adCfg, const ConfigURI& vrefCfg,
                     const ConfigURI& resCfg, bool useHardware, Logger& logger );

    AnalogToDigital( const ConfigURI& adCfg, const ConfigURI& vrefCfg,
                     const ConfigURI& resCfg, const ConfigURI& timeoutCfg, bool useHardware, Logger& logger );

    virtual ~AnalogToDigital();

    /// Starts a read from the device
    /// Useful for slow devices.
    void startRead();

    /// Reads from the device
    float readCounts();

    /// Reads from the device and multiplies by vref per bit
    float readVolts()
    {
        return read( vrefPerBit_ );
    }

    /// Reads from the device and multiplies by scale
    float read( float scale )
    {
        return readCounts() * scale;
    }

private:

    bool useHardware_;
    DeviceIOStream io_;
    float vrefPerBit_;
    Timespan timeout_;
    bool started_;
    Timestamp startTime_;
    Logger& logger_;

    static double ReadTimeout( const ConfigURI& timeoutUri, Logger& logger );

    static float ReadVRefPerBit( const ConfigURI& vrefCfg, const ConfigURI& resCfg, Logger& logger );
};

#endif /* ANALOGTODIGITAL_H_ */
