Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
Dynamicaly changing PinType
Hello to all,
I am wondering if it is possible to have names of pins stored in some array/or to be an array of names? Like declaring DigitalOut mypin(p20) - to be able to change mypin to something like mypin[0] - DigitalOut mypin[0](p20)?
I would like to create an array of pins, which can change their function based on variable's value. That include DigitalIn and Out, AnalogIn. If I say pinX.type == 1, then this would mean that pinX, which is let's say p18, to have AnalogIn function.
include the mbed library with this snippet
#include "mbed.h"
struct ARRAY {
PinName t;
int type;
float value;
char *name;
};
ARRAY io[3];
int main()
{
io[0].t = p18;
io[0].type = 1;
io[0].name = "px1";
if(io[0].type == 0) {
DigitalOut* px1 = new DigitalOut(io[0].t);
} else if(io[0].type == 1) {
DigitalIn* px1 = new DigitalIn(io[0].t);
} else if(io[0].type == 2) {
AnalogIn* px1 = new AnalogIn(io[0].t);
}
while(1) {
if(io[0].type == 0) {
*px1 = io[0].value; // DigitalOut px1(ios[0].t);
} else if(io[0].type == 1) {
io[0].value = *px1; // DigitalIn px1(ios[0].t);
} else if(io[0].type == 1) {
io[0].value = *px1.read; // AnalogIn px1(ios[0].t);
}
}
}
I have tried something like this. It works until I have only one declaration like: DigitalOut* px1 = new DigitalOut(io[0].t);. Then *px1 is declared OK. But when more conditions are added, compiler says *px1 is undefined.
Any thoughts whats wrong?
1 Answer
10 years, 4 months ago.
You have a scoping issue, if you declare a variable within an if statement then it is only valid within that if statement.
What would make more sense would be to store the pointers to the Input/Output class within your data structure. Since only one will be valid at a time you can use a union to save a bit of space. I would also use an enum for type rather than an int, it makes for more readable code and removed the risk of forgetting which magic number indicated which type of pin.
#include "mbed.h"
struct ARRAY {
PinName t;
enum {none, DigIn, DigOut, AnalogIn} type;
float value;
union {
DigitalOut *Dout;
DigitalIn *Din;
AnalogIn *Ain;
} pinPtr;
char *name;
};
ARRAY io[3];
int main()
{
io[0].t = p18;
io[0].type = DigIn;
io[0].name = "px1";
switch (io[0].type) {
case DigOut:
io[0].pinPtr.Dout = new DigitalOut(io[0].t);
break;
case DigIn :
io[0].pinPtr.Din= new DigitalIn(io[0].t);
break;
case AnalogIn:
io[0].pinPtr.Ain = new AnalogIn(io[0].t);
break;
default:
io[0].pinPtr.Ain = null; // since the memory location for the pointer is shared setting one to null sets them all to null.
break;
}
while(1) {
switch (io[0].type) {
case DigIn:
io[0].value = io[0].pinPtr.DIn->read(); // DigitalIn px1(ios[0].t);
break;
case DigOut:
*(io[0].pinPtr.DOut) = io[0].value; // DigitalOut px1(ios[0].t);
break;
case AnalogIn:
io[0].value = *(io[0].pinPtr.AIn); // AnalogIn px1(ios[0].t);
break;
default:
break;
}
}
}