RayLivingston:
I really don't see the point of "marshaller". You can accomplish the same thing by simply making the interrupt handler a static function (which marshaller is). Also make any member functions that are called by the interrupt handler, and any member data accessed by the interrupt handler, static. This does, however, make the class a "singleton". i.e. - there must only ever be ONE instance of the class attempting to be attached to that interrupt at any given time. But it appears to me the code you posted imposes the exact same restrictions, just implemented in a very convoluted manner.
Another simple way to handle the problem, if you cannot tolerate the class being a singleton, is to let the sketch actually do the attach interrupt, with the interrupt being attached to a function in the sketch that does nothing but call the interrupt handler of the appropriate class instance. This is a bit round-about, but very simple, and imposes minimal overhead (one additional function call).
Regards,
Ray L.
Letting the sketch handle the interrupt is indeed the simplest solution, but I wanted to challenge myself by seeing if I can let a class handle everything.
I've tried making counter static as follows (note this is edited from case 1):
class interrupt{
public:
interrupt(){}
static int counter;
void init(){
pinMode(2,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), interruptL, LOW);
}
static void interruptL(){
counter++;
}
};
interrupt myInterrupt;
void setup() {
myInterrupt.init();
}
void loop() {
Serial.println(myInterrupt.counter);
delay(200);
}
...and that creates another error:
C:\Users\darrel\AppData\Local\Temp\ccOITqgi.ltrans0.ltrans.o: In function `interruptL':
C:\Users\darrel\Desktop\LCD_TEST/LCD_TEST.ino:13: undefined reference to `interrupt::counter'
C:\Users\darrel\Desktop\LCD_TEST/LCD_TEST.ino:13: undefined reference to `interrupt::counter'
C:\Users\darrel\Desktop\LCD_TEST/LCD_TEST.ino:13: undefined reference to `interrupt::counter'
C:\Users\darrel\Desktop\LCD_TEST/LCD_TEST.ino:13: undefined reference to `interrupt::counter'
C:\Users\darrel\AppData\Local\Temp\ccOITqgi.ltrans0.ltrans.o: In function `loop':
C:\Users\darrel\Desktop\LCD_TEST/LCD_TEST.ino:24: undefined reference to `interrupt::counter'
C:\Users\darrel\AppData\Local\Temp\ccOITqgi.ltrans0.ltrans.o:C:\Users\darrel\Desktop\LCD_TEST/LCD_TEST.ino:24: more undefined references to `interrupt::counter' follow
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
Delta_G:
When you call a member function, a secret pointer parameter called "this" is passed which indicates which instance is calling the function. Oh but remember what I said on the first line, an ISR can take no parameters. So it can't take the this pointer. So an ISR can't take a member function.
While I understand the gist of your argument, this part confuses me. The ISR in both cases still takes no arguments right? Do you mean the ISR is being force-fed this as an argument?