Using interrupt in classes

26 Mar 2010 . Edited: 13 Oct 2010

Hi.

I'm making a class for catching simple button inputs on the mbed.

This is my .h:

#ifndef MBED_BTN_H
#define MBED_BTN_H

#include "mbed.h"

class BTN  {
public:
    BTN(PinName InterruptPort);

private:
    InterruptIn _interruptin; 
    void BTNDOWN(void);
    void BTNUP(void);
};

#endif

 

This is my .cpp

#include "mbed.h"
#include "BTN.h"


BTN::BTN(PinName InterruptPort)
        : _interruptin(InterruptPort) {
    _interruptin.mode(PullUp);
    _interruptin.rise(&BTNDOWN);
    _interruptin.fall(&BTNUP);
}

void BTN::BTNDOWN(){
}

void BTN::BTNUP(){
}


I'm getting these errors:

"Nonstandard form for taking the address of a member function (E504-D)" in file "/QUEUE/BTN.cpp"

"No instance of overloaded function "mbed::InterruptIn::rise" matches the argument list (E304)" in file "/QUEUE/BTN.cpp"

"      _interruptin.rise(&BTNDOWN); (E0)" in file "/QUEUE/BTN.cpp"

"                   ^ (E0)" in file "/QUEUE/BTN.cpp"

 

times two (one for the rise function, and one for the fall.)

Does anybody know what I'm doing wrong?

26 Mar 2010 . Edited: 26 Mar 2010

Hi Thomas,

You are basically there. You just are not correctly specifying the member function pointers. You need to pass the pointer to the class instance (which is itself in your case, i.e. "this"), and the member function should include the class name:

BTN::BTN(PinName InterruptPort)
        : _interruptin(InterruptPort) {
    _interruptin.mode(PullUp);
    _interruptin.rise(this, &BTN::BTNDOWN);
    _interruptin.fall(this, &BTN::BTNUP);
}

Then you should be up and running.

btw, you can also publish your code and link to it in a forum post to help make it easier for people to reproduce your problem quickly, and confirm their solution works before posting back.

Simon

26 Mar 2010

Thank you for the quick answer! It worked flawlessly, and I learned something new.

I will publish the code whenever I get a new problem I can't solve myself.

Thanks again :)