Skip to content

Commit

Permalink
v0.5.0 pre-release
Browse files Browse the repository at this point in the history
  • Loading branch information
siteswapjuggler committed Jan 30, 2020
1 parent b75b62f commit 165bdbf
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 106 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ The library add a class template to manage interpolation beetween values of vari
## Methods

* **go()** go to a new value
* **value()** get actual value of the interpolation
* **origin()** get last origin value of the interpolation
* **target()** get last target value of the interpolation
* **completion()** get the completion percentage of the interpolation
* **duration()** get last duration of the interpolation
* **update()** update value of the interpolation according to its parameters
* **pause()** pause the interpolation
* **resume()** resume from pause
* **update()** update value of the interpolation according to its parameters

* **getValue()** get actual value of the interpolation
* **getOrigin()** get last origin value of the interpolation
* **getTarget()** get last target value of the interpolation
* **getCompletion()** get the completion percentage of the interpolation
* **getDuration()** get last duration of the interpolation

* **setGrain()** set interpolation grain
* **setAutomation()** set automation mode

* **isPaused()** pause state
* **isRunning()** running state
* **isFinished()** finish state
Expand Down
24 changes: 24 additions & 0 deletions examples/grain_test/grain_test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <Ramp.h>

int lastValue;
rampInt myRamp;

void setup() {
Serial.begin(2000000);
myRamp.setGrain(1);
myRamp.go(5000,0,LINEAR);
myRamp.go(0, 15);
Serial.println();
}

void loop() {
int val = myRamp.update();
if (val != lastValue) {
Serial.print(val);
Serial.print("\t|\t");
Serial.print(myRamp.getCompletion());
Serial.print(" %");
Serial.println();
}
lastValue = val;
}
2 changes: 1 addition & 1 deletion examples/simple_test/simple_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void setup() {
Serial.begin(9600); // begin Serial communication

Serial.print("Value start at: "); //
Serial.println(myRamp.value()); // accessing start value
Serial.println(myRamp.getValue()); // accessing start value

Serial.println("Starting interpolation"); //
myRamp.go(255, 1000); // start interpolation (value to go to, duration)
Expand Down
11 changes: 5 additions & 6 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ rampDouble KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

completion KEYWORD2
duration KEYWORD2
value KEYWORD2
origin KEYWORD2
target KEYWORD2
getCompletion KEYWORD2
getDuration KEYWORD2
getValue KEYWORD2
getOrigin KEYWORD2
getTarget KEYWORD2
update KEYWORD2
go KEYWORD2
isFinished KEYWORD2
Expand All @@ -35,7 +35,6 @@ pause KEYWORD2
resume KEYWORD2
setAutomation KEYWORD2
setGrain KEYWORD2
setInterval KEYWORD2


#######################################
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Ramp
version=0.4.3
version=0.5.0
author=Sylvain Garnavault <[email protected]>
maintainer=Sylvain Garnavault <[email protected]>
sentence=A library that manage interpolation beetween values.
Expand Down
182 changes: 97 additions & 85 deletions src/Ramp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,74 +34,9 @@ void _ramp<T>::init(T _val) {
}

/*-----------------------------
CLASS METHODS
RAMP UPDATE
-----------------------------*/

template <class T>
float _ramp<T>::completion() {
float val = pos*10000/dur;
val /= 100.;
return (float) val;
}

template <class T>
unsigned long _ramp<T>::duration() {
return dur;
}

template <class T>
T _ramp<T>::value() {
return val;
}

template <class T>
T _ramp<T>::origin() {
return A;
}

template <class T>
T _ramp<T>::target() {
return B;
}

template <class T>
T _ramp<T>::go(T _val) {
return go(_val,0,NONE);
}

template <class T>
T _ramp<T>::go(T _val, unsigned long _dur) {
return go(_val,_dur,LINEAR);
}

template <class T>
T _ramp<T>::go(T _val, unsigned long _dur, ramp_mode _mode) {
return go(_val,_dur,_mode,ONCEFORWARD);
}

template <class T>
T _ramp<T>::go(T _val, unsigned long _dur, ramp_mode _mode, loop_mode _loop) {
A = val;
B = _val;
mode = _mode;
dur = _dur;
t = millis();

if (_dur == 0) val = _val;

if (_loop < ONCEBACKWARD) {
pos = 0;
speed = FORWARD;
}
else {
pos = dur;
speed = BACKWARD;
}
loop = _loop;
paused = false;
return val;
}

template <class T>
T _ramp<T>::update() {
bool doUpdate = true;
Expand All @@ -111,32 +46,34 @@ T _ramp<T>::update() {
if (automated) {
newTime = millis();
delta = newTime - t;
doUpdate = delta > grain;
doUpdate = delta >= grain;
}

if (mode != NONE && doUpdate) {

t = newTime;
if (!paused) {

// update ramp position within limits
switch (speed) {
case FORWARD:
if (pos <= dur - delta) {
if ((long long)pos + delta < dur) {
pos += delta;
}
else pos = dur;
break;
case BACKWARD:
if (pos >= delta) {
if ((long long)pos - delta > 0) {
pos -= delta;
}
else pos = 0;
break;
}

// update value according to the new position
if (mode != NONE) {
float k = (float)pos/(float)dur;
val = A + (B-A)*ramp_calc(k,mode);
constrain(val,A,B); //potential
}
else {
val = B;
Expand Down Expand Up @@ -175,15 +112,67 @@ T _ramp<T>::update() {
}

/*-----------------------------
RAMP STATES & ACTIONS
RAMP ACTIONS
-----------------------------*/

template <class T>
T _ramp<T>::go(T _val) {
return go(_val,0,NONE);
}

template <class T>
T _ramp<T>::go(T _val, unsigned long _dur) {
return go(_val,_dur,LINEAR);
}

template <class T>
T _ramp<T>::go(T _val, unsigned long _dur, ramp_mode _mode) {
return go(_val,_dur,_mode,ONCEFORWARD);
}

template <class T>
T _ramp<T>::go(T _val, unsigned long _dur, ramp_mode _mode, loop_mode _loop) {
A = val;
B = _val;
mode = _mode;
dur = _dur;
t = millis();

if (_dur == 0) val = B;

if (_loop < ONCEBACKWARD) {
pos = 0;
speed = FORWARD;
}
else {
pos = dur;
speed = BACKWARD;
}
loop = _loop;
paused = false;
return val;
}

template <class T>
void _ramp<T>::pause() {
paused = true;
}

template <class T>
void _ramp<T>::resume() {
paused = false;
}

/*-----------------------------
RAMP STATES
-----------------------------*/

template <class T>
bool _ramp<T>::isFinished() {
if (speed==FORWARD)
return (pos>=dur);
return (pos==dur);
if (speed==BACKWARD)
return (pos<=0);
return (pos==0);
return false;
}

Expand All @@ -197,32 +186,55 @@ bool _ramp<T>::isPaused() {
return (!paused);
}

/*-----------------------------
RAMP SETTERS
-----------------------------*/

template <class T>
void _ramp<T>::pause() {
paused = true;
void _ramp<T>::setGrain(unsigned long _grain) {
grain = _grain;
}

template <class T>
void _ramp<T>::resume() {
paused = false;
void _ramp<T>::setAutomation(bool _automated) {
automated = _automated;
}

/*-----------------------------
RAMP GETTERS
-----------------------------*/

template <class T>
void _ramp<T>::setAutomation(bool _automated) {
automated = _automated;
float _ramp<T>::getCompletion() {
float val = pos*10000/dur;
val /= 100.;
return (float) val;
}

template <class T>
void _ramp<T>::setInterval(unsigned long _interval) {
setGrain(_interval);
unsigned long _ramp<T>::getDuration() {
return dur;
}

template <class T>
void _ramp<T>::setGrain(unsigned long _grain) {
grain = _grain;
unsigned long _ramp<T>::getPosition() {
return pos;
}

template <class T>
T _ramp<T>::getValue() {
return val;
}

template <class T>
T _ramp<T>::getOrigin() {
return A;
}

template <class T>
T _ramp<T>::getTarget() {
return B;
}


/*-----------------------------
Expand Down Expand Up @@ -345,14 +357,14 @@ float ramp_calc(float k, ramp_mode m) {
s = p*asin(1/a) / (2*M_PI);
return -a*pow(2,10*k)*sin((k-s)*(2*M_PI)/p);

case ELASTIC_OUT: //BUG???
case ELASTIC_OUT:
a = 1;
p = 0.3;
s = p*asin(1/a) / (2*M_PI);
return (a*pow(2,-10*k)*sin((k-s)*(2*M_PI)/p)+1);


case ELASTIC_INOUT: //BUG???
case ELASTIC_INOUT:
k = k*2 - 1;
a = 1;
p = 0.3*1.5;
Expand Down
16 changes: 9 additions & 7 deletions src/Ramp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ class _ramp {
public:
_ramp(); // default constructor
_ramp(T); // constructor with initial value
unsigned long duration(); // get duration of the interpolation
float completion(); // get current ramp completion percentage [0.-100.]
T value(); // get current interpolated value
T origin(); // get origin value of the interpolation
T target(); // get target value of the interpolation
T update(); // update values
T go(T); // go directly to a new value

T go(T); // go directly to a new value
T go(T, unsigned long); // ramp to _val, within _dur
T go(T, unsigned long, ramp_mode); // ramp to _val, within _dur with ramp_mode
T go(T, unsigned long, ramp_mode, loop_mode); // ramp to _val, within _dur with ramp_mode and loop_mode

T getValue(); // get current interpolated value
T getOrigin(); // get origin value of the interpolation
T getTarget(); // get target value of the interpolation
float getCompletion(); // get current ramp completion percentage [0.-100.]
unsigned long getDuration(); // get duration of the interpolation
unsigned long getPosition(); // get current position of the interpolation

bool isFinished(); // is the ramp finished
bool isRunning(); // is the ramp running
Expand All @@ -111,7 +114,6 @@ class _ramp {

void setAutomation(bool); // set the automation mode
void setGrain(unsigned long); // set the grain (interval beetween updates)
void setInterval(unsigned long); // same as above
};

/*-----------------------------
Expand Down

0 comments on commit 165bdbf

Please sign in to comment.