/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Description du fichier si nécessaire.
	
*/

/* ************************************************************************** */
/* Revision:
### YYYMMDD JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

/* ************************************************************************** */

#ifndef LAZERPROBE_H
#define LAZERPROBE_H


//N'activer qu'une seule de ces deux options... USE_CUSTOM_SERIAL_DRIVER ou USE_UART_HARDWARE_CTRL
#define USE_CUSTOM_SERIAL_DRIVER
#ifndef USE_CUSTOM_SERIAL_DRIVER
//#define USE_UART_HARDWARE_CTRL
#endif


#include "GlobalDefine.h"
#include "qextserialport.h"
#include "AbstractLazerProbe.h"
#include <QReadWriteLock>
#include <QElapsedTimer>
#ifdef USE_CUSTOM_SERIAL_DRIVER
	#include <sys/types.h>
	#include <termios.h>
	#include <unistd.h>
	#include <stdio.h>
	#include <errno.h>
	#include <sys/stat.h>
	#include <fcntl.h>
#endif



#define LAZER_PROBE_HEADER_MASK 0xFC
#define LAZER_PROBE_DATA_NIBBLE_MASK 0x03


class CLazerProbe : public QObject, public CAbstractLazerProbe
{
	Q_OBJECT

	enum eLazerProbeRxStates
	{
		LP_WAIT_HEADER1_STATE,
		LP_GET_DATA1_STATE,
		LP_GET_DATA2_STATE,
		LP_CHECK_HEADER2_STATE,
		LP_IGNORE_DATA_STATE,
		LP_MAX_STATE
	};

public:
	CLazerProbe(unsigned int ProbeID, unsigned int ProbeType);
	~CLazerProbe();
	unsigned int OpenPort(QString PortName);
	unsigned int GetType(){return mProbeType;}
	QString GetPortName(){return mSerialPortName;}
	unsigned int GetID(){return mProbeID;}
	unsigned int StartAcquisition();
	unsigned int StopAcquisition();
	unsigned int GetLastData();
	unsigned int FlushProbeData();
	bool IsProbeAlive();
	void QuitProbeThread();


private:
	unsigned char mHeader1,mDataNibble,mData1,mData2,mHeader2;
	unsigned int mLPCurData;
	unsigned int mLazerProbeRxState;
	unsigned int mProbeType;
	unsigned int mProbeID;
	bool mRunThread;

	QString mSerialPortName;
	bool mIsAcquisitioning;

	uid_t euid, ruid;

	QReadWriteLock mRdWrLock;



	QextSerialPort *mSerialPort;
	QElapsedTimer mDataIntervalTimer;
	bool mIsLazerProbeAlive;

	unsigned int LPRxStateMachine(unsigned char newbyte);
	unsigned int LPAnalyseNewData();
	unsigned int SetUID();
	unsigned int ResetUID();

#ifdef USE_CUSTOM_SERIAL_DRIVER
	int fd;
	int mUartBaseAddress;
#endif

public slots:
	void ProbeDataAvailable();

signals:
    void NewProbeData(unsigned int,unsigned int);




};

#endif // LAZERPROBE_H
