/**
    05Robot - shared routines

    @author P.Kaczmarek,J.Tomczynski,T.Mankowski
    @version 1.0 2017
*/
#ifndef SYNCOBJECT_H
#define SYNCOBJECT_H 

#include <QThread>
#include <QTimer>
#include <QList>
#include <QSemaphore>
#include "CameraThread.h"
#include "CameraObject.h"
#include "ProcessorThread.h"
#include "ProcessorObject.h"
/** \addtogroup ComputerVisionModule
 *  @{
 */
class SyncObject : public QObject
{
     Q_OBJECT

public:
	SyncObject()
	{
		//t1=0;
		//t2=0;
		fps = 0.0;
		syncSem = new QSemaphore;
		syncTimer = new QTimer;
		connect ( this, SIGNAL( startTimer(int) ), syncTimer, SLOT( start(int) ) );
		connect ( this, SIGNAL( stopTimer() ), syncTimer, SLOT( stop() ) );
		isRunning = false;
	

    }
    ~SyncObject()
    {
		qDebug("deleting so");
		disconnect ( this, SIGNAL( startTimer(int) ), syncTimer, SLOT( start(int) ) );
		disconnect ( this, SIGNAL( stopTimer() ), syncTimer, SLOT( stop() ) );
		
		delete syncSem;
		delete syncTimer;
	}

public:
	QSemaphore * syncSem;

	bool isRunning;

    QList<CameraThread *> getCameraList() const;

    QList<ProcessorThread *> getProcessorList() const;

private:
    QTimer * syncTimer;
    QList<CameraThread*> cameraList;
	QList<ProcessorThread*> processorList;

    int64 t1,t2;
	float fps;
	bool first_run;

public slots:
	void startAcquisition(int T);
	void stopAcquisition();
	void connectCamera(CameraThread * ct);
	void disconnectCamera(CameraThread * ct);
	void connectProcessor(ProcessorThread * pt);
	void disconnectProcessor(ProcessorThread * pt);
	void sync();


signals:
	void startTimer(int);
	void stopTimer();

	void cStart();
	void cSync();
	void cGrab();
	void cStop();

	void pStart();
	void pProcess(int,int);
	void pStop();
	//void cameraConnected(int id);
	//void processorConnected(int id);
	void operationDone();
	void displayFPS(float);

};
/** @}*/

#endif
