Go Down

Topic: Arduino the Object Oriented way (Read 36209 times) previous topic - next topic

PaulMurrayCbr

Some changes made. I have a couple of annoying writing ticks: overuse of 'of course', saying "note that X" rather than simply saying "X". And I included a link to a relevant wikipedia page.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

msssltd

Genuinely don't know what you had in mind when you wrote that.
"Careful with that axe, Eugene(!)" Is a lengthy instrumental number by Pink Floyd.  The track was later reworked and re-named, "Come in number 51. Your time is up!"  Both titles refer to self indulgence.

What I had in mind was the irony, of attributing prejudices to stereotypes.


Robin2

#32
Jan 03, 2016, 01:49 pm Last Edit: Jan 03, 2016, 01:51 pm by Robin2
Apologies for my musical ignorance. I may recognize it if I heard it - but I am very poor at titles.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

lennarthennigs

Hey Paul,
thx for the article. I read through it and found it really helpful. I've been rewriting some of my Arduino files and created my first few classes.

I have a question though on constructor vs setup() use. Could you please give me some more details/examples on what can be put inside the constructor of a class and what should be part of a setup() method?

Cheers
Lennart

PaulMurrayCbr

I have a question though on constructor vs setup() use. Could you please give me some more details/examples on what can be put inside the constructor of a class and what should be part of a setup() method?
setup() for anything that calls methods from the arduino liobrary - anything to do with hardware initalisation. pinMode() is the most common thing.

constuctor for stuff that is "pure" C++. Especially for setting const variables.

The point to remember is that the Arduino environment does quite a bit of work before setup() gets called. I don't know what, exactly, but you can't rely on the hardware state being reasonable until then.

Constructors get called ... actually, I don't know when constructors get called. It may even be the case that thinks like assignments to const variables are pulled out by the compiler and baked into the binary image - they never really get executed at all.

I mean … maybe I'm wrong. Maybe it would all be perfectly ok to put things in constructors. But then again, maybe not.

For example, it's common for things like the LCD object provided by a library to have a constructor to assign the pins, but a separate initialize() method to be called in setup().
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Coding Badly

Constructors get called ... actually, I don't know when constructors get called.
Early.  Well before anything Arduino has run.

For example, it's common for things like the LCD object provided by a library to have a constructor to assign the pins, but a separate initialize() method to be called in setup().
Correct design.


westfw

#36
Oct 08, 2019, 08:37 am Last Edit: Oct 08, 2019, 09:17 am by westfw
Quote
Quote
Constructors get called ... actually, I don't know when constructors get called.
Early.  Well before anything Arduino has run.
In the compiler-added startup code, after "initialized data" is copied from flash into RAM and uninitialized data is cleared, and before main() is called.

PaulMurrayCbr

Early.  Well before anything Arduino has run.
In the compiler-added startup code, after "initialized data" is copied from flash into RAM and uninitialized data is cleared, and before main() is called.
IIRC, C++ destructor semantics are difficult to navigate when you have multiple virtual inheritance and things like that. The best practice seems to be not to write things that rely on clever destructor behaviour.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up