view core/ClockSyncThread.cpp @ 152:8f98b32d0e23 ClockSync

Last commit on this branch for a while. Overall not very succesful
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 05 Oct 2015 13:06:14 +0100
parents 134bff10e561
children
line wrap: on
line source
#include "ClockSyncThread.h"
#ifdef USE_JUCE
#else //declare static members TODO: rather refactor this similar to other threads so that only run and clockSyncTask are static
	myClock_t ClockSyncThread::lastTime; // Used for clock synchronization
	bool ClockSyncThread::listening;
	ClockSync ClockSyncThread::clockSync;
	VirtualClock* ClockSyncThread::virtualClock;
	bool ClockSyncThread::threadIsExiting;
	AuxiliaryTask ClockSyncThread::clockSyncTask;
#endif
#ifdef USE_JUCE
ClockSyncThread::ClockSyncThread(const String &threadName) :
			Thread(threadName)
#else
ClockSyncThread::ClockSyncThread()
#endif /* USE_JUCE */
{
};
ClockSyncThread::~ClockSyncThread(){
#ifdef USE_JUCE
	stopThread(1000);
#else
	stopThread();
#endif /* USE_JUCE */
}

ClockSync* gClockSync;
void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){
	gClockSync = &clockSync;
	setVirtualClock(aVirtualClock);
	listening=false;
	clockSync.init(isSlave, aPort, *virtualClock);
#ifdef USE_JUCE
	startThread(5);
#else
	threadIsExiting=false;
	clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,60, "clockSyncTask");
	//TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
#endif /* USE_JUCE */
}

#ifdef USE_JUCE
#else
void ClockSyncThread::startThread(){
	BeagleRT_scheduleAuxiliaryTask(clockSyncTask);
}
void ClockSyncThread::stopThread(){
	threadIsExiting=true;
}
bool ClockSyncThread::threadShouldExit(){
	return(gShouldStop || threadIsExiting );
}
#endif /* USE_JUCE */

void ClockSyncThread::setVirtualClock(VirtualClock &aVirtualClock){
	virtualClock=&aVirtualClock;
};
#ifndef USE_JUCE
extern I2c_Codec* gAudioCodec;
#endif
void ClockSyncThread::run(){
	printf("variable=[");
	while(!threadShouldExit()){
		static int count = 0;
		clockSync.sendReceiveLoop();
#ifndef USE_JUCE
//		if (count == 300){
//			printf("0 0 0\n");
//			gAudioCodec->setAudioSamplingRate(44101);
//		}
#endif
		count++;
//		double now=virtualClock->getNow();
//		printf("th(end+1)=%f;\n", now);
//		printf("act(end+1)=%lld;\n", Clock::getTimeUs());
	}
	printf("];\n");
//	printf("Thread is not running \n");
}