Go Down

Topic: not_assigned_pin (Read 901 times) previous topic - next topic


Hi, I was working on a class which expect for some pin numbers on int's constructor, but it can be initialized with 1, 2, 3 or 4 pins, depending on what the user's needs.

So I realized to use a constructor with default values, insted of overloaded constructors. And then it appears to be needed a "not_assigned_pin" value, which coud be -1, since there are no negative pin numbers, and most Arduino classes use int as the type for their pins. The constructor then will be something like this:

       IR38KHz(const int input_pin,
               const int ouput0_pin,
               const int ouput1_pin = not_assigned_pin,
               const int ouput2_pin = not_assigned_pin,
               const int ouput3_pin = not_assigned_pin);

We will declare this constant in our lib as:

const int not_assigned_pin = -1;

which es better than a #define (due to the type checking that it allows to the C++ compiler).

But, now this will be only in our hedader. How about make this a value available in the Arduino Core?

Is there something like this and I did not see it?


Coding Badly

How about make this a value available in the Arduino Core?

Where else will the constant be used?

Is there something like this and I did not see it?

I'm not aware of anything like it.


Where else will the constant be used?

Well, in any class that needs optional number of pins. For example, our particular case is an IR sensor associated with IR beacons, and the number of beacons depends on the particular user's application.

Another possible app is a generic HBridge driver class, because there are 2 bits HBridges (like some discrete H Bridges and some ICs too), 3 bits (like the L293, L298, L6225).

Another way of doing the same thing is overloading the constructor, but this consumes more flash, and is more error prone, since there will be duplicated code (or you will need a common member function with the shared code between the constructors, which consumes flash too, and is slower and "less C++ like").

The default params are a great C++ tool, but without a constant for not assigned pins I don't see a standard way of doing it.

More: I think this costant can't be bad in any way for the existent cores and libraries, hey, is just a small constant :)


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131