// Filename: input/device/isotrak/Isotrak.h
// Author: Jorge Bernadas (JB)
// Date of Creation: 11/02/2007
// Version: 1.0
//
// TODO(): Put general comment here!
#ifndef INPUT_DEVICE_ISOTRAK_ISOTRAK_H__
#define INPUT_DEVICE_ISOTRAK_ISOTRAK_H__

/*
#include "base/General.h"
#include "base/Types.h"
#include "base/singleton/Singleton.h"
#include "external/TSerial.h"
#include "input/device/isotrak/Receiver.h"
*/
#include "Receiver.h"
#include "../../../external/TSerial.h"
#include "../../../Includes/Basic.h"
#include "../../../Base/Singleton.h"

BEGIN_TEG_NAMESPACE();
//BEGIN_ENGINE_NAMESPACE();

class Isotrak : public Singleton<Isotrak> {
public:
  // enumerate used to specify the enabled receivers
  enum {kNoReceiverBits = 0, kReceiverOneBit, kReceiverTwoBit, kBothReceiversBits};

  // enumerate used to specify the receiver
  enum {kReceiverOne = 0, kReceiverTwo, kReceiverCount};

  // standard constructor and destructor
  Isotrak();
  ~Isotrak();

  // methods to connect and disconnect the isotrak device using the
  // specified port and baudrate
  bool Connect(const _TCHAR* port, int rate);
  void Disconnect();

  // methods to enable and disable receivers
  void EnableReceivers(int enableFlags);
  void DisableReceivers(int disableFlags);

  // method to enable or disable the quiet mode
  void SetQuietMode(bool enabled);
  void EnableQuietMode();
  void DisableQuietMode();

  // method to handle device alignment
  void ResetAlignment();
  void SetAlignment(const Vec3f& origin, const Vec3f& xAxis, const Vec3f& yAxis);
//  void GetAlignment();

  // method to read data from the device
  bool ReadData();

  // accesor methods to receivers
  Receiver& receiver(int index);
  const Receiver& receiver(int index) const;

private:
  // method to reset the system to the factory defaults (warning: this function
  // sleeps the actual thread for 5 seconds to wait for the system to reset)
  void ResetSystem();

  // method to toggle receivers on/off as the flags indicate
  void ToggleReceivers(int toggleFlags);

  // method to decode a binary value from two contiguous bytes
  static float DecodeBinary(const char* ptr, float scaleFactor);

  // serial device connection
  ext::TSerial tserial_;

  // variables to hold the isotrak receivers and the enable flags
  Receiver receiver_[kReceiverCount];
  int receiverFlags_;

  // constant scale factors used by the isotrak to decode binary input
  static const float kInchesScaleFactor;
  static const float kCentimetersScaleFactor;
  static const float kDegreesScaleFactor;
  static const float kRadiansScaleFactor;
  static const float kCosinesScaleFactor;
  static const float kQuaternionScaleFactor;
//  DISALLOW_COPY_CONSTRUCTORS(Isotrak);
};

//END_ENGINE_NAMESPACE();
END_TEG_NAMESPACE();

#endif  // INPUT_DEVICE_ISOTRAK_ISOTRAK_H__
