-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 38440e9
Showing
7 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The code in this repository is available under the [MIT License](https://secure.wikimedia.org/wikipedia/en/wiki/Mit_license). | ||
|
||
Copyright (c) 2012- Kyle McDonald | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# ofxTiming is an addon for [openFrameworks](http://openframeworks.cc) | ||
|
||
ofxTiming provides classes that implement common patterns associated with timing. | ||
|
||
## DelayTimer | ||
|
||
An asynchronous buffered intervalometer that will accept a framerate, and return a true `tick()` when it's time to execute an event associated with that interval. | ||
|
||
## FadeTimer | ||
|
||
A class that returns a state between 0 and 1 that will fade from one end to the other given `start()` or `stop()`, correctly handling intermediate cases. | ||
|
||
## Hysteresis | ||
|
||
A class for filtering and tracking noisy input. This also plugs into `FadeTimer`. | ||
|
||
## RateTimer | ||
|
||
A class for timing framerates or intervals of time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include "ofMain.h" | ||
|
||
/* | ||
DelayTimer is a synchronous timer, which means you can ask it | ||
whether it's time to do something again yet. | ||
Set it up with setFramerate() or setPeriod() (in seconds). | ||
Then call tick() to find out whether the timer has ticked | ||
since the last time you checked. | ||
setUseBuffer(true) means tick() will return true multiple times | ||
when multiple ticks have passed without you checking. | ||
*/ | ||
|
||
class DelayTimer { | ||
protected: | ||
float period; | ||
float lastTime; | ||
int ticks; | ||
bool useBuffer; | ||
public: | ||
DelayTimer() : | ||
period(0), | ||
lastTime(0), | ||
ticks(0), | ||
useBuffer(false) { | ||
}; | ||
void setFramerate(float framerate) { | ||
period = 1 / framerate; | ||
} | ||
void setPeriod(float period) { | ||
this->period = period; | ||
} | ||
float getPeriod() { | ||
return period; | ||
} | ||
void setUseBuffer(bool useBuffer) { | ||
this->useBuffer = useBuffer; | ||
} | ||
bool tick() { | ||
float curTime = ofGetElapsedTimef(); | ||
int curTicks = (int) (curTime / period); | ||
int lastTicks = (int) (lastTime / period); | ||
lastTime = curTime; | ||
ticks += curTicks - lastTicks; | ||
if(ticks > 0) { | ||
if(useBuffer) { | ||
ticks--; | ||
} else { | ||
ticks = 0; | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#pragma once | ||
|
||
#include "ofMain.h" | ||
#include "Hysteresis.h" | ||
|
||
class FadeTimer { | ||
protected: | ||
enum {RISING = true, FALLING = false}; | ||
double referenceTime, risingLength, fallingLength; | ||
bool direction; | ||
public: | ||
FadeTimer() | ||
:risingLength(0) | ||
,fallingLength(0) | ||
,direction(RISING) | ||
,referenceTime(0) { | ||
} | ||
void setLength(double length) { | ||
this->risingLength = length; | ||
this->fallingLength = length; | ||
} | ||
void setLength(double risingLength, double fallingLength) { | ||
this->risingLength = risingLength; | ||
this->fallingLength = fallingLength; | ||
} | ||
void start() { | ||
double curTime = ofGetElapsedTimef(); | ||
double state = get(); | ||
referenceTime = curTime - state * risingLength; | ||
direction = RISING; | ||
} | ||
void stop() { | ||
double curTime = ofGetElapsedTimef(); | ||
double state = get(); | ||
referenceTime = curTime - (1 - state) * fallingLength; | ||
direction = FALLING; | ||
} | ||
void update(Hysteresis& hysteresis) { | ||
if(hysteresis.wasTriggered()) { | ||
start(); | ||
} else if(hysteresis.wasUntriggered()) { | ||
stop(); | ||
} | ||
} | ||
double get() { | ||
if(referenceTime > 0) { | ||
double base = ofGetElapsedTimef() - referenceTime; | ||
if(direction == FALLING) { | ||
base /= fallingLength; | ||
base = 1 - base; | ||
} else { | ||
base /= risingLength; | ||
} | ||
if(base < 0) return 0; | ||
if(base > 1) return 1; | ||
return base; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
bool getActive() { | ||
return get() > 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#pragma once | ||
|
||
#include "ofMain.h" | ||
|
||
class Hysteresis { | ||
protected: | ||
unsigned long lastTime; | ||
bool lastValue, curValue; | ||
unsigned long risingDelay, fallingDelay; | ||
bool triggered, untriggered; | ||
public: | ||
Hysteresis() | ||
:risingDelay(0) | ||
,fallingDelay(0) | ||
,lastTime(0) | ||
,lastValue(false) | ||
,curValue(false) | ||
,triggered(false) | ||
,untriggered(false) | ||
{ | ||
} | ||
void setDelay(float risingDelay, float fallingDelay) { | ||
this->risingDelay = 1000 * risingDelay; | ||
this->fallingDelay = 1000 * fallingDelay; | ||
} | ||
void setDelay(float delay) { | ||
setDelay(delay, delay); | ||
} | ||
void update(bool value) { | ||
unsigned long curTime = ofGetElapsedTimeMillis(); | ||
if(value != curValue) { | ||
if(value != lastValue) { | ||
lastTime = curTime; | ||
} | ||
unsigned long& delay = value ? risingDelay : fallingDelay; | ||
if(curTime - lastTime > delay) { | ||
curValue = value; | ||
if(value) { | ||
triggered = true; | ||
} else { | ||
untriggered = true; | ||
} | ||
} | ||
} | ||
lastValue = value; | ||
} | ||
bool get() const { | ||
return curValue; | ||
} | ||
bool wasTriggered() { | ||
if(triggered) { | ||
triggered = false; | ||
return true; | ||
} | ||
return false; | ||
} | ||
bool wasUntriggered() { | ||
if(untriggered) { | ||
untriggered = false; | ||
return true; | ||
} | ||
return false; | ||
} | ||
float length() const { | ||
return lengthMillis() / 1000.; | ||
} | ||
unsigned long lengthMillis() const { | ||
return ofGetElapsedTimeMillis() - lastTime; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include "ofMain.h" | ||
|
||
class RateTimer { | ||
protected: | ||
float lastTick, averagePeriod, smoothing; | ||
bool secondTick; | ||
public: | ||
RateTimer() : | ||
smoothing(.9) { | ||
reset(); | ||
} | ||
void reset() { | ||
lastTick = 0, averagePeriod = 0, secondTick = false; | ||
} | ||
void setSmoothing(float smoothing) { | ||
this->smoothing = smoothing; | ||
} | ||
float getFramerate() { | ||
return averagePeriod == 0 ? 0 : 1 / averagePeriod; | ||
} | ||
void tick() { | ||
float curTick = ofGetElapsedTimef(); | ||
if(lastTick == 0) { | ||
secondTick = true; | ||
} else { | ||
float curDiff = curTick - lastTick;; | ||
if(secondTick) { | ||
averagePeriod = curDiff; | ||
secondTick = false; | ||
} else { | ||
averagePeriod = ofLerp(curDiff, averagePeriod, smoothing); | ||
} | ||
} | ||
lastTick = curTick; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include "DelayTimer.h" | ||
#include "FadeTimer.h" | ||
#include "Hysteresis.h" | ||
#include "RateTimer.h" |