Pages: [1]   Go Down
Author Topic: Creating objects on the fly?  (Read 857 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I was thinking of creating an app where the user would have an interface (physical or virtual) where they can add devices to a i2c chain. Instead of changing the code  and recompiling every time the user adds a device, I was thinking it would be better if I let the program create an object on the fly and parameters would be passed in like the device's ID number and other parameter on how it should behave.

So does this make sense? Are there examples I could follow? Is there a better solution?

Thanks.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

C++ allows for dynamic object creation by means of the new operator. The first thing you have to do is define what this "object" does, and how it interacts with the rest of the Arduino sketch. When you define your requirements, then you can take a good look at whether or not creating a class for these objects would be beneficial.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you can discover the devices at runtime and need functions and state associated with them, then it'd be reasonable to create objects to manage them. However, to be useful your objects would have to know what to do - which probably means that your application (sketch) would need to recognise the discovered devices and know what to do with them. For example, your sketch could be coded to real with any temp sensors discovered on a OneWire bus by polling them and logging the current temperature in some way. (Probably not a good example there, since the only information you'd need in that case would be the device address and you might just as easily hold the discovered addresses in an array rather than wrap each one in an object and put that in an array. But the principle is valid.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4781
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have an external data source then you won't need automatic discovery/configuration but what types of devices will be limited by what the original code can support. You've got 32k flash on an UNO which is enough to give a good bit of capability.

You will have to make a lot of soft-configuration though, right down to the pins.

OTOH if you made your add-ons intelligent then you could code the particulars into those. An ATtiny or 328P stand-alone is cheap. That's what happened with PC hard drives going from separate controllers into IDE (integrated drive electronics) drives. Maybe you can make your own version of SCSI (small computer system interface) bus, only Very Small Comp....



Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm going to try to implement the MVC (model, view, controller) design pattern to this.
The interface (view) would be through Processing or an iphone app. The arduino would take care of the model and controller part.

Say I'm going to add a thermometer and solenoid to the i2c chain. In the view I click add device, and I would add the devices mentioned. I would also use the view to control the logic to the devices as well; like IF thermometer >  78F then turn off solenoid.

So there would be predefined classes like a thermometer class and a relay class etc. Whatever you choose, Arduino would instantiate the correct class and pass in certain parameters... As you can see there would be some logic to this, that's why I'm thinking objects would be best. There are other logic too like maybe a logging system like: log temperature every hour into logging device.

So from a userbility POV, this is vastly superior to write new code and recompile into the arduino every time  you add new devices or if you want the device to behave differently.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4781
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but what types of devices will be limited by what the original code can support.

Which is why I suggested adding intelligence to your add-on devices. You will still need multiple classes but at least they can be generic. You'd be able to add non-I2C devices as well.

Last time I bought Tiny85 chips they ran me $1.34 ea for 10. 328P's were about $2.50 ea, I got 4.

Just let me know if you can make it hot-plug!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the input guys. I'll keep thinking about it. The goal is to create an intelligent plug and play system, easy for users, not necessarily easy to the developer.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7176
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino can certainly use OOP and you can do "new" as pointed out already. But bear in mind you only have 2K SRAM on arduino so don't get too comfy with OOP stuff. I would rather instantiate a fixed number of different objects and reuse them. If you get too excited and instantiate too many objects, you may run out of memory. Just my 2 cents.
Logged


0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did something similar for a system where users could arbitrarily add model trains to a layout.

Different manufacturers' trains used different control code, so I implemented a virtual control function in a base class, and had code specific to each system in derived classes.  The system would then dynamically create and object of the desired class when it detected the addition of a new train.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino can certainly use OOP and you can do "new" as pointed out already. But bear in mind you only have 2K SRAM on arduino so don't get too comfy with OOP stuff. I would rather instantiate a fixed number of different objects and reuse them. If you get too excited and instantiate too many objects, you may run out of memory. Just my 2 cents.

Thanks, I'll keep that in mind. I'm no stranger to memory management.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did something similar for a system where users could arbitrarily add model trains to a layout.

Different manufacturers' trains used different control code, so I implemented a virtual control function in a base class, and had code specific to each system in derived classes.  The system would then dynamically create and object of the desired class when it detected the addition of a new train.

Thanks a bunch. So my hunch is on the right track.
Logged

Pages: [1]   Go Up
Jump to: