user27706: I'm trying to recreate a small tween engine so I can interpolate values, leds hue, saturation and brightness in a easy way.
I'm having trouble to reference the value to interpolate into my tween object. If you look at the very last block, you'll see what i'm trying to achieve.
I've poor knowledge in pointers and references and it's driving me mad. Thanks for your help !

Pixel.h
class Pixel {
  public:
    Pixel(float a = 180, float b = 100, float c = 100);

    float hue;
    float saturation;
    float brightness;

    uint16_t toRGB();
}


Tween.h
class Tween {
  public:
    Tween(int fps = 60);

    float& target;
    float fps;

    void to(float& target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d));

    void tick();
    float apply();
    bool active = false;

  private:
    float a = 0;
    float b = 1;
    float (* ease)(float t, float b, float c, float d);

    float increment = 1;
    float progress  = 0;
};


Tween.cpp
#include <Arduino.h>
#include "Tween.h"

int const ITERATIONS = 1000;

Tween::Tween(int f)
{ 
  this->fps = f;
  this->active = false;
}

void Tween::to(float& target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d)) {
  if (this->active == true) {
    return;
  }

  this->target = target;
  this->active = true;

  this->a = from;
  this->b = to;

  this->ease = ease;

  int frame = 1000 / this->fps;
  int ms = duration * 1000;

  this->progress = 0;
  this->increment = ITERATIONS / (float)(ms / frame);
}

void Tween::tick() {  
  if (this->progress >= ITERATIONS) {
    this->progress = 0;
    this->active = false;
  }

  if (this->active == false) {
    return;
  }

  this->progress += this->increment;
}

float Tween::apply() {
  if (this->active == false) {
    return 0;
  }

  float value = this->ease(this->progress, 0, this->b, ITERATIONS);
  this->target = value;

  return value;
}


The use of those will be :
Pixel pixel = Pixel();
Tween tween = Tween(40);

void setup() {
  Serial.begin(115200);

  // reset the values of the pixel
  pixel.hue = 200;
  pixel.saturation = 100;
  pixel.brightness = 0;

  // setup the tween object
  tween.to(pixel.l, 0, 100, 3, Easing::linearTween);
}

void loop() {
  // progress
  tween.tick();

  // set pixel.l = tween value (this is where it's not working)
  tween.apply();

  Serial.println(pixel.brightness);

  // we defined 40fps in the tween so the delay should be 1000/40 => 25ms
  delay(25);
}

The easing library is actually Robert Penner's work translated to Arduino (https://github.com/tobiastoft/ArduinoEasing)

user2926: The member variable target does not need to be passed by reference at all.
void Tween::to(float target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d)) 
{
  if (this->active == true) 
    return;

  this->target = target;
  this->active = true;

  // etc...
}

And then just call it without any sort of address or reference operator.
// setup the tween object
tween.to(pixel.l, 0, 100, 3, Easing::linearTween);

(but you still need to sort out what you mean by "pixel.l")
If you wanted to pass a Pixel object to the Tween::to() function, I would use a pointer to the object (although you could use a reference too).
void Tween::to(Pixel *pixel, /* ... etc*/)) 
{
    target = pixel->hue;
    other  = pixel->saturation;
    foo    = pixel->brightness;

    // etc.
}

Calling it like:
// setup the tween object
tween.to(&pixel, 0, 100, 3, Easing::linearTween);

It's not really clear exactly what you're trying to achieve just reading the code sample.

27706: <p>I'm trying to recreate a small tween engine so I can interpolate values, leds hue, saturation and brightness in a easy way.</p>

<p>I'm having trouble to reference the value to interpolate into my tween object. If you look at the very last block, you'll see what i'm trying to achieve.</p>

<p>I've poor knowledge in pointers and references and it's driving me mad. Thanks for your help !</p>

<hr>

<p><strong>Pixel.h</strong></p>

<pre><code>class Pixel {
  public:
    Pixel(float a = 180, float b = 100, float c = 100);

    float hue;
    float saturation;
    float brightness;

    uint16_t toRGB();
}
</code></pre>

<hr>

<p><strong>Tween.h</strong></p>

<pre><code>class Tween {
  public:
    Tween(int fps = 60);

    float&amp; target;
    float fps;

    void to(float&amp; target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d));

    void tick();
    float apply();
    bool active = false;

  private:
    float a = 0;
    float b = 1;
    float (* ease)(float t, float b, float c, float d);

    float increment = 1;
    float progress  = 0;
};
</code></pre>

<hr>

<p><strong>Tween.cpp</strong></p>

<pre><code>#include &lt;Arduino.h&gt;
#include "Tween.h"

int const ITERATIONS = 1000;

Tween::Tween(int f)
{ 
  this-&gt;fps = f;
  this-&gt;active = false;
}

void Tween::to(float&amp; target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d)) {
  if (this-&gt;active == true) {
    return;
  }

  this-&gt;target = target;
  this-&gt;active = true;

  this-&gt;a = from;
  this-&gt;b = to;

  this-&gt;ease = ease;

  int frame = 1000 / this-&gt;fps;
  int ms = duration * 1000;

  this-&gt;progress = 0;
  this-&gt;increment = ITERATIONS / (float)(ms / frame);
}

void Tween::tick() {  
  if (this-&gt;progress &gt;= ITERATIONS) {
    this-&gt;progress = 0;
    this-&gt;active = false;
  }

  if (this-&gt;active == false) {
    return;
  }

  this-&gt;progress += this-&gt;increment;
}

float Tween::apply() {
  if (this-&gt;active == false) {
    return 0;
  }

  float value = this-&gt;ease(this-&gt;progress, 0, this-&gt;b, ITERATIONS);
  this-&gt;target = value;

  return value;
}
</code></pre>

<hr>

<p>The use of those will be :</p>

<pre><code>Pixel pixel = Pixel();
Tween tween = Tween(40);

void setup() {
  Serial.begin(115200);

  // reset the values of the pixel
  pixel.hue = 200;
  pixel.saturation = 100;
  pixel.brightness = 0;

  // setup the tween object
  tween.to(pixel.l, 0, 100, 3, Easing::linearTween);
}

void loop() {
  // progress
  tween.tick();

  // set pixel.l = tween value (this is where it's not working)
  tween.apply();

  Serial.println(pixel.brightness);

  // we defined 40fps in the tween so the delay should be 1000/40 =&gt; 25ms
  delay(25);
}
</code></pre>

<p>The easing library is actually Robert Penner's work translated to Arduino (<a href="https://github.com/tobiastoft/ArduinoEasing" rel="nofollow">https://github.com/tobiastoft/ArduinoEasing</a>)</p>
 2926: <p>The member variable <code>target</code> does not need to be passed by reference at all.</p>

<pre><code>void Tween::to(float target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d)) 
{
  if (this-&gt;active == true) 
    return;

  this-&gt;target = target;
  this-&gt;active = true;

  // etc...
}
</code></pre>

<p>And then just call it without any sort of address or reference operator.</p>

<pre><code>// setup the tween object
tween.to(pixel.l, 0, 100, 3, Easing::linearTween);
</code></pre>

<p>(but you still need to sort out what you mean by "pixel.l")</p>

<p>If you wanted to pass a Pixel object to the Tween::to() function, I would use a pointer to the object (although you could use a reference too).</p>

<pre><code>void Tween::to(Pixel *pixel, /* ... etc*/)) 
{
    target = pixel-&gt;hue;
    other  = pixel-&gt;saturation;
    foo    = pixel-&gt;brightness;

    // etc.
}
</code></pre>

<p>Calling it like:</p>

<pre><code>// setup the tween object
tween.to(&amp;pixel, 0, 100, 3, Easing::linearTween);
</code></pre>

<p>It's not really clear exactly what you're trying to achieve just reading the code sample.</p>
