1

I am trying to create a object for a LED matrix which could have a varying size throughout a program's runtime. To do this I am trying to dynamically create an object from the OctoWS2811 library and define it's size within the constructor of a display class. How would I do this properly? Is this possible? Thank you

Here is the normal way an OctoWS2811 object is created:

const int ledsPerPin = 120 * (32 / 8);

DMAMEM int displayMemory[ledsPerPin * 6];
int drawingMemory[ledsPerPin * 6];
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerPin, displayMemory, drawingMemory, config);

And here is an example of what I am trying to do:

#include <Arduino.h>
#include <OctoWS2811.h>

class Display
{
  OctoWS2811 *leds; // Dynamically allocated
  DMAMEM int *displayMemory;
  int *drawingMemory;
  const int config = WS2811_GRB | WS2811_800kHz;
  int width, height;

public:
  Display(int w, int h);
  void Draw();
};

Display::Display(int w, int h)
{
  width = w;
  height = h; //Must be multiple of 8
  int ledsPerPin = (height / 8) * width;
  displayMemory = new DMAMEM int[ledsPerPin * 6];
  drawingMemory = new int[ledsPerPin * 6];

  OctoWS2811 temp(ledsPerPin, displayMemory, drawingMemory, config);

  *leds = temp; //How do I do this properly?
}

void Display::Draw() {
  // Draw stuff with the object
  leds.setPixel(56, 0xFF0000);
}

void setup()
{
  Display Test(120, 32);
  // put your setup code here, to run once:
}

void loop()
{
  // put your main code here, to run repeatedly:
}

1 Answers1

-1

In order for this to work I need to use addressing like what is talked about in the post: C++ pointer to objects

the incorrect way to do this would be like this:

Edit: this is a bad idea I have learned. It "works" but leads to undefined behavior.

#include <Arduino.h>
#include <OctoWS2811.h>

class Display
{
  OctoWS2811 *leds; // Dynamically allocated
  DMAMEM int *displayMemory;
  int *drawingMemory;
  const int config = WS2811_GRB | WS2811_800kHz;
  int width, height;

public:
  Display(int w, int h);
  void Draw();
};

Display::Display(int w, int h)
{
  width = w;
  height = h; //Must be multiple of 8
  int ledsPerPin = (height / 8) * width;
  displayMemory = new DMAMEM int[ledsPerPin * 6];
  drawingMemory = new int[ledsPerPin * 6];

  OctoWS2811 temp(ledsPerPin, displayMemory, drawingMemory, config);

  leds = &temp; //Use an & symbol here
}

void Display::Draw() {
  // Draw stuff with the object
  leds->setPixel(56, 0xFF0000);   //Use an -> here
}

void setup()
{
  Display Test(120, 32);
  // put your setup code here, to run once:
}

void loop()
{
  // put your main code here, to run repeatedly:
}
  • This is assigning a pointer to a local variable that goes out of scope at the end of the contructor's body to a class member. That's a recipe for undefined behavior. – Miles Budnek Mar 24 '20 at 17:10
  • I think I am running into that right now. Do you have any suggestions? Is there a way to dynamically call this object? It also seems to be getting mad at my calling the DMAMEM array in the class's members. – Spencer Brennessel Mar 24 '20 at 17:13