Pages: [1]   Go Down
Author Topic: Return Value For Pin Mode?  (Read 436 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3440
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12463
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

[ I won't respond to messages, use the forum please ]

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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;
}
« Last Edit: August 11, 2012, 09:26:19 pm by Jack Christensen » Logged

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8596
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19065
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

  
« Last Edit: August 12, 2012, 11:11:05 am by cbartholomew » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)

Logged

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

Pages: [1]   Go Up
Jump to: