arduino-nRF5
arduino-nRF5 copied to clipboard
analogRead() should be able to map inputs 0-to-x as A0-to-Ax
The default Arduino analogRead() can take 2 different values for each ADC input:
- The "analog pin" value, which goes from 0 to NUM_ANALOG_INPUTS, which in most Arduino Uno compatible boards would be 0 to 5:
analogRead(0); analogRead(1); ... analogRead(5); - The pin number, in it's raw "arduino pin" form or
Axglobal variable:
Which in boards like the Uno would be the equivalent of:analogRead(A0); analogRead(A1); ... analogRead(A5);analogRead(14); analogRead(15); ... analogRead(19);
This is usually done by simply checking the range of the input argument, so in the case of the Uno the analogRead() function would have something along the lines of:
analogRead(uint8_t pin) {
if (pin < A0) {
pin += A0;
}
}
That works well because these boards are configured to have the analog pins mapped immediately after the digital pins and in successive order.
This Arduino core has variants with analog pin mappings that do not follow this convention, and so only "method 2" listed above has been implemented.
This breaks compatibility with any arduino sketches using "method 1", so we should work out a method to map these analog pins in the variant files in a way that allows both methods to work as expected.
@carlosperate I agree, could you submit a PR for this?
Maybe we can have a #define for non-compatible variants - the ones with non-Arduino like pin definitions to disable this option. What do you think?
Maybe I can put something together in a couple of days, depends on availability, and how much work left there might be for the other PRs.
I think we need to do a pin conversion similar to if (pin < A0) pin += A0;, but since this would be specific to each board, a macro/function would have to be defined per variant.
So, in essence, if the input is < NUM_ANALOG_INPUTS, then we'd apply this "variant dependant" macro/function, and if not then just continue as it is.
I was thinking something super simple like:
#ifndef ARDUINO_NON_STANDARD_ANALOG_PINS
if (pin < A0) {
pin += A0;
}
#endif
Then the non-standard variants like the generic, TinyBLE, nRF51 beacon (in the future) can have a #define ARDUINO_NON_STANDARD_ANALOG_PINS in the variant header. What do you think? I can prep a PR if this is ok.
Yes, something like that would do the trick, but then it would leave the variants with a "non-standard analog pin distribution" with the same problem as before.
We could do this, in the variant file:
#define ARDUINO_NON_STANDARD_ANALOG_PINS
static __inline__ uint32_t AnalogPinConver(uint32_t pin) {
// map pins
return pin;
}
And analogWrite:
#ifdef ARDUINO_NON_STANDARD_ANALOG_PINS
pin = AnalogPinConver(pin);
#else
if (pin < A0) {
pin += A0;
}
#endif
That's a good suggestion!
To confirm, AnalogPinConvert(0) returns the pin associated with A0?
@dlabun @jacobrosenthal any opinions on this?
yeah, the AnalogPinConver() function would have a lookup table for that conversion, so as you say input '0' would return the pin set for 'A0'.
The proposed solution discussed above can be found at #150.