7 years ago.

Pass pin number to class constructor to create private DigitalOut member

How would one go about writing a class whose constructor takes a pin number and creates a DigitalOut object for that pin number?

AFAIK, when creating a DigitalOut object you must know the pin number to be associated with an object when you declare the DigitalOut object .

For example, a regular usage of DigitalOut object would go like this:

regular creation of a DigitalOut object

#include "mbed.h"

DigitalOut myOutPin(targetPinNumer);

int main(){
    while(1){
        myOutPin = !myOutPin.read();
    }
}

That's pretty straightforward. By how do I move the DigitalOut object to be a member of a class?

In-class creation of a DigitalOut object

#include "mbed.h"
class customOutPin(){
    public:
        customOutPin(int targPin){
            outPin..... <--- there is no member of a DigitalOut object for assigning a pin to that object!   
        }
        void twiddle(){
            outPin = !outPin.read();   
        }
        
    private:
        DigitalOut outPin(*need to know pin number!*); <----    
    
}

#define OUT_PIN_TO_PASS_TO_CLASS PA_9

customOutPin myOutPin(OUT_PIN_TO_PASS_TO_CLASS);

int main(){
    while(1){
        myOutPin.twiddle();
    }
}

1 Answer

7 years ago.

Hello,
One solution could be as follows:

#include "mbed.h"

class CustomOutPin
{
public:
    CustomOutPin(PinName targetPin) :  _outPin(targetPin) {}
    void    twiddle(void) {
        _outPin = !_outPin.read();
    }
private:
    DigitalOut  _outPin;
};

#define OUT_PIN_TO_PASS_TO_CLASS    LED1
CustomOutPin  myOutPin(OUT_PIN_TO_PASS_TO_CLASS);

int main(void) {
    while(1) {
        myOutPin.twiddle();
        wait_ms(500);
    }
}


Another solution using pointer to DigitalOut as private member:

#include "mbed.h"

class CustomOutPin
{
public:
    CustomOutPin(PinName targetPin) : _outPin(new DigitalOut(targetPin)) {}
    ~CustomOutPin(void) { delete _outPin; }
    void    twiddle(void) {
        _outPin->write(!_outPin->read());
    }
private:
    DigitalOut*  _outPin;
};

#define OUT_PIN_TO_PASS_TO_CLASS    LED1
CustomOutPin  myOutPin(OUT_PIN_TO_PASS_TO_CLASS);

int main(void) {
    while(1) {
        myOutPin.twiddle();
        wait_ms(500);
    }
}

First solution is the normal way to do it. Second solution is a bit weird. If you want to use new operator, have a look at for example this, at line 3100: https://developer.mbed.org/users/wim/code/TextLCD/file/111ca62e8a59/TextLCD.cpp

In general however if you have no good reason for it, I would advice against using 'new'. Because in this example you created a potential memory leak by not defining a constructor where 'delete' is called ;). So just go for option 1 in general.

posted by Erik - 23 Mar 2017

I agree with Erik that the second solution is not a usual approach. Although it is not utilized in this example (the global object myOutPin is going to be destroyed only when you disconnect the power supply from the board), for the sake of completeness I have added also a destructor.

If for some reason (memory usage optimization) you would like to create the CustomOutPin object on the heap then you can do it for example as below:

#include "mbed.h"
 
class CustomOutPin
{
public:
    CustomOutPin(PinName targetPin) : _outPin(new DigitalOut(targetPin)) {}
    ~CustomOutPin(void) { delete _outPin; }
    void    twiddle(void) {
        _outPin->write(!_outPin->read());
    }
private:
    DigitalOut*  _outPin;
};
 
#define OUT_PIN_TO_PASS_TO_CLASS    LED1
 
int main(void) {

    CustomOutPin*  myOutPin = new CustomOutPin(OUT_PIN_TO_PASS_TO_CLASS);
 
    while(1) {
        myOutPin->twiddle();
        wait_ms(500);
    }
    
    delete myOutPin;    // Just for illustration. Never called in this example.
}
posted by Zoltan Hudak 23 Mar 2017