#ifndef __CONTROL_THREAD_H_INCLUDED__
#define __CONTROL_THREAD_H_INCLUDED__

#include "threads.h"
#include "mutex.h"
#include "elmo.h"

typedef enum {traverse, EOTLeft, EOTRight} Mode;

class ControlThread : public Thread {

	friend stringstream& operator<< (stringstream& out, const ControlThread& controller);

public:

	ControlThread (Elmo& winch, Elmo& levelWind)
		: m_Winch(winch), m_LevelWind(levelWind),
		  m_Started(false) {}

	~ControlThread(void) {}

	void SetWinchRPM(double rpm);

	void StartController(void);
	void StopController(void);
	void Query(void);

protected:
	virtual void* Run(void*);

private:

	struct ItemsToLog
	{
		double		requestedWinchRPM;
		double		levelWindBaseDAC_Voltage;
		double		winchDAC_Voltage;
		double		levelWindDAC_Voltage;
		double		motorTensionPounds;
		double		flangeTensionPounds;
		double		totalTensionPounds;
		double		levelWindError;
		uint32_t	winchCountsPerSecond;
		uint32_t	levelWindDirection;
		Mode		levelWindMode;
		bool		isEndOfTravelLeftSwitchClosed;
		bool		isEndOfTravelRightSwitchClosed;
		bool		isCableLeftSwitchClosed;
		bool		isCableRightSwitchClosed;
	};

	Mutex		m_ExclusiveAccess;
	Elmo&		m_Winch;
	Elmo&		m_LevelWind;
	double		m_RequestedWinchRPM;
	double		m_LevelWindBaseDAC_Voltage;
	double		m_WinchDAC_voltage;
	double		m_LevelWindDAC_voltage;
	double 		m_MotorTensionPounds;
	double	 	m_FlangeTensionPounds;
	double		m_TotalTensionPounds;
	double		m_LevelWindError;
	uint32_t	m_WinchCountsPerSecond;
	uint32_t	m_LevelWindDirection;
	Mode		m_LevelWindMode;
	bool		m_IsEndOfTravelLeftSwitchClosed;
	bool		m_IsEndOfTravelRightSwitchClosed;
	bool		m_IsCableLeftSwitchClosed;
	bool		m_IsCableRightSwitchClosed;
	bool		m_Started;
	ItemsToLog	m_l;
};

#endif // of __CONTROL_THREAD_H_INCLUDED__
