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@152: giuliomoro@152: ClockSync* gClockSync; giuliomoro@135: void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){ giuliomoro@152: gClockSync = &clockSync; 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@139: clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,60, "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: BeagleRT_scheduleAuxiliaryTask(clockSyncTask); 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@149: #ifndef USE_JUCE giuliomoro@149: extern I2c_Codec* gAudioCodec; giuliomoro@149: #endif giuliomoro@135: void ClockSyncThread::run(){ giuliomoro@149: printf("variable=["); giuliomoro@135: while(!threadShouldExit()){ giuliomoro@149: static int count = 0; giuliomoro@135: clockSync.sendReceiveLoop(); giuliomoro@149: #ifndef USE_JUCE giuliomoro@149: // if (count == 300){ giuliomoro@149: // printf("0 0 0\n"); giuliomoro@149: // gAudioCodec->setAudioSamplingRate(44101); giuliomoro@149: // } giuliomoro@149: #endif giuliomoro@149: count++; giuliomoro@135: // double now=virtualClock->getNow(); giuliomoro@139: // printf("th(end+1)=%f;\n", now); giuliomoro@135: // printf("act(end+1)=%lld;\n", Clock::getTimeUs()); giuliomoro@135: } giuliomoro@141: printf("];\n"); giuliomoro@141: // printf("Thread is not running \n"); giuliomoro@135: }