-
Notifications
You must be signed in to change notification settings - Fork 0
/
timerThread.pde
122 lines (99 loc) · 2.84 KB
/
timerThread.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//written after http://processing.org/discourse/beta/num_1213599231.html forum answer
class TimerThread extends Thread {
boolean running = false;
long previousTime;
boolean isActive = true;
double interval;
double total_time_passed_real;
double total_time_passed_ideal;
double timing_error;
double bpm = 0;
int task = 0;
boolean finishedJob = false;
TimerThread(double new_bpm)
{
bpm = new_bpm;
interval = (1000.0 / (bpm /240)) / 96;
previousTime = System.nanoTime();
}
void set_bpm(double new_bpm)
{
bpm = new_bpm;
interval = (1000.0 / (bpm / 240)) / 96;
total_time_passed_real = 0;
total_time_passed_ideal = 0;
}
void run()
{
long rest_period = 0;
while (this.isActive)
{
if (this.running)
{
// put the task here
if (!finishedJob) {
// in here
if (task == 1){ getData();
if (sendDataToMax){ sendLine();}
}
else if (task == 2) greetingManager();
else if (task == 3) {
for (int i = 0; i<trainLines.length; i++){
for (int j = 0; j<myTrain[i].length;j++){
myTrain[i][j].update();
/* if (myTrain[i][j].triggerCounter-- <= 0)
{
myTrain[i][j].trigger();
myTrain[i][j].triggerReset();
}*/
}
}
}
finishedJob = true;
}
// calculate time difference since last beat & wait
double timePassed = (System.nanoTime()-previousTime)*1.0e-6;
// sleep for a while
rest_period = (long) (interval *.75);
try
{
if ((rest_period > 1) && (timePassed < rest_period))
finishedJob = false;
Thread.sleep(rest_period);
}
catch(InterruptedException e)
{
println("force quit....");
}
// wake up a little early and watch the alarm clock
while (timePassed < (interval - timing_error))
{
timePassed = (System.nanoTime()-previousTime)*1.0e-6;
}
previousTime = System.nanoTime();
total_time_passed_real += timePassed;
total_time_passed_ideal += interval;
// if more time has passed between notes than should have passed, then slow things down a little
timing_error = total_time_passed_real - total_time_passed_ideal;
}
else {
try {
Thread.sleep(100);
}
catch(InterruptedException e) {
}
}
}
}
void do_stop()
{
this.running = false;
}
void do_start()
{
total_time_passed_real = 0;
total_time_passed_ideal = 0;
previousTime = System.nanoTime();
this.running = true;
}
}