gpt4 book ai didi

C++ 数组值不变

转载 作者:行者123 更新时间:2023-11-30 05:36:55 25 4
gpt4 key购买 nike

我正在使用 C++ 中的 Particle Photon 与 FastLED 合作,并尝试为像素阵列的一个元素分配一个新值。

本质上,我有一个这样声明的数组:

NSFastLED::CRGB leds[1];

我将其传递到我编写的“动画”类中以更改 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"
}
}

问题是,根本没有设置数组元素。如您所见,这甚至在我遇到问题的动画类内部。我也尝试过使用 (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++ 数组值不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com