我正在编写一些arduino代码,但事情还没计划好。
我在这里做错了什么?我已经阅读并尝试对虚拟函数进行自我教育,但是也许我错过了一些东西。转到QUESTIONSHERE,查找我需要回答的实际问题,但首先要进行一些解释:
RGBPixel和colorGenerator类均从colorSource派生,后者提供公共函数getR(),getG()和getB(),以便另一个像素或颜色修改器可以获取其当前颜色的副本。
从colorGenerator派生的类实现颜色生成代码,以便它们可以生成自己的颜色,而RGBPixels具有colorSource * parent成员,因此它们可以从colorGenerator或另一个RGBPixel获取颜色值。
在我的示例中,我有一个colorGenerator子类(CG_EmeraldWaters,应该为我创建各种绿色和蓝色),然后是数组中的许多RGBPixels。 RGBPixels [0]应该从GC_EmeraldWaters的实例中获取值,而RGBPixels [1]从RGBPixels [0],[2]和[n-1]中获取值。像素似乎很好地从其父级拉出了一种颜色,但是链中的第一个像素未正确查询colorGenerator,或者colorGenerator未正确更新。
要更新colorGenerator,一个colorController类将监视整个过程:
colorController.h:
#ifndef _COLORCONTROLLER_H
#define _COLORCONTROLLER_H
#include <list>
#include "colorGenerator.h"
#include "RGBPixel.h"
#include "globals.h"
#include "Arduino.h"
unsigned long millis();
typedef std::list<colorGenerator> generatorList;
class colorController
{
public:
virtual bool refresh();
protected:
generatorList generators;
};
#endif //_COLORCONTROLLER_H
如您所见,控制器具有colorGenerators列表和刷新它们的方法(从loop()调用),除非在子类中对其进行覆盖,否则它们将执行以下操作:
bool colorController::refresh()
{
for (generatorList::iterator it = generators.begin(); it != generators.end(); ++it)
it->refresh();
bool dirty = false;
for (int i = NUM_OF_LEDS-1; i >= 0; --i)
dirty |= RGBPixels[i].refresh();
return dirty;
}
CC_Cascade类(从colorController派生)设置如下:
CC_Cascade.h
#ifndef _CC_CASCADE_H
#define _CC_CASCADE_H
#include "colorController.h"
class CC_Cascade : public colorController
{
public:
CC_Cascade();
~CC_Cascade();
};
#endif //_CC_CASCADE_H
CC_Cascade.cpp
#include "CC_Cascade.h"
#include "CG_EmeraldWaters.h"
CC_Cascade::CC_Cascade()
{
colorGenerator * freshBubblingSpring = new CG_EmeraldWaters();
generators.push_back(*freshBubblingSpring);
RGBPixels[0].setParent(freshBubblingSpring);
RGBPixels[0].setDelay(40);
for (int i = 1; i < NUM_OF_LEDS; ++i)
{
RGBPixels[i].setParent(&RGBPixels[i-1]);
RGBPixels[i].setDelay(500-(9*i)); //FIXME: magic number only works for 50ish pixels
}
}
CC_Cascade::~CC_Cascade()
{
//TODO: delete generators
}
到目前为止清楚吗?
让我引起您对colorController :: refresh()函数的关注。应该发生的是,每次调用时,生成器列表中都有一个colorGenerator(因为CC_Cascade构造函数将其放置在其中),即CG_EmeraldWaters。 (通过迭代器)对此调用refresh()时,它将调用colorGenerator :: refresh(),后者又将调用updateColor()。在CG_EmeraldWaters的情况下,它被覆盖,因此应调用CG_EmeraldWaters :: updateColor,以提供绿松石色。使用一些串行写语句进行调试,我可以看到IN FACT colorGenerator :: updateColor()已被调用,因此在这种情况下,我希望显示为橙色,但这些都不影响像素的颜色,所有这些保持CG_EmeraldWaters构造器中设置的紫色。
有点混乱,我在colorGenerator :: updateColor()中添加了以下行:
RGBPixels[0].setColor(255,127,0);
第一个像素不是我希望的橙色,而是在紫色和橙色之间快速切换,这表明(IMHO)我的新代码行已完成工作,但随后该像素又从colorGenerator中拉回了原来的紫色,而且colorGenerator :: updateColor()不会改变colorGenerator的颜色(假设我没有编译错误,它在改变什么?)。
所以我的要求是:(QUESTIONSHERE)
1)如何在colorGenerator :: updateColor()中更改colorSource :: currentR(/ G / B)的值,假设currentR(/ G / B)在colorSource中声明为受保护的,并且colorGenerator直接来自于colorSource?
2)给定CG_EmeraldWaters的实例,我如何通过colorGenerator :: refresh()调用CG_EmeraldWaters :: updateColor(),如果在colorGenerator中将updateColor()声明为virtual并在CG_EmeraldWaters中重写,则CG_EmeraldWaters继承了该方法?
以下是colorGenerator和CG_EmeraldWaters的代码:
colorSource.h:
#ifndef _COLORSOURCE_H
#define _COLORSOURCE_H
#include "Arduino.h"
#ifdef DEBUG
#include "colorGenerator.h" //FIXME: delete Me
#endif
//#define byte unsigned char
typedef byte colorStorage_t;
class colorSource
{
public:
colorSource();
colorSource(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB);
void setColor(colorStorage_t newR, colorStorage_t newG, colorStorage_t newB);
//TODO: better implementation than this
colorStorage_t getR();
colorStorage_t getG();
colorStorage_t getB();
bool hasChanged();
protected:
colorStorage_t currentR;
colorStorage_t currentG;
colorStorage_t currentB;
bool dirty;
#ifdef DEBUG
friend colorGenerator; //FIXME: delete Me
#endif
};
#endif //_COLORSOURCE_H
colorSource.cpp:
#include "colorSource.h"
colorSource::colorSource()
{
//nothing here
}
colorSource::colorSource(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB)
:
currentR(initialR),
currentG(initialG),
currentB(initialB)
{
//intialised in the list
Serial.println("Constructed Color Source with initial color");
}
void colorSource::setColor(colorStorage_t newR, colorStorage_t newG, colorStorage_t newB)
{
currentR = newR;
currentG = newG;
currentB = newB;
}
colorStorage_t colorSource::getR()
{
return currentR;
}
colorStorage_t colorSource::getG()
{
return currentG;
}
colorStorage_t colorSource::getB()
{
return currentB;
}
bool colorSource::hasChanged()
{
return !dirty;
}
colorGenerator.h:
#ifndef _COLORGENERATOR_H
#define _COLORGENERATOR_H
#include "colorSource.h"
#ifdef DEBUG
#include "RGBPixel.h" //delete me, used for debugging!
#include "globals.h" //and me!
#endif
extern "C" unsigned long millis();
class colorGenerator : public colorSource
{
public:
colorGenerator(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB);
bool refresh();
protected:
virtual void updateColor();
unsigned long nextColorUpdate = 0;
unsigned short delay = 40;
};
#endif //_COLORGENERATOR_H
colorGenerator.cpp:
#include "Arduino.h"
#include "colorGenerator.h"
colorGenerator::colorGenerator(colorStorage_t initialR, colorStorage_t initialG, colorStorage_t initialB)
:
colorSource(initialR,initialG,initialB)
{
//intialised in the list
//Serial.println("Constructed Color Generator");
}
bool colorGenerator::refresh()
{
#ifdef DEBUG
Serial.print("colorGenerator::refresh()");
#endif
if (millis() < nextColorUpdate)
return false;
nextColorUpdate = millis() + (unsigned long) delay;
this->updateColor();
return true;
}
void colorGenerator::updateColor() //this function gets called (even if it has been overridden in a child class), but the code in it doesn't have the desired effect
{
#ifdef DEBUG
//Serial.print("colorGenerator::updateColor()");
//RGBPixels[0].setColor(255,127,0);
#endif
currentR = random(127,255);
currentG = random(0,127);
currentB = 0;
}
CG_EmeraldWaters.h:
#ifndef _CG_EMERALDWATERS_H
#define _CG_EMERALDWATERS_H
#include "colorGenerator.h"
#include "globals.h"
#include "RGBPixel.h"
class CG_EmeraldWaters : public colorGenerator
{
public:
CG_EmeraldWaters();
protected:
void updateColor();
};
#endif //_CG_EMERALDWATERS_H
CG_EmeraldWaters.cpp:
#include "Arduino.h"
#include "CG_EmeraldWaters.h"
CG_EmeraldWaters::CG_EmeraldWaters()
:
colorGenerator(255,0,255) //this color seems to stick! Changes made by updateColor() aren't propogated to the pixels.
{
//initialised in list
//Serial.println("Constructed Emerald Waters");
}
long random(long,long);
void CG_EmeraldWaters::updateColor() //this never seems to be called!
{
currentR = 0;
currentG = random(0,255);
currentB = random(0,255);
}
最后,主要的草图文件:
#include "FastSPI_LED2.h"
#include <StandardCplusplus.h>
#include "colorController.h"
#include "RGBPixel.h"
#include "globals.h"
#include "CC_Cascade.h"
colorController * currentColorController;
RGBPixel RGBPixels[NUM_OF_LEDS];
struct CRGB ledString[NUM_OF_LEDS];
void setup()
{
#ifdef DEBUG
//debugging:
Serial.begin(9600);
Serial.println("In Setup");
#endif
// sanity check delay - allows reprogramming if accidently blowing power w/leds
//delay(2000);
LEDS.setBrightness(8);
LEDS.addLeds<WS2801>(ledString, NUM_OF_LEDS);
currentColorController = new CC_Cascade();
}
void writeValuesToString()
{
for (int i = 0; i < NUM_OF_LEDS; ++i)
ledString[i] = CRGB(RGBPixels[i].getR(),RGBPixels[i].getG(),RGBPixels[i].getB());
LEDS.show();
}
void loop()
{
static bool dirty = false; //indicates whether pixel values have changed since last hardware write
//unsigned long lastHardwareWrite = 0; //time of last hardware write - only do this once per milisecond to avoid flicker (this method doesn't work, still flickers)
dirty |= currentColorController->refresh();
if (dirty)
{
dirty = false;
writeValuesToString();
delay(1); //to prevent flicker
}
}
最佳答案
您的问题是由于所谓的object slicing。这是怎么回事:当您声明类型为generatorList
的列表时
typedef std::list<colorGenerator> generatorList;
其成员仅限于
colorGenerator
中的内容。派生类无关紧要,因此当您按colorGenerator * freshBubblingSpring = new CG_EmeraldWaters();
generators.push_back(*freshBubblingSpring);
不在
CG_EmeraldWaters
中的colorGenerator
部分被“切掉”;您最终得到的是colorGenerator
版本。上面链接的Wikipedia文章中对此原因进行了描述。要解决此问题,请更改列表以包含指向
colorGenerator
实例的指针,最好是smart pointers。然后,切片问题将不再相关:typedef std::list<unique_ptr<colorGenerator> > generatorList;
...
unique_ptr<colorGenerator> freshBubblingSpring(new CG_EmeraldWaters());
generators.push_back(freshBubblingSpring);