WiringProject / Wiring

Wiring Framework
http://wiring.org.co/
Other
217 stars 168 forks source link

Allow pin numbers beside channel number for analogRead(). #11

Open kroimon opened 12 years ago

kroimon commented 12 years ago

I propose to change the values of the A0, A1, ... constants to be the pin number, not the ADC channel number. This way you can do things like digitalRead(A0); where you would otherwise have to find out the pin number of the pin labeled "A0" on your board.

The patch should be completely backwards-compatible.

mbolivar commented 12 years ago

+1.

For what it's worth, LeafLabs did exactly this in libmaple years ago, and we're glad we did.

We keep "D0", "D1" around for historical purposes, and also because it's convenient to use them when you want to unambiguously refer to a pin, but pins are just numbered from 0 to n, and that's it.

This is really convenient considering that on a lot of MCUs, tons of pins are capable of analog input, and it makes layout way easier to allow gaps between analog pins.

tochinet commented 12 years ago

I believe Arduino did this also long time ago. In 0022, I do things like the following all the time.

define TempPin A0

Temp=AnalogRead (TempPin);

On the other hand, I always used

define Switch1 4

Define BlueLed 13

And not "D4" or "D13"

In addition, on the MEGA there are pins way beyond 20 (to 54) and I checked that the analog pins 0-15 are defined as 54-69. That's the way it should be everywhere IMHO.

tochinet commented 12 years ago

One second thought though : how can this be "backwards compatible" at all ? I mean, how can this NOT break the previous uses of A0-A5 in Wiring if we change the values ? Does anyone have a trace of where is this used ?

In any case, it should be clearly flagged in the history file "revisions.txt"

kroimon commented 12 years ago

Well... until now, the Ax constants could only be used correctly with the analogRead() function, so I don't think they are used elsewhere. The old values of the constants still work as expected, so in case someone has the old constant values stored somewhere (e.g. in EEPROM) they still work.

This patch would make Wiring code a little more compatible with Arduino sketches which made this change long time ago. It also makes it a lot more intuitive to read digital values from the analog pins as you don't have to remember two different pin namings for one single pin.