mirror of
https://github.com/Makuna/NeoPixelBus.git
synced 2025-08-10 06:14:26 +02:00
Updated FAQ #3 (markdown)
34
FAQ-#3.md
34
FAQ-#3.md
@@ -1,5 +1,32 @@
|
||||
### I wish to dynamically set the number pixels, but I don't see a way to do this and other libraries expose a method to change the number of pixels like updateLength() or setPixelCount().
|
||||
The key word here is dynamic. The best practice when dealing with key feature change is to dynamical create the object and recreate it when things change. It will often lead to smaller program code size even though you may have to type more. See the following snippet of code as an example of how to do this.
|
||||
|
||||
There are two solutions that will allow you to support this.
|
||||
|
||||
#### Always define it with the max possible
|
||||
With this solution you define the strip with maximum but always consider it having less. The number of physical pixels doesn't matter.
|
||||
```
|
||||
const uint16_t MaxPixelCount = 144;
|
||||
uint16_t PixelCount = 32; // default number of pixels
|
||||
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(MaxPixelCount, 2);
|
||||
|
||||
void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// some arbitrary code, use PixelCount and not strip.PixelCount()
|
||||
for (uint16_t pixel = 0; pixel < PixelCount; pixel++) {
|
||||
strip.SetPixelColor(pixel, red);
|
||||
}
|
||||
}
|
||||
|
||||
void PixelCountChanged(uint16_t newCount) {
|
||||
PixelCount = newCount;
|
||||
}
|
||||
```
|
||||
|
||||
#### Dynamically create one
|
||||
The key word here is dynamic. The best practice when dealing with key feature change is to dynamical create the object and recreate it when things change. It will often lead to smaller program code size even though you may have to type more. See the following snippet of code as an example of how to do this.
|
||||
The caveat to this solution is that if it happens often, memory will become fragmented slowing down your sketch. This is best used when it happens rarely.
|
||||
```
|
||||
// declare your object as dynamic, a pointer to it, the *
|
||||
// a good practice is to set it NULL
|
||||
@@ -23,7 +50,10 @@ void PixelCountChanged(uint16_t newCount) {
|
||||
void loop() {
|
||||
// other parts of your code, you set the colors, and show
|
||||
if (strip != NULL) {
|
||||
strip->SetPixelColor(0, RgbColor(0));
|
||||
// some arbitrary code
|
||||
for (uint16_t pixel = 0; pixel < strip.PixelCount(); pixel++) {
|
||||
strip.SetPixelColor(pixel, red);
|
||||
}
|
||||
strip->Show();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user