EventQueue in class

Hello I am trying to implement a period ticker to sample potentiometer and write its value to pwmout. But using normal mbed2 solution wont work because I get Mutex error and now I am trying to understand event queue.

main.cpp

No thread

pot.cpp

private : thread t, ticker tc, eventqueue e, motor* m


pot()
t.start(callback(&e,&EventQueue::dispatch_forever));
tc.attach(callback(this,&pot::sample),200ms);

void sample()
currentsample = read()
e.call(this,&pot::update);

void update()
m->set_dutycycle(this->wheel, currentsample);

It compiled without mutex error but I am not getting any changes from potentiometer. Any idea?

Hello Muhammad,

Try the following:

#include "mbed.h"

class Pot
{
private:
    AnalogIn    _in;
    PwmOut      _out;
    Thread      _thread;
    EventQueue  _queue;
public:
    Pot(PinName inPin, PinName outPin) : _in(inPin), _out(outPin)
    {
        _thread.start(callback(&_queue, &EventQueue::dispatch_forever));
        _queue.call_every(200ms, callback(this, &Pot::update));
    }

    void update() {
        //printf("updating output\r\n");
        _out = _in;
    }
};

DigitalOut  led1(LED1);
Pot         pot(p15, p26);  // AnloIn pin, PwmOut pin

int main()
{
    while (true) {
        led1 = !led1;
        ThisThread::sleep_for(1s);
    }
}

Interesting enough I tried this method earlier and not work because I am passing class by pointer and its private member is pwmout.

Making it public and using its address works exactly like what I expected