6 years, 1 month ago.

Retrieve pinname from Mbed classes (like pwmout, digitalout ..etc)

What I found (I might be wrong) the pin name is saved in a protected struct. There is no way to retrieve the pin name when the class was defined earlier. I need that since I create the object during execution .. and I need to go back to that object and find out which pin name is used.

pwmout_t

is the responsible variable that I need to get the info from but it is protected.. why do mbed classes don't allow asking for pinname used in the class? I think I might do my own class or change the mbed-library to get that done .. Any idea?

1 Answer

6 years, 1 month ago.

I have run into similar issue before where it would be nice to read the settings from the peripheral. Yes, there appears to be no getter for the pin name in PwmOut.h. Several ways to solve this. Use the mbed source code in your project. Then add a very simple getter to the class yourself.

PinName getPinName() {
   return _pwm.pin;
}

If you don’t want to edit the source code, you can make your own PwmOut class that inherits from PwmOut.h. The pwm struct is protected so now your derived class will have access to it. You make your pwm objects of type MyPwmOut instead and can call the getPinName() function for that object.

#ifndef MY_PWM_OUT_H
#define MY_PWM_OUT_H

#include "mbed.h"

class MyPwmOut : public PwmOut{
public:
    /** Constructor
     */
    MyPwmOut(PinName pin) :
     PwmOut(pin) {}

    /** Get the Pin Name
        @returns
          PinName from PwmOut object
     */
    PinName getPinName() {
        return _pwm.pin;
    }

};

#endif

Other option of course is to just intercept the pin name wherever it is being loaded and make a shadow copy of it so you have it available.

Thank you all for your answers. But the question is more general.. Yes at the moment I need to get the pin name from a pwmout ..but generally it is good to allow that for all classes. There is no way at the moment without changing the source code.. The problem with changing source code is , you need to personally patch the mbed all the time .. It is more work to do. I hope mbed developers think about this issue. A better solution is required by mbed users.

posted by M J. 28 Mar 2018