Default contructor for RtosTimer

31 Dec 2013

Hi:

I would like to call the stop() member function from a RtosTimer object, however, this is a bit difficult:

#include "mbed.h"
#include "rtos.h"
 
DigitalOut Led1(LED1);
DigitalOut Led2(LED2);
 
int  State;
 
void Blink(void const *n) {
    if (State == 1)  {
        Led1 = 1;
        Led2 = 0;
        State = 2;
    }
    else if (State == 2)  {
        LedTimer.stop();
        Led1 = 0;
        Led2 = 1;
        LedTimer.start(10000); // longer timer alarm
        State = 1;
    }
}
 
int main(void) {

    State = 1;
 
    RtosTimer LedTimer(&Blink, osTimerPeriodic, NULL);
    LedTimer.start(500); // short timer alarm
    
    Thread::wait(osWaitForever);
}

As RtosTimer is defined within main(), Blink could not see it. If I define LedTimer() outside but before Blink, the code could compile but the code will crash (4 blinking blue LEDs).

Any suggestion?

31 Dec 2013

I got the solution myself.

#include "mbed.h"
#include "rtos.h"
 
DigitalOut Led1(LED1);
DigitalOut Led2(LED2);
 
int  State;

RtosTimer *LedTimer;

void Blink(void const *arg) {
    if (State == 1)  {
        Led1 = 1;
        Led2 = 0;
        State = 2;
    }
    else if (State == 2)  {
        LedTimer->stop();
        Led1 = 0;
        Led2 = 1;
        LedTimer->start(10000); // longer timer alarm
        State = 1;
    }
}

int main(void) {

    State = 1;
    LedTimer = new RtosTimer(&Blink, osTimerPeriodic, NULL);
    LedTimer->start(500); // short timer alarm
    
    Thread::wait(osWaitForever);
}