我正在编写一些 arduino 代码,但事情并没有完全按计划进行。
我在这里做错了什么?我已经阅读并尝试对自己进行有关虚拟功能的教育,但也许我错过了一些东西。对于我需要回答的实际问题,请转到 QUESTIONSHERE,但首先,一些解释:
类 RGBPixel 和 colorGenerator 都派生自 colorSource,它提供公共(public)函数 getR()、getG() 和 getB(),以便另一个像素或颜色修改器可以获取其当前颜色的拷贝。
派生自 colorGenerator 的类实现颜色生成代码,以便它们可以生成自己的颜色,而 RGBPixels 具有 colorSource *parent 成员,因此它们可以从 colorGenerator 或另一个 RGBPixel 获取颜色值。
在我的示例中,我有一个 colorGenerator 子类(CG_EmeraldWaters,它应该为我创建各种绿色和蓝色),然后是一个数组中的多个 RGBPixel。 RGBPixels[0] 应该从 GC_EmeraldWaters 的实例中获取它的值,而 RGBPixels[1] 应该从 RGBPixels[0] 中获取它的值,[2] 来自 [1],[n] 来自 [n-1]。像素似乎可以从它们的父级中提取颜色,但是链中的第一个像素没有正确查询 colorGenerator,或者 colorGenerator 没有正确更新。
为了更新 colorGenerator,colorController 类监督整个过程:
颜色 Controller .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
如您所见, Controller 有一个 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);
第一个像素不是我希望的橙色,而是在紫色和橙色之间快速交替,这表明(恕我直言)我的新代码行正在完成它的工作,但随后像素又从 colorGenerator 中提取了原来的紫色,并且以某种方式 colorGenerator::updateColor() 不会改变 colorGenerator 的颜色(假设我没有收到编译错误,它在改变什么?)。所以我的问题是:(QUESTIONSHERE)
1) 我如何从 colorGenerator::updateColor() 中更改 colorSource::currentR(/G/B) 的值,因为 currentR(/G/B) 在 colorSource 中被声明为 protected 并且 colorGenerator 直接派生自颜色来源?
2) 给定一个 CG_EmeraldWaters 的实例,我如何通过 colorGenerator::refresh() 调用 CG_EmeraldWaters::updateColor(),CG_EmeraldWaters 继承了它,因为 updateColor() 在 colorGenerator 中被声明为虚拟并在 CG_EmeraldWaters 中被覆盖?
下面是 colorGenerator 和 CG_EmeraldWaters 的代码:
颜色源.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
颜色源.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;
}
颜色生成器.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
颜色生成器.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
的版本.其原因在上面链接的维基百科文章中进行了描述。要解决此问题,请将列表更改为包含指针,最好是 smart pointers , 指向
colorGenerator
实例。那么切片问题将不再相关:typedef std::list<unique_ptr<colorGenerator> > generatorList;
...
unique_ptr<colorGenerator> freshBubblingSpring(new CG_EmeraldWaters());
generators.push_back(freshBubblingSpring);
关于c++ - 多态类的行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461108/