-->
Page 1 of 2

Universal I/O function

PostPosted: Tue Oct 27, 2015 7:46 pm
by Mmiscool
Hello,

I have been working on ESP8266 basic for some time now and one of the goals is never to have to declare what pin is to be used or the type of input or output before using it.

Currently I have implemented some hokey code to do this but think it could be improved with a universal function.

The idea would be to have a global array that catalogs what a pin is set as (digital read, digital write, pwm out, pwm in, servo, ect) and the last value sent to that pin (high/low, servo degrees, pwm , ect)


I am a bit foggy as to how to take a pin out of an existing state. With servos you can atach() and detach() them. With PWM out it seems tat you have to set it to 0 before doing a digital write or re purposing the pin for servo out put. Because it is an interpreter it must be able to do this on the fly as needed.

Is any one familiar enough with how all this works to help out a bit in creating such a function. It would be a tremendous help.

Hopefully this could be extended to handle things like tone and other arduino i/o functions as time goes on handling all the code to disengage a previous pin setting or request from a pin if necessary.

Some thing like:
int PinManagmentVar[20][2];

Code: Select allfloat UniversalPinIO(String PinCommand, byte pin, float PinValue)
{
 ....
}


to be able to be called in a similar fashion as follows:

Code: Select allbla = UniversalPinIO("pwmin",pin,0)   // return the value from the pin
bla = UniversalPinIO("pwmout",pin,0)     // Set the pin to the desired value and return the value sent
bla = UniversalPinIO("digitalwrite",pin,0)    // Set the pin to the desired value and return the value sent.
bla = UniversalPinIO("digitalread",pin,0)     // return the value from the pin
bla = UniversalPinIO("servo",pin,90)    //set servo to 90 degrees and return the value sent

Re: Universal I/O function

PostPosted: Wed Oct 28, 2015 4:36 pm
by Mmiscool
Could really use some help on this. Would be greatly appreciated.

Re: Universal I/O function

PostPosted: Wed Oct 28, 2015 5:14 pm
by Me-no-dev
something like this?
I imagine this is not what you are asking. Can you please explain in detail how and what you want to achieve ?
Code: Select allfloat UniversalPinIO(String PinCommand, byte pin, float PinValue)
{
if(PiCommand == "pinMode") pinMode(pin, PinValue);
elif(PiCommand == "digitalWrite") digitalWrite(pin, PinValue);
elif(PiCommand == "digitalRead") return digitalRead(pin);
elif(PiCommand == "analogRead") return analogRead(pin);
elif(PiCommand == "analogWrite") analogWrite(pin, PinValue);
elif(PiCommand == "servoWrite") servoWrite(pin, PinValue);
return 0xFF;
}

Re: Universal I/O function

PostPosted: Wed Oct 28, 2015 5:33 pm
by Mmiscool
The goal is a function that can be called and regardless of what the pin was previously being used for whether it be a servo, pwm in/out, or digital write/read.

The user of the function should never have to declare the pin mode before setting or requesting a value from a pin. Only the value they want it set at. If they are reading the pin it should return the value read from the pin.

If the pin was previously used it should be able to cleanly change to another use. I.e. go from being used for a servo to being used for pwm to being used for digital i/o and back again with one call to the function.


The code you have above looks to be a good start. The main bit would be making sure the pin is set up properly before using the out of the box arduino commands like you have in the function in your post.