I'm still struggling with strange values. However I don't know how to solve it. I thought its was related to the pointer and the array, but I'm not sure now.
I've updated my code and now use a normal global array (for this example I removed the multidimensional array that I used above).
My code:
header file:
#ifndef ColorLight_h
#define ColorLight_h
#include "WProgram.h"
class ColourBase
{
public:
ColourBase();
int baseHue; // hue value on the circle
float *wheelHSBColor;
};
class ColourPalette
{
public:
ColourPalette();
int mainHue;
int complementOffset;
int hueAngle;
int complementAngle;
ColourBase cArray[6];
void updatePalette();
void ColourAtRYBWheel(int, float*);
float lerp(float, float, float);
};
#endif
.cpp file
#include "WProgram.h"
#include "ColorLight.h"
const int rybHueWheel[25] =
{ 0 , 12 , 24 , 30 , 36 , 42 , 48 , 54 , 60 , 72 , 84 , 108, 120, 154, 180, 206, 225, 240, 260, 265, 280, 300, 315, 333, 360 };
ColourBase::ColourBase()
{ float wheelHSBColor[3];
}
ColourPalette::ColourPalette()
{ mainHue = 5;
complementOffset = 0;
hueAngle = 30;
complementAngle = 30;
updatePalette();
}
void ColourPalette::updatePalette()
{
Serial.println("update palette");
cArray[0].baseHue = mainHue;
cArray[1].baseHue = mainHue - hueAngle;
cArray[2].baseHue = mainHue + hueAngle;
cArray[3].baseHue = mainHue + 180 + complementOffset;
cArray[4].baseHue = cArray[3].baseHue - complementAngle;
cArray[5].baseHue = cArray[3].baseHue + complementAngle;
// convert to new hue numbers with the new ColorWheel (HueToRYB)
for (int i=0; i<6;i++)
{ ColourAtRYBWheel(cArray[i].baseHue, cArray[i].wheelHSBColor);
}
}
void ColourPalette::ColourAtRYBWheel(int hue, float c[3]) {
if (hue<0) hue = 360+hue;
hue = hue%360; // stick to 0 - 359
int rybWheelIndex = hue/15; // colourWheel has 24 base colours
float rybWheelAmount = (hue%15)/15.0; // interpolate amount
c[0] = lerp(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount);
c[1] = lerp(rybHueWheel[4], rybHueWheel[5], rybWheelAmount);
c[2] = lerp(255, 255, rybWheelAmount);
Serial.print("hue : ");
Serial.println(hue, DEC);
Serial.print("wheelindex: ");
Serial.println(rybWheelIndex, DEC);
Serial.print("wheelamount: ");
Serial.println(rybWheelAmount,DEC);
Serial.println("===================");
}
float ColourPalette::lerp(float a, float b, float x)
{ return(a*(1-x) + b*x);
}
If I print this I'll get:
update palette
hue : -32704
wheelindex: 0
wheelamount: -30704688.0000000000
===================
hue : -24509
wheelindex: 0
wheelamount: -30704688.0000000000
===================
hue : -12223
wheelindex: 0
wheelamount: -30704688.0000000000
===================
hue : 835
wheelindex: 0
wheelamount: -30704688.0000000000
===================
hue : -18366
wheelindex: 0
wheelamount: -30704688.0000000000
===================
hue : 15427
wheelindex: 0
wheelamount: -30704688.0000000000
===================?
However when I comment the lines that start with c[1] and [c2] I get the right output.
c[0] = lerp(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount);
//c[1] = lerp(rybHueWheel[4], rybHueWheel[5], rybWheelAmount);
//c[2] = lerp(255, 255, rybWheelAmount);
update palette
hue : 5
wheelindex: 0
wheelamount: 0.3333333492
===================
hue : 335
wheelindex: 22
wheelamount: 0.3333333492
===================
hue : 35
wheelindex: 2
wheelamount: 0.3333333492
===================
hue : 185
wheelindex: 12
wheelamount: 0.3333333492
===================
hue : 155
wheelindex: 10
wheelamount: 0.3333333492
===================
hue : 215
wheelindex: 14
wheelamount: 0.3333333492
===================?
So repeatedly calling the lerp function gives this bug. Has it the do with a lack of memory?
I don't know if its smart to use floats (since I read about speed issues), but I cannot find good examples of fixed point or integer linair interpolation functions.
Also the question if its possible to use the Flash library like this (Flash | Arduiniana) in my library. Is it just including the .h and files? Are there any examples on that?