Get the number of arguments passed to a function

11 Oct 2010

the function

void OneWireDevice ( PinName DataPin, PinName PowerPin)

I'd like to call this with only the data_pin or with both pins. But if it's called with just the data_pin I need to know that the power_pin is not being used. I googled around and found _NARGS returns then number of arguments sent to the function.
But, not with this complier.

ParasitePower = false;
if (_NARGS==2) {
ParasitePower = true;
}


11 Oct 2010

Do it like this:

void OneWireDevice ( PinName DataPin, PinName PowerPin = NC)
{
  bool ParasitePower = false;
  if ( PowerPin != NC )
  {
    ParasitePower = true;
  }
  [...]
}

[...]

OneWireDevice (p1); // just data; power pin becomes NC
OneWireDevice (p3, p4); // both pins are valid

 

11 Oct 2010

Thank you.

After a few more functions I'll be converting the code to a class.

12 Oct 2010

Does that only work for C++, or is it possible with C as well?

12 Oct 2010

Default arguments is a C++ feature, but you can always pass NC explicitly.

12 Oct 2010

void OneWireDevice ( PinName DataPin, PinName PowerPin = NC)

OneWireDevice (p1); // just data; power pin becomes NC

-----------------
What happens if I associate an I/O with a NC pin? 
DigitalOut ParasitePower(PowerPin); 

Will it just ignore things like

ParasitePower.write(0); // or the other format of
ParasitePower = 1; 

I'm thinking that if the complier ignores them I don't need to see if the 
flag is set before setting the output.

-----------------------
P.S. Do you know any good documentation on creating classes and objects and libraries?
   

12 Oct 2010

Hi Michael,

Take a look at http://mbed.org/cookbook/Writing-a-Library. Hopefully this gives a good start. Feel free to add to it once you've gone through the process with any tips to help others!

Simon

07 Nov 2010

There is one other minor issue on my list.

Sometime I want to define the class with one pin, other times two pins. The second pin is only needed when the device is in the parasite power mode (ie, it steals it's Vcc power from the dataline).

DS1820 probe1(p27,p28); // when it using parasite power or
DS1820 probe1(p27); // when it's externally powered

I have this code, ir compiles ok, but then seems to lock up the program in run mode

DS1820.h
DS1820(PinName dp, PinName pp=NC);

DS1820.cpp
DS1820::DS1820 (PinName dp, PinName pp) : _datapin(dp), _parasitepin(pp) {
if (_parasitepin == NC)

_parasite_power = false;
else
_parasite_power = true;
}

I'm thinking that     if (_parasitepin == NC) is attempting to use the DigitalInOut shortcut and it's actually attempting to read the port value and not the default NC assigned in the header. Is there some hidden (not in the documentation) variable that will return the pin number (or NC) of the I/O

 

07 Nov 2010

Try this:

DS1820.h
DS1820(PinName dp);
DS1820(PinName dp, PinName pp);

DS1820.cpp
DS1820::DS1820 (PinName dp, PinName pp) : _datapin(dp), _parasitepin(pp) {
_parasite_power = true;
}
DS1820::DS1820 (PinName dp) : _datapin(dp) {
_parasite_power = false;
}

You create two versions of the function with the same name. One version takes one parameter, the other two parameters. Internally, a function is identified by its name and its variables' "footprint" so the use of the same function name is unambiguous. This is also true for the constructor.

I think the lock up is occurring because even though the pin isn't explicitly referenced, it will be implicitly referenced by your code as written.

Harry

07 Nov 2010

Thanks, that got me pointed in the right direction.

It did generate the error
"No default constructor exists for class "mbed::DigitalOut" (E291)" in file "DS1820/DS1820.cpp"

So I changed it to this
DS1820::DS1820 (PinName dp, PinName pp) : _datapin(dp), _parasitepin(pp) {
_parasite_power = true;
}

DS1820::DS1820 (PinName dp) : _datapin(dp), _parasitepin(NC) {
_parasite_power = false;
}

Now it complies and runs. It's now time to go find a MOSFET and test the parasite power mode.