From a8c044ada867bf035fc2ee28431d6d4703edc4b8 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 2 Sep 2016 15:00:09 -0700 Subject: [PATCH] Updated FAQ #3 (markdown) --- FAQ-#3.md | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/FAQ-#3.md b/FAQ-#3.md index 4696cc4..573313b 100644 --- a/FAQ-#3.md +++ b/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 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(); } }