I want to be able to have my program create objects at runtime, depending on an input. I've tried the program below, but it doesnt work.
I think it is because the DigitalOut objects only exist inside the code blocks they are created in, but I don't know how to make them global or persistent.
Hi,
I want to be able to have my program create objects at runtime, depending on an input. I've tried the program below, but it doesnt work.
I think it is because the DigitalOut objects only exist inside the code blocks they are created in, but I don't know how to make them global or persistent.
Suggestions?
Thanks in advance.
<<code>>
#include "mbed.h"
DigitalIn button(p20);
int main() {
if (button) {
DigitalOut myled(LED1);
} else {
DigitalOut myled(LED4);
}
while (1) {
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
<</code>>
I think the example I gave was too trivial, what I'm really trying to do is :
#include "mbed.h"
#include "MSCFileSystem.h"
DigitalIn button(p20);
int main() {
if (button) {
LocalFileSystem fs("fs");
} else {
MSCFileSystem fs("fs");
}
FILE *fp = fopen("/fs/file.txt","r");
while (1) {
// Do somehting with contents of file.
}
}
In this scenario, I dont think I can have an array, because the the two objects are different types?
Thanks
Hi Igor,
Thanks, thats great!
I think the example I gave was too trivial, what I'm really trying to do is :
<<code>>
#include "mbed.h"
#include "MSCFileSystem.h"
DigitalIn button(p20);
int main() {
if (button) {
LocalFileSystem fs("fs");
} else {
MSCFileSystem fs("fs");
}
FILE *fp = fopen("/fs/file.txt","r");
while (1) {
// Do somehting with contents of file.
}
}
<</code>>
In this scenario, I dont think I can have an array, because the the two objects are different types?
Thanks
int main() {
LocalFileSystem *lfs = NULL;
MSCFileSystem *mfs = NULL;
if (button) {
lfs = new LocalFileSystem("fs");
} else {
mfs = new MSCFileSystem("fs");
}
// do stuff
if ( lfs )
delete lfs;
if ( mfs )
delete mfs;
}
You can use two distinct pointers in this case.
<<code>>
int main() {
LocalFileSystem *lfs = NULL;
MSCFileSystem *mfs = NULL;
if (button) {
lfs = new LocalFileSystem("fs");
} else {
mfs = new MSCFileSystem("fs");
}
// do stuff
if ( lfs )
delete lfs;
if ( mfs )
delete mfs;
}
<</code>>
Important Information for this Arm website
This site uses cookies to store information on your computer.
By continuing to use our site, you consent to our cookies.
If you are not happy with the use of these cookies, please review our
Cookie Policy
to learn how they can be disabled.
By disabling cookies, some features of the site will not work.
Access Warning
You do not have the correct permissions to perform this operation.
Hi,
I want to be able to have my program create objects at runtime, depending on an input. I've tried the program below, but it doesnt work.
I think it is because the DigitalOut objects only exist inside the code blocks they are created in, but I don't know how to make them global or persistent.
Suggestions?
Thanks in advance.