6 years, 6 months ago.

Call Serial attach method from static function in class

Hi, I have a class in which is running thread: Header file

class Server{
    
public:
    Server(PinName tx, PinName rx, PinName reset, Serial& serialPc, PinName led1, PinName led3);  
    
    Serial& pc;
    Serial esp;
    DigitalOut reset;
    

protected:
    DigitalOut  led1;      // (PTB18)
    DigitalOut  led3;     // (PTD1)

private:
    
    
    Timer t1;
    Timer t2;
    
    Thread* thread;
    **static void threadWorker(void const *p);**
    ...

And in my cpp file have this:

Server::Server(PinName tx, PinName rx, PinName reset, Serial& serialPc, PinName led1, PinName led3):
esp(tx, rx), pc(serialPc), reset(reset), led1(led1), led3(led3){
    
    esp.attach(callback(this, &Server::serverCallback));    
    ...
    thread = new Thread(threadWorker, this);
        
}

void Server::threadWorker(void const *p){
    Server* self = (Server*) p;
    
    ...
    while(1) {
        if(self->DataRX==1) {
            self->ReadWebData();
            if (self->servreq == 1 && self->weberror == 0) {
                    self->sendpage();
            }

            **self->esp.attach(callback(&self->Server::serverCallback));**

How to call non static method from static class? In this article: https://stackoverflow.com/questions/9713430/call-a-non-static-member-method-from-another-method , that is needed to make the non static method global in class or send a pointer to the non static method to the static method.

Can I point, using *self pointer, on the callback function? If not, why not? And how else can I resolve this problem? Thank you.

Be the first to answer this question.