andrewm@0: /*
andrewm@0: TouchKeys: multi-touch musical keyboard control software
andrewm@0: Copyright (c) 2013 Andrew McPherson
andrewm@0:
andrewm@0: This program is free software: you can redistribute it and/or modify
andrewm@0: it under the terms of the GNU General Public License as published by
andrewm@0: the Free Software Foundation, either version 3 of the License, or
andrewm@0: (at your option) any later version.
andrewm@0:
andrewm@0: This program is distributed in the hope that it will be useful,
andrewm@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0: GNU General Public License for more details.
andrewm@0:
andrewm@0: You should have received a copy of the GNU General Public License
andrewm@0: along with this program. If not, see .
andrewm@0:
andrewm@0: =====================================================================
andrewm@0:
andrewm@0: TimerNode.h: creates a Node object which runs its own thread to generate
andrewm@0: timestamps.
andrewm@0: */
andrewm@0:
andrewm@0: #ifndef KEYCONTROL_TIMER_H
andrewm@0: #define KEYCONTROL_TIMER_H
andrewm@0:
andrewm@0: #include
andrewm@0: #include "Types.h"
andrewm@0: #include "Node.h"
andrewm@0: #include "../JuceLibraryCode/JuceHeader.h"
andrewm@0:
andrewm@0: class TimerNode : public Node, public Thread {
andrewm@0: #if 0
andrewm@0: // ***** Class to implement the Juce thread *****
andrewm@0: private:
andrewm@0: class TimerThread : public Thread {
andrewm@0: public:
andrewm@0: TimerThread(Timer *timer, timestamp_type starting_timestamp)
andrewm@0: : timer_(timer), startingTimestamp_(starting_timestamp) {}
andrewm@0:
andrewm@0: ~TimerThread() {}
andrewm@0:
andrewm@0: void run() {
andrewm@0: timer_->runLoop(startingTimestamp_);
andrewm@0: }
andrewm@0:
andrewm@0: private:
andrewm@0: Timer *timer_;
andrewm@0: timestamp_type startingTimestamp_;
andrewm@0: }
andrewm@0: #endif
andrewm@0: public:
andrewm@0: // ***** Constructor *****
andrewm@0:
andrewm@0: TimerNode(capacity_type capacity, unsigned long long interval_micros, String threadName = "Timer")
andrewm@0: : Node(capacity), Thread(threadName), intervalMicros_(interval_micros), isRunning_(false) {}
andrewm@0:
andrewm@0: // ***** Destructor *****
andrewm@0:
andrewm@0: ~TimerNode() {
andrewm@0: stop();
andrewm@0: }
andrewm@0:
andrewm@0: // ***** Timing Methods *****
andrewm@0: //
andrewm@0: // These functions start and stop the timer without deleting the data it has generated.
andrewm@0:
andrewm@0: void start(timestamp_type where = 0);
andrewm@0: void stop();
andrewm@0:
andrewm@0: // Allow viewing of the interval as a timestamp type. Allow viewing or setting it as an integer number
andrewm@0: // of microseconds. Don't set it directly as a timestamp_type: if timestamp_type is floating point, it
andrewm@0: // gives a misleading impression of the behavior of the timer when the interval doesn't round to an even
andrewm@0: // number of microseconds.
andrewm@0:
andrewm@0: timestamp_diff_type interval() { return microseconds_to_timestamp(intervalMicros_); }
andrewm@0: unsigned long long& interval_micros() { return intervalMicros_; }
andrewm@0:
andrewm@0: // The loop runs in its own thread and feeds new ticks to the data source at regular intervals. Give it
andrewm@0: // the interval length in microseconds and the timestamp of the first tick.
andrewm@0: // static void staticRunLoop(Timer* timer, timestamp_type starting_timestamp) { timer->runLoop(starting_timestamp); }
andrewm@0: void run();
andrewm@0:
andrewm@0: private:
andrewm@0: //TimerThread *thread_;
andrewm@0: unsigned long long intervalMicros_;
andrewm@0: bool isRunning_;
andrewm@0: timestamp_type startingTimestamp_;
andrewm@0: };
andrewm@0:
andrewm@0: #endif /* KEYCONTROL_TIMER_H */