Pages: [1]   Go Down
Author Topic: Multiple in/outputs from functions  (Read 210 times)
0 Members and 1 Guest are viewing this topic.
Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to make functions that can get in and put out data for multiple pins at once and try to make it a use able library function.

pins[] = {2,3,4,5,6};
DataOut[] = digitalMultiRead(pins[]);

Example, DataOut[] result => HIGH,LOW,LOW,HIGH,LOW

This is goes in the wiring_digital.c in the core files.
I can't test it myself right now, but im almost certain it WON'T work, so if anyone can try it and respond back that would be great.
If it doesn't work, that fine. I just wanted to try to make a function without having to make it in my actual sketch.

Code:
int digitalMultiRead(uint8_t pin[]) 
{   
  int i = cnt = 0; //fixed
  int Out[cnt]; //Edit, missed that
  while(pin[i] != NULL)i++;

for( cnt = 0; cnt <=i; cnt++)
{
   uint8_t timer[cnt] = digitalPinToTimer(pin[cnt]);
   uint8_t bit[cnt] = digitalPinToBitMask(pin[cnt]);
   uint8_t port[cnt] = digitalPinToPort(pin[cnt]);

   if (port[cnt] == NOT_A_PIN) return LOW;

   // If the pin that support PWM output, we need to turn it off
   // before getting a digital reading.
   if (timer[cnt] != NOT_ON_TIMER) turnOffPWM(timer[cnt]);

   if (*portInputRegister(port[cnt]) & bit[cnt]) Out[cnt] = HIGH;
   Out[cnt] = LOW;
    }
return Out[];
}
« Last Edit: January 11, 2013, 11:42:07 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Out[] variable is on a stack and when you exit the code, it is gone.

You should pass a pointer (Out) to the function or you could make Out[] static (a wasteful way of doing things).

btw, 'i'  is uninitialized too.

My suggestion would be to start simple and then go fancy if you want.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A basic framework for what you want to do would be this:

Code:
void digitalXRead(unsigned char * pin, unsigned char *pout, unsigned char pins) {
  while (pins) {
    pins -=1;
    pout[pins]=digitalRead(pin[pins]);
  }
}

You may be able to add additional features (returning -1 to indicate error for example), but the gist is there.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, Great! Thank you
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49049
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void digitalXRead(unsigned char * pin, unsigned char *pout, unsigned char pins) {
This makes some undocumented assumptions that you must be aware of before using this function.
Logged

Pages: [1]   Go Up
Jump to: