pinMode in loop for analog and digital

I would like to do:

get pin and isDigital IOmode through browser

if (isDigital) {  // do this for the digital pins
   if  (IOmode=="INPUT"){
      pinMode(pin,OUTPUT)
  }
  else
      pinMode(pin,INPUT)
  }
else // same for ANALOG... what is different here?
   if  (IOmode=="INPUT"){
      pinMode(pin,OUTPUT)
  }
  else
      pinMode(pin,INPUT)
  }


how do I do pinMode for analog or digital?
the examples have 
pinMode(A0,OUTPUT)
or
pinMode(D1,OUTPUT)

what are A1 and D1 … are these integers…

how do I do this with parameters?

thx, any help welcome

KW

I haven't checked if the structure PIN_MAP actually is accessible for user code, but if it is you could check this way

	if(PIN_MAP[pin].pin_mode == OUTPUT)

And yes D0/A0 are integers and they are defined in spark_wiring.h and also documented under "Hardware Datasheet - Pins and I/O"
D0..7 is 0..7 and A0..7 is 10..17

Since you are doing exactly the same thing for isDigital as for else, you would not need to distinguish between them.

What do you mean with

In your code you already are doing it with parameters. pin, OUTPUT, INPUT are already parameters for the function call of pinMode() and this way you'd do it with your own functions.

There is a type for the different modes defined in spark_wiring.h, too:

typedef enum PinMode {
  OUTPUT,
  INPUT,
  INPUT_PULLUP,
  INPUT_PULLDOWN,
  AF_OUTPUT_PUSHPULL,	//Used internally for Alternate Function Output PushPull(TIM, UART, SPI etc)
  AF_OUTPUT_DRAIN,		//Used internally for Alternate Function Output Drain(I2C etc). External pullup resistors required.
  AN_INPUT  			//Used internally for ADC Input
} PinMode;

Have a look in the "Open Source" section

ok thanks, that answers all my questions:

i guess then the else section for analog would look like:
else
if (IOmode==“INPUT”){
pinMode(pin+10,OUTPUT)
}
else
pinMode(pin+10,INPUT)
}

with pin =0…7 for digital and analog
and isDigital =true if its a “D” and false other wise

kw

How would you test this

Where would you get the D from?
When you call pinMode(A5, OUTPUT); you actually call pinMode(15, 0); since the A5 is a preprocessor makro #define A5 (15) and OUTPUT is the first element in the enum PinMode and usually has the index 0.

One of the shortest ways to write what you do above would be

  pinMode(pin, (PIN_MAP[pin].pin_mode == INPUT) ? OUTPUT : INPUT);

But this does not really fit your purpose stated above

What do you actually want to achieve and what for? This might make it easier to help to actually do this.

One other idea for you @kw123 is to read the code in Tinker the startup app for the core. It does the kind of things you want to already so you can get ideas from it. It uses Arduino String objects instead of C char arrays, but the ideas are the same.

Please be careful with the enum for INPUT and OUTPUT–there was pull request to make the numeric values for those change to be identical to Arduino. I don’t know if that pull request is in or not right now.

https://github.com/spark/firmware/blob/master/src/application.cpp

Thanks for the pointers.

here my solution (just that section)works but not as elegant…

int theCount        =0;    
server.print("{");
while (server.readPOSTparam(name, NAMELEN, value, VALUELEN)){
    theCount      +=1;
    failedCMD=true;
    itoa(theCount,theCountChar,10);
    if (theCount >1)    strcpy(theString,",\"");
    else                strcpy(theString,"\"");
    strcat(theString,theCountChar);
    strcat(theString,"\":{");
    char *theComma = strchr(value,',');
    if (theComma == NULL) {
        strcat(theString,"\"badinput\":\"");
        strcat(theString,name);
        strcat(theString,value);
        strcat(theString,"\"}");
        server.print(theString);
    }
    else {
        strcat(theString,"\"type\":\"");
        if (value[0] == 'D') {
            isDigital = true;
            strcat(theString,"D");
        }
        else {
            isDigital = false;
            strcat(theString,"A");
        }
        strcat(theString,"\",\"cmd\":\"");
        strcat(theString,name);
        strcat(theString,"\"");

        strncpy(pinNum,value+1,(int)(theComma-value-1));
        strncpy(pinName,value,2);
        strcpy(pinValue,theComma+1);
        pinNumber = atoi(pinNum);
        pinValueInt = atoi(pinValue);
        strcat(theString,",\"pin\":\"");
        strcat(theString,pinName);
        strcat(theString,"\"");

        if (strcmp( name, commandRead ) == 0 ){
            if (isDigital)  valRead = digitalRead(pinNumber) ;
            else            valRead = analogRead(pinNumber+10);
            itoa(valRead,valReadChar,10);
            strcat(theString,",\"value\":\"");
            strcat(theString,valReadChar); 
            strcat(theString,"\"");
            failedCMD = false;
        }
        if (strcmp( name, commandWrite ) == 0 ){
            if (isDigital) digitalWrite(pinNumber, pinValueInt);
            else           analogWrite(pinNumber+10, pinValueInt);
            failedCMD = false;
        }
        if (strcmp( name, commandSet ) == 0 ){
            if  (strcmp(pinValue,modeINPUT)==0){
                if (isDigital) pinMode(pinNumber, INPUT); 
                else           pinMode(pinNumber+10, INPUT); 
                failedCMD = false;
            }
            if  (strcmp(pinValue,modeOUTPUT)==0){
                if (isDigital) pinMode(pinNumber, OUTPUT); 
                else           pinMode(pinNumber+10, OUTPUT); 
                failedCMD = false;
            }    
        } 
    }
    if (failedCMD) strcat(theString,",\"status\":\"failed");
    else           strcat(theString,",\"status\":\"OK");
    strcat(theString,"\"}");
    Serial.println(theString);
    server.print(theString);
}    
server.print("}");

and with curl -d “READ=A1,1” --max-time 5 "http://192.168.x.y/parsed.html"
and produces
{“1”:{“type”:“A”,“cmd”:“READ”,“pin”:“A1”,“value”:“4090”,“status”:“OK”}}
http return

that all works fine … but just for 30 minutes than the web server stop responding

the loop is still running.

I can reproduce that if the wifi connection gets lost() web server immediately stops and does not come back.
(
)e.g.: stop wifi on router and restart wifi

Is there a way to restart the server

thx so much for your help.

everything works but its not stable!!

KW

That works fine in the code

thx

KW