8 years, 1 month ago.

Function or Object

  1. include "mbed.h"

class Flasher { public: Flasher(PinName pin) : _pin(pin) { _pin(pin) means pass pin to the DigitalOut constructor _pin = 0; default the output to 0 }

void flash(int n) { for(int i=0; i<n*2; i++) { _pin = !_pin; wait(0.2); } }

In the ABOVE CODE .. what does this line , Flasher(PinName pin) : _pin(pin ) refers to. .is it a function declaration? Explain it.

1 Answer

8 years, 1 month ago.

Please use <<code>> and <</code>> when posting code, each on their own line, the system will then format the code correctly.

You didn't post the full code but I'm guessing that the class Flasher includes a private DigitalOut called _pin.

The line your are referring to is calling the digitalOut constructor with the parameter (pin) when the Flasher class object is created. This is standard c++ constructor syntax. Google c++ constructor member initialization for lots of examples and explanation.

Accepted Answer

<<code>> #include "mbed.h" <</code>>

<<code>> class Flasher { <</code>> <<code>> public: <</code>> <<code>> Flasher(PinName pin) : _pin(pin) <</code>> { // _pin(pin) means pass pin to the DigitalOut constructor <<code>> _pin = 0; <</code>> // default the output to 0 <<code>> } <</code>> <<code>> public: <</code>> <<code>> Flasher(PinName pin) : _pin(pin) <</code>> { // _pin(pin) means pass pin to the DigitalOut constructor <<code>> _pin = 0; <</code>> // default the output to 0 <<code>> } <</code>> <<code>> Flasher(PinName pin) : _pin(pin) <</code>> { // _pin(pin) means pass pin to the DigitalOut constructor <<code>> _pin = 0; <</code>> // default the output to 0 <<code>> } <</code>> { _pin(pin) means pass pin to the DigitalOut constructor <<code>> _pin = 0; <</code>> // default the output to 0 <<code>> } <</code>> default the output to 0 <<code>> } <</code>>

<<code>> void flash(int n) { <</code>> <<code>> for(int i=0; i<n*2; i++) { <</code>> <<code>> _pin = !_pin; <</code>> <<code>> wait(0.2); <</code>> <<code>> } <</code>> <<code>> } <</code>> <<code>> for(int i=0; i<n*2; i++) { <</code>> <<code>> _pin = !_pin; <</code>> <<code>> wait(0.2); <</code>> <<code>> } <</code>> <<code>> } <</code>> <<code>> _pin = !_pin; <</code>> <<code>> wait(0.2); <</code>> <<code>> } <</code>> <<code>> } <</code>> <<code>> wait(0.2); <</code>> <<code>> } <</code>> <<code>> } <</code>> <<code>> } <</code>> <<code>> } <</code>> <<code>> } <</code>>

<<code>> private: <</code>> <<code>> DigitalOut _pin; <</code>> <<code>> }; <</code>> <<code>> DigitalOut _pin; <</code>> <<code>> }; <</code>> <<code>> }; <</code>>

<<code>> Flasher led(LED2); <</code>> <<code>> Flasher out(p6); <</code>> <<code>> Flasher out(p6); <</code>>

<<code>> int main() { <</code>> <<code>> led.flash(5); <</code>> <<code>> led.flash(2); <</code>> <<code>> out.flash(10); <</code>> <<code>> } <</code>> <<code>> led.flash(5); <</code>> <<code>> led.flash(2); <</code>> <<code>> out.flash(10); <</code>> <<code>> } <</code>> <<code>> led.flash(2); <</code>> <<code>> out.flash(10); <</code>> <<code>> } <</code>> <<code>> out.flash(10); <</code>> <<code>> } <</code>> <<code>> } <</code>>

This is the code I'm referring to, its given as example for libraries in https://developer.mbed.org/cookbook/Writing-a-Library .

posted by Arun Joe 10 Mar 2016

Within the class Flasher there is a private DigitalOut _pin;

When creating a DigitalOut as a global you specify the pin when you create it. e.g.:

DigitalOut _pin(p6);

But when creating it as part of another class you can't do that because you don't know what the pin will be.

So instead when you create the main class (Flasher in this case) you tell the system to also call the constructor for _pin with the IO pin you want to use, that's what the : is doing. Constructors are a special type of function so in a way it is calling a function. This is completely standard c++, other than the class names none of this is mbed specific.

On the formatting

<<code>>
main() { // a comment
some code
}
<</code>>

not

<<code>> main() { // a comment <</code>>
<<code>> some code<</code>>
<<code>> }<</code>>

And you will get

main() {
  some code
}

instead of

<<code>> main() { // a comment <</code>> <<code>> some code<</code>> <<code>> }<</code>> <<code>> some code<</code>> <<code>> }<</code>> <<code>> }<</code>>

posted by Andy A 10 Mar 2016

Thank You for the Answer and hereafter I will follow the formatting as you said.

posted by Arun Joe 11 Mar 2016