Array of Objects

Hello,

I would like to be able to initialize and dispose/deinitialize a TM1637 object during runtime. I'm using the arduino to received via serial, commands to manage the IO Pins (add Inputs, Outputs...) but I never had to use objects until I had to manage TM1637.

Here's what I'd like to do:

typedef struct tm1637Displays_t
{
byte displayID; // Used as UDID for the DIsplay
TM1637Display displayObj;
};
byte maxDisplayQty = 5;
byte displayQty = 0;
tm1637Displays_t tm1637Displays[maxDisplayQty];

void loop()
{
[...]
// When we need to add a display
tm1637Displays[displayQty].displayID = newDisplayID;
tm1637Displays[displayQty].displayObj(CLK, DIO); // Initialize the object
displayQty++;

// Display something on display 2
byte displayID = 2;
tm1637Displays[displayID].displayObj.setBrightness(5, true); // Turn display ON and set Brightness
tm1637Displays[displayID].displayObj.showNumberDec(1, true); // Show something

// When we no longer need the display
tm1637Displays[displayID] = null; // Free the pins and dispose the object...

The C++ 'new' and 'delete' operators will do what you want. However, I have my suspicions that there's not really a good reason for doing it.

What you are asking for is to have a display added or discarded during runtime? How are you going to manage the wires wile its running?

As gfvalvo said above, it doesn't seem to make any sense.

-jim lee

A dynamic array of (4-digit 7-segment) display objects will be a problem since it appears that the only public constructor for the object requires two arguments: the clock and data pins. You can't create the objects until you know which pins you are going to use.

I would make an array of pointers. As global variables, they will initially be set to NULL and you can point them to actual objects later.

I, too, wonder how you will use a sketch that doesn't know what displays are connected or which pins will be used.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.