I am currently programming my RGB LEDCube and therefor I want to create color gradients in seperate classes. Those classes are derived from colorgnt.h. I want wo pass colorgnts by a function argument but it only accepts the base class. I know why but I saw other people do it like that. How can I fix this issue?
/*
LEDCube.h
*/
#ifndef LEDCube_H
#define LEDCube_H
#include "Arduino.h"
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#include "colorgnt.h"
#include "rainbowgnt.h"
#define NUMLEDS 343
class LEDCube
{
public:
LEDCube();
//OTHER STUFF
void rendergradient(colorgnt gradient);
};
#endif
/*
LEDCube.cpp
*/
#include "Arduino.h"
#include "LEDCube.h"
LEDCube::LEDCube() {
FastLED.addLeds<WS2811, 7>(leds, 343);
FastLED.setBrightness(255);
FastLED.clear();
}
//OTHER STUFF
void LEDCube::rendergradient(colorgnt *gradient) {
rgb8bit whatever;
}
void LEDCube::test() {
rainbowgnt *gnt;
rendergradient(gnt); //error note: no known conversion for argument 1 from 'rainbowgnt*' to 'colorgnt'
}
/*
rainbowgnt.h
*/
#ifndef rainbowgnt_H
#define rainbowgnt_H
#include "colorgnt.h"
#include "LEDCube.h"
class colorgnt; //is this neccessary?
class rainbowgnt: public colorgnt {
public:
rainbowgnt() {}
virtual rgb8bit rgb(uint8_t position) {
rgb8bit rgb;
rgb.r = 255;
rgb.g = 255;
rgb.b = 255;
return rgb;
}
};
#endif
/*
colorgnt.h
*/
#ifndef colorgnt_H
#define colorgnt_H
#include "LEDCube.h"
typedef struct rgb8bit {
uint8_t r;
uint8_t g;
uint8_t b;
};
class colorgnt {
public:
colorgnt() {}
virtual rgb8bit rgb(uint8_t position) {}
};
#endif
I saw bitnluni's lab doing it like that: GitHub - bitluni/bitluniHomeAutomation
He did this:
#ifndef LED_FUNCTION_H
#define LED_FUNCTION_H
const char *rgbNames[] = {"r", "g", "b"};
class LedStates;
class LedFunction
{
public:
LedStates *state;
LedFunction()
{
}
//OTHER STUFF
#endif
class RainbowFunction: public LedFunction
{
public:
RainbowFunction()
{
}
//OTHER STUFF
and then he passed a dervied function to a function as an argument but the argument is defined as the base class:
bool checkFadeAndSetLedFunction(LedFunction *f) //HERE he defined the argument as the base class
{
int fade = getArgValue("fade");
if(fade > -1)
{
targetLedStates.setFunction(f);
ledFader.start(fade);
}
else
currentLedStates.setFunction(f);
}
server.on("/rainbow", [](){
server.send(200, "text/plain", "rainbow");
checkFadeAndSetLedFunction(new RainbowFunction());
});
server.on("/wave", [](){
server.send(200, "text/plain", "wave");
WaveFunction *f = new WaveFunction();
f->init(server);
checkFadeAndSetLedFunction(f); //HERE he passed it
});
Thank you if you read until here