#ifndef DEVCOM_H
#define DEVCOM_H

#include <iwear-ser_sensor/rs232com.h>

#define STANDARD_PRIORITY 10
#define VERBOSE_DEVCOM false
#define VERBOSE_DEVCOM2 false

namespace iwear {
namespace sensor {
namespace ser_sensor {

  /**
   * wrapper function for starting the thread
   */  
  void *get_device_data_wrap(void *param);
  
  /**
   * The function of this class (Devcom = device communication) is to
   * communicate with the serial sensor device. With appropriate methods
   * can a connection to the equipment be developed and diminished. The
   * sensor data is picked out by the use of a Thread in accordance with 
   * the protocol for the equipment. As each sensor is queried frequently
   * in each case, can be adjusted by priorities.
   * @author Daniel Dahme, Uwe Krüger
   */
  class Devcom: public RS232com{
    
    friend void *get_device_data_wrap(void *param);
    pthread_t read;

  protected:
    
    /**
     * Constructor to get an instance of the Devcom class
     */
    Devcom();

    /**
     * Destructor to destroy an instance of the Devcom class
     */
    ~Devcom();
    
    /**
     * Set the priority for controlling how often get data of given channel.
     * @param channel The channel (0-7) whose priority is to be set.
     * @param prio The Priority for controlling the queries. A lower priority
     * meant that the channel is queried more frequently, a higher priority 
     * corresponds to a rarer inquiry.
     */
    void set_priority(int channel, int prio) const;

    /**
     * Get data of given channel.
     * @param channel The channel (0-7) to query for data.
     * @return A measured value between 0 and 1023, which behaves
     * proportionally to the voltage level at the channel.
     */
    int get_data(int channel) const;
   
    /**
     * Open the sensor device for communication and start getting data from
     * the serial sensor device.
     * @param device The port/device, at which the equipment is attached.
     */
    void open_device(char *device);

    /**
     * Stop getting data from the serial sensor device and close the
     * connection.
     */
    void close_device();
    
    /**
     * Test if the serial sensor device is connected at the port.
     * @return true, if the serial sensor device is connected, and the
     * connect method has been successfully called; false otherwise
     */
    bool device_connected() const;
    
  private:
    
    /**
     * Reset the electronic in order to get meaningful values. This is
     * necessary, in order to start the protocol again for communication
     * with the equipment.
     */
    void reset_electronic(const char *device);
  
    /**
     * Get the 12 bit channel address of the given channel number for
     * communicate with the serial sensor device using the protocol.
     * @param c The channel number (0-7)
     * @return The channel address for the protocol
     */
    int get_channel(const int c) const;
    
    /**
     * Threaded method for getting data from sensor device. This method reads 
     * out the data from serial sensor device using the method exchange_data
     * for buffering to answer fast queries.
     */
    void *get_device_data(char *device);       
   
    /**
     * Send channel address at sensor device and during that get data of the
     * channel with the address sent before. This method realises the protocol
     * of the serial sensor device.
     * @param channel The channel number (0-7) to get data from at the next
     * data exchange
     * @return the data (0-1023) of the given channel at the call before.
     * The measure value behaves proportionally to the voltage level at the
     * channel.
     */
    int exchange_data(int channel);
  };
}
}
}
#endif /*DEVCOM_H*/

