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@152
|
26
|
giuliomoro@152
|
27 ClockSync* gClockSync;
|
giuliomoro@135
|
28 void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){
|
giuliomoro@152
|
29 gClockSync = &clockSync;
|
giuliomoro@135
|
30 setVirtualClock(aVirtualClock);
|
giuliomoro@135
|
31 listening=false;
|
giuliomoro@135
|
32 clockSync.init(isSlave, aPort, *virtualClock);
|
giuliomoro@135
|
33 #ifdef USE_JUCE
|
giuliomoro@135
|
34 startThread(5);
|
giuliomoro@135
|
35 #else
|
giuliomoro@135
|
36 threadIsExiting=false;
|
giuliomoro@139
|
37 clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,60, "clockSyncTask");
|
giuliomoro@135
|
38 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
|
giuliomoro@135
|
39 #endif /* USE_JUCE */
|
giuliomoro@135
|
40 }
|
giuliomoro@135
|
41
|
giuliomoro@135
|
42 #ifdef USE_JUCE
|
giuliomoro@135
|
43 #else
|
giuliomoro@135
|
44 void ClockSyncThread::startThread(){
|
giuliomoro@135
|
45 BeagleRT_scheduleAuxiliaryTask(clockSyncTask);
|
giuliomoro@135
|
46 }
|
giuliomoro@135
|
47 void ClockSyncThread::stopThread(){
|
giuliomoro@135
|
48 threadIsExiting=true;
|
giuliomoro@135
|
49 }
|
giuliomoro@135
|
50 bool ClockSyncThread::threadShouldExit(){
|
giuliomoro@135
|
51 return(gShouldStop || threadIsExiting );
|
giuliomoro@135
|
52 }
|
giuliomoro@135
|
53 #endif /* USE_JUCE */
|
giuliomoro@135
|
54
|
giuliomoro@135
|
55 void ClockSyncThread::setVirtualClock(VirtualClock &aVirtualClock){
|
giuliomoro@135
|
56 virtualClock=&aVirtualClock;
|
giuliomoro@135
|
57 };
|
giuliomoro@149
|
58 #ifndef USE_JUCE
|
giuliomoro@149
|
59 extern I2c_Codec* gAudioCodec;
|
giuliomoro@149
|
60 #endif
|
giuliomoro@135
|
61 void ClockSyncThread::run(){
|
giuliomoro@149
|
62 printf("variable=[");
|
giuliomoro@135
|
63 while(!threadShouldExit()){
|
giuliomoro@149
|
64 static int count = 0;
|
giuliomoro@135
|
65 clockSync.sendReceiveLoop();
|
giuliomoro@149
|
66 #ifndef USE_JUCE
|
giuliomoro@149
|
67 // if (count == 300){
|
giuliomoro@149
|
68 // printf("0 0 0\n");
|
giuliomoro@149
|
69 // gAudioCodec->setAudioSamplingRate(44101);
|
giuliomoro@149
|
70 // }
|
giuliomoro@149
|
71 #endif
|
giuliomoro@149
|
72 count++;
|
giuliomoro@135
|
73 // double now=virtualClock->getNow();
|
giuliomoro@139
|
74 // printf("th(end+1)=%f;\n", now);
|
giuliomoro@135
|
75 // printf("act(end+1)=%lld;\n", Clock::getTimeUs());
|
giuliomoro@135
|
76 }
|
giuliomoro@141
|
77 printf("];\n");
|
giuliomoro@141
|
78 // printf("Thread is not running \n");
|
giuliomoro@135
|
79 }
|