0

I'm building a base class that creates color patterns using FastLed's CRGB structure. I use the base class as inheritance for another more specific class that modifies a vector of colors (Pixels[]) and at some point I want to pass the color of a specific element from my class to the main sketch. But i'm having some issues with it, since the content of Pixels[] doesn't seem to match what I actually write in there.

Pattern.h:

class Pattern
{
    public:
    CRGB PixelAt(int idx);
    CRGB Pixels[6];
}

Pattern.cpp:

CRGB Pattern::PixelAt(int idx) 
{
  // safety check
  if (idx >= 0 && idx < NumLeds)
  {
    return Pixels[idx];
  }
  else
  {
    Serial.print("Index out of bounds: "); Serial.println(idx);
    return CRGB::Black;
  }
}

TestPattern.h:

class TestPattern : public Pattern {
    void Tick(unsigned long millis);
}

TestPattern.cpp:

void TestPattern::Tick(unsigned long millis)
{
    for (int p = 0 ; p < NumLeds ; p++)
    {
      Pixels[p]=CRGB::Blue;
    }
}

Main Sketch:

for (int p = 0; p < NUM_PATTERNS; p++)
{
  // update pattern
  ledPatterns[p].Tick(millis());

  if (ledPatterns[p].Status == Enabled)
  {
    // sum the values of the pixels
    for (int l = 0; l < NUM_LEDS; l++) {
      leds[l] += ledPatterns[p].PixelAt(l);
    }
  }
}

I'm skipping parts of the code to make it easier to read, but I think with this much you should be able to help me make it work. I'm still getting acquainted with C and pointers, which is where I think the problem lies.

So how should I declare and use Pixels[] so that the Main Sketch, the base class and the inherited class all have consistent information?

12
  • On the face of it that all looks fine. What is the actual problem you are having though? You have gone through and told us what it is supposed to do, but you have forgotten to tell us what actually happens. – Majenko Sep 22 '16 at 9:47
  • I was not clear when i said "the content of Pixels[] doesn't seem to match what I actually write in there.". I write all the pixels to Blue and when i read them back in the main sketch i get random values. – Luis Ferreira Sep 22 '16 at 9:49
  • I figure out something else that is wrong. I have the Tick function in the parent class, but i actually want every child class to overload Tick with its own implementation, which is not happening. When i call Tick in the Main Sketch (also edited that in the main question since i forgot to put it there), the Tick that is being called is the one from the parent class, not the child class, why is that? I thought by default the child function would be called, and not the parent. – Luis Ferreira Sep 22 '16 at 9:53
  • @LuisFerreira thats usually caused by changing of object copies. Try to add Pattern(Pattern &) = deleted; and Pattern & operator=(const Pattern &) = deleted; into the Pattern class. And it won't let you copy this object. – KIIV Sep 22 '16 at 9:54
  • @KIIV you mean add those lines in the Header file? I get this compile error if i past them there in the public section: "error: invalid pure specifier (only '= 0' is allowed) before ';' token" – Luis Ferreira Sep 22 '16 at 9:59
1

After reading comments i got some more clues where to search and indeed the problem was somewhat in the inheritance and how i was calling the functions. This post helped me find the problem.

I forgot to post the piece of code where i was creating the patterns in the Main Sketch, this is how it was initially:

Pattern ledPatterns[NUM_PATTERNS];
ledPatterns[0] = TestPattern(NUM_LEDS, "Test Pattern 1", TestPat, 0, 0);
ledPatterns[1] = TestPattern(NUM_LEDS, "Test Pattern 2", TestPat, 0, 1);

...
ledPatterns[p].Tick(millis());
...

When i later called ledPatterns.Tick (the function that modifies the values, the function actually being called was the parent and not the child. Thus the unexpected result.

This is how i should have been initializing the patterns, since they are inherited:

Pattern* ledPatterns[NUM_PATTERNS];
ledPatterns[0] = new TestPattern(NUM_LEDS, "Test Pattern 1", TestPat, 0, 0);
ledPatterns[1] = new TestPattern(NUM_LEDS, "Test Pattern 2", TestPat, 0, 1);

...
ledPatterns[p]->Tick(millis());
...

Sorry for the huge confusion, i thought the problem was in a completely different part of the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.