我正在为我的房间做一个ledstip项目。 我在利用arduino来做这件事。 对于这个项目,我想使用C++,所以我可以使用OOP。 在我使我的ledstrips工作后,我想创建一个集群类,它使用strip类来控制LED带的特定部分。 我不能让它工作。 编译器没有给出错误,而且在使用函数desk.rgb(0,100,0);
之后,我也没有看到任何变化。
这是我的。h文件
#include <FastLED.h>
template<class T>
class Cluster {
public:
T Strip;
int first;
int last;
Cluster(T Strip, int first, int last) {
this->Strip = Strip;
this->first = first;
this->last = last;
}
void rgb(int r, int g, int b){
Strip.rgb( r, g, b, first, last);
}
};
template<byte pin, int AmountOfLeds>
class Strip {
public:
CRGB leds[AmountOfLeds];
void setup() {
FastLED.addLeds<WS2812, pin, GRB>(leds, AmountOfLeds);
rgb(0, 0, 0);
}
//hole strip
void rgb(int r, int g, int b) {
for (int i = 0; i <= AmountOfLeds - 1; i++) {
this->leds[i] = CRGB(r, g, b);
}
FastLED.show();
}
//single led
void rgb(int i, int r, int g, int b) {
this->leds[i] = CRGB(r, g, b);
FastLED.show();
}
//range
void rgb(int r, int g, int b, int f, int l) {
for (int i = f; i <= l; i++) {
this->leds[i] = CRGB(r, g, b);
}
FastLED.show();
}
void hsv(int h, int s, int v) {
for (int i = 0; i <= AmountOfLeds; i++) {
this->leds[i] = CHSV(h, s, v);
}
FastLED.show();
}
void hsv(int i, int h, int s, int v) {
this->leds[i] = CHSV(h, s, v);
FastLED.show();
}
void hsv(int h, int s, int v, int f, int l) {
for (int i = f; i <= l; i++) {
this->leds[i] = CHSV(h, s, v);
}
FastLED.show();
}
void hsvWhiteBalance(int S, int V) { //S is yellowness, V is brightness
hsv(15, S, V);
}
void rainbow(float V) {
for (int i = 0; i <= AmountOfLeds; i++) {
leds[i] = CHSV( float(i) * (255 / float(AmountOfLeds)), 255, V);
}
FastLED.show();
}
void rainbow(float p, float V) {
for (int i = 0; i <= AmountOfLeds; i++) {
leds[i] = CHSV( float(i) * (255.0 / float(AmountOfLeds) * p), 255, V);
}
FastLED.show();
}
};
这是我的。ino文件:
#include "LedClasses.h"
Strip<5, 190> DTVC;
Cluster<Strip<5, 190>> Desk(DTVC, 1, 42);
void setup() {
Serial.begin(9600);
DTVC.setup();
DTVC.hsvWhiteBalance(153, 50);
Desk.rgb(0,100,0);
//DTVC.rgb(Desk, 0, 100, 0);
}
提前谢谢你。
不起作用,因为在cluster
构造函数中,通过复制获取stripp
类。 然后,在您的示例中有2个stripe
实例:一个在全局上下文中,一个在cluster
中。 您可以在全局上下文中对实例调用Stripe::Setup
(它调用FastLED.Addleds
)(在FastLED库中注册Stripe::LEDS
公共字段的地址),但稍后您可以对位于Cluster
中且具有不同Stripe::LEDS
字段的实例调用RGB
。
为了快速修复它(虽然不是很干净),您可以重新设计您的构造函数以接受引用而不是复制:
class Cluster {
public:
T& strip;
int first;
int last;
Cluster(T& s, int f, int l): strip(s), first(f), last(l) {}
或者,您可以更多地重新设计您的体系结构,从而不太多地使用模板(您可以使用constexpr
参数)。