Go Down

Topic: Return Value For Pin Mode? (Read 464 times) previous topic - next topic

cbartholomew

Dear All

I'm writing a library for my future students, and it's going to have specific check points, like digital read == high.

My question is this: is there a way to know what pinMode is set to for a specific pin?

For example, we set pin mode using pinMode(pin, DIRECTION). The docs say, "returns nothing" - I was hoping we or anyone created a getter like: getPinMode(pin), returns null if not set?

Yes yes, if the pin isn't set - the student should know this - but I would like to at least write a little message like, "pinMode not set?"

Thanks Everyone

Arrch

You could read the modes from the ports, but you would have to map the Port/bit to their respective pins.

Another option would be to "override" the function and store the modes in a variable. An explanation of "overriding" it:

http://stackoverflow.com/questions/617554/override-a-function-call-in-c

Jack Christensen


For example, we set pin mode using pinMode(pin, DIRECTION). The docs say, "returns nothing" - I was hoping we or anyone created a getter like: getPinMode(pin), returns null if not set?


Pins always have a mode, regardless of whether pinMode() was called. When the MCU is reset (or powered up), all pins are initially set to INPUT.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

MarkT

The Arduino code is open source - if you look in <yourinstall>/hardware/arduino/core/arduino/wiring_digital.c you can see the code for pinMode().

It looks feasible to alter this to return the previous mode - perhaps suggest this to the Arduino team as a future improvement?  Or if you feel capable take a backup and experiment with your own version. 

You are right it would probably be best to add a new function getPinMode() to sense the current mode without changing it.

Another useful functionality might be a call to "register" control of a pin - so that conflicting libraries can automatically detect the conflict and return an error code at start up?   Some other library may think it owns a pin whatever the pinMode.  Something like pinUsed() and markPinUsed().
[ I won't respond to messages, use the forum please ]

Jack Christensen

#4
Aug 12, 2012, 04:22 am Last Edit: Aug 12, 2012, 04:26 am by Jack Christensen Reason: 1
Here is a possible implementation of getPinMode() which is a modification of digitalRead(), but instead of reading the input register (PINx), it reads the mode (data direction) register (DDRx).

Disclaimer: Quick and dirty, limited testing on an Uno, critics please have at it XD

Code: [Select]
#include <Streaming.h>    //http://arduiniana.org/libraries/streaming/

void setup(void)
{
    Serial.begin(115200);
    //set a few pins as output
    pinMode(2, OUTPUT);
    pinMode(13, OUTPUT);
    pinMode(A5, OUTPUT);
   
    //print the pins and their data direction
    for (int pin=0; pin<NUM_DIGITAL_PINS; pin++) {
        Serial << "Pin " << (pin<10 ? " " : "") << _DEC(pin);
        Serial << (getPinMode(pin) ? " OUTPUT" : " INPUT") << endl;
    }
}

void loop(void)
{
    //might as well actually use one of those output pins
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
}

int getPinMode(uint8_t pin)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);

if (port == NOT_A_PIN) return LOW;

if (*portModeRegister(port) & bit) return HIGH;
return LOW;
}
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Graynomad

Quote
Another useful functionality might be a call to "register" control of a pin - so that conflicting libraries can automatically detect the conflict and return an error code at start up?

That's similar to what I'm doing with some code I'm writing at the moment. It works well especially when there are non-obvious clashes, for example you use PWM and later try to set the same pin as an input.

In a nutshell any multiple mode changes to a pin are flagged, you can however mask pins if you expect to change their mode more than once.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Nick Gammon


Yes yes, if the pin isn't set - the student should know this - but I would like to at least write a little message like, "pinMode not set?"


Why is this important? Since pins default to input, you could simply make your own function that remembers the last time you set a mode and what you set it to.
http://www.gammon.com.au/electronics

GoForSmoke

It's not like you are setting the pins through an OS, you might as well check bit shifts or multiplies to see if they were carried out.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

cbartholomew

#8
Aug 12, 2012, 06:07 pm Last Edit: Aug 12, 2012, 06:11 pm by cbartholomew Reason: 1
Wow! Thank you all for your responses. This is great. I didn't know the code was open source; moreover, the example for the uno should work well (my class is using uno's as well). Man, this is awesome - thanks again for all the feedback and info.

It's important because I'm really limited to the amount of time that I have for "in-class" implementations. So, if I can at least say, "hey, this is a problem - go fix it" - it gives the student a more focused on approach on what needs to be solved, but yea - this is a great idea too - just setting the pin up myself, and remembering its setting.... cool...

I fear that I'm not going to have enough time to run around and assist each student w/ his or her problem, so it's nice for me to make this library that I can run simple unit tests against the code - ensuring that the student is using the functions provided by the arduino library correctly.

anyway, this is awesome - I wanted to thank you all again for the great feedback!

Christopher.

 

Jack Christensen

Christopher, just so I understand, the intent is to provide a library (that includes getPinMode() probably among other things) that the students will include in their sketch, and that will be called at some point(s) to check various things?

Or is the intent to run checks against source code, on some other platform?

I think the function I provided should work with any board that the Arduino environment supports, since it calls the same support functions as digitalRead(), and these are designed flexibly to operate with different boards. But like I say, I only tested it on an Uno (and a clone at that.)

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up