Retrieve current pin mode
There is currently no function to get the current pin mode. A possible implementation is:
int pinMode(uint8_t pin)
{
if (pin >= NUM_DIGITAL_PINS) return (-1);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg = portModeRegister(port);
if (*reg & bit) return (OUTPUT);
volatile uint8_t *out = portOutputRegister(port);
return ((*out & bit) ? INPUT_PULLUP : INPUT);
}
The return value is the pin mode or error code (-1) for illegal pin number.
Cheers!
Any news? it's very useful feature!
Only negative I can think of is the fact that for some variants the return type of portModeRegister() is of type volatile uint16_t *. On personal projects I have used the auto type specifier to fix this, but I don't know if Arduino is using c++11 by default. For now change I suggest changing it to uint16_t rather than uint8_t
No news on this one? I'd love to have it native! Thanks
No news on this one? I'd love to have it native! Thanks
If you (or anyone else for that matter) could provide a PR then we'd have something to discuss :wink: