giuliomoro@135
|
1 #include "ClockSyncThread.h"
|
giuliomoro@135
|
2 #ifdef USE_JUCE
|
giuliomoro@135
|
3 #else //declare static members TODO: rather refactor this similar to other threads so that only run and clockSyncTask are static
|
giuliomoro@135
|
4 myClock_t ClockSyncThread::lastTime; // Used for clock synchronization
|
giuliomoro@135
|
5 bool ClockSyncThread::listening;
|
giuliomoro@135
|
6 ClockSync ClockSyncThread::clockSync;
|
giuliomoro@135
|
7 VirtualClock* ClockSyncThread::virtualClock;
|
giuliomoro@135
|
8 bool ClockSyncThread::threadIsExiting;
|
giuliomoro@135
|
9 AuxiliaryTask ClockSyncThread::clockSyncTask;
|
giuliomoro@135
|
10 #endif
|
giuliomoro@135
|
11 #ifdef USE_JUCE
|
giuliomoro@135
|
12 ClockSyncThread::ClockSyncThread(const String &threadName) :
|
giuliomoro@135
|
13 Thread(threadName)
|
giuliomoro@135
|
14 #else
|
giuliomoro@135
|
15 ClockSyncThread::ClockSyncThread()
|
giuliomoro@135
|
16 #endif /* USE_JUCE */
|
giuliomoro@135
|
17 {
|
giuliomoro@135
|
18 };
|
giuliomoro@135
|
19 ClockSyncThread::~ClockSyncThread(){
|
giuliomoro@135
|
20 #ifdef USE_JUCE
|
giuliomoro@135
|
21 stopThread(1000);
|
giuliomoro@135
|
22 #else
|
giuliomoro@135
|
23 stopThread();
|
giuliomoro@135
|
24 #endif /* USE_JUCE */
|
giuliomoro@135
|
25 }
|
giuliomoro@135
|
26 void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){
|
giuliomoro@135
|
27 setVirtualClock(aVirtualClock);
|
giuliomoro@135
|
28 listening=false;
|
giuliomoro@135
|
29 clockSync.init(isSlave, aPort, *virtualClock);
|
giuliomoro@135
|
30 #ifdef USE_JUCE
|
giuliomoro@135
|
31 startThread(5);
|
giuliomoro@135
|
32 #else
|
giuliomoro@135
|
33 threadIsExiting=false;
|
giuliomoro@135
|
34 clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,98, "clockSyncTask");
|
giuliomoro@135
|
35 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
|
giuliomoro@135
|
36 #endif /* USE_JUCE */
|
giuliomoro@135
|
37 }
|
giuliomoro@135
|
38
|
giuliomoro@135
|
39 #ifdef USE_JUCE
|
giuliomoro@135
|
40 #else
|
giuliomoro@135
|
41 void ClockSyncThread::startThread(){
|
giuliomoro@135
|
42 printf("starting\n");
|
giuliomoro@135
|
43 BeagleRT_scheduleAuxiliaryTask(clockSyncTask);
|
giuliomoro@135
|
44 printf("started\n");
|
giuliomoro@135
|
45 }
|
giuliomoro@135
|
46 void ClockSyncThread::stopThread(){
|
giuliomoro@135
|
47 threadIsExiting=true;
|
giuliomoro@135
|
48 }
|
giuliomoro@135
|
49 bool ClockSyncThread::threadShouldExit(){
|
giuliomoro@135
|
50 return(gShouldStop || threadIsExiting );
|
giuliomoro@135
|
51 }
|
giuliomoro@135
|
52 #endif /* USE_JUCE */
|
giuliomoro@135
|
53
|
giuliomoro@135
|
54 void ClockSyncThread::setVirtualClock(VirtualClock &aVirtualClock){
|
giuliomoro@135
|
55 virtualClock=&aVirtualClock;
|
giuliomoro@135
|
56 };
|
giuliomoro@135
|
57
|
giuliomoro@135
|
58 void ClockSyncThread::run(){
|
giuliomoro@135
|
59 while(!threadShouldExit()){
|
giuliomoro@135
|
60 clockSync.sendReceiveLoop();
|
giuliomoro@135
|
61 usleep(5000);
|
giuliomoro@135
|
62 // double now=virtualClock->getNow();
|
giuliomoro@135
|
63 // printf("th(end+1)=%f;\n", now/44100.0f);
|
giuliomoro@135
|
64 // printf("act(end+1)=%lld;\n", Clock::getTimeUs());
|
giuliomoro@135
|
65 }
|
giuliomoro@135
|
66 printf("Thread is not running \n");
|
giuliomoro@135
|
67 }
|