Pages: [1]   Go Down
Author Topic: not_assigned_pin  (Read 595 times)
0 Members and 1 Guest are viewing this topic.
Argentina
Offline Offline
Full Member
***
Karma: 0
Posts: 188
Sé feliz
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Regards,
Juilán
http://robotgroup.com.ar
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 205
Posts: 12844
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How about make this a value available in the Arduino Core?
Where else will the constant be used?

Quote
Is there something like this and I did not see it?
I'm not aware of anything like it.

Logged

Argentina
Offline Offline
Full Member
***
Karma: 0
Posts: 188
Sé feliz
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset



Quote
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 smiley

Regards,
Julián



Logged

Pages: [1]   Go Up
Jump to: