C++ 数组值不变
C++ Array Values Not Changing
我在 C++ 中的 Particle Photon 上使用 FastLED 并尝试为像素阵列的元素之一分配新值。
基本上,我有一个这样声明的数组:
NSFastLED::CRGB leds[1];
我将其传递到我编写的 "animation" class 中以更改 LED 值:
void SomeClass::loop()
{
// Get the pointer to the current animation from a vector
Animation *currentAnim = animations.at(currentAnimation);
currentAnim->animate(leds);
...
}
在动画中,我试图做一些非常简单的事情——将 LED 阵列的一个元素设置为某个值。为了测试,即使将其设置为静态整数“0”也可以。
void MyAnimation::animate(NSFastLED::CRGB *leds)
{
for(int i = 0; i < numLeds; i++)
{
Serial.print(leds[i]); // "1"
leds[i] = 0;
Serial.print(leds[i]); // "1"
}
}
问题是根本没有设置数组元素。如您所见,这甚至在我遇到问题的动画 class 中。我也尝试过使用 (leds*)[i] = 0
,但也没有任何效果。
为什么数组中没有设置值?
您的数组数据类型是 NSFastLED::CRGB,它包含 RGB 值,可以像下面这样分配(来自 https://github.com/FastLED/FastLED/wiki/Pixel-reference)
如果你只想存储一个数字,你可以使用 int 而不是 NSFastLED::CRGB。
// The three color channel values can be referred to as "red", "green", and "blue"...
leds[i].red = 50;
leds[i].green = 100;
leds[i].blue = 150;
// ...or, using the shorter synonyms "r", "g", and "b"...
leds[i].r = 50;
leds[i].g = 100;
leds[i].b = 150;
// ...or as members of a three-element array:
leds[i][0] = 50; // red
leds[i][1] = 100; // green
leds[i][2] = 150; // blue
我在 C++ 中的 Particle Photon 上使用 FastLED 并尝试为像素阵列的元素之一分配新值。
基本上,我有一个这样声明的数组:
NSFastLED::CRGB leds[1];
我将其传递到我编写的 "animation" class 中以更改 LED 值:
void SomeClass::loop()
{
// Get the pointer to the current animation from a vector
Animation *currentAnim = animations.at(currentAnimation);
currentAnim->animate(leds);
...
}
在动画中,我试图做一些非常简单的事情——将 LED 阵列的一个元素设置为某个值。为了测试,即使将其设置为静态整数“0”也可以。
void MyAnimation::animate(NSFastLED::CRGB *leds)
{
for(int i = 0; i < numLeds; i++)
{
Serial.print(leds[i]); // "1"
leds[i] = 0;
Serial.print(leds[i]); // "1"
}
}
问题是根本没有设置数组元素。如您所见,这甚至在我遇到问题的动画 class 中。我也尝试过使用 (leds*)[i] = 0
,但也没有任何效果。
为什么数组中没有设置值?
您的数组数据类型是 NSFastLED::CRGB,它包含 RGB 值,可以像下面这样分配(来自 https://github.com/FastLED/FastLED/wiki/Pixel-reference)
如果你只想存储一个数字,你可以使用 int 而不是 NSFastLED::CRGB。
// The three color channel values can be referred to as "red", "green", and "blue"...
leds[i].red = 50;
leds[i].green = 100;
leds[i].blue = 150;
// ...or, using the shorter synonyms "r", "g", and "b"...
leds[i].r = 50;
leds[i].g = 100;
leds[i].b = 150;
// ...or as members of a three-element array:
leds[i][0] = 50; // red
leds[i][1] = 100; // green
leds[i][2] = 150; // blue