Adding hardware devices in software dynamically

Hello all,

I was thinking of a way to make a library dynamic.
That means in a way of dynamic allocation of objects.

Let's take the following example:
Let's assume we have a library for Lighting objects. These objects could be led strip objects with which we would want to interact.
Now it would be cool that, if we share the library, people could add led strips to the software by calling a function in the library.

For example:

LightingController::AddDevice(arguments);

The LightingController would then ideally add a new LED object with which we could interact.

For example:

LightingController.Devices.Led1.TurnOn();

Or

Lightingcontroller.Devices[0].TurnOn();

This is very C#-ish, but I was wondering if this is sort of possible?

If this is not possible, what would be a good way to keep things dynamic?
Any examples?

Thanks!

what would be a good way to keep things dynamic?

A simple list?

Using vectors or linked list?

Just remember that you have very limited space and that memory fragmentation can be deadly, so the simpler the better. I think a fixed array is best. While it puts a hard limit to the number of devices, you don't have the overhead of memory allocation or the pointers in every node of your linked list.

KeithRB:
Just remember that you have very limited space and that memory fragmentation can be deadly, so the simpler the better. I think a fixed array is best. While it puts a hard limit to the number of devices, you don't have the overhead of memory allocation or the pointers in every node of your linked list.

Well that's the discussion I would like to start; what other programmers think is the best solution for arduino.

The idea is that the code of the library should somehow be flexible. This of course can be done by using software defines in a configuration header.
These defines could be used to at least get some flexibility, since we can create an array of the defined size.

The other way would be lists, which indeed have some cons.

Thanks for your opinion Keith, I would love to hear other opinions. :slight_smile:

It sounds like you mean "dynamic" more in the sense for programmers, rather than users. In other words, once the code is compiled, the number of devices doesn't change. If that is the case, implement a begin or start type function that accepts the arguments of how many devices the programmer will be adding and use malloc() to allocate the memory. You don't have to tell the programmers to go digging through the library to change a setting and won't suffer from issues of fragmentation.

You could set it up to be capable of running some number of devices and then add or remove those dynamically, assuming you have power and wiring worked out.

UNO has 2k or RAM. MEGA2560 has 8k. That's for everything, heap (including buffers) and stack.

Dynamic memory -can- be done on an UNO much in the same way that you -can- put a dirt bike track in the back yard of a 1/4 acre home lot.

Thank you all for your opinions and suggestions!

If you have any suggestions left, please reply :slight_smile:

Take a look at the code for servo!

Mark

Thanks for your reply Holmes!
I had a look and that is exactly what I had in mind!

It is based on the static arrays, and works with a nice function to add devices!
This makes it easy for beginner programmers to add devices to my library!

Thanks!