Eventqueue as instead of RTOS timer

Hello guys ,

I Hope that any one can help me in the following
I used event queue as a timer instead of RTOS Timer to read a value from sensor and printf it
The problem is that I want to make that infinty times but with eventqueue get out of memory very early how can I solve this problem given that I enlrage the stacke of event in (mbed-os/events/mbed_lib.json). code :

EventQueue queue_temperatureTimer;

int temperatureTimer_id;

void startTimer(){

 temperatureTimer_id = queue_temperatureTimer.call_every(5000, &temperatureTimerExpired);

printf("temperatureTimer_id :%i\n",temperatureTimer_id);

if(temperatureTimer_id ==0 )
{

    printf("not enough memory to allocate the event\n");

}
else{

    printf("Timer started");

}

queue_temperatureTimer.dispatch();

}

void temperatureTimerExpired() {

t_uint32 temperatureValue

int cancelReturn;

/read value of Temperature Sensor/

printf("temperatureTimerExpired()\n");

queue_temperatureTimer.cancel(temperatureTimer_id);

printf("cancelReturn %i\n",cancelReturn);

temperatureValue= sal_getTemperatureValue();

 printf(" temperatureValue : %lu \n ",temperatureValue);

/restart the timer with Normal Value/
startTimer();
}
int main()
{
startTimer();
}

Hello Esraa,

You can try the following:

  • In the startTimer function replace
    queue_temperatureTimer.dispatch();

with

    queue_temperatureTimer.dispatch_forever();
  • Then replace the temperatureTimerExpired function with the following one:
void temperatureTimerExpired()
{
    t_uint32    temperatureValue;

    temperatureValue = sal_getTemperatureValue();
    printf(" temperatureValue : %lu \n ", temperatureValue);
}

Best regards,

Zoltan

1 Like

Thanks alot , this solution works with me …

I have another question is I want to restart the timer (the same timer )with different values each time the timer expired and in this time the same problem of stack overflow happened
so have you why that happens
and thanks a lot again for your help

Hello Esraa,

Try the following and observe LED1:

#include "mbed.h"

DigitalOut  led1(LED1);
EventQueue  eventQueue;
Thread      thread;
bool        call_in_Enabled = true;
int         timeout = 50;   // initial timeout in ms

void onTimeout()
{
    led1 = !led1;
    timeout += 10;          // change timeout
    if (timeout > 500) {
        timeout = 50;       // reset
    }

    call_in_Enabled = true; //enable call_in
}

int main()
{
    thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
    while (true) {
        if (call_in_Enabled) {
            call_in_Enabled = false;    // disable call_in
            eventQueue.call_in(timeout, onTimeout);
        }
    }
}

Best regards,

Zoltan

Really Thanks a lot for all the support you provided to me , that helped me a lot .As I’m new in mbed os