andrewm@0
|
1 /*
|
andrewm@0
|
2 TouchKeys: multi-touch musical keyboard control software
|
andrewm@0
|
3 Copyright (c) 2013 Andrew McPherson
|
andrewm@0
|
4
|
andrewm@0
|
5 This program is free software: you can redistribute it and/or modify
|
andrewm@0
|
6 it under the terms of the GNU General Public License as published by
|
andrewm@0
|
7 the Free Software Foundation, either version 3 of the License, or
|
andrewm@0
|
8 (at your option) any later version.
|
andrewm@0
|
9
|
andrewm@0
|
10 This program is distributed in the hope that it will be useful,
|
andrewm@0
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
andrewm@0
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
andrewm@0
|
13 GNU General Public License for more details.
|
andrewm@0
|
14
|
andrewm@0
|
15 You should have received a copy of the GNU General Public License
|
andrewm@0
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
andrewm@0
|
17
|
andrewm@0
|
18 =====================================================================
|
andrewm@0
|
19
|
andrewm@0
|
20 TimerNode.h: creates a Node object which runs its own thread to generate
|
andrewm@0
|
21 timestamps.
|
andrewm@0
|
22 */
|
andrewm@0
|
23
|
andrewm@0
|
24 #ifndef KEYCONTROL_TIMER_H
|
andrewm@0
|
25 #define KEYCONTROL_TIMER_H
|
andrewm@0
|
26
|
andrewm@0
|
27 #include <iostream>
|
andrewm@0
|
28 #include "Types.h"
|
andrewm@0
|
29 #include "Node.h"
|
andrewm@0
|
30 #include "../JuceLibraryCode/JuceHeader.h"
|
andrewm@0
|
31
|
andrewm@0
|
32 class TimerNode : public Node<timestamp_type>, public Thread {
|
andrewm@0
|
33 #if 0
|
andrewm@0
|
34 // ***** Class to implement the Juce thread *****
|
andrewm@0
|
35 private:
|
andrewm@0
|
36 class TimerThread : public Thread {
|
andrewm@0
|
37 public:
|
andrewm@0
|
38 TimerThread(Timer *timer, timestamp_type starting_timestamp)
|
andrewm@0
|
39 : timer_(timer), startingTimestamp_(starting_timestamp) {}
|
andrewm@0
|
40
|
andrewm@0
|
41 ~TimerThread() {}
|
andrewm@0
|
42
|
andrewm@0
|
43 void run() {
|
andrewm@0
|
44 timer_->runLoop(startingTimestamp_);
|
andrewm@0
|
45 }
|
andrewm@0
|
46
|
andrewm@0
|
47 private:
|
andrewm@0
|
48 Timer *timer_;
|
andrewm@0
|
49 timestamp_type startingTimestamp_;
|
andrewm@0
|
50 }
|
andrewm@0
|
51 #endif
|
andrewm@0
|
52 public:
|
andrewm@0
|
53 // ***** Constructor *****
|
andrewm@0
|
54
|
andrewm@0
|
55 TimerNode(capacity_type capacity, unsigned long long interval_micros, String threadName = "Timer")
|
andrewm@0
|
56 : Node<timestamp_type>(capacity), Thread(threadName), intervalMicros_(interval_micros), isRunning_(false) {}
|
andrewm@0
|
57
|
andrewm@0
|
58 // ***** Destructor *****
|
andrewm@0
|
59
|
andrewm@0
|
60 ~TimerNode() {
|
andrewm@0
|
61 stop();
|
andrewm@0
|
62 }
|
andrewm@0
|
63
|
andrewm@0
|
64 // ***** Timing Methods *****
|
andrewm@0
|
65 //
|
andrewm@0
|
66 // These functions start and stop the timer without deleting the data it has generated.
|
andrewm@0
|
67
|
andrewm@0
|
68 void start(timestamp_type where = 0);
|
andrewm@0
|
69 void stop();
|
andrewm@0
|
70
|
andrewm@0
|
71 // Allow viewing of the interval as a timestamp type. Allow viewing or setting it as an integer number
|
andrewm@0
|
72 // of microseconds. Don't set it directly as a timestamp_type: if timestamp_type is floating point, it
|
andrewm@0
|
73 // gives a misleading impression of the behavior of the timer when the interval doesn't round to an even
|
andrewm@0
|
74 // number of microseconds.
|
andrewm@0
|
75
|
andrewm@0
|
76 timestamp_diff_type interval() { return microseconds_to_timestamp(intervalMicros_); }
|
andrewm@0
|
77 unsigned long long& interval_micros() { return intervalMicros_; }
|
andrewm@0
|
78
|
andrewm@0
|
79 // The loop runs in its own thread and feeds new ticks to the data source at regular intervals. Give it
|
andrewm@0
|
80 // the interval length in microseconds and the timestamp of the first tick.
|
andrewm@0
|
81 // static void staticRunLoop(Timer* timer, timestamp_type starting_timestamp) { timer->runLoop(starting_timestamp); }
|
andrewm@0
|
82 void run();
|
andrewm@0
|
83
|
andrewm@0
|
84 private:
|
andrewm@0
|
85 //TimerThread *thread_;
|
andrewm@0
|
86 unsigned long long intervalMicros_;
|
andrewm@0
|
87 bool isRunning_;
|
andrewm@0
|
88 timestamp_type startingTimestamp_;
|
andrewm@0
|
89 };
|
andrewm@0
|
90
|
andrewm@0
|
91 #endif /* KEYCONTROL_TIMER_H */ |