giuliomoro@135: #include "ClockSyncThread.h" giuliomoro@135: #ifdef USE_JUCE giuliomoro@135: #else //declare static members TODO: rather refactor this similar to other threads so that only run and clockSyncTask are static giuliomoro@135: myClock_t ClockSyncThread::lastTime; // Used for clock synchronization giuliomoro@135: bool ClockSyncThread::listening; giuliomoro@135: ClockSync ClockSyncThread::clockSync; giuliomoro@135: VirtualClock* ClockSyncThread::virtualClock; giuliomoro@135: bool ClockSyncThread::threadIsExiting; giuliomoro@135: AuxiliaryTask ClockSyncThread::clockSyncTask; giuliomoro@135: #endif giuliomoro@135: #ifdef USE_JUCE giuliomoro@135: ClockSyncThread::ClockSyncThread(const String &threadName) : giuliomoro@135: Thread(threadName) giuliomoro@135: #else giuliomoro@135: ClockSyncThread::ClockSyncThread() giuliomoro@135: #endif /* USE_JUCE */ giuliomoro@135: { giuliomoro@135: }; giuliomoro@135: ClockSyncThread::~ClockSyncThread(){ giuliomoro@135: #ifdef USE_JUCE giuliomoro@135: stopThread(1000); giuliomoro@135: #else giuliomoro@135: stopThread(); giuliomoro@135: #endif /* USE_JUCE */ giuliomoro@135: } giuliomoro@135: void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){ giuliomoro@135: setVirtualClock(aVirtualClock); giuliomoro@135: listening=false; giuliomoro@135: clockSync.init(isSlave, aPort, *virtualClock); giuliomoro@135: #ifdef USE_JUCE giuliomoro@135: startThread(5); giuliomoro@135: #else giuliomoro@135: threadIsExiting=false; giuliomoro@135: clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,98, "clockSyncTask"); giuliomoro@135: //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled giuliomoro@135: #endif /* USE_JUCE */ giuliomoro@135: } giuliomoro@135: giuliomoro@135: #ifdef USE_JUCE giuliomoro@135: #else giuliomoro@135: void ClockSyncThread::startThread(){ giuliomoro@135: printf("starting\n"); giuliomoro@135: BeagleRT_scheduleAuxiliaryTask(clockSyncTask); giuliomoro@135: printf("started\n"); giuliomoro@135: } giuliomoro@135: void ClockSyncThread::stopThread(){ giuliomoro@135: threadIsExiting=true; giuliomoro@135: } giuliomoro@135: bool ClockSyncThread::threadShouldExit(){ giuliomoro@135: return(gShouldStop || threadIsExiting ); giuliomoro@135: } giuliomoro@135: #endif /* USE_JUCE */ giuliomoro@135: giuliomoro@135: void ClockSyncThread::setVirtualClock(VirtualClock &aVirtualClock){ giuliomoro@135: virtualClock=&aVirtualClock; giuliomoro@135: }; giuliomoro@135: giuliomoro@135: void ClockSyncThread::run(){ giuliomoro@135: while(!threadShouldExit()){ giuliomoro@135: clockSync.sendReceiveLoop(); giuliomoro@135: usleep(5000); giuliomoro@135: // double now=virtualClock->getNow(); giuliomoro@135: // printf("th(end+1)=%f;\n", now/44100.0f); giuliomoro@135: // printf("act(end+1)=%lld;\n", Clock::getTimeUs()); giuliomoro@135: } giuliomoro@135: printf("Thread is not running \n"); giuliomoro@135: }