Mbed : How to use protected memeber functions(API)

19 Apr 2018

Hi

I am begineer to Mbed Compiler & want to use PWM protected member functions like unlock_deep_sleep() but it's giving compile time error like : Function "mbed::PwmOut::unlock_deep_sleep" (declared at /extras/mbed-os.lib/drivers/PwmOut.h:197) is inaccessible.

I would be grateful if you could suggest for the same.

Regards Bharat Gopani

20 Apr 2018

Hello Bharat,

We are not allowed to call protected or private methods in application code. But we can derive a new class from the base one and use the protected methods or members of the base class within the derived class code. So you are allowed to design your own PwmOutDerived class and create public unlock_deep_sleep and lock_deep_sleep methods calling the base calass protected methods. Then you can call these public unlock_deep_sleep and lock_deep_sleep methods in the application code for example as below:

#include "mbed.h"

...

class PwmOutDerived : public PwmOut
{
public:
    PwmOutDerived(PinName pin) : PwmOut(pin) {}
    
    ...
    
    void    lock_deep_sleep()   { PwmOut::lock_deep_sleep(); }
    void    unlock_deep_sleep() { PwmOut::unlock_deep_sleep(); }
};

...

PwmOutDerived  myPwmOut(p21);

...

int main()
{
    ...

     while (1)
    {
        ...
        myPwmOut.unlock_deep_sleep();
        ...
        myPwmOut.lock_deep_sleep();
        ...
    }
}
23 Apr 2018

Hi Zoltan,

Thanks for the reply.

I tried this but it's giving compile time error , but anyway I want to know that whenever we enable PwmOut class, whether system goes into deep sleep mode or not.

Since when I enabled PwmOut class, system is crashed.

I would be grateful if you could help for the same.

Regards Bharat

23 Apr 2018

Quote:

I tried this but it's giving compile time error.

That's weird because the code compiles with no errors when I use the online compiler.
NOTE: Make sure you either substitute the periods (...) with your code or delete them from the program.

Quote:

I want to know that whenever we enable PwmOut class, whether system goes into deep sleep mode or not.

I don't think the system goes into deep sleep mode when PwmOut is used in a program.

Quote:

Since when I enabled PwmOut class, system is crashed.

Because no Nordic nRF51-DK board is available to me I'm afraid that I am not able to help you with that.
The system does not crash when using PwmOut with LPC1768 board.

23 Apr 2018

Thanks for the immediate reply.

PFB the code snippet for the PWM & LED1 .

When PwmOut is not enabled, LED blinks every second but when I enabled the PwmOut , LED is not blinking every second but PWM pulses are observed at pin P0.16 on the oscilloscope.

I just want confirm whether this issue is nRF51-DK or Mbed Compiler related.

  1. include "mbed.h"
  2. include "PwmOut.h"

PwmOut VMPWM(P0_16);    P0.16 for the PWM 

DigitalOut led1(P0_21);          P0.21 for LED

void periodicCallback(void) {      led1 = !led1; }    

int main(void) {

VMPWM.period(0.01f);       10 ms period VMPWM = 0.2f;                        20 % 

Ticker ticker;  ticker.attach(periodicCallback,1);      In Seconds. 

while(1)  ; }

23 Apr 2018
  • To keep correct formatting when posting code please use
<<code>>
your code
<</code>>


  • I confirm that the code below runs correctly (LED1 blinks every second and LED2 is dimmed) on an LPC1768 board.

#include "mbed.h"

DigitalOut  led1(LED1);
PwmOut      VMPWM(LED2);

void periodicCallback(void)
{
    led1 = !led1;
}

int main(void)
{
    VMPWM.period(0.01f);                //10 ms period
    VMPWM = 0.2f;                       //20 %
    Ticker  ticker;
    ticker.attach(periodicCallback, 1); //In Seconds.
    while (1);
}
23 Apr 2018

Hi Zoltan,

Thank you very much for your support.

Regards Bharat Gopani

09 Jul 2018

Hello everyone,

I am using nRF51-DK board with ARM Mbed IDE. I am detecting pulses at P0.09(UART Tx Pin) on the oscilloscope only when UART terminal is connected otherwise not.

And when I disconnecting from UART terminal , Pulses at P0.09 are stopped. May I know why this is type of working & what should I do to receive pulses at P0.09 even when UART terminal is not connected on the PC.

Secondly, I want to change serial port pin from P0.09 & P0.11 to P0.25 & P0.24 without Hardware flow control. What should I do for that ??.

<<code>>

  1. include "mbed.h"
  2. include "Serial.h"
  1. define NEED_CONSOLE_OUTPUT 0 Set this if you need debug messages on the console; it will have an impact on code-size and power consumption.
  2. if NEED_CONSOLE_OUTPUT
  3. define DEBUG(...) { pc.printf(VA_ARGS); }
  4. else
  5. define DEBUG(...) nothing
  6. endif #if NEED_CONSOLE_OUTPUT

Serial RN42(USBTX,USBRX);

void RN42_init(void);

void RN42_init(void) {

RN42.format(8,SerialBase::None,1);

RN42.baud(9600);

wait(0.25); }

int main(void) {

wait(1.0f); RN42_init();

while(1) {

RN42.putc('A'); RN42.printf("Hello World\n\r"); wait(1.0f); } }