vector of DigitalOuts?

21 Apr 2012

Hi all,

does anyone have a suggestion for storing a vector of DigitalOuts? I'm reading pin configuration at startup, so it's unknown how many DigitalOut pins will be connected at compile time.

The following doesn't compile, and my c++ is not good enough to understand why...

#include "mbed.h"
#include <vector>

vector<DigitalOut> outs;

int main() {

    outs.push_back( *(new DigitalOut( p17 )) );
    
    while(1) {
        // do whatever
    }
}

thanks, Paul

24 Apr 2012

store pointers

#include "mbed.h"
#include <vector>

vector<DigitalOut*> outs;

int main() {

    outs.push_back( (new DigitalOut( p17 )) );
    
    while(1) {
        // do whatever
    }
}

27 Apr 2012

Thanks for the reply...I guess I really need to go and study some basic c++ concepts, too long in Java and c#

What I ended up doing was wrapping the whole thing in a custom class (which I ended up needing anyway) with a private PinName and instantiating the DigitalOut as necessary. I don't know which one is more efficient, or even if it makes a difference, but I will try this solution if I need something similar in the future.