Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for function objects in attachInterrupt #1925

Closed
igrr opened this issue Apr 17, 2016 · 12 comments
Closed

Add support for function objects in attachInterrupt #1925

igrr opened this issue Apr 17, 2016 · 12 comments

Comments

@igrr
Copy link
Member

igrr commented Apr 17, 2016

Allow something like

attachInterrupt(pin, [](){
    // function body
  }, CHANGE);

and

class MyClass() {
public:
  MyClass(int pin1, int pin2) {
    attachInterrupt(pin1, std::bind(&MyClass::onPinChange, this, pin1), CHANGE);
    attachInterrupt(pin2, std::bind(&MyClass::onPinChange, this, pin2), CHANGE);
  }

protected:
  void onPinChange(int pin) {
    // handler here
  }
};

without breaking usage of attachInterrupt from C code.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@igrr igrr modified the milestones: 2.2.0, 2.3.0 Apr 17, 2016
@igrr igrr modified the milestones: 2.3.0, 2.4.0 Jun 3, 2016
@hreintke
Copy link
Contributor

Like to use this functionality within my application and willing to give it a try creating a PR for this.
Do you already have a basic idea on how you want to have it implemented.
If so, I can base my work on that.

@neu-rah
Copy link

neu-rah commented Nov 27, 2016

i've working on something like this, pin change interrupts with an optional cargo, https://github.com/neu-rah/PCINT

@hreintke
Copy link
Contributor

hreintke commented Dec 8, 2016

See #2745

@zenmanenergy
Copy link

Wow. Did this ever get into the core? This is a very cool idea.

@ferchinas
Copy link

ferchinas commented Aug 31, 2017

Hi:
I am trying to attach a method of my class to attend a Gpio interrupt. But I get the following error:
GwiModules.cpp: 283:122: error: cannot convert 'std::_Bind_helper<false, void (GwiManual::)(char), GwiManual const, int>::type {aka std::_Bind<std::_Mem_fn<void (GwiManual::)(char)>(GwiManual, int)>}' to 'void ()()' for argument '2' to 'void attachInterrupt(uint8_t, void ()(), int)

NodeMCU V1.0
Arduino IDE V1.8.3
Arduino/Esp8266 V2.3

GwiModules.h

  class GwiManual : public Controls
  {
  protected:
	  void event(void);							
	  
  public:
	  GwiManual();
	  void enable(void);	 
  };

GwiModules.cpp

GwiManual::GwiManual() {
	...
}

void GwiManual::event(void) {
	...
}

void GwiManual::enable(void) {
	...
	attachInterrupt(13, std::bind(&GwiManual::event,this), CHANGE);
}

@devyte
Copy link
Collaborator

devyte commented Aug 31, 2017 via email

@ferchinas
Copy link

I did not include it, but thanks to your message I discovered that this information is for the development version.
I can not find how to install it in the IDE, only found for stable version.

@devyte
Copy link
Collaborator

devyte commented Sep 1, 2017 via email

@ferchinas
Copy link

Finally get it installed and tested, it works very well, thank you very much for the help.

@devyte
Copy link
Collaborator

devyte commented Oct 17, 2017

@igrr the relevant PR is already merged. Should this be closed, or left open with label staged-for-release, or something else?

@tonton81
Copy link

shouldnt you also show the state change not just the pin itself? some people might want to check rising and falling edges, not just changes, if a state was returned the user wouldnt need to call digitalRead when the interrupt fires to know whether the pin changed high or low as well

@devyte
Copy link
Collaborator

devyte commented Jul 12, 2018

This is already implemented via #2745 .

@devyte devyte modified the milestones: 2.5.0, 2.4.2 Jul 12, 2018
@devyte devyte closed this as completed Aug 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants